-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCodeActionProvider.ts
95 lines (87 loc) · 3.58 KB
/
CodeActionProvider.ts
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
import { CodeAction, CodeActionParams, DiagnosticSeverity, CodeActionKind, TextDocument } from 'vscode-languageserver';
import { isNullOrUndefined } from "util";
export const QUICKFIX_UPPERCASE_MSG = "Keyword should be uppercased.";
export const QUICKFIX_SPACE_BEFORE_EOS_MSG = "Invalid scl. Expecting a space before end of statement operator \".\".";
export const QUICKFIX_NO_EOS_MSG = "Invalid scl. No end of statement operator \".\" specified.";
export const QUICKFIX_CHOICE_MSG = "Possible valid values: ";
/**
* Provide quickfix only for:
* not fully uppercased keywords
*
* @export
* @param {TextDocument} textDocument
* @param {CodeActionParams} parms
* @returns {CodeAction[]}
*/
export function quickfix(textDocument: TextDocument, parms: CodeActionParams): CodeAction[]{
const diagnostics = parms.context.diagnostics;
if (isNullOrUndefined(diagnostics) || diagnostics.length === 0) {
return [];
}
const codeActions: CodeAction[] = [];
diagnostics.forEach((diag) => {
if (diag.severity === DiagnosticSeverity.Warning && diag.message.includes(QUICKFIX_UPPERCASE_MSG)) {
codeActions.push({
title: "Uppercase the keyword",
kind: CodeActionKind.QuickFix,
diagnostics: [diag],
edit: {
changes: {
[parms.textDocument.uri]: [{
range: diag.range, newText: textDocument.getText(diag.range).toUpperCase()
}]
}
}
});
return;
}
if (diag.severity === DiagnosticSeverity.Error && diag.message.includes(QUICKFIX_SPACE_BEFORE_EOS_MSG)) {
codeActions.push({
title: "Adding space between value and end of statement operator",
kind: CodeActionKind.QuickFix,
diagnostics: [diag],
edit: {
changes: {
[parms.textDocument.uri]: [{
range: diag.range, newText: " " + textDocument.getText(diag.range)
}]
}
}
});
return;
}
if (diag.severity === DiagnosticSeverity.Error && diag.message.includes(QUICKFIX_NO_EOS_MSG)) {
codeActions.push({
title: "Adding end of statement operator",
kind: CodeActionKind.QuickFix,
diagnostics: [diag],
edit: {
changes: {
[parms.textDocument.uri]: [{
range: diag.range, newText: textDocument.getText(diag.range) + " . "
}]
}
}
});
return;
}
if (diag.severity === DiagnosticSeverity.Error && !isNullOrUndefined(diag.relatedInformation) &&
diag.relatedInformation[0].message.includes(QUICKFIX_CHOICE_MSG)) {
const actions = diag.relatedInformation[0].message.substring(QUICKFIX_CHOICE_MSG.length).split(",");
codeActions.push({
title: `Change to a possible valid value: ${actions[0].trim()}`,
kind: CodeActionKind.QuickFix,
diagnostics: [diag],
edit: {
changes: {
[parms.textDocument.uri]: [{
range: diag.range, newText: actions[0].trim()
}]
}
}
});
return;
}
});
return codeActions;
}