You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by GitBox <gi...@apache.org> on 2022/03/09 07:45:41 UTC

[GitHub] [netbeans] sdedic opened a new pull request #3735: Support for Call Hierarchy for LSP

sdedic opened a new pull request #3735:
URL: https://github.com/apache/netbeans/pull/3735


   This PR adds support for `textDocument/prepareCallHierarchy`, `callHierarchy/incomingCalls` and `callHierarchy/outgoingCalls` messages (see [LSP Protocol Spec 3.16](https://microsoft.github.io/language-server-protocol/specification#textDocument_prepareCallHierarchy) ).
   
   I decided to **reuse** `StructureElement` invented as a part of Outline support: elements reported by the Call Hierarchy have exactly the same meaning, plus the `uri`, which is implicit in the Outline. I have added `file` property to `StructureElement` and relevant `StructureProvider.Builder` method.
   
   In order to share code between Java Editor module (that implements Outline) and Java Refactoring module (which implements the Call Hierarchy, but does not export it through any API), I've introduced helper methods for LSP into `java.sourceui` module: `lsp.api` module represents an abstract UI, after all, so adapting Java-specific structures to abstract ones seemed reasonable. This API allows to move Java Element -> StructureElement conversion to `java.sourceui`, and can be called from both `java.editor` and `java.refactoring`.
   
   The other reason to choose `java.sourceui` for introducing the API is that the `ElementOpen` implementation is here, and can be adjusted to support StructureElement creation without exposing additional APIs.
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] sdedic commented on pull request #3735: Support for Call Hierarchy for LSP

Posted by GitBox <gi...@apache.org>.
sdedic commented on pull request #3735:
URL: https://github.com/apache/netbeans/pull/3735#issuecomment-1065058412


   Sorry, errors found during testing...


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] sdedic merged pull request #3735: Support for Call Hierarchy for LSP

Posted by GitBox <gi...@apache.org>.
sdedic merged pull request #3735:
URL: https://github.com/apache/netbeans/pull/3735


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] sdedic commented on a change in pull request #3735: Support for Call Hierarchy for LSP

Posted by GitBox <gi...@apache.org>.
sdedic commented on a change in pull request #3735:
URL: https://github.com/apache/netbeans/pull/3735#discussion_r823086267



