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
81 lines (75 loc) · 2.95 KB
/
pasteAction.ts
File metadata and controls
81 lines (75 loc) · 2.95 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'use strict';
import { TextEncoder } from 'util';
import { commands, env, ExtensionContext, Range, TextEditor, Uri, window, workspace } from 'vscode';
import { LanguageClient } from 'vscode-languageclient/node';
import { apiManager } from './apiManager';
import { Commands } from './commands';
import fs = require('fs');
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) => {
if (wasApplied && editor.document.languageId === "java") {
const fileURI = editor.document.uri.toString();
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 } });
}
}
});
}
let serverReady = false;
export async function pasteFile(folder: fs.PathLike): Promise<void> {
const clipboardText: string = await env.clipboard.readText();
let filePath = folder.toString();
fs.stat(folder, async (err, stats) => {
// If given path to selected folder is invalid (no folder is selected)
if (filePath === clipboardText || stats.isFile() || (filePath === "." && workspace.workspaceFolders !== undefined)) {
filePath = workspace.workspaceFolders[0].uri.fsPath;
}
if (!serverReady) {
await apiManager.getApiInstance().serverReady().then( async () => {
serverReady = true;
});
}
const fileString: string = await commands.executeCommand(Commands.EXECUTE_WORKSPACE_COMMAND, Commands.RESOLVE_PASTED_TEXT, filePath, clipboardText);
const fileUri = fileString !== null ? Uri.file(fileString) : null;
if (fileUri !== null){
try {
await workspace.fs.writeFile(fileUri, new TextEncoder().encode(clipboardText));
window.showTextDocument(fileUri, { preview: false });
} catch (error: unknown) {
// Do nothing (file does not have write permissions)
}
}
});
}