Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/git/src/gitEditor.ts
3314 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
import * as path from 'path';
6
import { CancellationToken, DocumentLink, DocumentLinkProvider, l10n, Range, TabInputText, TextDocument, Uri, window, workspace } from 'vscode';
7
import { IIPCHandler, IIPCServer } from './ipc/ipcServer';
8
import { ITerminalEnvironmentProvider } from './terminal';
9
import { EmptyDisposable, IDisposable } from './util';
10
import { Model } from './model';
11
import { Repository } from './repository';
12
13
interface GitEditorRequest {
14
commitMessagePath?: string;
15
}
16
17
export class GitEditor implements IIPCHandler, ITerminalEnvironmentProvider {
18
19
private env: { [key: string]: string };
20
private disposable: IDisposable = EmptyDisposable;
21
22
readonly featureDescription = 'git editor';
23
24
constructor(ipc?: IIPCServer) {
25
if (ipc) {
26
this.disposable = ipc.registerHandler('git-editor', this);
27
}
28
29
this.env = {
30
GIT_EDITOR: `"${path.join(__dirname, ipc ? 'git-editor.sh' : 'git-editor-empty.sh')}"`,
31
VSCODE_GIT_EDITOR_NODE: process.execPath,
32
VSCODE_GIT_EDITOR_EXTRA_ARGS: '',
33
VSCODE_GIT_EDITOR_MAIN: path.join(__dirname, 'git-editor-main.js')
34
};
35
}
36
37
async handle({ commitMessagePath }: GitEditorRequest): Promise<any> {
38
if (commitMessagePath) {
39
const uri = Uri.file(commitMessagePath);
40
const doc = await workspace.openTextDocument(uri);
41
await window.showTextDocument(doc, { preview: false });
42
43
return new Promise((c) => {
44
const onDidClose = window.tabGroups.onDidChangeTabs(async (tabs) => {
45
if (tabs.closed.some(t => t.input instanceof TabInputText && t.input.uri.toString() === uri.toString())) {
46
onDidClose.dispose();
47
return c(true);
48
}
49
});
50
});
51
}
52
}
53
54
getEnv(): { [key: string]: string } {
55
const config = workspace.getConfiguration('git');
56
return config.get<boolean>('useEditorAsCommitInput') ? this.env : {};
57
}
58
59
getTerminalEnv(): { [key: string]: string } {
60
const config = workspace.getConfiguration('git');
61
return config.get<boolean>('useEditorAsCommitInput') && config.get<boolean>('terminalGitEditor') ? this.env : {};
62
}
63
64
dispose(): void {
65
this.disposable.dispose();
66
}
67
}
68
69
export class GitEditorDocumentLinkProvider implements DocumentLinkProvider {
70
private readonly _regex = /^#\s+(modified|new file|deleted|renamed|copied|type change):\s+(?<file1>.*?)(?:\s+->\s+(?<file2>.*))*$/gm;
71
72
constructor(private readonly _model: Model) { }
73
74
provideDocumentLinks(document: TextDocument, token: CancellationToken): DocumentLink[] {
75
if (token.isCancellationRequested) {
76
return [];
77
}
78
79
const repository = this._model.getRepository(document.uri);
80
if (!repository) {
81
return [];
82
}
83
84
const links: DocumentLink[] = [];
85
for (const match of document.getText().matchAll(this._regex)) {
86
if (!match.groups) {
87
continue;
88
}
89
90
const { file1, file2 } = match.groups;
91
92
if (file1) {
93
links.push(this._createDocumentLink(repository, document, match, file1));
94
}
95
if (file2) {
96
links.push(this._createDocumentLink(repository, document, match, file2));
97
}
98
}
99
100
return links;
101
}
102
103
private _createDocumentLink(repository: Repository, document: TextDocument, match: RegExpExecArray, file: string): DocumentLink {
104
const startIndex = match[0].indexOf(file);
105
const startPosition = document.positionAt(match.index + startIndex);
106
const endPosition = document.positionAt(match.index + startIndex + file.length);
107
108
const documentLink = new DocumentLink(
109
new Range(startPosition, endPosition),
110
Uri.file(path.join(repository.root, file)));
111
documentLink.tooltip = l10n.t('Open File');
112
113
return documentLink;
114
}
115
}
116
117