From 589e207b49de51b803c2163017bff1a882026098 Mon Sep 17 00:00:00 2001 From: Rome Li Date: Tue, 9 Jul 2019 20:39:21 +0800 Subject: [PATCH 1/5] Support textDocument/selectionRange Signed-off-by: Rome Li --- .../core/internal/handlers/InitHandler.java | 3 + .../internal/handlers/JDTLanguageServer.java | 17 +++ .../handlers/SelectionRangeHandler.java | 113 +++++++++++++++ .../preferences/ClientPreferences.java | 4 + .../internal/preferences/Preferences.java | 21 +++ .../org.eclipse.jdt.ls.tp.target | 4 +- .../maven/salut/src/main/java/java/Foo4.java | 32 +++++ .../handlers/SelectionRangeHandlerTest.java | 136 ++++++++++++++++++ 8 files changed, 328 insertions(+), 2 deletions(-) create mode 100644 org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/SelectionRangeHandler.java create mode 100644 org.eclipse.jdt.ls.tests/projects/maven/salut/src/main/java/java/Foo4.java create mode 100644 org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/SelectionRangeHandlerTest.java diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/InitHandler.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/InitHandler.java index 25ac6190ac..82d2295e98 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/InitHandler.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/InitHandler.java @@ -215,6 +215,9 @@ InitializeResult initialize(InitializeParams param) { if (!preferenceManager.getClientPreferences().isImplementationDynamicRegistered()) { capabilities.setImplementationProvider(Boolean.TRUE); } + if (!preferenceManager.getClientPreferences().isSelectionRangeDynamicRegistered()) { + capabilities.setSelectionRangeProvider(Boolean.TRUE); + } TextDocumentSyncOptions textDocumentSyncOptions = new TextDocumentSyncOptions(); textDocumentSyncOptions.setOpenClose(Boolean.TRUE); textDocumentSyncOptions.setSave(new SaveOptions(Boolean.TRUE)); diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/JDTLanguageServer.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/JDTLanguageServer.java index b39029ff02..7a2235f0c9 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/JDTLanguageServer.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/JDTLanguageServer.java @@ -109,6 +109,8 @@ import org.eclipse.lsp4j.Registration; import org.eclipse.lsp4j.RegistrationParams; import org.eclipse.lsp4j.RenameParams; +import org.eclipse.lsp4j.SelectionRange; +import org.eclipse.lsp4j.SelectionRangeParams; import org.eclipse.lsp4j.SignatureHelp; import org.eclipse.lsp4j.SymbolInformation; import org.eclipse.lsp4j.TextDocumentIdentifier; @@ -242,6 +244,9 @@ public void initialized(InitializedParams params) { if (preferenceManager.getClientPreferences().isImplementationDynamicRegistered()) { registerCapability(Preferences.IMPLEMENTATION_ID, Preferences.IMPLEMENTATION); } + if (preferenceManager.getClientPreferences().isSelectionRangeDynamicRegistered()) { + registerCapability(Preferences.SELECTION_RANGE_ID, Preferences.SELECTION_RANGE); + } // we do not have the user setting initialized yet at this point but we should // still call to enable defaults in case client does not support configuration changes syncCapabilitiesToSettings(); @@ -305,6 +310,9 @@ private void syncCapabilitiesToSettings() { if (preferenceManager.getClientPreferences().isFoldgingRangeDynamicRegistered()) { toggleCapability(preferenceManager.getPreferences().isFoldingRangeEnabled(), Preferences.FOLDINGRANGE_ID, Preferences.FOLDINGRANGE, null); } + if (preferenceManager.getClientPreferences().isSelectionRangeDynamicRegistered()) { + toggleCapability(preferenceManager.getPreferences().isSelectionRangeEnabled(), Preferences.SELECTION_RANGE_ID, Preferences.SELECTION_RANGE, null); + } } private CodeActionOptions getCodeActionOptions() { @@ -774,6 +782,15 @@ public CompletableFuture> foldingRange(FoldingRangeRequestPar }); } + @Override + public CompletableFuture> selectionRange(SelectionRangeParams params) { + logInfo(">> document/selectionRange"); + return computeAsyncWithClientProgress((monitor) -> { + waitForLifecycleJobs(monitor); + return new SelectionRangeHandler().selectionRange(params, monitor); + }); + } + @Override public CompletableFuture listOverridableMethods(CodeActionParams params) { logInfo(">> java/listOverridableMethods"); diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/SelectionRangeHandler.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/SelectionRangeHandler.java new file mode 100644 index 0000000000..5041bdbcb5 --- /dev/null +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/SelectionRangeHandler.java @@ -0,0 +1,113 @@ +/******************************************************************************* + * Copyright (c) 2019 Microsoft Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Microsoft Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.jdt.ls.core.internal.handlers; + +import java.util.ArrayList; +import java.util.List; +import java.util.ListIterator; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.jdt.core.ITypeRoot; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.Comment; +import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.dom.Javadoc; +import org.eclipse.jdt.core.dom.NodeFinder; +import org.eclipse.jdt.core.manipulation.CoreASTProvider; +import org.eclipse.jdt.ls.core.internal.JDTUtils; +import org.eclipse.lsp4j.Position; +import org.eclipse.lsp4j.Range; +import org.eclipse.lsp4j.SelectionRange; +import org.eclipse.lsp4j.SelectionRangeParams; + +public class SelectionRangeHandler { + + public List selectionRange(SelectionRangeParams params, IProgressMonitor monitor) { + if (params.getPositions() == null || params.getPositions().size() <= 0) { + return null; + } + + ITypeRoot root = JDTUtils.resolveTypeRoot(params.getTextDocument().getUri()); + if (root == null) { + return null; + } + + CompilationUnit ast = CoreASTProvider.getInstance().getAST(root, CoreASTProvider.WAIT_YES, monitor); + + // extra logic to check within the line comments and block comments, which are not parts of the AST + @SuppressWarnings("unchecked") + List comments = new ArrayList(ast.getCommentList()); + comments.removeIf(comment -> { + return (comment instanceof Javadoc); // Javadoc nodes are already in the AST + }); + + List $ = new ArrayList<>(); + for (Position pos : params.getPositions()) { + try { + int offset = JsonRpcHelpers.toOffset(root.getBuffer(), pos.getLine(), pos.getCharacter()); + ASTNode node = NodeFinder.perform(ast, offset, 0); + if (node == null) { + continue; + } + + // find all the ancestors + List nodes = new ArrayList<>(); + while (node != null) { + nodes.add(node); + node = node.getParent(); + } + + // find all the ranges corresponding to the parent nodes + SelectionRange selectionRange = null; + ListIterator iterator = nodes.listIterator(nodes.size()); + while (iterator.hasPrevious()) { + node = iterator.previous(); + Range range = JDTUtils.toRange(root, node.getStartPosition(), node.getLength()); + selectionRange = new SelectionRange(range, selectionRange); + } + + // find in comments + ASTNode containingComment = containingComment(comments, offset); + if (containingComment != null) { + Range range = JDTUtils.toRange(root, containingComment.getStartPosition(), containingComment.getLength()); + selectionRange = new SelectionRange(range, selectionRange); + } + + if (selectionRange != null) { + $.add(selectionRange); + } + } catch (JavaModelException e) { + e.printStackTrace(); + } + } + + return $; + } + + /** + * Finds the comment that contains the specified position + * + * @param comments + * @param offset + * @return + */ + public ASTNode containingComment(List comments, int offset) { + for (Comment comment : comments) { + ASTNode result = NodeFinder.perform(comment, offset, 0); + if (result != null) { + return result; + } + } + + return null; + } +} diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/ClientPreferences.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/ClientPreferences.java index 99015cfb90..517069ae19 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/ClientPreferences.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/ClientPreferences.java @@ -149,6 +149,10 @@ public boolean isImplementationDynamicRegistered() { return v3supported && isDynamicRegistrationSupported(capabilities.getTextDocument().getImplementation()); } + public boolean isSelectionRangeDynamicRegistered() { + return v3supported && isDynamicRegistrationSupported(capabilities.getTextDocument().getSelectionRange()); + } + public boolean isWillSaveRegistered() { return v3supported && capabilities.getTextDocument().getSynchronization() != null && isTrue(capabilities.getTextDocument().getSynchronization().getWillSave()); } diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java index 455337a49a..610962e857 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java @@ -169,6 +169,11 @@ public class Preferences { */ public static final String FOLDINGRANGE_ENABLED_KEY = "java.foldingRange.enabled"; + /** + * Preference key to enable/disable the selection range. + */ + public static final String SELECTIONRANGE_ENABLED_KEY = "java.selectionRange.enabled"; + /** * A named preference that holds the favorite static members. *

@@ -270,6 +275,7 @@ public class Preferences { public static final String FOLDINGRANGE = "textDocument/foldingRange"; public static final String WORKSPACE_CHANGE_FOLDERS = "workspace/didChangeWorkspaceFolders"; public static final String IMPLEMENTATION = "textDocument/implementation"; + public static final String SELECTION_RANGE = "textDocument/selectionRange"; public static final String FORMATTING_ID = UUID.randomUUID().toString(); public static final String FORMATTING_ON_TYPE_ID = UUID.randomUUID().toString(); @@ -291,6 +297,7 @@ public class Preferences { public static final String WORKSPACE_CHANGE_FOLDERS_ID = UUID.randomUUID().toString(); public static final String WORKSPACE_WATCHED_FILES_ID = UUID.randomUUID().toString(); public static final String IMPLEMENTATION_ID = UUID.randomUUID().toString(); + public static final String SELECTION_RANGE_ID = UUID.randomUUID().toString(); private Map configuration; private Severity incompleteClasspathSeverity; @@ -315,6 +322,7 @@ public class Preferences { private boolean completionEnabled; private boolean completionOverwrite; private boolean foldingRangeEnabled; + private boolean selectionRangeEnabled; private boolean guessMethodArguments; private boolean javaFormatComments; private boolean hashCodeEqualsTemplateUseJava7Objects; @@ -428,6 +436,7 @@ public Preferences() { completionEnabled = true; completionOverwrite = true; foldingRangeEnabled = true; + selectionRangeEnabled = true; guessMethodArguments = false; javaFormatComments = true; hashCodeEqualsTemplateUseJava7Objects = false; @@ -514,6 +523,9 @@ public static Preferences createFrom(Map configuration) { boolean foldingRangeEnable = getBoolean(configuration, FOLDINGRANGE_ENABLED_KEY, true); prefs.setFoldingRangeEnabled(foldingRangeEnable); + boolean selectionRangeEnabled = getBoolean(configuration, SELECTIONRANGE_ENABLED_KEY, true); + prefs.setSelectionRangeEnabled(selectionRangeEnabled); + boolean guessMethodArguments = getBoolean(configuration, JAVA_COMPLETION_GUESS_METHOD_ARGUMENTS_KEY, false); prefs.setGuessMethodArguments(guessMethodArguments); @@ -707,6 +719,11 @@ public Preferences setFoldingRangeEnabled(boolean enabled) { return this; } + public Preferences setSelectionRangeEnabled(boolean enabled) { + this.selectionRangeEnabled = enabled; + return this; + } + public Preferences setGuessMethodArguments(boolean guessMethodArguments) { this.guessMethodArguments = guessMethodArguments; return this; @@ -901,6 +918,10 @@ public boolean isFoldingRangeEnabled() { return foldingRangeEnabled; } + public boolean isSelectionRangeEnabled() { + return selectionRangeEnabled; + } + public boolean isGuessMethodArguments() { return guessMethodArguments; } diff --git a/org.eclipse.jdt.ls.target/org.eclipse.jdt.ls.tp.target b/org.eclipse.jdt.ls.target/org.eclipse.jdt.ls.tp.target index 978de22d14..3cdb004217 100644 --- a/org.eclipse.jdt.ls.target/org.eclipse.jdt.ls.tp.target +++ b/org.eclipse.jdt.ls.target/org.eclipse.jdt.ls.tp.target @@ -37,8 +37,8 @@ - - + + diff --git a/org.eclipse.jdt.ls.tests/projects/maven/salut/src/main/java/java/Foo4.java b/org.eclipse.jdt.ls.tests/projects/maven/salut/src/main/java/java/Foo4.java new file mode 100644 index 0000000000..ce4bcde133 --- /dev/null +++ b/org.eclipse.jdt.ls.tests/projects/maven/salut/src/main/java/java/Foo4.java @@ -0,0 +1,32 @@ +package java; + +/** + * This is the test data for SelectionRangeHandlerTest. To ask for a selection range, we need to specify a position. + * To make those positions visible, >< pairs are used if possible, and a position is what's between > and <. + * All other positions are specified in the test code dierectly. + */ +class Foo4 { + /** + * Class constructor to test >< Javadoc + */ + Foo4() { + System.out.println("string >< literal"); // test line >< comment + + /* test block >< comment */ + memberFunc(1, 0.0); + } + + void memberFunc(int paramA/* test block >< comment in param list */, double paramB) { + try { + switch(paramA) { + case 0: + System.out.println(paramB); + break; + default: + System.out.println(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/SelectionRangeHandlerTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/SelectionRangeHandlerTest.java new file mode 100644 index 0000000000..e03b957657 --- /dev/null +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/SelectionRangeHandlerTest.java @@ -0,0 +1,136 @@ +/******************************************************************************* + * Copyright (c) 2019 Microsoft Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Microsoft Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.jdt.ls.core.internal.handlers; + +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; +import java.util.ListIterator; + +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jdt.ls.core.internal.ClassFileUtil; +import org.eclipse.jdt.ls.core.internal.WorkspaceHelper; +import org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest; +import org.eclipse.lsp4j.Position; +import org.eclipse.lsp4j.Range; +import org.eclipse.lsp4j.SelectionRange; +import org.eclipse.lsp4j.SelectionRangeParams; +import org.eclipse.lsp4j.TextDocumentIdentifier; +import org.junit.Before; +import org.junit.Test; + +import com.google.common.collect.Lists; + +public class SelectionRangeHandlerTest extends AbstractProjectsManagerBasedTest { + + private IProject project; + private static Range TYPE_DECL_RANGE = new Range(new Position(2, 0), new Position(31, 1)); + private static Range COMP_UNIT_RAGE = new Range(new Position(0, 0), new Position(32, 0)); + + @Before + public void setup() throws Exception { + importProjects(Arrays.asList("maven/salut")); + project = WorkspaceHelper.getProject("salut"); + } + + @Test + public void testJavadoc() throws CoreException { + SelectionRange range = getSelectionRange("java.Foo4", new Position(9, 31)); + assertTrue(validateSelectionRange(range, new Range(new Position(9, 4), new Position(9, 40)), // text element + new Range(new Position(9, 4), new Position(9, 40)), // tag element + new Range(new Position(8, 1), new Position(10, 4)), // javadoc + new Range(new Position(8, 1), new Position(16, 2)), // method declaration + TYPE_DECL_RANGE, COMP_UNIT_RAGE)); + } + + @Test + public void testComments() throws CoreException { + // line comment + SelectionRange range = getSelectionRange("java.Foo4", new Position(12, 57)); + assertTrue(validateSelectionRange(range, new Range(new Position(12, 43), new Position(12, 66)), // line comment + new Range(new Position(11, 8), new Position(16, 2)), // block + new Range(new Position(8, 1), new Position(16, 2)), // method declaration + TYPE_DECL_RANGE, COMP_UNIT_RAGE)); + + // block comment + range = getSelectionRange("java.Foo4", new Position(14, 17)); + assertTrue(validateSelectionRange(range, new Range(new Position(14, 2), new Position(14, 29)), // block comment + new Range(new Position(11, 8), new Position(16, 2)), // block + new Range(new Position(8, 1), new Position(16, 2)), // method declaration + TYPE_DECL_RANGE, COMP_UNIT_RAGE)); + + // block comment in param list + range = getSelectionRange("java.Foo4", new Position(18, 42)); + assertTrue(validateSelectionRange(range, new Range(new Position(18, 27), new Position(18, 68)), // block comment + new Range(new Position(18, 1), new Position(30, 2)), // method declaration + TYPE_DECL_RANGE, COMP_UNIT_RAGE)); + } + + @Test + public void testStringLiteral() throws CoreException { + SelectionRange range = getSelectionRange("java.Foo4", new Position(12, 30)); + assertTrue(validateSelectionRange(range, new Range(new Position(12, 21), new Position(12, 40)), // string literal + new Range(new Position(12, 2), new Position(12, 41)), // method invocation + new Range(new Position(12, 2), new Position(12, 42)), // expression statement + new Range(new Position(11, 8), new Position(16, 2)), // block + new Range(new Position(8, 1), new Position(16, 2)), // method declaration + TYPE_DECL_RANGE, COMP_UNIT_RAGE)); + } + + @Test + public void testParamList() throws CoreException { + SelectionRange range = getSelectionRange("java.Foo4", new Position(18, 24)); + assertTrue(validateSelectionRange(range, new Range(new Position(18, 21), new Position(18, 27)), // simple name + new Range(new Position(18, 17), new Position(18, 27)), // single variable declaration + new Range(new Position(18, 1), new Position(30, 2)), // method declaration + TYPE_DECL_RANGE, COMP_UNIT_RAGE)); + + } + + @Test + public void testSwitch() throws CoreException { + SelectionRange range = getSelectionRange("java.Foo4", new Position(22, 27)); + assertTrue(validateSelectionRange(range, new Range(new Position(22, 24), new Position(22, 30)), // simple name + new Range(new Position(22, 5), new Position(22, 31)), // method invocation + new Range(new Position(22, 5), new Position(22, 32)), // expression statement + new Range(new Position(20, 3), new Position(26, 4)), // switch statement + new Range(new Position(19, 6), new Position(27, 3)), // block + new Range(new Position(19, 2), new Position(29, 3)), // try statement + new Range(new Position(18, 85), new Position(30, 2)), // block + new Range(new Position(18, 1), new Position(30, 2)), // method declaration + TYPE_DECL_RANGE, COMP_UNIT_RAGE)); + } + + private SelectionRange getSelectionRange(String className, Position position) throws CoreException { + SelectionRangeParams params = new SelectionRangeParams(); + params.setPositions(Lists.newArrayList(position)); + params.setTextDocument(new TextDocumentIdentifier(ClassFileUtil.getURI(project, className))); + return new SelectionRangeHandler().selectionRange(params, monitor).get(0); + } + + private boolean validateSelectionRange(SelectionRange range, Range... ranges) { + ListIterator iterator = Arrays.asList(ranges).listIterator(); + while (range != null && iterator.hasNext()) { + if (!range.getRange().equals(iterator.next())) { + return false; + } + + range = range.getParent(); + } + + if (range != null || iterator.hasNext()) { + return false; + } + + return true; + } +} From e4436a5ecc87bb54568dcecffce073e24aafaf68 Mon Sep 17 00:00:00 2001 From: Rome Li Date: Wed, 10 Jul 2019 08:48:03 +0800 Subject: [PATCH 2/5] Use Typefox p2 repository Signed-off-by: Rome Li --- .../org.eclipse.jdt.ls.tp.target | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/org.eclipse.jdt.ls.target/org.eclipse.jdt.ls.tp.target b/org.eclipse.jdt.ls.target/org.eclipse.jdt.ls.tp.target index 3cdb004217..228b836b21 100644 --- a/org.eclipse.jdt.ls.target/org.eclipse.jdt.ls.tp.target +++ b/org.eclipse.jdt.ls.target/org.eclipse.jdt.ls.tp.target @@ -1,4 +1,6 @@ - + + + @@ -36,14 +38,14 @@ - - - - + + + + - + \ No newline at end of file From e96dd2360e8f4e6780c73cb236647d291a8507f0 Mon Sep 17 00:00:00 2001 From: Rome Li Date: Wed, 10 Jul 2019 09:02:07 +0800 Subject: [PATCH 3/5] Update sample code package name Signed-off-by: Rome Li --- .../src/main/java/{java => org/sample}/Foo4.java | 2 +- .../handlers/SelectionRangeHandlerTest.java | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) rename org.eclipse.jdt.ls.tests/projects/maven/salut/src/main/java/{java => org/sample}/Foo4.java (97%) diff --git a/org.eclipse.jdt.ls.tests/projects/maven/salut/src/main/java/java/Foo4.java b/org.eclipse.jdt.ls.tests/projects/maven/salut/src/main/java/org/sample/Foo4.java similarity index 97% rename from org.eclipse.jdt.ls.tests/projects/maven/salut/src/main/java/java/Foo4.java rename to org.eclipse.jdt.ls.tests/projects/maven/salut/src/main/java/org/sample/Foo4.java index ce4bcde133..28d0d98422 100644 --- a/org.eclipse.jdt.ls.tests/projects/maven/salut/src/main/java/java/Foo4.java +++ b/org.eclipse.jdt.ls.tests/projects/maven/salut/src/main/java/org/sample/Foo4.java @@ -1,4 +1,4 @@ -package java; +package org.sample; /** * This is the test data for SelectionRangeHandlerTest. To ask for a selection range, we need to specify a position. diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/SelectionRangeHandlerTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/SelectionRangeHandlerTest.java index e03b957657..344a81ab4c 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/SelectionRangeHandlerTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/SelectionRangeHandlerTest.java @@ -44,7 +44,7 @@ public void setup() throws Exception { @Test public void testJavadoc() throws CoreException { - SelectionRange range = getSelectionRange("java.Foo4", new Position(9, 31)); + SelectionRange range = getSelectionRange("org.sample.Foo4", new Position(9, 31)); assertTrue(validateSelectionRange(range, new Range(new Position(9, 4), new Position(9, 40)), // text element new Range(new Position(9, 4), new Position(9, 40)), // tag element new Range(new Position(8, 1), new Position(10, 4)), // javadoc @@ -55,21 +55,21 @@ public void testJavadoc() throws CoreException { @Test public void testComments() throws CoreException { // line comment - SelectionRange range = getSelectionRange("java.Foo4", new Position(12, 57)); + SelectionRange range = getSelectionRange("org.sample.Foo4", new Position(12, 57)); assertTrue(validateSelectionRange(range, new Range(new Position(12, 43), new Position(12, 66)), // line comment new Range(new Position(11, 8), new Position(16, 2)), // block new Range(new Position(8, 1), new Position(16, 2)), // method declaration TYPE_DECL_RANGE, COMP_UNIT_RAGE)); // block comment - range = getSelectionRange("java.Foo4", new Position(14, 17)); + range = getSelectionRange("org.sample.Foo4", new Position(14, 17)); assertTrue(validateSelectionRange(range, new Range(new Position(14, 2), new Position(14, 29)), // block comment new Range(new Position(11, 8), new Position(16, 2)), // block new Range(new Position(8, 1), new Position(16, 2)), // method declaration TYPE_DECL_RANGE, COMP_UNIT_RAGE)); // block comment in param list - range = getSelectionRange("java.Foo4", new Position(18, 42)); + range = getSelectionRange("org.sample.Foo4", new Position(18, 42)); assertTrue(validateSelectionRange(range, new Range(new Position(18, 27), new Position(18, 68)), // block comment new Range(new Position(18, 1), new Position(30, 2)), // method declaration TYPE_DECL_RANGE, COMP_UNIT_RAGE)); @@ -77,7 +77,7 @@ public void testComments() throws CoreException { @Test public void testStringLiteral() throws CoreException { - SelectionRange range = getSelectionRange("java.Foo4", new Position(12, 30)); + SelectionRange range = getSelectionRange("org.sample.Foo4", new Position(12, 30)); assertTrue(validateSelectionRange(range, new Range(new Position(12, 21), new Position(12, 40)), // string literal new Range(new Position(12, 2), new Position(12, 41)), // method invocation new Range(new Position(12, 2), new Position(12, 42)), // expression statement @@ -88,7 +88,7 @@ public void testStringLiteral() throws CoreException { @Test public void testParamList() throws CoreException { - SelectionRange range = getSelectionRange("java.Foo4", new Position(18, 24)); + SelectionRange range = getSelectionRange("org.sample.Foo4", new Position(18, 24)); assertTrue(validateSelectionRange(range, new Range(new Position(18, 21), new Position(18, 27)), // simple name new Range(new Position(18, 17), new Position(18, 27)), // single variable declaration new Range(new Position(18, 1), new Position(30, 2)), // method declaration @@ -98,7 +98,7 @@ public void testParamList() throws CoreException { @Test public void testSwitch() throws CoreException { - SelectionRange range = getSelectionRange("java.Foo4", new Position(22, 27)); + SelectionRange range = getSelectionRange("org.sample.Foo4", new Position(22, 27)); assertTrue(validateSelectionRange(range, new Range(new Position(22, 24), new Position(22, 30)), // simple name new Range(new Position(22, 5), new Position(22, 31)), // method invocation new Range(new Position(22, 5), new Position(22, 32)), // expression statement From 7d9b27f644f5da7293b301c306e0e958ad2bcb4c Mon Sep 17 00:00:00 2001 From: Rome Li Date: Thu, 11 Jul 2019 09:50:13 +0800 Subject: [PATCH 4/5] Use 0.0.0 version to reference LSP4J Signed-off-by: Rome Li --- org.eclipse.jdt.ls.target/org.eclipse.jdt.ls.tp.target | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/org.eclipse.jdt.ls.target/org.eclipse.jdt.ls.tp.target b/org.eclipse.jdt.ls.target/org.eclipse.jdt.ls.tp.target index 228b836b21..af42feb9d8 100644 --- a/org.eclipse.jdt.ls.target/org.eclipse.jdt.ls.tp.target +++ b/org.eclipse.jdt.ls.target/org.eclipse.jdt.ls.tp.target @@ -44,8 +44,8 @@ - + - \ No newline at end of file + From 220cf00e1cec4f929e4a10aa623ea7ff4a78ad97 Mon Sep 17 00:00:00 2001 From: Rome Li Date: Thu, 11 Jul 2019 10:52:55 +0800 Subject: [PATCH 5/5] Resolve comments regarding return value and logging Signed-off-by: Rome Li --- .../core/internal/handlers/SelectionRangeHandler.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/SelectionRangeHandler.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/SelectionRangeHandler.java index 5041bdbcb5..968cad9f85 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/SelectionRangeHandler.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/SelectionRangeHandler.java @@ -11,6 +11,7 @@ package org.eclipse.jdt.ls.core.internal.handlers; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.ListIterator; @@ -24,6 +25,7 @@ import org.eclipse.jdt.core.dom.NodeFinder; import org.eclipse.jdt.core.manipulation.CoreASTProvider; import org.eclipse.jdt.ls.core.internal.JDTUtils; +import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin; import org.eclipse.lsp4j.Position; import org.eclipse.lsp4j.Range; import org.eclipse.lsp4j.SelectionRange; @@ -32,13 +34,13 @@ public class SelectionRangeHandler { public List selectionRange(SelectionRangeParams params, IProgressMonitor monitor) { - if (params.getPositions() == null || params.getPositions().size() <= 0) { - return null; + if (params.getPositions() == null || params.getPositions().isEmpty()) { + return Collections.emptyList(); } ITypeRoot root = JDTUtils.resolveTypeRoot(params.getTextDocument().getUri()); if (root == null) { - return null; + return Collections.emptyList(); } CompilationUnit ast = CoreASTProvider.getInstance().getAST(root, CoreASTProvider.WAIT_YES, monitor); @@ -86,7 +88,7 @@ public List selectionRange(SelectionRangeParams params, IProgres $.add(selectionRange); } } catch (JavaModelException e) { - e.printStackTrace(); + JavaLanguageServerPlugin.logException("Failed to calculate selection range", e); } }