Skip to content

Commit a3912b7

Browse files
committed
Remembers editor annotation state
1 parent 9433552 commit a3912b7

File tree

7 files changed

+214
-142
lines changed

7 files changed

+214
-142
lines changed

src/annotations/annotationController.ts

Lines changed: 159 additions & 119 deletions
Large diffs are not rendered by default.

src/annotations/annotationProvider.ts

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
'use strict';
2-
import { Disposable, ExtensionContext, TextDocument, TextEditor, TextEditorDecorationType, TextEditorSelectionChangeEvent, window, workspace } from 'vscode';
2+
import { DecorationOptions, Disposable, ExtensionContext, TextDocument, TextEditor, TextEditorDecorationType, TextEditorSelectionChangeEvent, Uri, window, workspace } from 'vscode';
33
import { FileAnnotationType } from '../annotations/annotationController';
44
import { TextDocumentComparer } from '../comparers';
55
import { ExtensionKey, IConfig } from '../configuration';
66

7-
export abstract class AnnotationProviderBase extends Disposable {
7+
export type TextEditorCorrelationKey = string;
8+
9+
export abstract class AnnotationProviderBase extends Disposable {
10+
11+
static getCorrelationKey(editor: TextEditor | undefined): TextEditorCorrelationKey {
12+
return editor !== undefined ? (editor as any).id : '';
13+
}
814

915
public annotationType: FileAnnotationType;
16+
public correlationKey: TextEditorCorrelationKey;
1017
public document: TextDocument;
1118

1219
protected _config: IConfig;
20+
protected _decorations: DecorationOptions[] | undefined;
1321
protected _disposable: Disposable;
1422

1523
constructor(
@@ -20,14 +28,14 @@ import { ExtensionKey, IConfig } from '../configuration';
2028
) {
2129
super(() => this.dispose());
2230

31+
this.correlationKey = AnnotationProviderBase.getCorrelationKey(this.editor);
2332
this.document = this.editor.document;
2433

2534
this._config = workspace.getConfiguration().get<IConfig>(ExtensionKey)!;
2635

27-
const subscriptions: Disposable[] = [];
28-
29-
subscriptions.push(window.onDidChangeTextEditorSelection(this._onTextEditorSelectionChanged, this));
30-
36+
const subscriptions: Disposable[] = [
37+
window.onDidChangeTextEditorSelection(this._onTextEditorSelectionChanged, this)
38+
];
3139
this._disposable = Disposable.from(...subscriptions);
3240
}
3341

@@ -43,6 +51,16 @@ import { ExtensionKey, IConfig } from '../configuration';
4351
return this.selection(e.selections[0].active.line);
4452
}
4553

54+
get editorId(): string {
55+
if (this.editor === undefined || this.editor.document === undefined) return '';
56+
return (this.editor as any).id;
57+
}
58+
59+
get editorUri(): Uri | undefined {
60+
if (this.editor === undefined || this.editor.document === undefined) return undefined;
61+
return this.editor.document.uri;
62+
}
63+
4664
async clear() {
4765
if (this.editor !== undefined) {
4866
try {
@@ -68,6 +86,20 @@ import { ExtensionKey, IConfig } from '../configuration';
6886
await this.provideAnnotation(this.editor === undefined ? undefined : this.editor.selection.active.line);
6987
}
7088

89+
restore(editor: TextEditor, force: boolean = false) {
90+
// If the editor isn't disposed then we don't need to do anything
91+
// Explicitly check for `false`
92+
if (!force && (this.editor as any)._disposed === false) return;
93+
94+
this.editor = editor;
95+
this.correlationKey = AnnotationProviderBase.getCorrelationKey(editor);
96+
this.document = editor.document;
97+
98+
if (this._decorations !== undefined && this._decorations.length) {
99+
this.editor.setDecorations(this.decoration!, this._decorations);
100+
}
101+
}
102+
71103
abstract async provideAnnotation(shaOrLine?: string | number): Promise<boolean>;
72104
abstract async selection(shaOrLine?: string | number): Promise<void>;
73105
abstract async validate(): Promise<boolean>;

src/annotations/gutterBlameAnnotationProvider.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class GutterBlameAnnotationProvider extends BlameAnnotationProviderBase {
3636
const renderOptions = Annotations.gutterRenderOptions(this._config.theme, cfg.heatmap, options);
3737
const separateLines = this._config.theme.annotations.file.gutter.separateLines;
3838

39-
const decorations: DecorationOptions[] = [];
39+
this._decorations = [];
4040
const decorationsMap: { [sha: string]: DecorationOptions | undefined } = Object.create(null);
4141

4242
let commit: GitBlameCommit | undefined;
@@ -77,7 +77,7 @@ export class GutterBlameAnnotationProvider extends BlameAnnotationProviderBase {
7777

7878
gutter.range = new Range(line, 0, line, 0);
7979

80-
decorations.push(gutter);
80+
this._decorations.push(gutter);
8181

8282
continue;
8383
}
@@ -92,7 +92,7 @@ export class GutterBlameAnnotationProvider extends BlameAnnotationProviderBase {
9292
range: new Range(line, 0, line, 0)
9393
} as DecorationOptions;
9494

95-
decorations.push(gutter);
95+
this._decorations.push(gutter);
9696

9797
continue;
9898
}
@@ -108,12 +108,12 @@ export class GutterBlameAnnotationProvider extends BlameAnnotationProviderBase {
108108

109109
gutter.range = new Range(line, 0, line, 0);
110110

111-
decorations.push(gutter);
111+
this._decorations.push(gutter);
112112
decorationsMap[l.sha] = gutter;
113113
}
114114

115-
if (decorations.length) {
116-
this.editor.setDecorations(this.decoration!, decorations);
115+
if (this._decorations.length) {
116+
this.editor.setDecorations(this.decoration!, this._decorations);
117117
}
118118

119119
const duration = process.hrtime(start);

src/annotations/hoverBlameAnnotationProvider.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class HoverBlameAnnotationProvider extends BlameAnnotationProviderBase {
2222
const now = Date.now();
2323
const renderOptions = Annotations.hoverRenderOptions(this._config.theme, cfg.heatmap);
2424

25-
const decorations: DecorationOptions[] = [];
25+
this._decorations = [];
2626
const decorationsMap: { [sha: string]: DecorationOptions } = Object.create(null);
2727

2828
let commit: GitBlameCommit | undefined;
@@ -39,7 +39,7 @@ export class HoverBlameAnnotationProvider extends BlameAnnotationProviderBase {
3939
range: new Range(line, 0, line, 0)
4040
} as DecorationOptions;
4141

42-
decorations.push(hover);
42+
this._decorations.push(hover);
4343

4444
continue;
4545
}
@@ -50,13 +50,13 @@ export class HoverBlameAnnotationProvider extends BlameAnnotationProviderBase {
5050
hover = Annotations.hover(commit, renderOptions, now);
5151
hover.range = new Range(line, 0, line, 0);
5252

53-
decorations.push(hover);
53+
this._decorations.push(hover);
5454
decorationsMap[l.sha] = hover;
5555

5656
}
5757

58-
if (decorations.length) {
59-
this.editor.setDecorations(this.decoration!, decorations);
58+
if (this._decorations.length) {
59+
this.editor.setDecorations(this.decoration!, this._decorations);
6060
}
6161

6262
const duration = process.hrtime(start);

src/annotations/recentChangesAnnotationProvider.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class RecentChangesAnnotationProvider extends AnnotationProviderBase {
3333
const cfg = this._config.annotations.file.recentChanges;
3434
const dateFormat = this._config.defaultDateFormat;
3535

36-
const decorators: DecorationOptions[] = [];
36+
this._decorations = [];
3737

3838
for (const chunk of diff.chunks) {
3939
let count = chunk.currentPosition.start - 2;
@@ -47,7 +47,7 @@ export class RecentChangesAnnotationProvider extends AnnotationProviderBase {
4747
const range = this.editor.document.validateRange(new Range(new Position(count, 0), new Position(count, endOfLineIndex)));
4848

4949
if (cfg.hover.details) {
50-
decorators.push({
50+
this._decorations.push({
5151
hoverMessage: Annotations.getHoverMessage(commit, dateFormat, this.git.hasRemotes(commit.repoPath), this._config.blame.file.annotationType),
5252
range: range
5353
} as DecorationOptions);
@@ -58,14 +58,14 @@ export class RecentChangesAnnotationProvider extends AnnotationProviderBase {
5858
message = Annotations.getHoverDiffMessage(commit, line);
5959
}
6060

61-
decorators.push({
61+
this._decorations.push({
6262
hoverMessage: message,
6363
range: range
6464
} as DecorationOptions);
6565
}
6666
}
6767

68-
this.editor.setDecorations(this.highlightDecoration!, decorators);
68+
this.editor.setDecorations(this.highlightDecoration!, this._decorations);
6969

7070
const duration = process.hrtime(start);
7171
Logger.log(`${(duration[0] * 1000) + Math.floor(duration[1] / 1000000)} ms to compute recent changes annotations`);

src/commands/clearFileAnnotations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class ClearFileAnnotationsCommand extends EditorCommand {
1414
if (editor === undefined || editor.document === undefined || editor.document.isDirty) return undefined;
1515

1616
try {
17-
return this.annotationController.clear(editor.viewColumn || -1);
17+
return this.annotationController.clear(editor);
1818
}
1919
catch (ex) {
2020
Logger.error(ex, 'ClearFileAnnotationsCommand');

src/comparers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class TextEditorComparer extends Comparer<TextEditor> {
3434
if (options.usePosition && (lhs.viewColumn !== rhs.viewColumn)) return false;
3535

3636
if (options.useId && (!lhs.document || !rhs.document)) {
37-
if ((lhs as any)._id !== (rhs as any)._id) return false;
37+
if ((lhs as any).id !== (rhs as any).id) return false;
3838

3939
return true;
4040
}

0 commit comments

Comments
 (0)