Bläddra i källkod

Adds stricter typing for remotes with providers

main
Eric Amodio 4 år sedan
förälder
incheckning
739564fc78
5 ändrade filer med 34 tillägg och 48 borttagningar
  1. +3
    -9
      src/commands/openInRemote.ts
  2. +2
    -2
      src/commands/remoteProviders.ts
  3. +4
    -6
      src/git/gitService.ts
  4. +4
    -2
      src/git/models/remote.ts
  5. +21
    -29
      src/quickpicks/remotesQuickPick.ts

+ 3
- 9
src/commands/openInRemote.ts Visa fil

@ -1,7 +1,7 @@
'use strict';
import { TextEditor, Uri } from 'vscode';
import { GlyphChars } from '../constants';
import { GitRemote, GitService, RemoteResource, RemoteResourceType } from '../git/gitService';
import { GitRemote, GitService, RemoteProvider, RemoteResource, RemoteResourceType } from '../git/gitService';
import { Logger } from '../logger';
import { Messages } from '../messages';
import { CommandQuickPickItem, OpenRemoteCommandQuickPickItem, RemotesQuickPick } from '../quickpicks';
@ -10,7 +10,7 @@ import { ActiveEditorCommand, command, Commands } from './common';
export interface OpenInRemoteCommandArgs {
remote?: string;
remotes?: GitRemote[];
remotes?: GitRemote<RemoteProvider>[];
resource?: RemoteResource;
clipboard?: boolean;
@ -36,13 +36,7 @@ export class OpenInRemoteCommand extends ActiveEditorCommand {
}
try {
let remote: GitRemote | undefined;
if (args.remotes.length > 1) {
remote = args.remotes.find(r => r.default);
} else if (args.remotes.length === 1) {
remote = args.remotes[0];
}
const remote = args.remotes.length === 1 ? args.remotes[0] : args.remotes.find(r => r.default);
if (remote != null) {
this.ensureRemoteBranchName(args);
const command = new OpenRemoteCommandQuickPickItem(remote, args.resource, args.clipboard);

+ 2
- 2
src/commands/remoteProviders.ts Visa fil

@ -43,7 +43,7 @@ export class ConnectRemoteProviderCommand extends Command {
const remotes = await Container.git.getRemotes(args.repoPath);
const remote = remotes.find(r => r.name === args.remote);
if (!remote?.provider?.hasApi()) return false;
if (!remote?.provider.hasApi()) return false;
const connected = await remote.provider.connect();
if (connected && !remotes.some(r => r.default)) {
@ -95,7 +95,7 @@ export class DisconnectRemoteProviderCommand extends Command {
if (args?.repoPath == null || args?.remote == null) return undefined;
const remote = (await Container.git.getRemotes(args.repoPath)).find(r => r.name === args.remote);
if (!remote?.provider?.hasApi()) return undefined;
if (!remote?.provider.hasApi()) return undefined;
return remote.provider.disconnect();
}

+ 4
- 6
src/git/gitService.ts Visa fil

@ -81,7 +81,7 @@ import {
RepositoryChangeEvent
} from './git';
import { GitUri } from './gitUri';
import { RemoteProviderFactory, RemoteProviders, RemoteProviderWithApi } from './remotes/factory';
import { RemoteProvider, RemoteProviderFactory, RemoteProviders, RemoteProviderWithApi } from './remotes/factory';
import { GitReflogParser, GitShortLogParser } from './parsers/parsers';
import { fsExists, isWindows } from './shell';
import { PullRequest, PullRequestDateFormatting } from './models/models';
@ -2467,8 +2467,8 @@ export class GitService implements Disposable {
@log()
async getRemotes(
repoPath: string | undefined,
options: { includeAll?: boolean; sort?: boolean } = {}
): Promise<GitRemote[]> {
options: { sort?: boolean } = {}
): Promise<GitRemote<RemoteProvider>[]> {
if (repoPath === undefined) return [];
const repository = await this.getRepository(repoPath);
@ -2476,9 +2476,7 @@ export class GitService implements Disposable {
? repository.getRemotes({ sort: options.sort })
: this.getRemotesCore(repoPath, undefined, { sort: options.sort }));
if (options.includeAll) return remotes;
return remotes.filter(r => r.provider !== undefined);
return remotes.filter(r => r.provider != null) as GitRemote<RemoteProvider>[];
}
async getRemotesCore(

+ 4
- 2
src/git/models/remote.ts Visa fil

@ -8,7 +8,9 @@ export enum GitRemoteType {
Push = 'push'
}
export class GitRemote {
export class GitRemote<
TProvider extends RemoteProvider | undefined = RemoteProvider | RemoteProviderWithApi | undefined
> {
static is(remote: any): remote is GitRemote {
return remote instanceof GitRemote;
}
@ -28,7 +30,7 @@ export class GitRemote {
public readonly scheme: string,
public readonly domain: string,
public readonly path: string,
public readonly provider: RemoteProvider | RemoteProviderWithApi | undefined,
public readonly provider: TProvider,
public readonly types: { type: GitRemoteType; url: string }[]
) {}

+ 21
- 29
src/quickpicks/remotesQuickPick.ts Visa fil

@ -7,42 +7,39 @@ import {
GitRemote,
GitService,
GitUri,
RemoteProvider,
RemoteResource,
RemoteResourceType
} from '../git/gitService';
import { CommandQuickPickItem, getQuickPickIgnoreFocusOut } from './commonQuickPicks';
export class OpenRemoteCommandQuickPickItem extends CommandQuickPickItem {
private remote: GitRemote;
private resource: RemoteResource;
constructor(remote: GitRemote, resource: RemoteResource, public readonly clipboard?: boolean) {
constructor(
private readonly remote: GitRemote<RemoteProvider>,
private readonly resource: RemoteResource,
public readonly clipboard?: boolean
) {
super(
{
label: clipboard
? `$(link-external) Copy ${getNameFromRemoteResource(resource)} Url to Clipboard from ${
remote.provider!.name
remote.provider.name
}`
: `$(link-external) Open ${getNameFromRemoteResource(resource)} on ${remote.provider!.name}`,
description: `$(repo) ${remote.provider!.path}`
: `$(link-external) Open ${getNameFromRemoteResource(resource)} on ${remote.provider.name}`,
description: `$(repo) ${remote.provider.path}`
},
undefined,
undefined
);
this.remote = remote;
this.resource = resource;
}
execute(): Thenable<{} | undefined> {
if (this.clipboard) return this.remote.provider!.copy(this.resource);
return this.remote.provider!.open(this.resource);
return this.clipboard ? this.remote.provider.copy(this.resource) : this.remote.provider.open(this.resource);
}
}
export class OpenRemotesCommandQuickPickItem extends CommandQuickPickItem {
constructor(remotes: GitRemote[], resource: RemoteResource, goBackCommand?: CommandQuickPickItem) {
constructor(remotes: GitRemote<RemoteProvider>[], resource: RemoteResource, goBackCommand?: CommandQuickPickItem) {
const name = getNameFromRemoteResource(resource);
let description;
@ -95,13 +92,7 @@ export class OpenRemotesCommandQuickPickItem extends CommandQuickPickItem {
break;
}
let remote: GitRemote | undefined;
if (remotes.length > 1) {
remote = remotes.find(r => r.default);
} else if (remotes.length === 1) {
remote = remotes[0];
}
let remote = remotes.length === 1 ? remotes[0] : remotes.find(r => r.default);
if (remote != null) {
const commandArgs: OpenInRemoteCommandArgs = {
remotes: remotes,
@ -110,8 +101,8 @@ export class OpenRemotesCommandQuickPickItem extends CommandQuickPickItem {
};
super(
{
label: `$(link-external) Open ${name} on ${remote.provider!.name}`,
description: `${description} in ${GlyphChars.Space}$(repo) ${remote.provider!.path}`
label: `$(link-external) Open ${name} on ${remote.provider.name}`,
description: `${description} in ${GlyphChars.Space}$(repo) ${remote.provider.path}`
},
Commands.OpenInRemote,
[undefined, commandArgs]
@ -121,10 +112,11 @@ export class OpenRemotesCommandQuickPickItem extends CommandQuickPickItem {
}
remote = remotes[0];
// Use the real provider name if there is only 1 provider
const provider = remotes.every(r => r.provider !== undefined && r.provider.name === remote!.provider!.name)
? remote.provider!.name
: 'Remote';
let providerName = remote.provider.name;
// Only use the real provider name if there is only 1 type of provider
if (!remotes.every(r => r.provider.name === providerName)) {
providerName = 'Remote';
}
const commandArgs: OpenInRemoteCommandArgs = {
remotes: remotes,
@ -133,7 +125,7 @@ export class OpenRemotesCommandQuickPickItem extends CommandQuickPickItem {
};
super(
{
label: `$(link-external) Open ${name} on ${provider}${GlyphChars.Ellipsis}`,
label: `$(link-external) Open ${name} on ${providerName}${GlyphChars.Ellipsis}`,
description: `${description}`
},
Commands.OpenInRemote,
@ -144,7 +136,7 @@ export class OpenRemotesCommandQuickPickItem extends CommandQuickPickItem {
export class RemotesQuickPick {
static async show(
remotes: GitRemote[],
remotes: GitRemote<RemoteProvider>[],
placeHolder: string,
resource: RemoteResource,
clipboard?: boolean,

Laddar…
Avbryt
Spara