Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/merge-conflict/src/codelensProvider.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as vscode from 'vscode';
7
import * as interfaces from './interfaces';
8
9
export default class MergeConflictCodeLensProvider implements vscode.CodeLensProvider, vscode.Disposable {
10
private codeLensRegistrationHandle?: vscode.Disposable | null;
11
private config?: interfaces.IExtensionConfiguration;
12
private tracker: interfaces.IDocumentMergeConflictTracker;
13
14
constructor(trackerService: interfaces.IDocumentMergeConflictTrackerService) {
15
this.tracker = trackerService.createTracker('codelens');
16
}
17
18
begin(config: interfaces.IExtensionConfiguration) {
19
this.config = config;
20
21
if (this.config.enableCodeLens) {
22
this.registerCodeLensProvider();
23
}
24
}
25
26
configurationUpdated(updatedConfig: interfaces.IExtensionConfiguration) {
27
28
if (updatedConfig.enableCodeLens === false && this.codeLensRegistrationHandle) {
29
this.codeLensRegistrationHandle.dispose();
30
this.codeLensRegistrationHandle = null;
31
}
32
else if (updatedConfig.enableCodeLens === true && !this.codeLensRegistrationHandle) {
33
this.registerCodeLensProvider();
34
}
35
36
this.config = updatedConfig;
37
}
38
39
40
dispose() {
41
if (this.codeLensRegistrationHandle) {
42
this.codeLensRegistrationHandle.dispose();
43
this.codeLensRegistrationHandle = null;
44
}
45
}
46
47
async provideCodeLenses(document: vscode.TextDocument, _token: vscode.CancellationToken): Promise<vscode.CodeLens[] | null> {
48
49
if (!this.config || !this.config.enableCodeLens) {
50
return null;
51
}
52
53
const conflicts = await this.tracker.getConflicts(document);
54
const conflictsCount = conflicts?.length ?? 0;
55
vscode.commands.executeCommand('setContext', 'mergeConflictsCount', conflictsCount);
56
57
if (!conflictsCount) {
58
return null;
59
}
60
61
const items: vscode.CodeLens[] = [];
62
63
conflicts.forEach(conflict => {
64
const acceptCurrentCommand: vscode.Command = {
65
command: 'merge-conflict.accept.current',
66
title: vscode.l10n.t("Accept Current Change"),
67
arguments: ['known-conflict', conflict]
68
};
69
70
const acceptIncomingCommand: vscode.Command = {
71
command: 'merge-conflict.accept.incoming',
72
title: vscode.l10n.t("Accept Incoming Change"),
73
arguments: ['known-conflict', conflict]
74
};
75
76
const acceptBothCommand: vscode.Command = {
77
command: 'merge-conflict.accept.both',
78
title: vscode.l10n.t("Accept Both Changes"),
79
arguments: ['known-conflict', conflict]
80
};
81
82
const diffCommand: vscode.Command = {
83
command: 'merge-conflict.compare',
84
title: vscode.l10n.t("Compare Changes"),
85
arguments: [conflict]
86
};
87
88
const range = document.lineAt(conflict.range.start.line).range;
89
items.push(
90
new vscode.CodeLens(range, acceptCurrentCommand),
91
new vscode.CodeLens(range, acceptIncomingCommand),
92
new vscode.CodeLens(range, acceptBothCommand),
93
new vscode.CodeLens(range, diffCommand)
94
);
95
});
96
97
return items;
98
}
99
100
private registerCodeLensProvider() {
101
this.codeLensRegistrationHandle = vscode.languages.registerCodeLensProvider([
102
{ scheme: 'file' },
103
{ scheme: 'vscode-vfs' },
104
{ scheme: 'untitled' },
105
{ scheme: 'vscode-userdata' },
106
], this);
107
}
108
}
109
110