Browse Source

Maintains line when opening changes from hovers

main
Eric Amodio 5 years ago
parent
commit
2bce0e5b74
6 changed files with 79 additions and 72 deletions
  1. +25
    -16
      src/annotations/annotations.ts
  2. +6
    -1
      src/annotations/blameAnnotationProvider.ts
  3. +5
    -5
      src/annotations/recentChangesAnnotationProvider.ts
  4. +35
    -48
      src/commands/diffWith.ts
  5. +2
    -1
      src/git/formatters/commitFormatter.ts
  6. +6
    -1
      src/hovers/lineHoverController.ts

+ 25
- 16
src/annotations/annotations.ts View File

@ -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;
@ -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);
@ -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

+ 6
- 1
src/annotations/blameAnnotationProvider.ts View File

@ -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,

+ 5
- 5
src/annotations/recentChangesAnnotationProvider.ts View File

@ -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))
@ -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;
}
}

+ 35
- 48
src/commands/diffWith.ts View File

@ -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;
@ -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);
@ -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;

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

@ -123,7 +123,8 @@ export class CommitFormatter extends Formatter {
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) {

+ 6
- 1
src/hovers/lineHoverController.ts View File

@ -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;
@ -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);
}

Loading…
Cancel
Save