Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add organize imports command #341

Closed
wants to merge 1 commit into from

Conversation

yaohaizh
Copy link
Collaborator

@yaohaizh yaohaizh commented Oct 24, 2017

Fixes #253

Signed-off-by: Yaohai Zheng yaozheng@microsoft.com

@fbricon @aeschli @gorkem

src/commands.ts Outdated
@@ -67,5 +67,15 @@ export namespace Commands {
/**
* Open Java Language Server Log file
*/
export const OPEN_SERVER_LOG = 'java.open.serverLog';
export const OPEN_SERVER_LOG = 'java.open.serverLog';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something happened with the indentation here and below.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

basically tabs were replaced with spaces. I think we need to set a project preference for indentation. But until then we should try to keep indentation coherent all over the place.
Ultimately we can do a major sweep to format all files at once, but that's the subject of another commit.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to keep the indentation consistent, and will accept either tab or whitespace.

As I went through the vscode-java codebase, there are mixed with tab and whitespaces. We need make them consistent anyway.

@gorkem
Copy link
Contributor

gorkem commented Oct 24, 2017

Can you also add the new command to tests at https://github.com/redhat-developer/vscode-java/blob/master/test/extension.test.ts#L25

@fbricon
Copy link
Collaborator

fbricon commented Oct 24, 2017

I can't bind a keyboard shortcut to the organize import command from the VSCode UI.

screen shot 2017-10-24 at 7 10 42 pm

It works when editing the keybindings.json manually.

However, when calling the shortcut on a file with imports to organize, an error occurs on the server:


[Error - 19:07:04] 24-Oct-2017 7:02:16 PM Failed to resolve [object Object]
Illegal character in path at index 0: [object Object]
java.net.URISyntaxException: Illegal character in path at index 0: [object Object]
	at java.net.URI$Parser.fail(URI.java:2848)
	at java.net.URI$Parser.checkChars(URI.java:3021)
	at java.net.URI$Parser.parseHierarchical(URI.java:3105)
	at java.net.URI$Parser.parse(URI.java:3063)
	at java.net.URI.<init>(URI.java:588)
	at org.eclipse.jdt.ls.core.internal.JDTUtils.toURI(JDTUtils.java:591)
	at org.eclipse.jdt.ls.core.internal.JDTUtils.resolveCompilationUnit(JDTUtils.java:107)
	at org.eclipse.jdt.ls.core.internal.JDTDelegateCommandHandler.organizeImports(JDTDelegateCommandHandler.java:50)
	at org.eclipse.jdt.ls.core.internal.JDTDelegateCommandHandler.executeCommand(JDTDelegateCommandHandler.java:39)
	at org.eclipse.jdt.ls.core.internal.handlers.WorkspaceExecuteCommandHandler$1.run(WorkspaceExecuteCommandHandler.java:133)
	at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
	at org.eclipse.jdt.ls.core.internal.handlers.WorkspaceExecuteCommandHandler.executeCommand(WorkspaceExecuteCommandHandler.java:128)
	at org.eclipse.jdt.ls.core.internal.handlers.JDTLanguageServer.lambda$2(JDTLanguageServer.java:258)
	at java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:602)
	at java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:577)
	at java.util.concurrent.CompletableFuture$Completion.exec(CompletableFuture.java:443)
	at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
	at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
	at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
	at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)

package.json Outdated
@@ -150,6 +150,11 @@
"command": "java.open.serverLog",
"title": "Open Java Language Server log file",
"category": "Java"
},
{
"command": "vscode.java.edit.organizeImports",
Copy link
Collaborator

@fbricon fbricon Oct 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we skip the vscode prefix? Having all commands under the java namespace is more consistent

package.json Outdated
"menus": {
"editor/context": [
{
"command": "vscode.java.edit.organizeImports",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again, skip vscode prefix

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cannot be skip. There are two commands here actually.

  1. One is appeared on the VSCode UI side, which is starting with prefix "vscode"
  2. Another one is just starting with "java...", which used for underlying talking with JDT.LS, which is neutral.

src/commands.ts Outdated
/**
* VSCode UI for organize imports commands
*/
export const VSCODE_EDIT_ORGANIZE_IMPORTS = 'vscode.java.edit.organizeImports';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does having the same command id for client and server cause any issue?

src/extension.ts Outdated
@@ -160,6 +160,22 @@ export function activate(context: ExtensionContext) {
}
});

commands.registerCommand(Commands.VSCODE_EDIT_ORGANIZE_IMPORTS, async (uri: Uri) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to check the uri is actually an Uri

src/extension.ts Outdated
commands.registerCommand(Commands.VSCODE_EDIT_ORGANIZE_IMPORTS, async (uri: Uri) => {
const edit = await <any>commands.executeCommand(Commands.EXECUTE_WORKSPACE_COMMAND, Commands.EDIT_ORGANIZE_IMPORTS, uri.toString());
const workspaceEdit = new WorkspaceEdit();

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any way to reuse what we have L156?

package.json Outdated
"editor/context": [
{
"command": "vscode.java.edit.organizeImports",
"when": "resourceLangId == java",
Copy link
Collaborator

@fbricon fbricon Oct 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we call the command from a folder (not just files) in the explorer?

package.json Outdated
"explorer/context": [
{
"command": "java.edit.organizeImports",
"when": "resourceLangId == java",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we enable it for folders too?

@fbricon
Copy link
Collaborator

fbricon commented Oct 27, 2017

Any idea why calling a keyboard shortcut doesn't work? With:

{
    "key": "alt+cmd+o",
    "when": "editorTextFocus && !editorReadnly",
    "command": "java.edit.organizeImports"
}

the command receives an empty Object, instead of a URI, so nothing happens

@yaohaizh
Copy link
Collaborator Author

It seems the keybinding doesn't provide any parameters as the context menu do. We need change the behavior of these commands.

Signed-off-by: Yaohai Zheng <yaozheng@microsoft.com>
@yaohaizh
Copy link
Collaborator Author

For adding commands of "Organize Imports" in folder/workspace in VSCode, there is an issue here. We cannot determine current project(Could we? ). If so, the "Organize Imports" will be appeared in all VSCode instance, which is not desired behavior.

@yaohaizh yaohaizh changed the title Add organize imports command[WIP] Add organize imports command Oct 31, 2017
@fbricon
Copy link
Collaborator

fbricon commented Oct 31, 2017

Ok, we can figure out how to get folders working later on the client side. Though I still think we can have it supported on the server side

@aeschli
Copy link
Collaborator

aeschli commented Oct 31, 2017

@yaohaizh If you want to limit the command to only show up for certain files, this is how it works: https://github.com/Microsoft/vscode/blob/cabf4b2997fddf7eabff83d4d22ffa72381545b1/extensions/markdown/package.json#L191

@fbricon
Copy link
Collaborator

fbricon commented Oct 31, 2017

@aeschli that's what @yaohaizh did. The question is how to enable it for folders of java projects?

@fbricon
Copy link
Collaborator

fbricon commented Oct 31, 2017

Merged as 9779734 (with keybinding to Shift+Alt+o)

@fbricon fbricon closed this Oct 31, 2017
@yaohaizh yaohaizh deleted the yaohai_dev branch November 1, 2017 09:24
@fbricon fbricon added this to the End October 2017 milestone Nov 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants