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
): Promise<MarkdownString | undefined> {
const documentRef = uri.sha;
if (commit instanceof GitBlameCommit) {
if (GitBlameCommit.is(commit)) {
// TODO: Figure out how to optimize this
let ref;
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 } } {
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>(
@ -247,7 +247,7 @@ export function isCommandViewContextWithCommit(
): context is CommandViewItemContext & { node: ViewNode & { commit: T } } {
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(
@ -255,7 +255,7 @@ export function isCommandViewContextWithContributor(
): context is CommandViewItemContext & { node: ViewNode & { contributor: GitContributor } } {
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(
@ -275,7 +275,7 @@ export function isCommandViewContextWithFileCommit(
const node = context.node as ViewNode & { commit: GitCommit; file: GitFile; repoPath: string };
return (
node.file !== undefined &&
node.commit instanceof GitCommit &&
GitCommit.is(node.commit) &&
(node.file.repoPath !== undefined || node.repoPath !== undefined)
);
}
@ -307,7 +307,7 @@ export function isCommandViewContextWithRemote(
): context is CommandViewItemContext & { node: ViewNode & { remote: GitRemote } } {
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(
@ -548,7 +548,7 @@ export async function openEditor(
): Promise<TextEditor | undefined> {
const { rethrow, ...opts } = options;
try {
if (uri instanceof GitUri) {
if (GitUri.is(uri)) {
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(argsOrCommit: DiffWithCommandArgs | GitCommit, line?: number): string {
let args: DiffWithCommandArgs | GitCommit;
if (argsOrCommit instanceof GitCommit) {
if (GitCommit.is(argsOrCommit)) {
const commit = argsOrCommit;
if (commit.isUncommitted) {

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

@ -86,12 +86,7 @@ export class DiffWithRevisionCommand extends ActiveEditorCommand {
}
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(
{
label: `go back ${GlyphChars.ArrowBack}`,

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

@ -117,12 +117,11 @@ export class OpenFileRevisionCommand extends ActiveEditorCommand {
}
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(
{
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);
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);
if (workingUri === undefined) {
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
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);
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}` : ''}${
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}` : ''})`;
}

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

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

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

@ -2021,7 +2021,7 @@ export class GitService implements Disposable {
options: { ref?: string } = {}
): Promise<string | undefined> {
if (filePathOrUri == null) return this.getHighlanderRepoPath();
if (filePathOrUri instanceof GitUri) return filePathOrUri.repoPath;
if (GitUri.is(filePathOrUri)) return filePathOrUri.repoPath;
const cc = Logger.getCorrelationContext();
@ -2153,7 +2153,7 @@ export class GitService implements Disposable {
isVslsScheme = undefined;
}
else {
if (repoPathOrUri instanceof GitUri) {
if (GitUri.is(repoPathOrUri)) {
if (repoPathOrUri.repoPath) {
const repo = repositoryTree.get(repoPathOrUri.repoPath);
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) {
static is(uri: any): uri is GitUri {
return uri instanceof GitUri;
}
readonly repoPath?: string;
readonly sha?: string;
readonly versionedPath?: string;
@ -176,7 +180,7 @@ export class GitUri extends ((Uri as any) as UriEx) {
equals(uri: Uri | undefined) {
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 {
@ -259,7 +263,7 @@ export class GitUri extends ((Uri as any) as UriEx) {
exit: uri => `returned ${Logger.toLoggable(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);
@ -315,7 +319,7 @@ export class GitUri extends ((Uri as any) as UriEx) {
let fileName: string;
if (fileNameOrUri instanceof Uri) {
if (fileNameOrUri instanceof GitUri) return fileNameOrUri.getFormattedPath(options);
if (GitUri.is(fileNameOrUri)) return fileNameOrUri.getFormattedPath(options);
fileName = fileNameOrUri.fsPath;
}

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

@ -2,6 +2,13 @@
import { GitCommit, GitCommitLine, GitCommitType } from './commit';
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(
repoPath: string,
sha: string,

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

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

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

@ -42,6 +42,19 @@ export const CommitFormatting = {
};
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(
public readonly type: GitCommitType,
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';
export class GitContributor {
static is(contributor: any): contributor is GitContributor {
return contributor instanceof GitContributor;
}
constructor(
public readonly repoPath: 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 {
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;
nextFileName?: string;

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

@ -9,6 +9,10 @@ export enum GitRemoteType {
}
export class GitRemote {
static is(remote: any): remote is GitRemote {
return remote instanceof GitRemote;
}
constructor(
public readonly repoPath: 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) {
if (uri instanceof GitUri) {
if (GitUri.is(uri)) {
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';
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(

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

@ -2,6 +2,10 @@
import { memoize } from '../../system';
export class GitTag {
static is(tag: any): tag is GitTag {
return tag instanceof GitTag;
}
constructor(
public readonly repoPath: 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>> {
let document;
if (documentOrId instanceof GitUri) {
if (GitUri.is(documentOrId)) {
try {
document = await workspace.openTextDocument(documentOrId.documentUri({ useVersionedPath: true }));
}
@ -273,7 +273,7 @@ export class DocumentTracker implements Disposable {
}
private async _get(documentOrId: string | TextDocument | Uri) {
if (documentOrId instanceof GitUri) {
if (GitUri.is(documentOrId)) {
documentOrId = GitUri.toKey(documentOrId.documentUri({ useVersionedPath: true }));
}
else if (typeof documentOrId === 'string' || documentOrId instanceof Uri) {

Loading…
Cancel
Save