Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/merge-conflict/src/contentProvider.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
8
export default class MergeConflictContentProvider implements vscode.TextDocumentContentProvider, vscode.Disposable {
9
10
static scheme = 'merge-conflict.conflict-diff';
11
12
constructor(private context: vscode.ExtensionContext) {
13
}
14
15
begin() {
16
this.context.subscriptions.push(
17
vscode.workspace.registerTextDocumentContentProvider(MergeConflictContentProvider.scheme, this)
18
);
19
}
20
21
dispose() {
22
}
23
24
async provideTextDocumentContent(uri: vscode.Uri): Promise<string | null> {
25
try {
26
const { scheme, ranges } = JSON.parse(uri.query) as { scheme: string; ranges: [{ line: number; character: number }[], { line: number; character: number }[]][] };
27
28
// complete diff
29
const document = await vscode.workspace.openTextDocument(uri.with({ scheme, query: '' }));
30
31
let text = '';
32
let lastPosition = new vscode.Position(0, 0);
33
34
ranges.forEach(rangeObj => {
35
const [conflictRange, fullRange] = rangeObj;
36
const [start, end] = conflictRange;
37
const [fullStart, fullEnd] = fullRange;
38
39
text += document.getText(new vscode.Range(lastPosition.line, lastPosition.character, fullStart.line, fullStart.character));
40
text += document.getText(new vscode.Range(start.line, start.character, end.line, end.character));
41
lastPosition = new vscode.Position(fullEnd.line, fullEnd.character);
42
});
43
44
const documentEnd = document.lineAt(document.lineCount - 1).range.end;
45
text += document.getText(new vscode.Range(lastPosition.line, lastPosition.character, documentEnd.line, documentEnd.character));
46
47
return text;
48
}
49
catch (ex) {
50
await vscode.window.showErrorMessage('Unable to show comparison');
51
return null;
52
}
53
}
54
}
55