Skip to content

Commit 4d936f0

Browse files
committed
Enable autoimport on paste for non-empty files
Signed-off-by: Fred Bricon <[email protected]>
1 parent 7b6347e commit 4d936f0

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Change Log
22

33
## 0.51.0 (October 16th, 2019)
4-
* enhancement - automatically trigger auto-import on paste. Can be disabled with `java.actionsOnPaste.organizeImports`. See [#1075](https://github.com/redhat-developer/vscode-java/issues/1075).
4+
* enhancement - [experimental] automatically trigger auto-import on paste. Can be disabled with `java.actionsOnPaste.organizeImports`. See [#1075](https://github.com/redhat-developer/vscode-java/issues/1075).
55
* enhancement - made code snippets context sensitive. See [JLS#977](https://github.com/eclipse/eclipse.jdt.ls/issues/977).
66
* enhancement - improve snippet documentation rendering. See [JLS#1205](https://github.com/eclipse/eclipse.jdt.ls/issues/1205).
77
* bug fix - fixed preview features enabled at an invalid source release level 12, preview can be enabled only at source level 13. See [#1086](https://github.com/redhat-developer/vscode-java/issues/1086).

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ The following settings are supported:
118118
* `java.selectionRange.enabled`: Enable/disable Smart Selection support for Java. Disabling this option will not affect the VS Code built-in word-based and bracket-based smart selection.
119119

120120
New in 0.51.0:
121-
* `java.actionsOnPaste.organizeImports`: Triggers "Organize imports" when java code is pasted into an empty file.
121+
* `java.actionsOnPaste.organizeImports`: [experimental] Triggers "Organize imports" when code is pasted into a java file.
122122

123123
Troubleshooting
124124
===============

src/commands.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ export namespace Commands {
128128
* Organize imports.
129129
*/
130130
export const ORGANIZE_IMPORTS = "java.action.organizeImports";
131+
/**
132+
* Organize imports silently.
133+
*/
134+
export const ORGANIZE_IMPORTS_SILENTLY = "java.edit.organizeImports";
131135
/**
132136
* Choose type to import.
133137
*/

src/settings.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,7 @@ export function registerOverridePasteCommand(languageClient: LanguageClient, con
155155

156156
const clipboardText: string = await env.clipboard.readText();
157157
const editor: TextEditor = window.activeTextEditor;
158-
159158
const documentText: string = editor.document.getText();
160-
161159
const isCursorOnly = editor.selection.isEmpty;
162160
let action: Thenable<boolean>;
163161
if (isCursorOnly) {
@@ -174,18 +172,21 @@ export function registerOverridePasteCommand(languageClient: LanguageClient, con
174172
}
175173

176174
action.then((wasApplied) => {
177-
const hasText: boolean = documentText !== null && /\S/.test(documentText);
178175
const fileURI = editor.document.uri.toString();
179-
if (wasApplied && !hasText && fileURI.endsWith(".java")) { // only organizing imports if document is blank
180-
// Organize imports only requires uri from CodeActionParams
181-
const codeActionParams = { textDocument: { uri: fileURI } };
182-
commands.executeCommand(Commands.ORGANIZE_IMPORTS, codeActionParams);
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+
}
183184
}
184185
});
185-
}));
186+
}));
186187

187-
pasteSubscriptionIndex = length - 1;
188-
languageClient.info(`Registered 'java.${ORGANIZE_IMPORTS_ON_PASTE}' command.`);
188+
pasteSubscriptionIndex = length - 1;
189+
languageClient.info(`Registered 'java.${ORGANIZE_IMPORTS_ON_PASTE}' command.`);
189190
}
190191

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

0 commit comments

Comments
 (0)