Skip to content

Commit 2bce0e5

Browse files
committed
Maintains line when opening changes from hovers
1 parent d09db90 commit 2bce0e5

File tree

6 files changed

+79
-72
lines changed

6 files changed

+79
-72
lines changed

src/annotations/annotations.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ export class Annotations {
109109
static getHoverDiffMessage(
110110
commit: GitCommit,
111111
uri: GitUri,
112-
hunkLine: GitDiffHunkLine | undefined
112+
hunkLine: GitDiffHunkLine | undefined,
113+
editorLine?: number
113114
): MarkdownString | undefined {
114115
if (hunkLine === undefined || commit.previousSha === undefined) return undefined;
115116

@@ -118,26 +119,33 @@ export class Annotations {
118119
let message: string;
119120
if (commit.isUncommitted) {
120121
if (uri.sha !== undefined && GitService.isStagedUncommitted(uri.sha)) {
121-
message = `[\`Changes\`](${DiffWithCommand.getMarkdownCommandArgs(commit)} "Open Changes")   ${
122-
GlyphChars.Dash
123-
}   [\`${commit.previousShortSha}\`](${ShowQuickCommitDetailsCommand.getMarkdownCommandArgs(
122+
message = `[\`Changes\`](${DiffWithCommand.getMarkdownCommandArgs(
123+
commit,
124+
editorLine
125+
)} "Open Changes")   ${GlyphChars.Dash}   [\`${
126+
commit.previousShortSha
127+
}\`](${ShowQuickCommitDetailsCommand.getMarkdownCommandArgs(
124128
commit.previousSha!
125129
)} "Show Commit Details") ${GlyphChars.ArrowLeftRightLong} _${uri.shortSha}_\n${diff}`;
126130
}
127131
else {
128-
message = `[\`Changes\`](${DiffWithCommand.getMarkdownCommandArgs(commit)} "Open Changes")   ${
129-
GlyphChars.Dash
130-
}   _uncommitted changes_\n${diff}`;
132+
message = `[\`Changes\`](${DiffWithCommand.getMarkdownCommandArgs(
133+
commit,
134+
editorLine
135+
)} "Open Changes")   ${GlyphChars.Dash}   _uncommitted changes_\n${diff}`;
131136
}
132137
}
133138
else {
134-
message = `[\`Changes\`](${DiffWithCommand.getMarkdownCommandArgs(commit)} "Open Changes")   ${
135-
GlyphChars.Dash
136-
}   [\`${commit.previousShortSha}\`](${ShowQuickCommitDetailsCommand.getMarkdownCommandArgs(
137-
commit.previousSha!
138-
)} "Show Commit Details") ${GlyphChars.ArrowLeftRightLong} [\`${
139-
commit.shortSha
140-
}\`](${ShowQuickCommitDetailsCommand.getMarkdownCommandArgs(commit.sha)} "Show Commit Details")\n${diff}`;
139+
message = `[\`Changes\`](${DiffWithCommand.getMarkdownCommandArgs(
140+
commit,
141+
editorLine
142+
)} "Open Changes")   ${GlyphChars.Dash}   [\`${
143+
commit.previousShortSha
144+
}\`](${ShowQuickCommitDetailsCommand.getMarkdownCommandArgs(commit.previousSha!)} "Show Commit Details") ${
145+
GlyphChars.ArrowLeftRightLong
146+
} [\`${commit.shortSha}\`](${ShowQuickCommitDetailsCommand.getMarkdownCommandArgs(
147+
commit.sha
148+
)} "Show Commit Details")\n${diff}`;
141149
}
142150

143151
const markdown = new MarkdownString(message);
@@ -173,8 +181,9 @@ export class Annotations {
173181
const line = editorLine + 1;
174182
const commitLine = commit.lines.find(l => l.line === line) || commit.lines[0];
175183

176-
const hunkLine = await Container.git.getDiffForLine(uri, commitLine.originalLine - 1, ref);
177-
const message = this.getHoverDiffMessage(commit, uri, hunkLine);
184+
const commitEditorLine = commitLine.originalLine - 1;
185+
const hunkLine = await Container.git.getDiffForLine(uri, commitEditorLine, ref);
186+
const message = this.getHoverDiffMessage(commit, uri, hunkLine, commitEditorLine);
178187

179188
return {
180189
hoverMessage: message

src/annotations/blameAnnotationProvider.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,17 @@ export abstract class BlameAnnotationProviderBase extends AnnotationProviderBase
226226
}
227227
}
228228

229+
let editorLine = this.editor.selection.active.line;
230+
const line = editorLine + 1;
231+
const commitLine = commit.lines.find(l => l.line === line) || commit.lines[0];
232+
editorLine = commitLine.originalLine - 1;
233+
229234
const message = Annotations.getHoverMessage(
230235
logCommit || commit,
231236
Container.config.defaultDateFormat,
232237
await Container.git.getRemotes(commit.repoPath),
233238
this.annotationType,
234-
this.editor.selection.active.line
239+
editorLine
235240
);
236241
return new Hover(
237242
message,

src/annotations/recentChangesAnnotationProvider.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ export class RecentChangesAnnotationProvider extends AnnotationProviderBase {
4545
for (const hunk of diff.hunks) {
4646
// Subtract 2 because editor lines are 0-based and we will be adding 1 in the first iteration of the loop
4747
let count = hunk.currentPosition.start - 2;
48-
for (const line of hunk.lines) {
49-
if (line.current === undefined) continue;
48+
for (const hunkLine of hunk.lines) {
49+
if (hunkLine.current === undefined) continue;
5050

5151
count++;
5252

53-
if (line.current.state === 'unchanged') continue;
53+
if (hunkLine.current.state === 'unchanged') continue;
5454

5555
const range = this.editor.document.validateRange(
5656
new Range(new Position(count, 0), new Position(count, Number.MAX_SAFE_INTEGER))
@@ -66,14 +66,14 @@ export class RecentChangesAnnotationProvider extends AnnotationProviderBase {
6666
dateFormat,
6767
await Container.git.getRemotes(commit.repoPath),
6868
this.annotationType,
69-
this.editor.selection.active.line
69+
count
7070
),
7171
range: range
7272
});
7373
}
7474

7575
if (cfg.hovers.annotations.changes) {
76-
message = Annotations.getHoverDiffMessage(commit, this._uri, line);
76+
message = Annotations.getHoverDiffMessage(commit, this._uri, hunkLine, count);
7777
if (message === undefined) continue;
7878
}
7979
}

src/commands/diffWith.ts

Lines changed: 35 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ export interface DiffWithCommandArgsRevision {
1515
}
1616

1717
export interface DiffWithCommandArgs {
18-
lhs?: DiffWithCommandArgsRevision;
19-
rhs?: DiffWithCommandArgsRevision;
20-
repoPath?: string;
18+
lhs: DiffWithCommandArgsRevision;
19+
rhs: DiffWithCommandArgsRevision;
20+
repoPath: string | undefined;
2121

2222
line?: number;
2323
showOptions?: TextDocumentShowOptions;
@@ -26,59 +26,43 @@ export interface DiffWithCommandArgs {
2626
@command()
2727
export class DiffWithCommand extends ActiveEditorCommand {
2828
static getMarkdownCommandArgs(args: DiffWithCommandArgs): string;
29-
static getMarkdownCommandArgs(commit1: GitCommit, commit2: GitCommit): string;
30-
static getMarkdownCommandArgs(argsOrCommit1: DiffWithCommandArgs | GitCommit, commit2?: GitCommit): string {
29+
static getMarkdownCommandArgs(commit: GitCommit, line?: number): string;
30+
static getMarkdownCommandArgs(argsOrCommit: DiffWithCommandArgs | GitCommit, line?: number): string {
3131
let args: DiffWithCommandArgs | GitCommit;
32-
if (argsOrCommit1 instanceof GitCommit) {
33-
const commit1 = argsOrCommit1;
34-
35-
if (commit2 === undefined) {
36-
if (commit1.isUncommitted) {
37-
args = {
38-
repoPath: commit1.repoPath,
39-
lhs: {
40-
sha: 'HEAD',
41-
uri: commit1.uri
42-
},
43-
rhs: {
44-
sha: '',
45-
uri: commit1.uri
46-
}
47-
};
48-
}
49-
else {
50-
args = {
51-
repoPath: commit1.repoPath,
52-
lhs: {
53-
sha:
54-
commit1.previousSha !== undefined
55-
? commit1.previousSha
56-
: GitService.deletedOrMissingSha,
57-
uri: commit1.previousUri!
58-
},
59-
rhs: {
60-
sha: commit1.sha,
61-
uri: commit1.uri
62-
}
63-
};
64-
}
32+
if (argsOrCommit instanceof GitCommit) {
33+
const commit = argsOrCommit;
34+
35+
if (commit.isUncommitted) {
36+
args = {
37+
repoPath: commit.repoPath,
38+
lhs: {
39+
sha: 'HEAD',
40+
uri: commit.uri
41+
},
42+
rhs: {
43+
sha: '',
44+
uri: commit.uri
45+
},
46+
line: line
47+
};
6548
}
6649
else {
6750
args = {
68-
repoPath: commit1.repoPath,
51+
repoPath: commit.repoPath,
6952
lhs: {
70-
sha: commit1.sha,
71-
uri: commit1.uri
53+
sha: commit.previousSha !== undefined ? commit.previousSha : GitService.deletedOrMissingSha,
54+
uri: commit.previousUri!
7255
},
7356
rhs: {
74-
sha: commit2.sha,
75-
uri: commit2.uri
76-
}
57+
sha: commit.sha,
58+
uri: commit.uri
59+
},
60+
line: line
7761
};
7862
}
7963
}
8064
else {
81-
args = argsOrCommit1;
65+
args = argsOrCommit;
8266
}
8367

8468
return super.getMarkdownCommandArgsCore<DiffWithCommandArgs>(Commands.DiffWith, args);
@@ -88,14 +72,17 @@ export class DiffWithCommand extends ActiveEditorCommand {
8872
super(Commands.DiffWith);
8973
}
9074

91-
async execute(editor?: TextEditor, uri?: Uri, args: DiffWithCommandArgs = {}): Promise<any> {
75+
async execute(editor?: TextEditor, uri?: Uri, args?: DiffWithCommandArgs): Promise<any> {
76+
if (args === undefined || args.lhs === undefined || args.rhs === undefined) return undefined;
77+
9278
args = {
9379
...args,
9480
lhs: { ...(args.lhs as DiffWithCommandArgsRevision) },
9581
rhs: { ...(args.rhs as DiffWithCommandArgsRevision) },
96-
showOptions: { ...args.showOptions }
82+
showOptions: args.showOptions === undefined ? undefined : { ...args.showOptions }
9783
};
98-
if (args.repoPath === undefined || args.lhs === undefined || args.rhs === undefined) return undefined;
84+
85+
if (args.repoPath === undefined) return undefined;
9986

10087
try {
10188
let lhsSha = args.lhs.sha;

src/git/formatters/commitFormatter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ export class CommitFormatter extends Formatter<GitCommit, CommitFormatOptions> {
123123
let commands = `[\`${this.id}\`](${ShowQuickCommitDetailsCommand.getMarkdownCommandArgs(
124124
this._item.sha
125125
)} "Show Commit Details") [\`${GlyphChars.MuchGreaterThan}\`](${DiffWithCommand.getMarkdownCommandArgs(
126-
this._item
126+
this._item,
127+
this._options.line
127128
)} "Open Changes") `;
128129

129130
if (this._item.previousSha !== undefined) {

src/hovers/lineHoverController.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ export class LineHoverController implements Disposable {
107107
}
108108
}
109109

110+
let editorLine = position.line;
111+
const line = editorLine + 1;
112+
const commitLine = commit.lines.find(l => l.line === line) || commit.lines[0];
113+
editorLine = commitLine.originalLine - 1;
114+
110115
const trackedDocument = await Container.tracker.get(document);
111116
if (trackedDocument === undefined) return undefined;
112117

@@ -115,7 +120,7 @@ export class LineHoverController implements Disposable {
115120
Container.config.defaultDateFormat,
116121
await Container.git.getRemotes(commit.repoPath),
117122
fileAnnotations,
118-
position.line
123+
editorLine
119124
);
120125
return new Hover(message, range);
121126
}

0 commit comments

Comments
 (0)