##########
File path: ide/api.lsp/src/org/netbeans/api/lsp/CallHierarchyEntry.java
##########
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.api.lsp;
+
+import java.util.List;
+import org.netbeans.api.annotations.common.CheckForNull;
+import org.netbeans.api.annotations.common.NonNull;
+import org.netbeans.spi.lsp.CallHierarchyProvider;
+
+/**
+ * Represents an entry in a call hierarchy chain. The entry identifies an element in a source which is the origin or target
+ * of an invocation/call. 
+ * 
+ * @since 1.69
+ * @author sdedic
+ */
+public final class CallHierarchyEntry {
+    /**
+     * The call origin or target element.
+     */
+    private StructureElement    element;
+    
+    /**
+     * Opaque implementation-specific data.
+     */
+    private String  customData;
+
+    /**
+     * Returns description of the call hierarchy element. See {@link StructureElemen} for structure details,
+     * @return description of the structural languagein call hierarchy
+     */
+    @NonNull
+    public StructureElement getElement() {
+        return element;
+    }
+    
+    /**
+     * Returns an opaque, mime type specific data which shall be interpreted by the {@link CallHierarchyProvider}
+     * in subsequent calls. For example, method signature can be put here.
+     * @return provider-specific data
+     */
+    @CheckForNull
+    public String getCustomData() {
+        return customData;
+    }
+
+    /**
+     * Constructs a new entry object.
+     * @param element represents call target or call origin, depending on usage
+     * @param customData implementation-specific data
+     */
+    public CallHierarchyEntry(StructureElement element, String customData) {
+        this.element = element;
+        this.customData = customData;
+    }
+    
+    
+    /**
+     * This structure is used for two purposes. For <b>outgoing calls</b> the {@link #getItem}
+     * returns the call target, and {@link #getRanges} returns locations in the origin
+     * {@link CallHierarchyElement} where the target is invoked from. For <b>incoming calls</b>
+     * the {@link #getItem} identifies the element that makes the call, while {@link #getRanges}
+     * locations where the call was made from from that element.
+     * 
+     */
+    public static final class Call {
+        private final CallHierarchyEntry item;
+        private final List<CallRange> ranges;
+
+        public Call(@NonNull CallHierarchyEntry item, List<CallRange> ranges) {
+            this.item = item;
+            this.ranges = ranges;
+        }
+        
+        /**
+         * @return Target or origin element of the call.
+         */
+        @NonNull
+        public CallHierarchyEntry getItem() {
+            return item;
+        }
+
+        /**
+         * @return text locations within the origin element where the target is invoked.
+         */
+        public List<CallRange> getRanges() {
+            return ranges;
+        }
+    }
+    
+    /**
+     * Represents a textual range. Range covers text from `startOffset' to `endOffset' (exclusive).
+     */
+    public static final class CallRange {
+        private final int startOffset;

Review comment:
       OK, will introduce a toplevel `Range` ... plus I guess I could add `getSelectionRange` and `getExtendedRange` to the `StructureElement` - right ?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] ppisl commented on a change in pull request #3735: Support for Call Hierarchy for LSP

Posted by GitBox <gi...@apache.org>.
ppisl commented on a change in pull request #3735:
URL: https://github.com/apache/netbeans/pull/3735#discussion_r823057321



##########
File path: java/java.sourceui/src/org/netbeans/modules/java/source/ui/LspElementUtils.java
##########
@@ -0,0 +1,335 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.java.source.ui;
+
+import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.NewClassTree;
+import com.sun.source.tree.Tree;
+import com.sun.source.util.TreePath;
+import com.sun.source.util.TreePathScanner;
+import java.io.IOException;
+import java.util.EnumSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionException;
+import java.util.concurrent.atomic.AtomicBoolean;
+import javax.lang.model.element.Element;
+import javax.lang.model.element.ElementKind;
+import javax.lang.model.element.ExecutableElement;
+import javax.lang.model.element.PackageElement;
+import javax.lang.model.element.TypeElement;
+import javax.lang.model.element.TypeParameterElement;
+import javax.lang.model.element.VariableElement;
+import javax.lang.model.type.ArrayType;
+import javax.lang.model.type.TypeKind;
+import javax.lang.model.type.TypeMirror;
+import org.netbeans.api.java.source.CompilationInfo;
+import org.netbeans.api.java.source.ElementHandle;
+import org.netbeans.api.java.source.ElementUtilities.ElementAcceptor;
+import org.netbeans.api.java.source.JavaSource;
+import org.netbeans.api.java.source.TreePathHandle;
+import org.netbeans.api.java.source.TreeUtilities;
+import org.netbeans.api.java.source.TypeUtilities;
+import org.netbeans.api.java.source.ui.ElementHeaders;
+import org.netbeans.api.lsp.StructureElement;
+import org.netbeans.spi.lsp.StructureProvider;
+import org.openide.filesystems.FileObject;
+
+/**
+ *
+ * @author sdedic
+ */
+public class LspElementUtils {
+    
+    public  static StructureElement element2StructureElement(CompilationInfo info, Element el, ElementAcceptor childAcceptor) {
+        TreePath path = info.getTrees().getPath(el);
+        if (path == null) {
+            return null;
+        }
+        TreeUtilities tu = info.getTreeUtilities();
+        if (tu.isSynthetic(path)) {
+            return null;
+        }
+
+        StructureProvider.Builder builder = StructureProvider.newBuilder(createName(info, el), ElementHeaders.javaKind2Structure(el));
+        builder.detail(createDetail(info, el));
+        setOffsets(info, el, builder);
+        if (info.getElements().isDeprecated(el)) {
+            builder.addTag(StructureElement.Tag.Deprecated);
+        }
+
+        if (childAcceptor != null) {
+            for (Element child : el.getEnclosedElements()) {
+                TreePath p = info.getTrees().getPath(el);

Review comment:
       `el` is not changed in this `for` cycle, so why we get `TreePath` in every cycle? Probably it should be `child` the param of `getPath()`?

##########
File path: java/refactoring.java/nbproject/project.xml
##########
@@ -52,6 +52,15 @@
                         <specification-version>1.0</specification-version>
                     </run-dependency>
                 </dependency>
+                <dependency>
+                    <code-name-base>org.netbeans.api.lsp</code-name-base>
+                    <build-prerequisite/>
+                    <compile-dependency/>
+                    <run-dependency>
+                        <release-version>1</release-version>
+                        <specification-version>1.8</specification-version>

Review comment:
       The new version of LSP API is 1.9. Version 1.8 introduced StructureElement, which is changed in this new version 1.9.

##########
File path: ide/api.lsp/src/org/netbeans/api/lsp/CallHierarchyEntry.java
##########
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.api.lsp;
+
+import java.util.List;
+import org.netbeans.api.annotations.common.CheckForNull;
+import org.netbeans.api.annotations.common.NonNull;
+import org.netbeans.spi.lsp.CallHierarchyProvider;
+
+/**
+ * Represents an entry in a call hierarchy chain. The entry identifies an element in a source which is the origin or target
+ * of an invocation/call. 
+ * 
+ * @since 1.69

Review comment:
       Shouldn't be 1.9? 

##########
File path: ide/api.lsp/src/org/netbeans/api/lsp/CallHierarchyEntry.java
##########
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.api.lsp;
+
+import java.util.List;
+import org.netbeans.api.annotations.common.CheckForNull;
+import org.netbeans.api.annotations.common.NonNull;
+import org.netbeans.spi.lsp.CallHierarchyProvider;
+
+/**
+ * Represents an entry in a call hierarchy chain. The entry identifies an element in a source which is the origin or target
+ * of an invocation/call. 
+ * 
+ * @since 1.69
+ * @author sdedic
+ */
+public final class CallHierarchyEntry {
+    /**
+     * The call origin or target element.
+     */
+    private StructureElement    element;
+    
+    /**
+     * Opaque implementation-specific data.
+     */
+    private String  customData;
+
+    /**
+     * Returns description of the call hierarchy element. See {@link StructureElemen} for structure details,
+     * @return description of the structural languagein call hierarchy
+     */
+    @NonNull
+    public StructureElement getElement() {
+        return element;
+    }
+    
+    /**
+     * Returns an opaque, mime type specific data which shall be interpreted by the {@link CallHierarchyProvider}
+     * in subsequent calls. For example, method signature can be put here.
+     * @return provider-specific data
+     */
+    @CheckForNull
+    public String getCustomData() {
+        return customData;
+    }
+
+    /**
+     * Constructs a new entry object.
+     * @param element represents call target or call origin, depending on usage
+     * @param customData implementation-specific data
+     */
+    public CallHierarchyEntry(StructureElement element, String customData) {
+        this.element = element;
+        this.customData = customData;
+    }
+    
+    
+    /**
+     * This structure is used for two purposes. For <b>outgoing calls</b> the {@link #getItem}
+     * returns the call target, and {@link #getRanges} returns locations in the origin
+     * {@link CallHierarchyElement} where the target is invoked from. For <b>incoming calls</b>
+     * the {@link #getItem} identifies the element that makes the call, while {@link #getRanges}
+     * locations where the call was made from from that element.
+     * 
+     */
+    public static final class Call {
+        private final CallHierarchyEntry item;
+        private final List<CallRange> ranges;
+
+        public Call(@NonNull CallHierarchyEntry item, List<CallRange> ranges) {
+            this.item = item;
+            this.ranges = ranges;
+        }
+        
+        /**
+         * @return Target or origin element of the call.
+         */
+        @NonNull
+        public CallHierarchyEntry getItem() {
+            return item;
+        }
+
+        /**
+         * @return text locations within the origin element where the target is invoked.
+         */
+        public List<CallRange> getRanges() {
+            return ranges;
+        }
+    }
+    
+    /**
+     * Represents a textual range. Range covers text from `startOffset' to `endOffset' (exclusive).
+     */
+    public static final class CallRange {
+        private final int startOffset;

Review comment:
       Probably we should introduce a generic name and use it in other structures too. There are more places, where we use start and end offset range. Something like Position?
   
   There are two ranges in StructureElement that can be replaced with such Position ... . This is a suggestion and doesn't have to be part of this PR, but we probably should simplify it. 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists