Skip to content

Commit

Permalink
Adds hightlight changes commands to commit files
Browse files Browse the repository at this point in the history
Fixes recent changes for revision files
  • Loading branch information
eamodio committed Nov 12, 2019
1 parent 0cf2f6e commit d60fdfd
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 6 deletions.
28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2545,6 +2545,16 @@
"light": "images/light/icon-add.svg"
}
},
{
"command": "gitlens.views.highlightChanges",
"title": "Highlight Changes",
"category": "GitLens"
},
{
"command": "gitlens.views.highlightRevisionChanges",
"title": "Highlight Revision Changes",
"category": "GitLens"
},
{
"command": "gitlens.views.restore",
"title": "Restore",
Expand Down Expand Up @@ -3670,6 +3680,14 @@
"command": "gitlens.views.addRemote",
"when": "false"
},
{
"command": "gitlens.views.highlightChanges",
"when": "false"
},
{
"command": "gitlens.views.highlightRevisionChanges",
"when": "false"
},
{
"command": "gitlens.views.restore",
"when": "false"
Expand Down Expand Up @@ -5003,6 +5021,16 @@
"when": "viewItem =~ /gitlens:file\\b((?=.*?\\b\\+committed\\b)|:results)/",
"group": "2_gitlens_quickopen_@2"
},
{
"command": "gitlens.views.highlightChanges",
"when": "viewItem =~ /gitlens:file\\b((?=.*?\\b\\+committed\\b)|:results)/",
"group": "3_gitlens_explore@1"
},
{
"command": "gitlens.views.highlightRevisionChanges",
"when": "viewItem =~ /gitlens:file\\b((?=.*?\\b\\+committed\\b)|:results)/",
"group": "3_gitlens_explore@2"
},
{
"command": "gitlens.showFileHistoryInView",
"when": "viewItem =~ /gitlens:file\\b/",
Expand Down
8 changes: 6 additions & 2 deletions src/annotations/fileAnnotationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,9 @@ export class FileAnnotationController implements Disposable {
): Promise<boolean> {
if (this.getToggleMode(type) === AnnotationsToggleMode.Window) {
let first = this._annotationType === undefined;
const reset = !first && this._annotationType !== type;
const reset =
(!first && this._annotationType !== type) ||
(this._annotationType === FileAnnotationType.RecentChanges && typeof shaOrLine === 'string');

this._annotationType = type;

Expand Down Expand Up @@ -406,7 +408,9 @@ export class FileAnnotationController implements Disposable {
const provider = this.getProvider(editor);
if (provider === undefined) return this.show(editor, type, shaOrLine);

const reopen = provider.annotationType !== type;
const reopen =
provider.annotationType !== type ||
(type === FileAnnotationType.RecentChanges && typeof shaOrLine === 'string');
if (on === true && !reopen) return true;

if (this.isInWindowToggle()) {
Expand Down
40 changes: 37 additions & 3 deletions src/annotations/recentChangesAnnotationProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
'use strict';
import { MarkdownString, Position, Range, TextEditor, TextEditorDecorationType } from 'vscode';
import {
MarkdownString,
Position,
Range,
Selection,
TextEditor,
TextEditorDecorationType,
TextEditorRevealType
} from 'vscode';
import { FileAnnotationType } from '../configuration';
import { Container } from '../container';
import { GitUri } from '../git/gitService';
Expand Down Expand Up @@ -29,10 +37,26 @@ export class RecentChangesAnnotationProvider extends AnnotationProviderBase {

this.annotationType = FileAnnotationType.RecentChanges;

const commit = await Container.git.getCommitForFile(this._uri.repoPath, this._uri.fsPath);
let ref1 = this._uri.sha;
let ref2;
if (typeof shaOrLine === 'string') {
if (shaOrLine !== this._uri.sha) {
ref2 = `${shaOrLine}^`;
}
}

const commit = await Container.git.getCommitForFile(this._uri.repoPath, this._uri.fsPath, {
ref: ref2 ?? ref1
});
if (commit === undefined) return false;

const diff = await Container.git.getDiffForFile(this._uri, commit.sha);
if (ref2 !== undefined) {
ref2 = commit.ref;
} else {
ref1 = commit.ref;
}

const diff = await Container.git.getDiffForFile(this._uri, ref1, ref2);
if (diff === undefined) return false;

let start = process.hrtime();
Expand All @@ -42,6 +66,8 @@ export class RecentChangesAnnotationProvider extends AnnotationProviderBase {

this.decorations = [];

let selection: Selection | undefined;

for (const hunk of diff.hunks) {
// Subtract 2 because editor lines are 0-based and we will be adding 1 in the first iteration of the loop
let count = hunk.currentPosition.start - 2;
Expand All @@ -55,6 +81,9 @@ export class RecentChangesAnnotationProvider extends AnnotationProviderBase {
const range = this.editor.document.validateRange(
new Range(new Position(count, 0), new Position(count, Number.MAX_SAFE_INTEGER))
);
if (selection === undefined) {
selection = new Selection(range.start, range.end);
}

let message: MarkdownString | undefined = undefined;

Expand Down Expand Up @@ -93,6 +122,11 @@ export class RecentChangesAnnotationProvider extends AnnotationProviderBase {
this.editor.setDecorations(this.decoration, this.decorations);

Logger.log(cc, `${Strings.getDurationMilliseconds(start)} ms to apply recent changes annotations`);

if (selection !== undefined) {
this.editor.selection = selection;
this.editor.revealRange(selection, TextEditorRevealType.InCenterIfOutsideViewport);
}
}

return true;
Expand Down
2 changes: 1 addition & 1 deletion src/git/gitService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,7 @@ export class GitService implements Disposable {
});
if (log === undefined) return undefined;

const commit = options.ref && log.commits.get(options.ref);
const commit = options.ref ? log.commits.get(options.ref) : undefined;
if (commit === undefined && !options.firstIfNotFound && options.ref) {
// If the ref isn't a valid sha we will never find it, so let it fall through so we return the first
if (Git.isSha(options.ref) || Git.isUncommitted(options.ref)) return undefined;
Expand Down
41 changes: 41 additions & 0 deletions src/views/viewCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
} from './nodes';
import { debug } from '../system';
import { runGitCommandInTerminal } from '../terminal';
import { FileAnnotationType } from '../config';

interface CompareSelectedInfo {
ref: string;
Expand Down Expand Up @@ -124,6 +125,8 @@ export class ViewCommands {
);
commands.registerCommand('gitlens.views.openChangedFileRevisions', this.openChangedFileRevisions, this);
commands.registerCommand('gitlens.views.applyChanges', this.applyChanges, this);
commands.registerCommand('gitlens.views.highlightChanges', this.highlightChanges, this);
commands.registerCommand('gitlens.views.highlightRevisionChanges', this.highlightRevisionChanges, this);
commands.registerCommand('gitlens.views.restore', this.restore, this);
commands.registerCommand('gitlens.views.switchToBranch', this.switch, this);
commands.registerCommand('gitlens.views.switchToCommit', this.switch, this);
Expand Down Expand Up @@ -342,6 +345,44 @@ export class ViewCommands {
return undefined;
}

@debug()
private async highlightChanges(node: CommitFileNode | ResultsFileNode | StashFileNode) {
if (
!(node instanceof CommitFileNode) &&
!(node instanceof StashFileNode) &&
!(node instanceof ResultsFileNode)
) {
return;
}

void (await this.openFile(node));
void (await Container.fileAnnotations.toggle(
window.activeTextEditor,
FileAnnotationType.RecentChanges,
node.ref,
true
));
}

@debug()
private async highlightRevisionChanges(node: CommitFileNode | ResultsFileNode | StashFileNode) {
if (
!(node instanceof CommitFileNode) &&
!(node instanceof StashFileNode) &&
!(node instanceof ResultsFileNode)
) {
return;
}

void (await this.openFileRevision(node, { showOptions: { preserveFocus: true, preview: true } }));
void (await Container.fileAnnotations.toggle(
window.activeTextEditor,
FileAnnotationType.RecentChanges,
node.ref,
true
));
}

@debug()
private async merge(node: BranchNode | TagNode) {
if (!(node instanceof BranchNode) && !(node instanceof TagNode)) return undefined;
Expand Down

0 comments on commit d60fdfd

Please sign in to comment.