|
|
@ -1,10 +1,10 @@ |
|
|
|
'use strict'; |
|
|
|
import {CancellationToken, CodeLens, CodeLensProvider, commands, ExtensionContext, Location, Position, Range, SymbolInformation, SymbolKind, TextDocument, Uri} from 'vscode'; |
|
|
|
import {Commands, VsCodeCommands, WorkspaceState} from './constants'; |
|
|
|
import {CancellationToken, CodeLens, CodeLensProvider, commands, DocumentSelector, ExtensionContext, Location, Position, Range, SymbolInformation, SymbolKind, TextDocument, Uri} from 'vscode'; |
|
|
|
import {Commands, DocumentSchemes, VsCodeCommands, WorkspaceState} from './constants'; |
|
|
|
import GitProvider, {IGitBlame, IGitBlameCommit} from './gitProvider'; |
|
|
|
import * as moment from 'moment'; |
|
|
|
|
|
|
|
export class GitBlameCodeLens extends CodeLens { |
|
|
|
export class GitCodeLens extends CodeLens { |
|
|
|
constructor(private git: GitProvider, public fileName: string, public blameRange: Range, range: Range) { |
|
|
|
super(range); |
|
|
|
} |
|
|
@ -21,26 +21,30 @@ export class GitHistoryCodeLens extends CodeLens { |
|
|
|
} |
|
|
|
|
|
|
|
export default class GitCodeLensProvider implements CodeLensProvider { |
|
|
|
static selector: DocumentSelector = { scheme: DocumentSchemes.File }; |
|
|
|
|
|
|
|
constructor(context: ExtensionContext, private git: GitProvider) { } |
|
|
|
|
|
|
|
provideCodeLenses(document: TextDocument, token: CancellationToken): CodeLens[] | Thenable<CodeLens[]> { |
|
|
|
this.git.getBlameForFile(document.fileName); |
|
|
|
const fileName = document.fileName; |
|
|
|
|
|
|
|
this.git.getBlameForFile(fileName); |
|
|
|
|
|
|
|
return (commands.executeCommand(VsCodeCommands.ExecuteDocumentSymbolProvider, document.uri) as Promise<SymbolInformation[]>).then(symbols => { |
|
|
|
let lenses: CodeLens[] = []; |
|
|
|
symbols.forEach(sym => this._provideCodeLens(document, sym, lenses)); |
|
|
|
const lenses: CodeLens[] = []; |
|
|
|
symbols.forEach(sym => this._provideCodeLens(fileName, document, sym, lenses)); |
|
|
|
|
|
|
|
// Check if we have a lens for the whole document -- if not add one
|
|
|
|
if (!lenses.find(l => l.range.start.line === 0 && l.range.end.line === 0)) { |
|
|
|
const docRange = document.validateRange(new Range(0, 1000000, 1000000, 1000000)); |
|
|
|
lenses.push(new GitBlameCodeLens(this.git, document.fileName, docRange, new Range(0, 0, 0, docRange.start.character))); |
|
|
|
lenses.push(new GitHistoryCodeLens(this.git.repoPath, document.fileName, new Range(0, 1, 0, docRange.start.character))); |
|
|
|
const blameRange = document.validateRange(new Range(0, 1000000, 1000000, 1000000)); |
|
|
|
lenses.push(new GitCodeLens(this.git, fileName, blameRange, new Range(0, 0, 0, blameRange.start.character))); |
|
|
|
lenses.push(new GitHistoryCodeLens(this.git.repoPath, fileName, new Range(0, 1, 0, blameRange.start.character))); |
|
|
|
} |
|
|
|
return lenses; |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
private _provideCodeLens(document: TextDocument, symbol: SymbolInformation, lenses: CodeLens[]): void { |
|
|
|
private _provideCodeLens(fileName: string, document: TextDocument, symbol: SymbolInformation, lenses: CodeLens[]): void { |
|
|
|
switch (symbol.kind) { |
|
|
|
case SymbolKind.Package: |
|
|
|
case SymbolKind.Module: |
|
|
@ -66,16 +70,16 @@ export default class GitCodeLensProvider implements CodeLensProvider { |
|
|
|
startChar += Math.floor(symbol.name.length / 2); |
|
|
|
} |
|
|
|
|
|
|
|
lenses.push(new GitBlameCodeLens(this.git, document.fileName, symbol.location.range, line.range.with(new Position(line.range.start.line, startChar)))); |
|
|
|
lenses.push(new GitHistoryCodeLens(this.git.repoPath, document.fileName, line.range.with(new Position(line.range.start.line, startChar + 1)))); |
|
|
|
lenses.push(new GitCodeLens(this.git, fileName, symbol.location.range, line.range.with(new Position(line.range.start.line, startChar)))); |
|
|
|
lenses.push(new GitHistoryCodeLens(this.git.repoPath, fileName, line.range.with(new Position(line.range.start.line, startChar + 1)))); |
|
|
|
} |
|
|
|
|
|
|
|
resolveCodeLens(lens: CodeLens, token: CancellationToken): Thenable<CodeLens> { |
|
|
|
if (lens instanceof GitBlameCodeLens) return this._resolveGitBlameCodeLens(lens, token); |
|
|
|
if (lens instanceof GitCodeLens) return this._resolveGitBlameCodeLens(lens, token); |
|
|
|
if (lens instanceof GitHistoryCodeLens) return this._resolveGitHistoryCodeLens(lens, token); |
|
|
|
} |
|
|
|
|
|
|
|
_resolveGitBlameCodeLens(lens: GitBlameCodeLens, token: CancellationToken): Thenable<CodeLens> { |
|
|
|
_resolveGitBlameCodeLens(lens: GitCodeLens, token: CancellationToken): Thenable<CodeLens> { |
|
|
|
return new Promise<CodeLens>((resolve, reject) => { |
|
|
|
lens.getBlame().then(blame => { |
|
|
|
if (!blame.lines.length) { |
|
|
@ -104,4 +108,4 @@ export default class GitCodeLensProvider implements CodeLensProvider { |
|
|
|
}; |
|
|
|
return Promise.resolve(lens); |
|
|
|
} |
|
|
|
} |
|
|
|
} |