Browse Source

Adds is method to many types

main
Eric Amodio 5 years ago
parent
commit
0c9811028b
22 changed files with 85 additions and 42 deletions
  1. +1
    -1
      src/annotations/annotations.ts
  2. +6
    -6
      src/commands/common.ts
  3. +1
    -1
      src/commands/diffWith.ts
  4. +1
    -6
      src/commands/diffWithRevision.ts
  5. +5
    -6
      src/commands/openFileRevision.ts
  6. +1
    -1
      src/commands/openWorkingFile.ts
  7. +1
    -6
      src/commands/showQuickFileHistory.ts
  8. +2
    -2
      src/extension.ts
  9. +2
    -2
      src/git/formatters/commitFormatter.ts
  10. +1
    -1
      src/git/fsProvider.ts
  11. +2
    -2
      src/git/gitService.ts
  12. +7
    -3
      src/git/gitUri.ts
  13. +7
    -0
      src/git/models/blameCommit.ts
  14. +4
    -0
      src/git/models/branch.ts
  15. +13
    -0
      src/git/models/commit.ts
  16. +4
    -0
      src/git/models/contributor.ts
  17. +9
    -0
      src/git/models/logCommit.ts
  18. +4
    -0
      src/git/models/remote.ts
  19. +1
    -1
      src/git/models/repository.ts
  20. +7
    -2
      src/git/models/stashCommit.ts
  21. +4
    -0
      src/git/models/tag.ts
  22. +2
    -2
      src/trackers/documentTracker.ts

+ 1
- 1
src/annotations/annotations.ts View File

