forked from redhat-developer/yaml-language-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyamlCodeActions.ts
More file actions
221 lines (201 loc) · 7.57 KB
/
yamlCodeActions.ts
File metadata and controls
221 lines (201 loc) · 7.57 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { TextDocument } from 'vscode-languageserver-textdocument';
import {
ClientCapabilities,
CodeAction,
CodeActionKind,
CodeActionParams,
Command,
Diagnostic,
Position,
Range,
TextEdit,
WorkspaceEdit,
} from 'vscode-languageserver';
import { YamlCommands } from '../../commands';
import * as path from 'path';
import { TextBuffer } from '../utils/textBuffer';
import { LanguageSettings } from '../yamlLanguageService';
import { YAML_SOURCE } from '../parser/jsonParser07';
import { getFirstNonWhitespaceCharacterAfterOffset } from '../utils/strings';
interface YamlDiagnosticData {
schemaUri: string[];
}
export class YamlCodeActions {
private indentation = ' ';
constructor(private readonly clientCapabilities: ClientCapabilities) {}
configure(settings: LanguageSettings): void {
this.indentation = settings.indentation;
}
getCodeAction(document: TextDocument, params: CodeActionParams): CodeAction[] | undefined {
if (!params.context.diagnostics) {
return;
}
const result = [];
result.push(...this.getConvertToBooleanActions(params.context.diagnostics, document));
result.push(...this.getJumpToSchemaActions(params.context.diagnostics));
result.push(...this.getTabToSpaceConverting(params.context.diagnostics, document));
result.push(...this.getUnusedAnchorsDelete(params.context.diagnostics, document));
return result;
}
private getJumpToSchemaActions(diagnostics: Diagnostic[]): CodeAction[] {
const isOpenTextDocumentEnabled = this.clientCapabilities?.window?.showDocument?.support ?? false;
if (!isOpenTextDocumentEnabled) {
return [];
}
const schemaUriToDiagnostic = new Map<string, Diagnostic[]>();
for (const diagnostic of diagnostics) {
const schemaUri = (diagnostic.data as YamlDiagnosticData)?.schemaUri || [];
for (const schemaUriStr of schemaUri) {
if (schemaUriStr) {
if (!schemaUriToDiagnostic.has(schemaUriStr)) {
schemaUriToDiagnostic.set(schemaUriStr, []);
}
schemaUriToDiagnostic.get(schemaUriStr).push(diagnostic);
}
}
}
const result = [];
for (const schemaUri of schemaUriToDiagnostic.keys()) {
const action = CodeAction.create(
`Jump to schema location (${path.basename(schemaUri)})`,
Command.create('JumpToSchema', YamlCommands.JUMP_TO_SCHEMA, schemaUri)
);
action.diagnostics = schemaUriToDiagnostic.get(schemaUri);
result.push(action);
}
return result;
}
private getTabToSpaceConverting(diagnostics: Diagnostic[], document: TextDocument): CodeAction[] {
const result: CodeAction[] = [];
const textBuff = new TextBuffer(document);
const processedLine: number[] = [];
for (const diag of diagnostics) {
if (diag.message === 'Using tabs can lead to unpredictable results') {
if (processedLine.includes(diag.range.start.line)) {
continue;
}
const lineContent = textBuff.getLineContent(diag.range.start.line);
let replacedTabs = 0;
let newText = '';
for (let i = diag.range.start.character; i <= diag.range.end.character; i++) {
const char = lineContent.charAt(i);
if (char !== '\t') {
break;
}
replacedTabs++;
newText += this.indentation;
}
processedLine.push(diag.range.start.line);
let resultRange = diag.range;
if (replacedTabs !== diag.range.end.character - diag.range.start.character) {
resultRange = Range.create(
diag.range.start,
Position.create(diag.range.end.line, diag.range.start.character + replacedTabs)
);
}
result.push(
CodeAction.create(
'Convert Tab to Spaces',
createWorkspaceEdit(document.uri, [TextEdit.replace(resultRange, newText)]),
CodeActionKind.QuickFix
)
);
}
}
if (result.length !== 0) {
const replaceEdits: TextEdit[] = [];
for (let i = 0; i <= textBuff.getLineCount(); i++) {
const lineContent = textBuff.getLineContent(i);
let replacedTabs = 0;
let newText = '';
for (let j = 0; j < lineContent.length; j++) {
const char = lineContent.charAt(j);
if (char !== ' ' && char !== '\t') {
if (replacedTabs !== 0) {
replaceEdits.push(TextEdit.replace(Range.create(i, j - replacedTabs, i, j), newText));
replacedTabs = 0;
newText = '';
}
break;
}
if (char === ' ' && replacedTabs !== 0) {
replaceEdits.push(TextEdit.replace(Range.create(i, j - replacedTabs, i, j), newText));
replacedTabs = 0;
newText = '';
continue;
}
if (char === '\t') {
newText += this.indentation;
replacedTabs++;
}
}
// line contains only tabs
if (replacedTabs !== 0) {
replaceEdits.push(TextEdit.replace(Range.create(i, 0, i, textBuff.getLineLength(i)), newText));
}
}
if (replaceEdits.length > 0) {
result.push(
CodeAction.create(
'Convert all Tabs to Spaces',
createWorkspaceEdit(document.uri, replaceEdits),
CodeActionKind.QuickFix
)
);
}
}
return result;
}
private getUnusedAnchorsDelete(diagnostics: Diagnostic[], document: TextDocument): CodeAction[] {
const result = [];
const buffer = new TextBuffer(document);
for (const diag of diagnostics) {
if (diag.message.startsWith('Unused anchor') && diag.source === YAML_SOURCE) {
const range = Range.create(diag.range.start, diag.range.end);
const actual = buffer.getText(range);
const lineContent = buffer.getLineContent(range.end.line);
const lastWhitespaceChar = getFirstNonWhitespaceCharacterAfterOffset(lineContent, range.end.character);
range.end.character = lastWhitespaceChar;
const action = CodeAction.create(
`Delete unused anchor: ${actual}`,
createWorkspaceEdit(document.uri, [TextEdit.del(range)]),
CodeActionKind.QuickFix
);
action.diagnostics = [diag];
result.push(action);
}
}
return result;
}
private getConvertToBooleanActions(diagnostics: Diagnostic[], document: TextDocument): CodeAction[] {
const results: CodeAction[] = [];
for (const diagnostic of diagnostics) {
if (diagnostic.message === 'Incorrect type. Expected "boolean".') {
const value = document.getText(diagnostic.range).toLocaleLowerCase();
if (value === '"true"' || value === '"false"' || value === "'true'" || value === "'false'") {
const newValue = value.includes('true') ? 'true' : 'false';
results.push(
CodeAction.create(
'Convert to boolean',
createWorkspaceEdit(document.uri, [TextEdit.replace(diagnostic.range, newValue)]),
CodeActionKind.QuickFix
)
);
}
}
}
return results;
}
}
function createWorkspaceEdit(uri: string, edits: TextEdit[]): WorkspaceEdit {
const changes = {};
changes[uri] = edits;
const edit: WorkspaceEdit = {
changes,
};
return edit;
}