This repository has been archived by the owner on Apr 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextension.js
55 lines (40 loc) · 2.5 KB
/
extension.js
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
var vscode = require('vscode');
var window = vscode.window;
function activate(context) {
var disposable = vscode.commands.registerCommand('extension.pasteAndFormat', function () {
//Check if current document is markdown
if(window.activeTextEditor.document.languageId === 'markdown'){
vscode.commands.executeCommand('workbench.action.markdown.togglePreview');
} else {
// Save current position
var start = window.activeTextEditor.selection.active;
// Paste from clipboard
vscode.commands.executeCommand('editor.action.clipboardPasteAction').then(function () {
var end = window.activeTextEditor.selection.active; // Get position after paste
var selection = new vscode.Selection(start.line, start.character, end.line, end.character); // Create selection
window.activeTextEditor.selection = selection; // Apply selection to editor
// Format selection, when text is selected, that text is the only thing that will be formatted
vscode.commands.executeCommand('editor.action.format').then(function () {
// This is where I really would like the deselection to happen but it runs before
// formatting is done, I've tried window.onDidChangeTextEditorSelection, but that doesn't
// seem to work how I would like it to eighther.
// Until issue #1775 is solved I just use a timeout.
setTimeout(function () {
// Hopefully the format command is done when this happens
var line = window.activeTextEditor.selection.end.line;
var character = window.activeTextEditor.selection.end.character;
// Set both start and end of selection to the same point so that nothing is selected
var newSelection = new vscode.Selection(line, character, line, character); // Create selection
window.activeTextEditor.selection = newSelection; // Apply selection to editor
}, 100);
});
});
}
});
context.subscriptions.push(disposable);
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {
}
exports.deactivate = deactivate;