-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
gitLineTracker.ts
196 lines (163 loc) · 5.63 KB
/
gitLineTracker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import type { TextEditor } from 'vscode';
import { Disposable } from 'vscode';
import { GlyphChars } from '../constants';
import type { Container } from '../container';
import type { GitCommit } from '../git/models/commit';
import { configuration } from '../system/configuration';
import { debug } from '../system/decorators/log';
import { getLogScope, setLogScopeExit } from '../system/logger.scope';
import type {
DocumentBlameStateChangeEvent,
DocumentContentChangeEvent,
DocumentDirtyIdleTriggerEvent,
DocumentDirtyStateChangeEvent,
} from './documentTracker';
import type { GitDocumentState } from './gitDocumentTracker';
import type { LinesChangeEvent, LineSelection } from './lineTracker';
import { LineTracker } from './lineTracker';
export interface GitLineState {
commit: GitCommit;
}
export class GitLineTracker extends LineTracker<GitLineState> {
constructor(private readonly container: Container) {
super();
}
protected override async fireLinesChanged(e: LinesChangeEvent) {
let updated = false;
if (!this.suspended && !e.pending && e.selections != null && e.editor != null) {
updated = await this.updateState(e.selections, e.editor);
}
super.fireLinesChanged(updated ? e : { ...e, selections: undefined, suspended: this.suspended });
}
private _subscriptionOnlyWhenActive: Disposable | undefined;
protected override onStart(): Disposable | undefined {
this.onResume();
return Disposable.from(
{ dispose: () => this.onSuspend() },
this.container.tracker.onDidChangeBlameState(this.onBlameStateChanged, this),
this.container.tracker.onDidChangeDirtyState(this.onDirtyStateChanged, this),
this.container.tracker.onDidTriggerDirtyIdle(this.onDirtyIdleTriggered, this),
);
}
protected override onResume(): void {
if (this._subscriptionOnlyWhenActive == null) {
this._subscriptionOnlyWhenActive = this.container.tracker.onDidChangeContent(this.onContentChanged, this);
}
}
protected override onSuspend(): void {
this._subscriptionOnlyWhenActive?.dispose();
this._subscriptionOnlyWhenActive = undefined;
}
@debug<GitLineTracker['onBlameStateChanged']>({
args: {
0: e => `editor/doc=${e.editor.document.uri.toString(true)}, blameable=${e.blameable}`,
},
})
private onBlameStateChanged(_e: DocumentBlameStateChangeEvent<GitDocumentState>) {
this.notifyLinesChanged('editor');
}
@debug<GitLineTracker['onContentChanged']>({
args: {
0: e => `editor/doc=${e.editor.document.uri.toString(true)}`,
},
})
private onContentChanged(e: DocumentContentChangeEvent<GitDocumentState>) {
if (
e.contentChanges.some(
scope =>
this.selections?.some(
selection =>
(scope.range.end.line >= selection.active && selection.active >= scope.range.start.line) ||
(scope.range.start.line >= selection.active && selection.active >= scope.range.end.line),
),
)
) {
this.notifyLinesChanged('editor');
}
}
@debug<GitLineTracker['onDirtyIdleTriggered']>({
args: {
0: e => `editor/doc=${e.editor.document.uri.toString(true)}`,
},
})
private onDirtyIdleTriggered(e: DocumentDirtyIdleTriggerEvent<GitDocumentState>) {
const maxLines = configuration.get('advanced.blame.sizeThresholdAfterEdit');
if (maxLines > 0 && e.document.lineCount > maxLines) return;
this.resume();
}
@debug<GitLineTracker['onDirtyStateChanged']>({
args: {
0: e => `editor/doc=${e.editor.document.uri.toString(true)}, dirty=${e.dirty}`,
},
})
private onDirtyStateChanged(e: DocumentDirtyStateChangeEvent<GitDocumentState>) {
if (e.dirty) {
this.suspend();
} else {
this.resume({ force: true });
}
}
@debug<GitLineTracker['updateState']>({
args: { 0: selections => selections?.map(s => s.active).join(','), 1: e => e.document.uri.toString(true) },
exit: true,
})
private async updateState(selections: LineSelection[], editor: TextEditor): Promise<boolean> {
const scope = getLogScope();
if (!this.includes(selections)) {
setLogScopeExit(scope, ` ${GlyphChars.Dot} lines no longer match`);
return false;
}
const trackedDocument = await this.container.tracker.getOrAdd(editor.document);
if (!trackedDocument.isBlameable) {
setLogScopeExit(scope, ` ${GlyphChars.Dot} document is not blameable`);
return false;
}
if (selections.length === 1) {
const blameLine = await this.container.git.getBlameForLine(
trackedDocument.uri,
selections[0].active,
editor?.document,
);
if (blameLine == null) {
setLogScopeExit(scope, ` ${GlyphChars.Dot} blame failed`);
return false;
}
if (blameLine.commit != null && blameLine.commit.file == null) {
debugger;
}
this.setState(blameLine.line.line - 1, { commit: blameLine.commit });
} else {
const blame = await this.container.git.getBlame(trackedDocument.uri, editor.document);
if (blame == null) {
setLogScopeExit(scope, ` ${GlyphChars.Dot} blame failed`);
return false;
}
for (const selection of selections) {
const commitLine = blame.lines[selection.active];
const commit = blame.commits.get(commitLine.sha);
if (commit != null && commit.file == null) {
debugger;
}
if (commit == null) {
debugger;
this.resetState(selection.active);
} else {
this.setState(selection.active, { commit: commit });
}
}
}
// Check again because of the awaits above
if (!this.includes(selections)) {
setLogScopeExit(scope, ` ${GlyphChars.Dot} lines no longer match`);
return false;
}
if (!trackedDocument.isBlameable) {
setLogScopeExit(scope, ` ${GlyphChars.Dot} document is not blameable`);
return false;
}
if (editor.document.isDirty) {
trackedDocument.setForceDirtyStateChangeOnNextDocumentChange();
}
return true;
}
}