diff options
author | Ryan Cumming <[email protected]> | 2019-06-27 12:30:23 +0100 |
---|---|---|
committer | Ryan Cumming <[email protected]> | 2019-06-29 08:39:36 +0100 |
commit | abc0784e57610a0cceca63301489918015418df6 (patch) | |
tree | 05aec9fef88f31cee82e3507903a1dbcd6b4d30d /editors/code/src/test/vscode_diagnostics.test.ts | |
parent | 0e1912de528b5092c10eedaf94c43c67d5f86f1a (diff) |
Fix `cargo watch` code action filtering
There are two issues with the implementation of `provideCodeActions`
introduced in #1439:
1. We're returning the code action based on the file its diagnostic is
in; not the file the suggested fix is in. I'm not sure how often
fixes are suggested cross-file but it's something we should handle.
2. We're not filtering code actions based on the passed range. The means
if there is any suggestion in a file we'll show an action for every
line of the file. I naively thought that VS Code would filter for us
but that was wrong.
Unfortunately the VS Code `CodeAction` object is very complex - it can
handle edits across multiple files, run commands, etc. This makes it
complex to check them for equality or see if any of their edits
intersects with a specified range.
To make it easier to work with suggestions this introduces a
`SuggestedFix` model object and a `SuggestFixCollection` code action
provider. This is a layer between the raw Rust JSON and VS Code's
`CodeAction`s. I was reluctant to introduce another layer of abstraction
here but my attempt to work directly with VS Code's model objects was
worse.
Diffstat (limited to 'editors/code/src/test/vscode_diagnostics.test.ts')
-rw-r--r-- | editors/code/src/test/vscode_diagnostics.test.ts | 182 |
1 files changed, 0 insertions, 182 deletions
diff --git a/editors/code/src/test/vscode_diagnostics.test.ts b/editors/code/src/test/vscode_diagnostics.test.ts deleted file mode 100644 index 9c5d812fa..000000000 --- a/editors/code/src/test/vscode_diagnostics.test.ts +++ /dev/null | |||
@@ -1,182 +0,0 @@ | |||
1 | import * as assert from 'assert'; | ||
2 | import * as vscode from 'vscode'; | ||
3 | |||
4 | import { | ||
5 | areCodeActionsEqual, | ||
6 | areDiagnosticsEqual | ||
7 | } from '../utils/vscode_diagnostics'; | ||
8 | |||
9 | const uri = vscode.Uri.file('/file/1'); | ||
10 | |||
11 | const range1 = new vscode.Range( | ||
12 | new vscode.Position(1, 2), | ||
13 | new vscode.Position(3, 4) | ||
14 | ); | ||
15 | |||
16 | const range2 = new vscode.Range( | ||
17 | new vscode.Position(5, 6), | ||
18 | new vscode.Position(7, 8) | ||
19 | ); | ||
20 | |||
21 | describe('areDiagnosticsEqual', () => { | ||
22 | it('should treat identical diagnostics as equal', () => { | ||
23 | const diagnostic1 = new vscode.Diagnostic( | ||
24 | range1, | ||
25 | 'Hello, world!', | ||
26 | vscode.DiagnosticSeverity.Error | ||
27 | ); | ||
28 | |||
29 | const diagnostic2 = new vscode.Diagnostic( | ||
30 | range1, | ||
31 | 'Hello, world!', | ||
32 | vscode.DiagnosticSeverity.Error | ||
33 | ); | ||
34 | |||
35 | assert(areDiagnosticsEqual(diagnostic1, diagnostic2)); | ||
36 | }); | ||
37 | |||
38 | it('should treat diagnostics with different sources as inequal', () => { | ||
39 | const diagnostic1 = new vscode.Diagnostic( | ||
40 | range1, | ||
41 | 'Hello, world!', | ||
42 | vscode.DiagnosticSeverity.Error | ||
43 | ); | ||
44 | diagnostic1.source = 'rustc'; | ||
45 | |||
46 | const diagnostic2 = new vscode.Diagnostic( | ||
47 | range1, | ||
48 | 'Hello, world!', | ||
49 | vscode.DiagnosticSeverity.Error | ||
50 | ); | ||
51 | diagnostic2.source = 'clippy'; | ||
52 | |||
53 | assert(!areDiagnosticsEqual(diagnostic1, diagnostic2)); | ||
54 | }); | ||
55 | |||
56 | it('should treat diagnostics with different ranges as inequal', () => { | ||
57 | const diagnostic1 = new vscode.Diagnostic( | ||
58 | range1, | ||
59 | 'Hello, world!', | ||
60 | vscode.DiagnosticSeverity.Error | ||
61 | ); | ||
62 | |||
63 | const diagnostic2 = new vscode.Diagnostic( | ||
64 | range2, | ||
65 | 'Hello, world!', | ||
66 | vscode.DiagnosticSeverity.Error | ||
67 | ); | ||
68 | |||
69 | assert(!areDiagnosticsEqual(diagnostic1, diagnostic2)); | ||
70 | }); | ||
71 | |||
72 | it('should treat diagnostics with different messages as inequal', () => { | ||
73 | const diagnostic1 = new vscode.Diagnostic( | ||
74 | range1, | ||
75 | 'Hello, world!', | ||
76 | vscode.DiagnosticSeverity.Error | ||
77 | ); | ||
78 | |||
79 | const diagnostic2 = new vscode.Diagnostic( | ||
80 | range1, | ||
81 | 'Goodbye!, world!', | ||
82 | vscode.DiagnosticSeverity.Error | ||
83 | ); | ||
84 | |||
85 | assert(!areDiagnosticsEqual(diagnostic1, diagnostic2)); | ||
86 | }); | ||
87 | |||
88 | it('should treat diagnostics with different severities as inequal', () => { | ||
89 | const diagnostic1 = new vscode.Diagnostic( | ||
90 | range1, | ||
91 | 'Hello, world!', | ||
92 | vscode.DiagnosticSeverity.Warning | ||
93 | ); | ||
94 | |||
95 | const diagnostic2 = new vscode.Diagnostic( | ||
96 | range1, | ||
97 | 'Hello, world!', | ||
98 | vscode.DiagnosticSeverity.Error | ||
99 | ); | ||
100 | |||
101 | assert(!areDiagnosticsEqual(diagnostic1, diagnostic2)); | ||
102 | }); | ||
103 | }); | ||
104 | |||
105 | describe('areCodeActionsEqual', () => { | ||
106 | it('should treat identical actions as equal', () => { | ||
107 | const codeAction1 = new vscode.CodeAction( | ||
108 | 'Fix me!', | ||
109 | vscode.CodeActionKind.QuickFix | ||
110 | ); | ||
111 | |||
112 | const codeAction2 = new vscode.CodeAction( | ||
113 | 'Fix me!', | ||
114 | vscode.CodeActionKind.QuickFix | ||
115 | ); | ||
116 | |||
117 | const edit = new vscode.WorkspaceEdit(); | ||
118 | edit.replace(uri, range1, 'Replace with this'); | ||
119 | codeAction1.edit = edit; | ||
120 | codeAction2.edit = edit; | ||
121 | |||
122 | assert(areCodeActionsEqual(codeAction1, codeAction2)); | ||
123 | }); | ||
124 | |||
125 | it('should treat actions with different types as inequal', () => { | ||
126 | const codeAction1 = new vscode.CodeAction( | ||
127 | 'Fix me!', | ||
128 | vscode.CodeActionKind.Refactor | ||
129 | ); | ||
130 | |||
131 | const codeAction2 = new vscode.CodeAction( | ||
132 | 'Fix me!', | ||
133 | vscode.CodeActionKind.QuickFix | ||
134 | ); | ||
135 | |||
136 | const edit = new vscode.WorkspaceEdit(); | ||
137 | edit.replace(uri, range1, 'Replace with this'); | ||
138 | codeAction1.edit = edit; | ||
139 | codeAction2.edit = edit; | ||
140 | |||
141 | assert(!areCodeActionsEqual(codeAction1, codeAction2)); | ||
142 | }); | ||
143 | |||
144 | it('should treat actions with different titles as inequal', () => { | ||
145 | const codeAction1 = new vscode.CodeAction( | ||
146 | 'Fix me!', | ||
147 | vscode.CodeActionKind.Refactor | ||
148 | ); | ||
149 | |||
150 | const codeAction2 = new vscode.CodeAction( | ||
151 | 'Do something different!', | ||
152 | vscode.CodeActionKind.Refactor | ||
153 | ); | ||
154 | |||
155 | const edit = new vscode.WorkspaceEdit(); | ||
156 | edit.replace(uri, range1, 'Replace with this'); | ||
157 | codeAction1.edit = edit; | ||
158 | codeAction2.edit = edit; | ||
159 | |||
160 | assert(!areCodeActionsEqual(codeAction1, codeAction2)); | ||
161 | }); | ||
162 | |||
163 | it('should treat actions with different edits as inequal', () => { | ||
164 | const codeAction1 = new vscode.CodeAction( | ||
165 | 'Fix me!', | ||
166 | vscode.CodeActionKind.Refactor | ||
167 | ); | ||
168 | const edit1 = new vscode.WorkspaceEdit(); | ||
169 | edit1.replace(uri, range1, 'Replace with this'); | ||
170 | codeAction1.edit = edit1; | ||
171 | |||
172 | const codeAction2 = new vscode.CodeAction( | ||
173 | 'Fix me!', | ||
174 | vscode.CodeActionKind.Refactor | ||
175 | ); | ||
176 | const edit2 = new vscode.WorkspaceEdit(); | ||
177 | edit2.replace(uri, range1, 'Replace with this other thing'); | ||
178 | codeAction2.edit = edit2; | ||
179 | |||
180 | assert(!areCodeActionsEqual(codeAction1, codeAction2)); | ||
181 | }); | ||
182 | }); | ||