forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeActionProvider.ts
More file actions
43 lines (39 loc) · 2.06 KB
/
codeActionProvider.ts
File metadata and controls
43 lines (39 loc) · 2.06 KB
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
'use strict';
import * as vscode from 'vscode';
import {TextDocument, Range, CodeActionContext, CancellationToken, Command} from 'vscode';
import * as proxy from './jediProxy';
import * as telemetryContracts from '../common/telemetryContracts';
export class PythonCodeActionsProvider implements vscode.CodeActionProvider {
public constructor(context: vscode.ExtensionContext) {
}
provideCodeActions(document: TextDocument, range: Range, context: CodeActionContext, token: CancellationToken): Thenable<Command[]> {
return new Promise<Command[]>((resolve, reject) => {
let commands: Command[] = [
{
command: 'python.sortImports',
title: 'Sort Imports'
}
];
if (vscode.window.activeTextEditor.document === document && !vscode.window.activeTextEditor.selection.isEmpty) {
let wordRange = document.getWordRangeAtPosition(range.start);
// If no word has been selected by the user, then don't display rename
// If something has been selected, then ensure we have selected a word (i.e. end and start matches the word range)
let selectionIsEmpty = range.isEmpty;
if (wordRange && !wordRange.isEmpty && wordRange.isEqual(vscode.window.activeTextEditor.selection)) {
let word = document.getText(wordRange).trim();
if (word.length > 0) {
commands.push({ command: 'editor.action.rename', title: 'Rename Symbol' });
}
}
}
if (!range.isEmpty) {
let word = document.getText(range).trim();
if (word.trim().length > 0) {
commands.push({ command: 'python.refactorExtractVariable', title: 'Extract Variable', arguments: [range] });
commands.push({ command: 'python.refactorExtractMethod', title: 'Extract Method', arguments: [range] });
}
}
resolve(commands);
});
}
}