-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathReplacePhysicalWithLogical.js
125 lines (107 loc) · 3.5 KB
/
ReplacePhysicalWithLogical.js
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
const vscode = require('vscode');
/**
* @typedef Diagnostic
* @type {object}
* @property {vscode.Range} range - the range.
* @property {string} message - the message.
* @property {string} code - the code.
* @property {string} source - the source.
* @property {object} term - the term.
* @property {string} term.search - the search term.
* @property {string} term.replacement - the replacement term.
*/
class ReplacePhysicalWithLogical {
constructor(context, updateStatusBarItem) {
const commandQuickFix = vscode.commands.registerCommand(
'logical-properties.replacePhysicalWithLogicalQuickFix',
async (range, replacement) => {
const editor = vscode.window.activeTextEditor;
await new Promise((resolve) =>
editor
.edit((editBuilder) => editBuilder.replace(range, replacement))
.then(resolve)
);
updateStatusBarItem();
}
);
const replaceAsync = async (diagnostic) => {
return new Promise((resolve) => {
vscode.commands
.executeCommand(
'logical-properties.replacePhysicalWithLogicalQuickFix',
diagnostic.range,
diagnostic.term.replacement
)
.then(resolve);
});
};
const commandReplaceAll = vscode.commands.registerCommand(
'logical-properties.replacePhysicalWithLogicalReplaceAll',
async () => {
const currentDocument = vscode.window.activeTextEditor?.document;
if (!currentDocument) {
return;
}
const diagnosticsResponses = vscode.languages.getDiagnostics();
for (let diagnosticsResponse of diagnosticsResponses.reverse()) {
const [documentUri, diagnostics] =
/** @type {[vscode.Uri, Diagnostic[]]} */ (
/** @type {unknown} */ (diagnosticsResponse)
);
if (documentUri === currentDocument.uri && diagnostics?.length) {
diagnostics.reverse().sort((diagnosticA, diagnosticB) => {
if (diagnosticB.range.start.line > diagnosticA.range.start.line) {
return -1;
}
if (diagnosticB.range.start.line < diagnosticA.range.start.line) {
return 1;
}
if (
diagnosticB.range.start.character >
diagnosticA.range.start.character
) {
return 1;
}
if (
diagnosticB.range.start.character <
diagnosticA.range.start.character
) {
return -1;
}
});
for (let diagnostic of diagnostics) {
if (diagnostic.code === 'physical-property-detected') {
await replaceAsync(diagnostic);
}
}
}
}
updateStatusBarItem();
}
);
context.subscriptions.push(commandReplaceAll);
context.subscriptions.push(commandQuickFix);
}
provideCodeActions(document, range, context) {
// for each diagnostic entry that has the matching `code`, create a code action command
return context.diagnostics
.filter((diagnostic) => diagnostic.code === 'physical-property-detected')
.map((diagnostic) => this.createCommandCodeActionQuickFix(diagnostic));
}
createCommandCodeActionQuickFix(diagnostic) {
const action = new vscode.CodeAction(
`Replace "${diagnostic.term.search}" with "${diagnostic.term.replacement}".`,
vscode.CodeActionKind.QuickFix
);
action.diagnostics = [diagnostic];
action.isPreferred = true;
action.command = {
command: 'logical-properties.replacePhysicalWithLogicalQuickFix',
title: 'Use logical property',
tooltip: 'Replace physical property with logical property.',
arguments: [diagnostic.range, diagnostic.term.replacement],
};
return action;
}
}
module.exports = ReplacePhysicalWithLogical;