This repository has been archived by the owner on May 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
rosiefix.ts
168 lines (159 loc) · 5.43 KB
/
rosiefix.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
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
import { getFixesForDocument } from '../diagnostics/diagnostics';
import { RosieFix, RosieFixEdit } from './rosieTypes';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { Position, Range, CodeAction, CodeActionKind, TextEdit } from 'vscode-languageserver-types';
/**
* Returns whether either the start line or col of the argument Rosie fix edit is negative.
*/
const hasInvalidStartOffset = (
fixEdit: RosieFixEdit
): boolean => {
return fixEdit.start.line - 1 < 0 || fixEdit.start.col - 1 < 0;
};
/**
* Returns whether either the end line or col of the argument Rosie fix edit is negative.
*/
const hasInvalidEndOffset = (
fixEdit: RosieFixEdit
): boolean => {
return fixEdit.end.line - 1 < 0 || fixEdit.end.col - 1 < 0;
};
/**
* Creates the provided CodeAction's underlying WorkspaceEdit, and sets it based on the given RosieFixEdits.
*
* @param codeAction the code action to compute the 'edit' property of
* @param document the document in which the code action is being invoked
* @param rosieFixEdits the code edits from the RosieFix
*/
export const createAndSetRuleFixCodeActionEdit = (
codeAction: CodeAction,
document: TextDocument,
rosieFixEdits: RosieFixEdit[]
) => {
const textEdits = rosieFixEdits
.map(fixEdit => mapFixEditToTextEdit(fixEdit, document))
.filter(textEdit => textEdit !== undefined) as TextEdit[];
//Stores the list of code edits that this quick fix will apply
codeAction.edit = {
changes: {
[document.uri]: textEdits
}
};
};
/**
* Maps the Rosie specific RosieFixEdit to an LSP specific TextEdit,
* so that it can later be applied in a document.
*
* It returns undefined in the following cases:
* <ul>
* <li>when the edit's start line or col is negative</li>
* <li>when the edit's end line or col is negative</li>
* <li>when the edit's start offset is greater than its end offset</li>
* <li>when the edit has an unknown type</li>
* </ul>
*/
const mapFixEditToTextEdit = (
fixEdit: RosieFixEdit,
document: TextDocument
): TextEdit | undefined => {
if (fixEdit.editType === 'add') {
if (hasInvalidStartOffset(fixEdit))
return undefined;
const insertPosition = Position.create(
fixEdit.start.line - 1,
fixEdit.start.col - 1
);
return TextEdit.insert(insertPosition, fixEdit.content || '');
}
if (fixEdit.editType === 'update') {
return validateOffsetsAndCreateTextEdit(fixEdit, document,
(startPosition, endPosition) => {
return TextEdit.replace(Range.create(startPosition, endPosition), fixEdit.content || '');
});
}
if (fixEdit.editType === 'remove') {
return validateOffsetsAndCreateTextEdit(fixEdit, document,
(startPosition, endPosition) => {
return TextEdit.del(Range.create(startPosition, endPosition));
});
}
return undefined;
};
/**
* Validates the fix edit for negative line and col values, as well as whether the
* start offset is less than the end offset. If all is well, creates the appropriate
* TextEdit based on the given callback.
*/
const validateOffsetsAndCreateTextEdit = (
fixEdit: RosieFixEdit,
document: TextDocument,
createTextEdit: (start: Position, end: Position) => TextEdit | undefined
): TextEdit | undefined => {
if (hasInvalidStartOffset(fixEdit) || hasInvalidEndOffset(fixEdit))
return undefined;
const startPosition = Position.create(
fixEdit.start.line - 1,
fixEdit.start.col - 1
);
const endPosition = Position.create(
fixEdit.end.line - 1,
fixEdit.end.col - 1
);
if (document.offsetAt(startPosition) <= document.offsetAt(endPosition)) {
return createTextEdit(startPosition, endPosition);
}
return undefined;
};
/**
* Constructs and collects the applicable rule fix CodeActions in the current document
* for the requested range.
*
* @param document the current document
* @param range the range for which the CodeActions have to be collected
*/
export const provideApplyFixCodeActions = (
document: TextDocument,
range: Range
): CodeAction[] => {
const fixes = getFixesForDocument(document.uri, range);
return fixes
? fixes?.map(rosieFix => createRuleFix(document, rosieFix))
: [];
};
/**
* Creates a rule fix CodeAction from the argument RosieFix.
*
* Exported for testing purposes.
*
* @param document the document in which the CodeAction is being registered
* @param rosieFix the Rosie specific fix to convert
*/
export const createRuleFix = (
document: TextDocument,
rosieFix: RosieFix
): CodeAction => {
/*
From CodeAction's documentation:
If a code action provides an edit and a command, first the edit is executed and then the command.
*/
return {
title: `Fix: ${rosieFix.description}`,
kind: CodeActionKind.QuickFix,
//Registers the 'codiga.applyFix' command for this CodeAction, so that we can execute further
// logic when the quick fix gets invoked, e.g. to record the rule fix mutation.
command: {
command: 'codiga.applyFix',
title: 'Apply Fix'
},
isPreferred: true,
//Data the is reserved between 'onCodeAction()' and 'onCodeActionResolve()'.
data: {
fixKind: "rosie.rule.fix",
//Don't need to send the whole document, the URI is enough.
//Also, sending the entire document object is problematic because some properties of that object
// are lost, for some reason, between 'onCodeAction()' and 'onCodeActionResolve()'.
documentUri: document.uri,
rosieFixEdits: rosieFix.edits
}
};
};