Skip to content

Commit

Permalink
Maintains line when opening changes from hovers
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Apr 22, 2019
1 parent d09db90 commit 2bce0e5
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 72 deletions.
41 changes: 25 additions & 16 deletions src/annotations/annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ export class Annotations {
static getHoverDiffMessage(
commit: GitCommit,
uri: GitUri,
hunkLine: GitDiffHunkLine | undefined
hunkLine: GitDiffHunkLine | undefined,
editorLine?: number
): MarkdownString | undefined {
if (hunkLine === undefined || commit.previousSha === undefined) return undefined;

Expand All @@ -118,26 +119,33 @@ export class Annotations {
let message: string;
if (commit.isUncommitted) {
if (uri.sha !== undefined && GitService.isStagedUncommitted(uri.sha)) {
message = `[\`Changes\`](${DiffWithCommand.getMarkdownCommandArgs(commit)} "Open Changes")   ${
GlyphChars.Dash
}   [\`${commit.previousShortSha}\`](${ShowQuickCommitDetailsCommand.getMarkdownCommandArgs(
message = `[\`Changes\`](${DiffWithCommand.getMarkdownCommandArgs(
commit,
editorLine
)} "Open Changes")   ${GlyphChars.Dash}   [\`${
commit.previousShortSha
}\`](${ShowQuickCommitDetailsCommand.getMarkdownCommandArgs(
commit.previousSha!
)} "Show Commit Details") ${GlyphChars.ArrowLeftRightLong} _${uri.shortSha}_\n${diff}`;
}
else {
message = `[\`Changes\`](${DiffWithCommand.getMarkdownCommandArgs(commit)} "Open Changes")   ${
GlyphChars.Dash
}   _uncommitted changes_\n${diff}`;
message = `[\`Changes\`](${DiffWithCommand.getMarkdownCommandArgs(
commit,
editorLine
)} "Open Changes")   ${GlyphChars.Dash}   _uncommitted changes_\n${diff}`;
}
}
else {
message = `[\`Changes\`](${DiffWithCommand.getMarkdownCommandArgs(commit)} "Open Changes")   ${
GlyphChars.Dash
}   [\`${commit.previousShortSha}\`](${ShowQuickCommitDetailsCommand.getMarkdownCommandArgs(
commit.previousSha!
)} "Show Commit Details") ${GlyphChars.ArrowLeftRightLong} [\`${
commit.shortSha
}\`](${ShowQuickCommitDetailsCommand.getMarkdownCommandArgs(commit.sha)} "Show Commit Details")\n${diff}`;
message = `[\`Changes\`](${DiffWithCommand.getMarkdownCommandArgs(
commit,
editorLine
)} "Open Changes")   ${GlyphChars.Dash}   [\`${
commit.previousShortSha
}\`](${ShowQuickCommitDetailsCommand.getMarkdownCommandArgs(commit.previousSha!)} "Show Commit Details") ${
GlyphChars.ArrowLeftRightLong
} [\`${commit.shortSha}\`](${ShowQuickCommitDetailsCommand.getMarkdownCommandArgs(
commit.sha
)} "Show Commit Details")\n${diff}`;
}

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

const hunkLine = await Container.git.getDiffForLine(uri, commitLine.originalLine - 1, ref);
const message = this.getHoverDiffMessage(commit, uri, hunkLine);
const commitEditorLine = commitLine.originalLine - 1;
const hunkLine = await Container.git.getDiffForLine(uri, commitEditorLine, ref);
const message = this.getHoverDiffMessage(commit, uri, hunkLine, commitEditorLine);

return {
hoverMessage: message
Expand Down
7 changes: 6 additions & 1 deletion src/annotations/blameAnnotationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,17 @@ export abstract class BlameAnnotationProviderBase extends AnnotationProviderBase
}
}

let editorLine = this.editor.selection.active.line;
const line = editorLine + 1;
const commitLine = commit.lines.find(l => l.line === line) || commit.lines[0];
editorLine = commitLine.originalLine - 1;

const message = Annotations.getHoverMessage(
logCommit || commit,
Container.config.defaultDateFormat,
await Container.git.getRemotes(commit.repoPath),
this.annotationType,
this.editor.selection.active.line
editorLine
);
return new Hover(
message,
Expand Down
10 changes: 5 additions & 5 deletions src/annotations/recentChangesAnnotationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ export class RecentChangesAnnotationProvider extends AnnotationProviderBase {
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;
for (const line of hunk.lines) {
if (line.current === undefined) continue;
for (const hunkLine of hunk.lines) {
if (hunkLine.current === undefined) continue;

count++;

if (line.current.state === 'unchanged') continue;
if (hunkLine.current.state === 'unchanged') continue;

const range = this.editor.document.validateRange(
new Range(new Position(count, 0), new Position(count, Number.MAX_SAFE_INTEGER))
Expand All @@ -66,14 +66,14 @@ export class RecentChangesAnnotationProvider extends AnnotationProviderBase {
dateFormat,
await Container.git.getRemotes(commit.repoPath),
this.annotationType,
this.editor.selection.active.line
count
),
range: range
});
}

if (cfg.hovers.annotations.changes) {
message = Annotations.getHoverDiffMessage(commit, this._uri, line);
message = Annotations.getHoverDiffMessage(commit, this._uri, hunkLine, count);
if (message === undefined) continue;
}
}
Expand Down
83 changes: 35 additions & 48 deletions src/commands/diffWith.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ export interface DiffWithCommandArgsRevision {
}

export interface DiffWithCommandArgs {
lhs?: DiffWithCommandArgsRevision;
rhs?: DiffWithCommandArgsRevision;
repoPath?: string;
lhs: DiffWithCommandArgsRevision;
rhs: DiffWithCommandArgsRevision;
repoPath: string | undefined;

line?: number;
showOptions?: TextDocumentShowOptions;
Expand All @@ -26,59 +26,43 @@ export interface DiffWithCommandArgs {
@command()
export class DiffWithCommand extends ActiveEditorCommand {
static getMarkdownCommandArgs(args: DiffWithCommandArgs): string;
static getMarkdownCommandArgs(commit1: GitCommit, commit2: GitCommit): string;
static getMarkdownCommandArgs(argsOrCommit1: DiffWithCommandArgs | GitCommit, commit2?: GitCommit): string {
static getMarkdownCommandArgs(commit: GitCommit, line?: number): string;
static getMarkdownCommandArgs(argsOrCommit: DiffWithCommandArgs | GitCommit, line?: number): string {
let args: DiffWithCommandArgs | GitCommit;
if (argsOrCommit1 instanceof GitCommit) {
const commit1 = argsOrCommit1;

if (commit2 === undefined) {
if (commit1.isUncommitted) {
args = {
repoPath: commit1.repoPath,
lhs: {
sha: 'HEAD',
uri: commit1.uri
},
rhs: {
sha: '',
uri: commit1.uri
}
};
}
else {
args = {
repoPath: commit1.repoPath,
lhs: {
sha:
commit1.previousSha !== undefined
? commit1.previousSha
: GitService.deletedOrMissingSha,
uri: commit1.previousUri!
},
rhs: {
sha: commit1.sha,
uri: commit1.uri
}
};
}
if (argsOrCommit instanceof GitCommit) {
const commit = argsOrCommit;

if (commit.isUncommitted) {
args = {
repoPath: commit.repoPath,
lhs: {
sha: 'HEAD',
uri: commit.uri
},
rhs: {
sha: '',
uri: commit.uri
},
line: line
};
}
else {
args = {
repoPath: commit1.repoPath,
repoPath: commit.repoPath,
lhs: {
sha: commit1.sha,
uri: commit1.uri
sha: commit.previousSha !== undefined ? commit.previousSha : GitService.deletedOrMissingSha,
uri: commit.previousUri!
},
rhs: {
sha: commit2.sha,
uri: commit2.uri
}
sha: commit.sha,
uri: commit.uri
},
line: line
};
}
}
else {
args = argsOrCommit1;
args = argsOrCommit;
}

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

async execute(editor?: TextEditor, uri?: Uri, args: DiffWithCommandArgs = {}): Promise<any> {
async execute(editor?: TextEditor, uri?: Uri, args?: DiffWithCommandArgs): Promise<any> {
if (args === undefined || args.lhs === undefined || args.rhs === undefined) return undefined;

args = {
...args,
lhs: { ...(args.lhs as DiffWithCommandArgsRevision) },
rhs: { ...(args.rhs as DiffWithCommandArgsRevision) },
showOptions: { ...args.showOptions }
showOptions: args.showOptions === undefined ? undefined : { ...args.showOptions }
};
if (args.repoPath === undefined || args.lhs === undefined || args.rhs === undefined) return undefined;

if (args.repoPath === undefined) return undefined;

try {
let lhsSha = args.lhs.sha;
Expand Down
3 changes: 2 additions & 1 deletion src/git/formatters/commitFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ export class CommitFormatter extends Formatter<GitCommit, CommitFormatOptions> {
let commands = `[\`${this.id}\`](${ShowQuickCommitDetailsCommand.getMarkdownCommandArgs(
this._item.sha
)} "Show Commit Details") [\`${GlyphChars.MuchGreaterThan}\`](${DiffWithCommand.getMarkdownCommandArgs(
this._item
this._item,
this._options.line
)} "Open Changes") `;

if (this._item.previousSha !== undefined) {
Expand Down
7 changes: 6 additions & 1 deletion src/hovers/lineHoverController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ export class LineHoverController implements Disposable {
}
}

let editorLine = position.line;
const line = editorLine + 1;
const commitLine = commit.lines.find(l => l.line === line) || commit.lines[0];
editorLine = commitLine.originalLine - 1;

const trackedDocument = await Container.tracker.get(document);
if (trackedDocument === undefined) return undefined;

Expand All @@ -115,7 +120,7 @@ export class LineHoverController implements Disposable {
Container.config.defaultDateFormat,
await Container.git.getRemotes(commit.repoPath),
fileAnnotations,
position.line
editorLine
);
return new Hover(message, range);
}
Expand Down

0 comments on commit 2bce0e5

Please sign in to comment.