-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
63 lines (44 loc) · 2.11 KB
/
extension.js
File metadata and controls
63 lines (44 loc) · 2.11 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const vscode = require('vscode');
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
console.log('Congratulations, your extension "python-function-spreader" is now active!');
let disposable = vscode.commands.registerCommand('python-function-spreader.execute-spreader', function () {
const config = vscode.workspace.getConfiguration("python-function-spreader");
const python_template_list = config.get("function-list");
vscode.window.showQuickPick(python_template_list)
.then(result => { selection_word_choice_function_spreader(result) });
});
context.subscriptions.push(disposable);
}
function deactivate() { }
async function selection_word_choice_function_spreader(python_template) {
const editor = vscode.window.activeTextEditor;
if (!editor) { return; }
const document = editor.document;
if (editor.selection.isEmpty) { return; }
let sorted_selection_list = editor.selections.sort(function (a, b) {
if (a.start.line < b.start.line) return 1;
if (a.start.line > b.start.line) return -1;
return 0;
});
for (let selection_index in sorted_selection_list) {
if (!sorted_selection_list[selection_index].isSingleLine) {
continue
}
let selection_position = sorted_selection_list[selection_index];
if (selection_position.start.character == selection_position.end.character) { continue; }
const selection_range = new vscode.Range(selection_position.start, selection_position.end);
const word = document.getText(selection_range);
let python_command = "\n" + eval("`" + python_template + "`");
let cursor_end_character = document.lineAt(selection_position.start.line).range.end.character;
let insert_position = new vscode.Position(selection_position.start.line, cursor_end_character);
let python_snippet = new vscode.SnippetString(python_command);
editor.insertSnippet(python_snippet, insert_position, { "undoStopAfter": false, "undoStopBefore": false });
}
}
module.exports = {
activate,
deactivate
}