@ -77,7 +77,7 @@ export class Annotations {
hunkLine?: GitDiffHunkLine hunkLine?: GitDiffHunkLine
): Promise<MarkdownString | undefined> { ): Promise<MarkdownString | undefined> {
const documentRef = uri.sha; const documentRef = uri.sha;
if (commit instanceof GitBlameCommit) {
if (GitBlameCommit.is(commit)) {
// TODO: Figure out how to optimize this // TODO: Figure out how to optimize this
let ref; let ref;
if (commit.isUncommitted) { if (commit.isUncommitted) {

+ 6
- 6
src/commands/common.ts View File

@ -239,7 +239,7 @@ export function isCommandViewContextWithBranch(
): context is CommandViewItemContext & { node: ViewNode & { branch: GitBranch } } { ): context is CommandViewItemContext & { node: ViewNode & { branch: GitBranch } } {
if (context.type !== 'viewItem') return false; if (context.type !== 'viewItem') return false;
return (context.node as ViewNode & { branch: GitBranch }).branch instanceof GitBranch;
return GitBranch.is((context.node as ViewNode & { branch: GitBranch }).branch);
} }
export function isCommandViewContextWithCommit<T extends GitCommit>( export function isCommandViewContextWithCommit<T extends GitCommit>(
@ -247,7 +247,7 @@ export function isCommandViewContextWithCommit(
): context is CommandViewItemContext & { node: ViewNode & { commit: T } } { ): context is CommandViewItemContext & { node: ViewNode & { commit: T } } {
if (context.type !== 'viewItem') return false; if (context.type !== 'viewItem') return false;
return (context.node as ViewNode & { commit: GitCommit }).commit instanceof GitCommit;
return GitCommit.is((context.node as ViewNode & { commit: GitCommit }).commit);
} }
export function isCommandViewContextWithContributor( export function isCommandViewContextWithContributor(
@ -255,7 +255,7 @@ export function isCommandViewContextWithContributor(
): context is CommandViewItemContext & { node: ViewNode & { contributor: GitContributor } } { ): context is CommandViewItemContext & { node: ViewNode & { contributor: GitContributor } } {
if (context.type !== 'viewItem') return false; if (context.type !== 'viewItem') return false;
return (context.node as ViewNode & { contributor: GitContributor }).contributor instanceof GitContributor;
return GitContributor.is((context.node as ViewNode & { contributor: GitContributor }).contributor);
} }
export function isCommandViewContextWithFile( export function isCommandViewContextWithFile(
@ -275,7 +275,7 @@ export function isCommandViewContextWithFileCommit(
const node = context.node as ViewNode & { commit: GitCommit; file: GitFile; repoPath: string }; const node = context.node as ViewNode & { commit: GitCommit; file: GitFile; repoPath: string };
return ( return (
node.file !== undefined && node.file !== undefined &&
node.commit instanceof GitCommit &&
GitCommit.is(node.commit) &&
(node.file.repoPath !== undefined || node.repoPath !== undefined) (node.file.repoPath !== undefined || node.repoPath !== undefined)
); );
} }
@ -307,7 +307,7 @@ export function isCommandViewContextWithRemote(
): context is CommandViewItemContext & { node: ViewNode & { remote: GitRemote } } { ): context is CommandViewItemContext & { node: ViewNode & { remote: GitRemote } } {
if (context.type !== 'viewItem') return false; if (context.type !== 'viewItem') return false;
return (context.node as ViewNode & { remote: GitRemote }).remote instanceof GitRemote;
return GitRemote.is((context.node as ViewNode & { remote: GitRemote }).remote);
} }
export function isCommandViewContextWithRepo( export function isCommandViewContextWithRepo(
@ -548,7 +548,7 @@ export async function openEditor(
): Promise<TextEditor | undefined> { ): Promise<TextEditor | undefined> {
const { rethrow, ...opts } = options; const { rethrow, ...opts } = options;
try { try {
if (uri instanceof GitUri) {
if (GitUri.is(uri)) {
uri = uri.documentUri(); uri = uri.documentUri();
} }

+ 1
- 1
src/commands/diffWith.ts View File

@ -29,7 +29,7 @@ export class DiffWithCommand extends ActiveEditorCommand {
static getMarkdownCommandArgs(commit: GitCommit, line?: number): string; static getMarkdownCommandArgs(commit: GitCommit, line?: number): string;
static getMarkdownCommandArgs(argsOrCommit: DiffWithCommandArgs | GitCommit, line?: number): string { static getMarkdownCommandArgs(argsOrCommit: DiffWithCommandArgs | GitCommit, line?: number): string {
let args: DiffWithCommandArgs | GitCommit; let args: DiffWithCommandArgs | GitCommit;
if (argsOrCommit instanceof GitCommit) {
if (GitCommit.is(argsOrCommit)) {
const commit = argsOrCommit; const commit = argsOrCommit;
if (commit.isUncommitted) { if (commit.isUncommitted) {

+ 1
- 6
src/commands/diffWithRevision.ts View File

@ -86,12 +86,7 @@ export class DiffWithRevisionCommand extends ActiveEditorCommand {
} }
commandArgs = { ...args }; commandArgs = { ...args };
const icon =
args.reference instanceof GitTag
? '$(tag) '
: args.reference instanceof GitBranch
? '$(git-branch) '
: '';
const icon = GitTag.is(args.reference) ? '$(tag) ' : GitBranch.is(args.reference) ? '$(git-branch) ' : '';
const currentCommand = new CommandQuickPickItem( const currentCommand = new CommandQuickPickItem(
{ {
label: `go back ${GlyphChars.ArrowBack}`, label: `go back ${GlyphChars.ArrowBack}`,

+ 5
- 6
src/commands/openFileRevision.ts View File

@ -117,12 +117,11 @@ export class OpenFileRevisionCommand extends ActiveEditorCommand {
} }
commandArgs = { ...args }; commandArgs = { ...args };
const icon =
args.reference instanceof GitTag
? '$(tag) '
: args.reference instanceof GitBranch
? '$(git-branch) '
: '';
const icon = GitTag.is(args.reference)
? '$(tag) '
: GitBranch.is(args.reference)
? '$(git-branch) '
: '';
const currentCommand = new CommandQuickPickItem( const currentCommand = new CommandQuickPickItem(
{ {
label: `go back ${GlyphChars.ArrowBack}`, label: `go back ${GlyphChars.ArrowBack}`,

+ 1
- 1
src/commands/openWorkingFile.ts View File

@ -36,7 +36,7 @@ export class OpenWorkingFileCommand extends ActiveEditorCommand {
} }
args.uri = await GitUri.fromUri(uri); args.uri = await GitUri.fromUri(uri);
if (args.uri instanceof GitUri && args.uri.sha) {
if (GitUri.is(args.uri) && args.uri.sha) {
const workingUri = await Container.git.getWorkingUri(args.uri.repoPath!, args.uri); const workingUri = await Container.git.getWorkingUri(args.uri.repoPath!, args.uri);
if (workingUri === undefined) { if (workingUri === undefined) {
return window.showWarningMessage( return window.showWarningMessage(

+ 1
- 6
src/commands/showQuickFileHistory.ts View File

@ -111,12 +111,7 @@ export class ShowQuickFileHistoryCommand extends ActiveEditorCachedCommand {
} }
} }
const icon =
args.reference instanceof GitTag
? '$(tag) '
: args.reference instanceof GitBranch
? '$(git-branch) '
: '';
const icon = GitTag.is(args.reference) ? '$(tag) ' : GitBranch.is(args.reference) ? '$(git-branch) ' : '';
// Create a command to get back to where we are right now // Create a command to get back to where we are right now
const currentCommand = new CommandQuickPickItem( const currentCommand = new CommandQuickPickItem(
{ {

+ 2
- 2
src/extension.ts View File

@ -18,13 +18,13 @@ export async function activate(context: ExtensionContext) {
setCommandContext(CommandContext.Enabled, true); setCommandContext(CommandContext.Enabled, true);
Logger.configure(context, configuration.get<TraceLevel>(configuration.name('outputLevel').value), o => { Logger.configure(context, configuration.get<TraceLevel>(configuration.name('outputLevel').value), o => {
if (o instanceof GitUri) {
if (GitUri.is(o)) {
return `GitUri(${o.toString(true)}${o.repoPath ? ` repoPath=${o.repoPath}` : ''}${ return `GitUri(${o.toString(true)}${o.repoPath ? ` repoPath=${o.repoPath}` : ''}${
o.sha ? ` sha=${o.sha}` : '' o.sha ? ` sha=${o.sha}` : ''
})`; })`;
} }
if (o instanceof GitCommit) {
if (GitCommit.is(o)) {
return `GitCommit(${o.sha ? ` sha=${o.sha}` : ''}${o.repoPath ? ` repoPath=${o.repoPath}` : ''})`; return `GitCommit(${o.sha ? ` sha=${o.sha}` : ''}${o.repoPath ? ` repoPath=${o.repoPath}` : ''})`;
} }

+ 2
- 2
src/git/formatters/commitFormatter.ts View File

@ -142,14 +142,14 @@ export class CommitFormatter extends Formatter {
get changes() { get changes() {
return this._padOrTruncate( return this._padOrTruncate(
this._item instanceof GitLogCommit ? this._item.getFormattedDiffStatus() : emptyStr,
GitLogCommit.is(this._item) ? this._item.getFormattedDiffStatus() : emptyStr,
this._options.tokenOptions.changes this._options.tokenOptions.changes
); );
} }
get changesShort() { get changesShort() {
return this._padOrTruncate( return this._padOrTruncate(
this._item instanceof GitLogCommit
GitLogCommit.is(this._item)
? this._item.getFormattedDiffStatus({ compact: true, separator: emptyStr }) ? this._item.getFormattedDiffStatus({ compact: true, separator: emptyStr })
: emptyStr, : emptyStr,
this._options.tokenOptions.changesShort this._options.tokenOptions.changesShort

+ 1
- 1
src/git/fsProvider.ts View File

@ -20,7 +20,7 @@ import { Iterables, Strings, TernarySearchTree } from '../system';
const emptyArray = new Uint8Array(0); const emptyArray = new Uint8Array(0);
export function fromGitLensFSUri(uri: Uri): { path: string; ref: string; repoPath: string } { export function fromGitLensFSUri(uri: Uri): { path: string; ref: string; repoPath: string } {
const gitUri = uri instanceof GitUri ? uri : GitUri.fromRevisionUri(uri);
const gitUri = GitUri.is(uri) ? uri : GitUri.fromRevisionUri(uri);
return { path: gitUri.relativePath, ref: gitUri.sha!, repoPath: gitUri.repoPath! }; return { path: gitUri.relativePath, ref: gitUri.sha!, repoPath: gitUri.repoPath! };
} }

+ 2
- 2
src/git/gitService.ts View File

@ -2021,7 +2021,7 @@ export class GitService implements Disposable {
options: { ref?: string } = {} options: { ref?: string } = {}
): Promise<string | undefined> { ): Promise<string | undefined> {
if (filePathOrUri == null) return this.getHighlanderRepoPath(); if (filePathOrUri == null) return this.getHighlanderRepoPath();
if (filePathOrUri instanceof GitUri) return filePathOrUri.repoPath;
if (GitUri.is(filePathOrUri)) return filePathOrUri.repoPath;
const cc = Logger.getCorrelationContext(); const cc = Logger.getCorrelationContext();
@ -2153,7 +2153,7 @@ export class GitService implements Disposable {
isVslsScheme = undefined; isVslsScheme = undefined;
} }
else { else {
if (repoPathOrUri instanceof GitUri) {
if (GitUri.is(repoPathOrUri)) {
if (repoPathOrUri.repoPath) { if (repoPathOrUri.repoPath) {
const repo = repositoryTree.get(repoPathOrUri.repoPath); const repo = repositoryTree.get(repoPathOrUri.repoPath);
if (repo !== undefined) return repo; if (repo !== undefined) return repo;

+ 7
- 3
src/git/gitUri.ts View File

@ -36,6 +36,10 @@ interface UriEx {
} }
export class GitUri extends ((Uri as any) as UriEx) { export class GitUri extends ((Uri as any) as UriEx) {
static is(uri: any): uri is GitUri {
return uri instanceof GitUri;
}
readonly repoPath?: string; readonly repoPath?: string;
readonly sha?: string; readonly sha?: string;
readonly versionedPath?: string; readonly versionedPath?: string;
@ -176,7 +180,7 @@ export class GitUri extends ((Uri as any) as UriEx) {
equals(uri: Uri | undefined) { equals(uri: Uri | undefined) {
if (!UriComparer.equals(this, uri)) return false; if (!UriComparer.equals(this, uri)) return false;
return this.sha === (uri instanceof GitUri ? uri.sha : undefined);
return this.sha === (GitUri.is(uri) ? uri.sha : undefined);
} }
getFormattedPath(options: { relativeTo?: string; separator?: string; suffix?: string } = {}): string { getFormattedPath(options: { relativeTo?: string; separator?: string; suffix?: string } = {}): string {
@ -259,7 +263,7 @@ export class GitUri extends ((Uri as any) as UriEx) {
exit: uri => `returned ${Logger.toLoggable(uri)}` exit: uri => `returned ${Logger.toLoggable(uri)}`
}) })
static async fromUri(uri: Uri) { static async fromUri(uri: Uri) {
if (uri instanceof GitUri) return uri;
if (GitUri.is(uri)) return uri;
if (!Container.git.isTrackable(uri)) return new GitUri(uri); if (!Container.git.isTrackable(uri)) return new GitUri(uri);
@ -315,7 +319,7 @@ export class GitUri extends ((Uri as any) as UriEx) {
let fileName: string; let fileName: string;
if (fileNameOrUri instanceof Uri) { if (fileNameOrUri instanceof Uri) {
if (fileNameOrUri instanceof GitUri) return fileNameOrUri.getFormattedPath(options);
if (GitUri.is(fileNameOrUri)) return fileNameOrUri.getFormattedPath(options);
fileName = fileNameOrUri.fsPath; fileName = fileNameOrUri.fsPath;
} }

+ 7
- 0
src/git/models/blameCommit.ts View File

@ -2,6 +2,13 @@
import { GitCommit, GitCommitLine, GitCommitType } from './commit'; import { GitCommit, GitCommitLine, GitCommitType } from './commit';
export class GitBlameCommit extends GitCommit { export class GitBlameCommit extends GitCommit {
static is(commit: any): commit is GitBlameCommit {
return (
commit instanceof GitBlameCommit
//|| (commit.repoPath !== undefined && commit.sha !== undefined && commit.type === GitCommitType.Blame)
);
}
constructor( constructor(
repoPath: string, repoPath: string,
sha: string, sha: string,

+ 4
- 0
src/git/models/branch.ts View File

@ -11,6 +11,10 @@ export interface GitTrackingState {
} }
export class GitBranch { export class GitBranch {
static is(branch: any): branch is GitBranch {
return branch instanceof GitBranch;
}
readonly detached: boolean; readonly detached: boolean;
readonly id: string; readonly id: string;
readonly tracking?: string; readonly tracking?: string;

+ 13
- 0
src/git/models/commit.ts View File

@ -42,6 +42,19 @@ export const CommitFormatting = {
}; };
export abstract class GitCommit { export abstract class GitCommit {
static is(commit: any): commit is GitCommit {
return (
commit instanceof GitCommit
// || (commit.repoPath !== undefined &&
// commit.sha !== undefined &&
// (commit.type === GitCommitType.Blame ||
// commit.type === GitCommitType.Log ||
// commit.type === GitCommitType.LogFile ||
// commit.type === GitCommitType.Stash ||
// commit.type === GitCommitType.StashFile))
);
}
constructor( constructor(
public readonly type: GitCommitType, public readonly type: GitCommitType,
public readonly repoPath: string, public readonly repoPath: string,

+ 4
- 0
src/git/models/contributor.ts View File

@ -4,6 +4,10 @@ import { GravatarDefaultStyle } from '../../configuration';
import { getGravatarUri } from '../../avatars'; import { getGravatarUri } from '../../avatars';
export class GitContributor { export class GitContributor {
static is(contributor: any): contributor is GitContributor {
return contributor instanceof GitContributor;
}
constructor( constructor(
public readonly repoPath: string, public readonly repoPath: string,
public readonly name: string, public readonly name: string,

+ 9
- 0
src/git/models/logCommit.ts View File

@ -24,6 +24,15 @@ export interface GitLogCommitLine {
} }
export class GitLogCommit extends GitCommit { export class GitLogCommit extends GitCommit {
static is(commit: any): commit is GitLogCommit {
return (
commit instanceof GitLogCommit
// || (commit.repoPath !== undefined &&
// commit.sha !== undefined &&
// (commit.type === GitCommitType.Log || commit.type === GitCommitType.LogFile))
);
}
nextSha?: string; nextSha?: string;
nextFileName?: string; nextFileName?: string;

+ 4
- 0
src/git/models/remote.ts View File

@ -9,6 +9,10 @@ export enum GitRemoteType {
} }
export class GitRemote { export class GitRemote {
static is(remote: any): remote is GitRemote {
return remote instanceof GitRemote;
}
constructor( constructor(
public readonly repoPath: string, public readonly repoPath: string,
public readonly id: string, public readonly id: string,

+ 1
- 1
src/git/models/repository.ts View File

@ -228,7 +228,7 @@ export class Repository implements Disposable {
} }
containsUri(uri: Uri) { containsUri(uri: Uri) {
if (uri instanceof GitUri) {
if (GitUri.is(uri)) {
uri = uri.repoPath !== undefined ? GitUri.file(uri.repoPath) : uri.documentUri(); uri = uri.repoPath !== undefined ? GitUri.file(uri.repoPath) : uri.documentUri();
} }

+ 7
- 2
src/git/models/stashCommit.ts View File

@ -4,8 +4,13 @@ import { GitFile } from './file';
import { GitLogCommit } from './logCommit'; import { GitLogCommit } from './logCommit';
export class GitStashCommit extends GitLogCommit { export class GitStashCommit extends GitLogCommit {
static is(commit: GitLogCommit): commit is GitStashCommit {
return commit.isStash;
static is(commit: any): commit is GitStashCommit {
return (
commit instanceof GitStashCommit
// || (commit.repoPath !== undefined &&
// commit.sha !== undefined &&
// (commit.type === GitCommitType.Stash || commit.type === GitCommitType.StashFile))
);
} }
constructor( constructor(

+ 4
- 0
src/git/models/tag.ts View File

@ -2,6 +2,10 @@
import { memoize } from '../../system'; import { memoize } from '../../system';
export class GitTag { export class GitTag {
static is(tag: any): tag is GitTag {
return tag instanceof GitTag;
}
constructor( constructor(
public readonly repoPath: string, public readonly repoPath: string,
public readonly name: string, public readonly name: string,

+ 2
- 2
src/trackers/documentTracker.ts View File

@ -236,7 +236,7 @@ export class DocumentTracker implements Disposable {
private async _add(documentOrId: TextDocument | Uri): Promise<TrackedDocument<T>> { private async _add(documentOrId: TextDocument | Uri): Promise<TrackedDocument<T>> {
let document; let document;
if (documentOrId instanceof GitUri) {
if (GitUri.is(documentOrId)) {
try { try {
document = await workspace.openTextDocument(documentOrId.documentUri({ useVersionedPath: true })); document = await workspace.openTextDocument(documentOrId.documentUri({ useVersionedPath: true }));
} }
@ -273,7 +273,7 @@ export class DocumentTracker implements Disposable {
} }
private async _get(documentOrId: string | TextDocument | Uri) { private async _get(documentOrId: string | TextDocument | Uri) {
if (documentOrId instanceof GitUri) {
if (GitUri.is(documentOrId)) {
documentOrId = GitUri.toKey(documentOrId.documentUri({ useVersionedPath: true })); documentOrId = GitUri.toKey(documentOrId.documentUri({ useVersionedPath: true }));
} }
else if (typeof documentOrId === 'string' || documentOrId instanceof Uri) { else if (typeof documentOrId === 'string' || documentOrId instanceof Uri) {

Loading…
Cancel
Save