Skip to content

Commit 57f1090

Browse files
committed
Use new Range | Selection argument
1 parent 6b5d9dc commit 57f1090

File tree

1 file changed

+9
-29
lines changed

1 file changed

+9
-29
lines changed

src/features/codeActionProvider.ts

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,41 +24,21 @@ export default class CodeActionProvider extends AbstractProvider implements vsco
2424
this.addDisposables(new CompositeDisposable(registerCommandDisposable));
2525
}
2626

27-
public async provideCodeActions(document: vscode.TextDocument, range: vscode.Range, context: vscode.CodeActionContext, token: vscode.CancellationToken): Promise<vscode.CodeAction[]> {
27+
public async provideCodeActions(document: vscode.TextDocument, range: vscode.Range | vscode.Selection, context: vscode.CodeActionContext, token: vscode.CancellationToken): Promise<vscode.CodeAction[]> {
2828
let options = this.optionProvider.GetLatestOptions();
2929
if (options.disableCodeActions) {
3030
return;
3131
}
3232

33-
let line: number;
34-
let column: number;
35-
let selection: protocol.V2.Range;
33+
let line = range.start.line;
34+
let column = range.start.character;
35+
let selection: protocol.V2.Range | undefined;
3636

37-
// VS Code will pass the range of the word at the editor caret, even if there isn't a selection.
38-
// To ensure that we don't suggest selection-based refactorings when there isn't a selection, we first
39-
// find the text editor for this document and verify that there is a selection.
40-
let editor = vscode.window.visibleTextEditors.find(e => e.document === document);
41-
if (editor) {
42-
if (editor.selection.isEmpty) {
43-
// The editor does not have a selection. Use the active position of the selection (i.e. the caret).
44-
let active = editor.selection.active;
45-
46-
line = active.line;
47-
column = active.character;
48-
}
49-
else {
50-
// The editor has a selection. Use it.
51-
let start = editor.selection.start;
52-
let end = editor.selection.end;
53-
54-
selection = {
55-
Start: { Line: start.line, Column: start.character },
56-
End: { Line: end.line, Column: end.character }
57-
};
58-
}
59-
}
60-
else {
61-
// We couldn't find the editor, so just use the range we were provided.
37+
// Only suggest selection-based refactorings when a selection exists.
38+
// If there is no selection and the editor isn't focused,
39+
// VS Code will pass us an empty Selection rather than a Range,
40+
// hence the extra range.isEmpty check.
41+
if (range instanceof vscode.Selection && !range.isEmpty) {
6242
selection = {
6343
Start: { Line: range.start.line, Column: range.start.character },
6444
End: { Line: range.end.line, Column: range.end.character }

0 commit comments

Comments
 (0)