Skip to content

Commit 4a9389e

Browse files
committed
Fix autoimport on multi-cursor paste
Signed-off-by: Fred Bricon <fbricon@gmail.com>
1 parent c0b3aec commit 4a9389e

1 file changed

Lines changed: 44 additions & 38 deletions

File tree

src/settings.ts

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ function excludeProjectSettingsFilesForWorkspace(workspaceUri: Uri) {
9999
config.update('exclude', excludedInspectedValue.workspaceValue, ConfigurationTarget.Workspace);
100100
} else if (result === changeItem.never) {
101101
const storeInWorkspace = getJavaConfiguration().inspect(EXCLUDE_FILE_CONFIG).workspaceValue;
102-
getJavaConfiguration().update(EXCLUDE_FILE_CONFIG, false, storeInWorkspace?ConfigurationTarget.Workspace: ConfigurationTarget.Global);
102+
getJavaConfiguration().update(EXCLUDE_FILE_CONFIG, false, storeInWorkspace ? ConfigurationTarget.Workspace : ConfigurationTarget.Global);
103103
}
104104
});
105105
}
@@ -150,43 +150,49 @@ export function handleJavaPasteConfigurationChange(languageClient: LanguageClien
150150
}
151151

152152
export function registerOverridePasteCommand(languageClient: LanguageClient, context: ExtensionContext): void {
153-
// referencing https://github.com/gazugafan/vscode-indent-on-paste/blob/master/src/extension.ts
154-
const length = context.subscriptions.push(commands.registerCommand('editor.action.clipboardPasteAction', async () => {
155-
156-
const clipboardText: string = await env.clipboard.readText();
157-
const editor: TextEditor = window.activeTextEditor;
158-
const documentText: string = editor.document.getText();
159-
const isCursorOnly = editor.selection.isEmpty;
160-
let action: Thenable<boolean>;
161-
if (isCursorOnly) {
162-
action = editor.edit(textInserter => {
163-
textInserter.insert(editor.selection.start, clipboardText);
164-
});
165-
}
166-
else {
167-
const start = editor.selection.start;
168-
const end = editor.selection.end;
169-
action = editor.edit(textInserter => {
170-
textInserter.replace(new Range(start, end), clipboardText);
171-
});
172-
}
173-
174-
action.then((wasApplied) => {
175-
const fileURI = editor.document.uri.toString();
176-
if (wasApplied && fileURI.endsWith(".java")) {
177-
const hasText: boolean = documentText !== null && /\S/.test(documentText);
178-
if (hasText) {
179-
// Organize imports silently to avoid surprising the user
180-
commands.executeCommand(Commands.ORGANIZE_IMPORTS_SILENTLY, fileURI);
181-
} else {
182-
commands.executeCommand(Commands.ORGANIZE_IMPORTS, { textDocument: { uri: fileURI } });
183-
}
184-
}
185-
});
186-
}));
187-
188-
pasteSubscriptionIndex = length - 1;
189-
languageClient.info(`Registered 'java.${ORGANIZE_IMPORTS_ON_PASTE}' command.`);
153+
// referencing https://github.com/gazugafan/vscode-indent-on-paste/blob/master/src/extension.ts
154+
const length = context.subscriptions.push(commands.registerCommand('editor.action.clipboardPasteAction', async () => {
155+
156+
const clipboardText: string = await env.clipboard.readText();
157+
const editor: TextEditor = window.activeTextEditor;
158+
const documentText: string = editor.document.getText();
159+
const numCursors = editor.selections.length;
160+
let bits: string[] = [];
161+
if (numCursors > 1) {
162+
bits = clipboardText.split(/\r?\n/);
163+
}
164+
const action = editor.edit(textInserter => {
165+
for (let i = 0; i < numCursors; i++) {
166+
const selection = editor.selections[i];
167+
const isCursorOnly = selection.isEmpty;
168+
const text = bits.length === numCursors ? bits[i] : clipboardText;
169+
if (isCursorOnly) {
170+
textInserter.insert(selection.start, text);
171+
}
172+
else {
173+
const start = selection.start;
174+
const end = selection.end;
175+
textInserter.replace(new Range(start, end), text);
176+
}
177+
}
178+
});
179+
180+
action.then((wasApplied) => {
181+
const fileURI = editor.document.uri.toString();
182+
if (wasApplied && fileURI.endsWith(".java")) {
183+
const hasText: boolean = documentText !== null && /\S/.test(documentText);
184+
if (hasText) {
185+
// Organize imports silently to avoid surprising the user
186+
commands.executeCommand(Commands.ORGANIZE_IMPORTS_SILENTLY, fileURI);
187+
} else {
188+
commands.executeCommand(Commands.ORGANIZE_IMPORTS, { textDocument: { uri: fileURI } });
189+
}
190+
}
191+
});
192+
}));
193+
194+
pasteSubscriptionIndex = length - 1;
195+
languageClient.info(`Registered 'java.${ORGANIZE_IMPORTS_ON_PASTE}' command.`);
190196
}
191197

192198
export function unregisterOverridePasteCommand(languageClient: LanguageClient, context: ExtensionContext) {

0 commit comments

Comments
 (0)