Search in sources :

Example 1 with SelectionRange

use of org.eclipse.lsp4j.SelectionRange in project eclipse.jdt.ls by eclipse.

the class SelectionRangeHandlerTest method testSwitch.

@Test
public void testSwitch() throws CoreException {
    SelectionRange range = getSelectionRange("org.sample.Foo4", new Position(22, 27));
    assertTrue(validateSelectionRange(// simple name
    range, // simple name
    new Range(new Position(22, 24), new Position(22, 30)), // method invocation
    new Range(new Position(22, 5), new Position(22, 31)), // expression statement
    new Range(new Position(22, 5), new Position(22, 32)), // switch statement
    new Range(new Position(20, 3), new Position(26, 4)), // block
    new Range(new Position(19, 6), new Position(27, 3)), // try statement
    new Range(new Position(19, 2), new Position(29, 3)), // block
    new Range(new Position(18, 85), new Position(30, 2)), // method declaration
    new Range(new Position(18, 1), new Position(30, 2)), TYPE_DECL_RANGE, COMP_UNIT_RAGE));
}
Also used : SelectionRange(org.eclipse.lsp4j.SelectionRange) Position(org.eclipse.lsp4j.Position) Range(org.eclipse.lsp4j.Range) SelectionRange(org.eclipse.lsp4j.SelectionRange) Test(org.junit.Test) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)

Example 2 with SelectionRange

use of org.eclipse.lsp4j.SelectionRange in project eclipse.jdt.ls by eclipse.

the class SelectionRangeHandlerTest method testParamList.

@Test
public void testParamList() throws CoreException {
    SelectionRange range = getSelectionRange("org.sample.Foo4", new Position(18, 24));
    assertTrue(validateSelectionRange(// simple name
    range, // simple name
    new Range(new Position(18, 21), new Position(18, 27)), // single variable declaration
    new Range(new Position(18, 17), new Position(18, 27)), // method declaration
    new Range(new Position(18, 1), new Position(30, 2)), TYPE_DECL_RANGE, COMP_UNIT_RAGE));
}
Also used : SelectionRange(org.eclipse.lsp4j.SelectionRange) Position(org.eclipse.lsp4j.Position) Range(org.eclipse.lsp4j.Range) SelectionRange(org.eclipse.lsp4j.SelectionRange) Test(org.junit.Test) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)

Example 3 with SelectionRange

use of org.eclipse.lsp4j.SelectionRange in project eclipse.jdt.ls by eclipse.

the class SelectionRangeHandlerTest method testStringLiteral.

@Test
public void testStringLiteral() throws CoreException {
    SelectionRange range = getSelectionRange("org.sample.Foo4", new Position(12, 30));
    assertTrue(validateSelectionRange(// string literal
    range, // string literal
    new Range(new Position(12, 21), new Position(12, 40)), // method invocation
    new Range(new Position(12, 2), new Position(12, 41)), // expression statement
    new Range(new Position(12, 2), new Position(12, 42)), // block
    new Range(new Position(11, 8), new Position(16, 2)), // method declaration
    new Range(new Position(8, 1), new Position(16, 2)), TYPE_DECL_RANGE, COMP_UNIT_RAGE));
}
Also used : SelectionRange(org.eclipse.lsp4j.SelectionRange) Position(org.eclipse.lsp4j.Position) Range(org.eclipse.lsp4j.Range) SelectionRange(org.eclipse.lsp4j.SelectionRange) Test(org.junit.Test) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)

Example 4 with SelectionRange

use of org.eclipse.lsp4j.SelectionRange in project eclipse.jdt.ls by eclipse.

the class SelectionRangeHandler method selectionRange.

public List<SelectionRange> selectionRange(SelectionRangeParams params, IProgressMonitor monitor) {
    if (params.getPositions() == null || params.getPositions().isEmpty()) {
        return Collections.emptyList();
    }
    ITypeRoot root = JDTUtils.resolveTypeRoot(params.getTextDocument().getUri());
    if (root == null) {
        return Collections.emptyList();
    }
    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<Comment> comments = new ArrayList<Comment>(ast.getCommentList());
    comments.removeIf(comment -> {
        // Javadoc nodes are already in the AST
        return (comment instanceof Javadoc);
    });
    List<SelectionRange> $ = 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<ASTNode> 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<ASTNode> 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) {
            JavaLanguageServerPlugin.logException("Failed to calculate selection range", e);
        }
    }
    return $;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) Comment(org.eclipse.jdt.core.dom.Comment) JavaModelException(org.eclipse.jdt.core.JavaModelException) Position(org.eclipse.lsp4j.Position) ITypeRoot(org.eclipse.jdt.core.ITypeRoot) ArrayList(java.util.ArrayList) Javadoc(org.eclipse.jdt.core.dom.Javadoc) Range(org.eclipse.lsp4j.Range) SelectionRange(org.eclipse.lsp4j.SelectionRange) SelectionRange(org.eclipse.lsp4j.SelectionRange) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 5 with SelectionRange

use of org.eclipse.lsp4j.SelectionRange in project eclipse.jdt.ls by eclipse.

the class SelectionRangeHandlerTest method testComments.

@Test
public void testComments() throws CoreException {
    // line comment
    SelectionRange range = getSelectionRange("org.sample.Foo4", new Position(12, 57));
    assertTrue(validateSelectionRange(// line comment
    range, // line comment
    new Range(new Position(12, 43), new Position(12, 66)), // block
    new Range(new Position(11, 8), new Position(16, 2)), // method declaration
    new Range(new Position(8, 1), new Position(16, 2)), TYPE_DECL_RANGE, COMP_UNIT_RAGE));
    // block comment
    range = getSelectionRange("org.sample.Foo4", new Position(14, 17));
    assertTrue(validateSelectionRange(// block comment
    range, // block comment
    new Range(new Position(14, 2), new Position(14, 29)), // block
    new Range(new Position(11, 8), new Position(16, 2)), // method declaration
    new Range(new Position(8, 1), new Position(16, 2)), TYPE_DECL_RANGE, COMP_UNIT_RAGE));
    // block comment in param list
    range = getSelectionRange("org.sample.Foo4", new Position(18, 42));
    assertTrue(validateSelectionRange(// block comment
    range, // block comment
    new Range(new Position(18, 27), new Position(18, 68)), // method declaration
    new Range(new Position(18, 1), new Position(30, 2)), TYPE_DECL_RANGE, COMP_UNIT_RAGE));
}
Also used : SelectionRange(org.eclipse.lsp4j.SelectionRange) Position(org.eclipse.lsp4j.Position) Range(org.eclipse.lsp4j.Range) SelectionRange(org.eclipse.lsp4j.SelectionRange) Test(org.junit.Test) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)

Aggregations

Position (org.eclipse.lsp4j.Position)6 Range (org.eclipse.lsp4j.Range)6 SelectionRange (org.eclipse.lsp4j.SelectionRange)6 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)1 ITypeRoot (org.eclipse.jdt.core.ITypeRoot)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 ASTNode (org.eclipse.jdt.core.dom.ASTNode)1 Comment (org.eclipse.jdt.core.dom.Comment)1 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)1 Javadoc (org.eclipse.jdt.core.dom.Javadoc)1