-
Notifications
You must be signed in to change notification settings - Fork 39.9k
Expand file tree
/
Copy pathdiagnostics.ts
More file actions
244 lines (194 loc) · 9.22 KB
/
diagnostics.ts
File metadata and controls
244 lines (194 loc) · 9.22 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { CodeAction, CodeActionKind, CodeActionProvider, Diagnostic, DiagnosticCollection, DiagnosticSeverity, Disposable, Range, Selection, TextDocument, Uri, WorkspaceEdit, l10n, languages, workspace } from 'vscode';
import { mapEvent, filterEvent, dispose } from './util';
import { Model } from './model';
export enum DiagnosticCodes {
empty_message = 'empty_message',
line_length = 'line_length'
}
export class GitCommitInputBoxDiagnosticsManager {
private readonly diagnostics: DiagnosticCollection;
private readonly severity = DiagnosticSeverity.Warning;
private readonly disposables: Disposable[] = [];
constructor(private readonly model: Model) {
this.diagnostics = languages.createDiagnosticCollection();
this.migrateInputValidationSettings()
.then(() => {
mapEvent(filterEvent(workspace.onDidChangeTextDocument, e => e.document.uri.scheme === 'vscode-scm' || e.document.languageId === 'git-commit'), e => e.document)(this.onDidChangeTextDocument, this, this.disposables);
filterEvent(workspace.onDidCloseTextDocument, e => e.languageId === 'git-commit')(doc => this.diagnostics.delete(doc.uri), this, this.disposables);
filterEvent(workspace.onDidChangeConfiguration, e => e.affectsConfiguration('git.inputValidation') || e.affectsConfiguration('git.inputValidationLength') || e.affectsConfiguration('git.inputValidationSubjectLength'))(this.onDidChangeConfiguration, this, this.disposables);
});
}
public getDiagnostics(uri: Uri): ReadonlyArray<Diagnostic> {
return this.diagnostics.get(uri) ?? [];
}
private async migrateInputValidationSettings(): Promise<void> {
try {
const config = workspace.getConfiguration('git');
const inputValidation = config.inspect<'always' | 'warn' | 'off' | boolean>('inputValidation');
if (inputValidation === undefined) {
return;
}
// Workspace setting
if (typeof inputValidation.workspaceValue === 'string') {
await config.update('inputValidation', inputValidation.workspaceValue !== 'off', false);
}
// User setting
if (typeof inputValidation.globalValue === 'string') {
await config.update('inputValidation', inputValidation.workspaceValue !== 'off', true);
}
} catch { }
}
private onDidChangeConfiguration(): void {
for (const repository of this.model.repositories) {
this.onDidChangeTextDocument(repository.inputBox.document);
}
for (const document of workspace.textDocuments) {
if (document.languageId === 'git-commit') {
this.onDidChangeTextDocument(document);
}
}
}
private onDidChangeTextDocument(document: TextDocument): void {
const config = workspace.getConfiguration('git');
const inputValidation = config.get<boolean>('inputValidation', false);
if (!inputValidation) {
this.diagnostics.set(document.uri, undefined);
return;
}
if (/^\s+$/.test(document.getText())) {
const documentRange = new Range(document.lineAt(0).range.start, document.lineAt(document.lineCount - 1).range.end);
const diagnostic = new Diagnostic(documentRange, l10n.t('Current commit message only contains whitespace characters'), this.severity);
diagnostic.code = DiagnosticCodes.empty_message;
this.diagnostics.set(document.uri, [diagnostic]);
return;
}
const diagnostics: Diagnostic[] = [];
const inputValidationLength = config.get<number>('inputValidationLength', 50);
const inputValidationSubjectLength = config.get<number | undefined>('inputValidationSubjectLength', undefined);
const isCommitEditor = document.languageId === 'git-commit';
let isFirstMessageLine = true;
for (let index = 0; index < document.lineCount; index++) {
const line = document.lineAt(index);
// Skip git comment/metadata lines in the commit editor
if (isCommitEditor && line.text.startsWith('#')) {
continue;
}
const threshold = isFirstMessageLine ? inputValidationSubjectLength ?? inputValidationLength : inputValidationLength;
isFirstMessageLine = false;
if (line.text.length > threshold) {
const charactersOver = line.text.length - threshold;
const lineLengthMessage = charactersOver === 1
? l10n.t('{0} character over {1} in current line', charactersOver, threshold)
: l10n.t('{0} characters over {1} in current line', charactersOver, threshold);
const diagnostic = new Diagnostic(line.range, lineLengthMessage, this.severity);
diagnostic.code = DiagnosticCodes.line_length;
diagnostics.push(diagnostic);
}
}
this.diagnostics.set(document.uri, diagnostics);
}
dispose() {
dispose(this.disposables);
}
}
export class GitCommitInputBoxCodeActionsProvider implements CodeActionProvider {
private readonly disposables: Disposable[] = [];
constructor(private readonly diagnosticsManager: GitCommitInputBoxDiagnosticsManager) {
this.disposables.push(languages.registerCodeActionsProvider({ scheme: 'vscode-scm' }, this));
this.disposables.push(languages.registerCodeActionsProvider({ language: 'git-commit' }, this));
}
provideCodeActions(document: TextDocument, range: Range | Selection): CodeAction[] {
const codeActions: CodeAction[] = [];
const diagnostics = this.diagnosticsManager.getDiagnostics(document.uri);
const wrapAllLinesCodeAction = this.getWrapAllLinesCodeAction(document, diagnostics);
for (const diagnostic of diagnostics) {
if (!diagnostic.range.contains(range)) {
continue;
}
switch (diagnostic.code) {
case DiagnosticCodes.empty_message: {
const workspaceEdit = new WorkspaceEdit();
workspaceEdit.delete(document.uri, diagnostic.range);
const codeAction = new CodeAction(l10n.t('Clear whitespace characters'), CodeActionKind.QuickFix);
codeAction.diagnostics = [diagnostic];
codeAction.edit = workspaceEdit;
codeActions.push(codeAction);
break;
}
case DiagnosticCodes.line_length: {
const workspaceEdit = this.getWrapLineWorkspaceEdit(document, diagnostic.range);
const codeAction = new CodeAction(l10n.t('Hard wrap line'), CodeActionKind.QuickFix);
codeAction.diagnostics = [diagnostic];
codeAction.edit = workspaceEdit;
codeActions.push(codeAction);
if (wrapAllLinesCodeAction) {
wrapAllLinesCodeAction.diagnostics = [diagnostic];
codeActions.push(wrapAllLinesCodeAction);
}
break;
}
}
}
return codeActions;
}
private getWrapLineWorkspaceEdit(document: TextDocument, range: Range): WorkspaceEdit {
const lineSegments = this.wrapTextDocumentLine(document, range.start.line);
const workspaceEdit = new WorkspaceEdit();
workspaceEdit.replace(document.uri, range, lineSegments.join('\n'));
return workspaceEdit;
}
private getWrapAllLinesCodeAction(document: TextDocument, diagnostics: readonly Diagnostic[]): CodeAction | undefined {
const lineLengthDiagnostics = diagnostics.filter(d => d.code === DiagnosticCodes.line_length);
if (lineLengthDiagnostics.length < 2) {
return undefined;
}
const wrapAllLinesCodeAction = new CodeAction(l10n.t('Hard wrap all lines'), CodeActionKind.QuickFix);
wrapAllLinesCodeAction.edit = this.getWrapAllLinesWorkspaceEdit(document, lineLengthDiagnostics);
return wrapAllLinesCodeAction;
}
private getWrapAllLinesWorkspaceEdit(document: TextDocument, diagnostics: Diagnostic[]): WorkspaceEdit {
const workspaceEdit = new WorkspaceEdit();
for (const diagnostic of diagnostics) {
const lineSegments = this.wrapTextDocumentLine(document, diagnostic.range.start.line);
workspaceEdit.replace(document.uri, diagnostic.range, lineSegments.join('\n'));
}
return workspaceEdit;
}
private wrapTextDocumentLine(document: TextDocument, line: number): string[] {
const config = workspace.getConfiguration('git');
const inputValidationLength = config.get<number>('inputValidationLength', 50);
const inputValidationSubjectLength = config.get<number | undefined>('inputValidationSubjectLength', undefined);
const lineLengthThreshold = line === 0 ? inputValidationSubjectLength ?? inputValidationLength : inputValidationLength;
const lineSegments: string[] = [];
const lineText = document.lineAt(line).text.trim();
let position = 0;
while (lineText.length - position > lineLengthThreshold) {
const lastSpaceBeforeThreshold = lineText.lastIndexOf(' ', position + lineLengthThreshold);
if (lastSpaceBeforeThreshold !== -1 && lastSpaceBeforeThreshold > position) {
lineSegments.push(lineText.substring(position, lastSpaceBeforeThreshold));
position = lastSpaceBeforeThreshold + 1;
} else {
// Find first space after threshold
const firstSpaceAfterThreshold = lineText.indexOf(' ', position + lineLengthThreshold);
if (firstSpaceAfterThreshold !== -1) {
lineSegments.push(lineText.substring(position, firstSpaceAfterThreshold));
position = firstSpaceAfterThreshold + 1;
} else {
lineSegments.push(lineText.substring(position));
position = lineText.length;
}
}
}
if (position < lineText.length) {
lineSegments.push(lineText.substring(position));
}
return lineSegments;
}
dispose() {
dispose(this.disposables);
}
}