forked from redhat-developer/vscode-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpasteAction.ts
More file actions
51 lines (46 loc) · 1.73 KB
/
pasteAction.ts
File metadata and controls
51 lines (46 loc) · 1.73 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
'use strict';
import { commands, env, ExtensionContext, Range, TextEditor, window } from 'vscode';
import { LanguageClient } from 'vscode-languageclient/node';
import { Commands } from './commands';
export function registerCommands(languageClient: LanguageClient, context: ExtensionContext) {
context.subscriptions.push(commands.registerCommand(Commands.CLIPBOARD_ONPASTE, () => {
registerOrganizeImportsOnPasteCommand();
}));
}
export async function registerOrganizeImportsOnPasteCommand(): Promise<void> {
const clipboardText: string = await env.clipboard.readText();
const editor: TextEditor = window.activeTextEditor;
const documentText: string = editor.document.getText();
const numCursors = editor.selections.length;
let bits: string[] = [];
if (numCursors > 1) {
bits = clipboardText.split(/\r?\n/);
}
const action = editor.edit(textInserter => {
for (let i = 0; i < numCursors; i++) {
const selection = editor.selections[i];
const isCursorOnly = selection.isEmpty;
const text = bits.length === numCursors ? bits[i] : clipboardText;
if (isCursorOnly) {
textInserter.insert(selection.start, text);
}
else {
const start = selection.start;
const end = selection.end;
textInserter.replace(new Range(start, end), text);
}
}
});
action.then((wasApplied) => {
const fileURI = editor.document.uri.toString();
if (wasApplied && fileURI.endsWith(".java")) {
const hasText: boolean = documentText !== null && /\S/.test(documentText);
if (hasText) {
// Organize imports silently to avoid surprising the user
commands.executeCommand(Commands.ORGANIZE_IMPORTS_SILENTLY, fileURI);
} else {
commands.executeCommand(Commands.ORGANIZE_IMPORTS, { textDocument: { uri: fileURI } });
}
}
});
}