sdedic commented on code in PR #6123:
URL: https://github.com/apache/netbeans/pull/6123#discussion_r1242309088


##########
enterprise/micronaut/src/org/netbeans/modules/micronaut/symbol/MicronautSymbolSearcher.java:
##########
@@ -0,0 +1,259 @@
+/*
+ * 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.micronaut.symbol;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.URI;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.Objects;
+import java.util.Set;
+import javax.swing.Icon;
+import org.netbeans.api.java.project.JavaProjectConstants;
+import org.netbeans.api.project.FileOwnerQuery;
+import org.netbeans.api.project.Project;
+import org.netbeans.api.project.ProjectUtils;
+import org.netbeans.api.project.SourceGroup;
+import org.netbeans.modules.csl.api.IndexSearcher;
+import org.netbeans.modules.csl.api.Modifier;
+import org.netbeans.modules.csl.api.OffsetRange;
+import org.netbeans.modules.csl.spi.GsfUtilities;
+import org.netbeans.modules.csl.spi.ParserResult;
+import org.netbeans.modules.parsing.impl.indexing.CacheFolder;
+import org.netbeans.modules.parsing.spi.indexing.support.QuerySupport.Kind;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.filesystems.URLMapper;
+import org.openide.util.Exceptions;
+import org.openide.util.lookup.ServiceProvider;
+
+/**
+ *
+ * @author Dusan Balek
+ */
+@ServiceProvider(service=IndexSearcher.class)
+public class MicronautSymbolSearcher implements IndexSearcher {
+
+    @Override
+    public Set<? extends Descriptor> getTypes(Project project, String 
textForQuery, Kind searchType, Helper helper) {
+        return Collections.emptySet();
+    }
+
+    @Override
+    public Set<? extends Descriptor> getSymbols(Project project, String 
textForQuery, Kind searchType, Helper helper) {
+        if (project == null || !textForQuery.startsWith("@")) {
+            return Collections.emptySet();
+        }
+        Set<Descriptor> symbols = new HashSet<>();
+        for (SourceGroup sg : 
ProjectUtils.getSources(project).getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA))
 {
+            try {
+                FileObject cacheRoot = 
getCacheRoot(sg.getRootFolder().toURL());
+                if (cacheRoot != null) {
+                    Enumeration<? extends FileObject> children = 
cacheRoot.getChildren(true);
+                    while (children.hasMoreElements()) {
+                        FileObject child = children.nextElement();
+                        if (child.hasExt("mn")) { //NOI18N
+                            loadSymbols(child, symbols);
+                        }
+                    }
+                }
+            } catch (IOException ex) {}
+        }
+        return symbols;
+    }
+
+    private static void loadSymbols(FileObject input, Set<Descriptor> symbols) 
{
+        try (BufferedReader br = new BufferedReader(new 
InputStreamReader(input.getInputStream(), StandardCharsets.UTF_8))) {
+            FileObject fo = null;
+            String line;
+            while ((line = br.readLine()) != null) {
+                if (line.startsWith("url: ")) { //NOI18N
+                    String url = line.substring(5);
+                    fo = URLMapper.findFileObject(URI.create(url).toURL());
+                    if (fo == null) {

Review Comment:
   this seems to cause a stale index (file removed, index not updated) to be 
skipped, the entire source root is skipped. OK ?



##########
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/WorkspaceServiceImpl.java:
##########
@@ -926,33 +927,34 @@ public boolean cancel(boolean mayInterruptIfRunning) {
         WORKER.post(() -> {
             try {
                 List<WorkspaceSymbol> symbols = new ArrayList<>();
-                if (client.getNbCodeCapabilities().wantsJavaSupport()) {
-                    SearchType searchType = getSearchType(queryFin, exactFin, 
false, null, null);
+                SearchType searchType = getSearchType(queryFin, exactFin, 
false, null, null);
 
-                    // CSL Part
-                    Collection<? extends IndexSearcher> providers = 
Lookup.getDefault().lookupAll(IndexSearcher.class);
-                    Set<? extends IndexSearcher.Descriptor> descriptors;
+                // CSL Part
+                Collection<? extends IndexSearcher> providers = 
Lookup.getDefault().lookupAll(IndexSearcher.class);
+                Set<? extends IndexSearcher.Descriptor> descriptors;
+                for (Project project : openedProjects) {
                     if (!providers.isEmpty()) {
                         for (IndexSearcher provider : providers) {
-                            descriptors = provider.getSymbols(null, queryFin, 
Utils.searchType2QueryKind(searchType), null);
+                            descriptors = provider.getSymbols(project, 
queryFin, Utils.searchType2QueryKind(searchType), null);
                             for (IndexSearcher.Descriptor desc : descriptors) {
                                 FileObject fo = desc.getFileObject();
                                 org.netbeans.modules.csl.api.ElementHandle 
element = desc.getElement();
                                 if (fo != null) {
-                                    Position startPos = 
Utils.createPosition(fo, desc.getOffset());
-                                    Position endPos = Utils.createPosition(fo, 
desc.getOffset() + desc.getSimpleName().length());
+                                    Position pos = Utils.createPosition(fo, 
desc.getOffset());
                                     WorkspaceSymbol symbol = new 
WorkspaceSymbol(
                                             desc.getSimpleName(),
                                             
Utils.cslElementKind2SymbolKind(element.getKind()),
-                                            Either.forLeft(new 
Location(Utils.toUri(fo), new Range(startPos, endPos))),
+                                            Either.forLeft(new 
Location(Utils.toUri(fo), new Range(pos, pos))),

Review Comment:
   Former startPos <-> endPos reduced to just pos ?



##########
enterprise/micronaut/src/org/netbeans/modules/micronaut/symbol/MicronautSymbolFinder.java:
##########
@@ -0,0 +1,336 @@
+/*
+ * 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.micronaut.symbol;
+
+import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.MethodTree;
+import com.sun.source.util.TreePathScanner;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.WeakHashMap;
+import java.util.stream.Collectors;
+import javax.lang.model.element.AnnotationMirror;
+import javax.lang.model.element.AnnotationValue;
+import javax.lang.model.element.Element;
+import javax.lang.model.element.ElementKind;
+import javax.lang.model.element.ExecutableElement;
+import javax.lang.model.element.Name;
+import javax.lang.model.element.TypeElement;
+import javax.lang.model.type.DeclaredType;
+import javax.lang.model.type.TypeKind;
+import javax.lang.model.type.TypeMirror;
+import javax.lang.model.util.ElementFilter;
+import javax.lang.model.util.Elements;
+import javax.lang.model.util.Types;
+import org.netbeans.api.editor.mimelookup.MimeRegistration;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.source.CompilationController;
+import org.netbeans.api.java.source.JavaSource;
+import org.netbeans.api.project.FileOwnerQuery;
+import org.netbeans.api.project.Project;
+import org.netbeans.modules.parsing.api.Snapshot;
+import org.netbeans.modules.parsing.spi.Parser;
+import org.netbeans.modules.parsing.spi.indexing.Context;
+import org.netbeans.modules.parsing.spi.indexing.EmbeddingIndexer;
+import org.netbeans.modules.parsing.spi.indexing.EmbeddingIndexerFactory;
+import org.netbeans.modules.parsing.spi.indexing.Indexable;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.util.Exceptions;
+import org.openide.util.Pair;
+
+/**
+ *
+ * @author Dusan Balek
+ */
+public final class MicronautSymbolFinder extends EmbeddingIndexer {
+
+    public static final String NAME = "mn"; // NOI18N
+    public static final int VERSION = 1;
+    public static final MicronautSymbolFinder INSTANCE = new 
MicronautSymbolFinder();
+    public static final String[] META_ANNOTATIONS = new String[] {
+        "io.micronaut.http.annotation.HttpMethodMapping",
+        "io.micronaut.context.annotation.Bean",
+        "jakarta.inject.Qualifier",
+        "jakarta.inject.Scope"
+    };
+
+    private final Map<Project, Boolean> map = new WeakHashMap<>();
+
+    @Override
+    protected void index(Indexable indexable, Parser.Result parserResult, 
Context context) {
+        CompilationController cc = CompilationController.get(parserResult);
+        if (initialize(cc)) {
+            try {
+                cc.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
+                List<SymbolLocation> symbols = scan(cc);
+                if (!symbols.isEmpty()) {
+                    store(context.getIndexFolder(), indexable.getURL(), 
indexable.getRelativePath(), symbols);
+                }
+            } catch (IOException ex) {
+                Exceptions.printStackTrace(ex);
+            }
+        }
+    }
+
+    private synchronized boolean initialize(CompilationController cc) {
+        Project p = FileOwnerQuery.getOwner(cc.getFileObject());
+        Boolean ret = map.get(p);
+        if (ret == null) {
+            ClassPath cp = ClassPath.getClassPath(p.getProjectDirectory(), 
ClassPath.COMPILE);

Review Comment:
   Just TODO: the map might need to be updated / invalidated as CP changes 
(i.e. jars resolved ?)



##########
enterprise/micronaut/src/org/netbeans/modules/micronaut/symbol/MicronautSymbolFinder.java:
##########
@@ -0,0 +1,336 @@
+/*
+ * 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.micronaut.symbol;
+
+import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.MethodTree;
+import com.sun.source.util.TreePathScanner;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.WeakHashMap;
+import java.util.stream.Collectors;
+import javax.lang.model.element.AnnotationMirror;
+import javax.lang.model.element.AnnotationValue;
+import javax.lang.model.element.Element;
+import javax.lang.model.element.ElementKind;
+import javax.lang.model.element.ExecutableElement;
+import javax.lang.model.element.Name;
+import javax.lang.model.element.TypeElement;
+import javax.lang.model.type.DeclaredType;
+import javax.lang.model.type.TypeKind;
+import javax.lang.model.type.TypeMirror;
+import javax.lang.model.util.ElementFilter;
+import javax.lang.model.util.Elements;
+import javax.lang.model.util.Types;
+import org.netbeans.api.editor.mimelookup.MimeRegistration;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.source.CompilationController;
+import org.netbeans.api.java.source.JavaSource;
+import org.netbeans.api.project.FileOwnerQuery;
+import org.netbeans.api.project.Project;
+import org.netbeans.modules.parsing.api.Snapshot;
+import org.netbeans.modules.parsing.spi.Parser;
+import org.netbeans.modules.parsing.spi.indexing.Context;
+import org.netbeans.modules.parsing.spi.indexing.EmbeddingIndexer;
+import org.netbeans.modules.parsing.spi.indexing.EmbeddingIndexerFactory;
+import org.netbeans.modules.parsing.spi.indexing.Indexable;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.util.Exceptions;
+import org.openide.util.Pair;
+
+/**
+ *
+ * @author Dusan Balek
+ */
+public final class MicronautSymbolFinder extends EmbeddingIndexer {
+
+    public static final String NAME = "mn"; // NOI18N
+    public static final int VERSION = 1;
+    public static final MicronautSymbolFinder INSTANCE = new 
MicronautSymbolFinder();
+    public static final String[] META_ANNOTATIONS = new String[] {
+        "io.micronaut.http.annotation.HttpMethodMapping",
+        "io.micronaut.context.annotation.Bean",
+        "jakarta.inject.Qualifier",
+        "jakarta.inject.Scope"
+    };
+
+    private final Map<Project, Boolean> map = new WeakHashMap<>();
+
+    @Override
+    protected void index(Indexable indexable, Parser.Result parserResult, 
Context context) {
+        CompilationController cc = CompilationController.get(parserResult);
+        if (initialize(cc)) {
+            try {
+                cc.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
+                List<SymbolLocation> symbols = scan(cc);
+                if (!symbols.isEmpty()) {
+                    store(context.getIndexFolder(), indexable.getURL(), 
indexable.getRelativePath(), symbols);
+                }
+            } catch (IOException ex) {
+                Exceptions.printStackTrace(ex);
+            }
+        }
+    }
+
+    private synchronized boolean initialize(CompilationController cc) {
+        Project p = FileOwnerQuery.getOwner(cc.getFileObject());
+        Boolean ret = map.get(p);
+        if (ret == null) {
+            ClassPath cp = ClassPath.getClassPath(p.getProjectDirectory(), 
ClassPath.COMPILE);
+            ret = 
cp.findResource("io/micronaut/http/annotation/HttpMethodMapping.class") != null;
+            map.put(p, ret);
+        }
+        return ret;
+    }
+
+    private List<SymbolLocation> scan(CompilationController cc) {
+        final List<SymbolLocation> ret = new ArrayList<>();
+        TreePathScanner<Void, String> scanner = new TreePathScanner<Void, 
String>() {
+            @Override
+            public Void visitClass(ClassTree node, String path) {
+                Element cls = cc.getTrees().getElement(this.getCurrentPath());
+                if (cls != null) {
+                    Pair<AnnotationMirror, AnnotationMirror> metaAnnotated = 
isMetaAnnotated(cls);
+                    if (metaAnnotated != null) {
+                        Element annEl = 
metaAnnotated.first().getAnnotationType().asElement();
+                        if 
("io.micronaut.http.annotation.Controller".contentEquals(((TypeElement) 
annEl).getQualifiedName())) {
+                            for (Map.Entry<? extends ExecutableElement, ? 
extends AnnotationValue> entry : 
metaAnnotated.first().getElementValues().entrySet()) {
+                                if 
("value".contentEquals(entry.getKey().getSimpleName())) {
+                                    path = (String) 
entry.getValue().getValue();
+                                }
+                            }
+                        }
+                        String name = "@+ '" + 
getBeanName(node.getSimpleName().toString()) + "' (@" + annEl.getSimpleName()
+                                + (metaAnnotated.second() != null ? " <: @" + 
metaAnnotated.second().getAnnotationType().asElement().getSimpleName() : "")
+                                + ") " + node.getSimpleName();
+                        int[] span = cc.getTreeUtilities().findNameSpan(node);
+                        ret.add(new SymbolLocation(name, span[0], span[1]));
+                    }
+                }
+                return super.visitClass(node, path);
+            }
+
+            @Override
+            public Void visitMethod(MethodTree node, String path) {
+                MthIterator it = new 
MthIterator(cc.getTrees().getElement(this.getCurrentPath()), cc.getElements(), 
cc.getTypes());
+                while (it.hasNext()) {
+                    for (AnnotationMirror ann : 
it.next().getAnnotationMirrors()) {
+                        String method = getEndpointMethod((TypeElement) 
ann.getAnnotationType().asElement());
+                        if (method != null) {
+                            for (Map.Entry<? extends ExecutableElement, ? 
extends AnnotationValue> entry : ann.getElementValues().entrySet()) {
+                                if 
("value".contentEquals(entry.getKey().getSimpleName()) || 
"uri".contentEquals(entry.getKey().getSimpleName())) {
+                                    String name = '@' + (path != null ? path : 
"") + entry.getValue().getValue() + " -- " + method;
+                                    int[] span = 
cc.getTreeUtilities().findNameSpan(node);
+                                    ret.add(new SymbolLocation(name, span[0], 
span[1]));
+                                    return null;
+                                }
+                            }
+                        }
+                    }
+                }
+                return null;
+            }
+        };
+        scanner.scan(cc.getCompilationUnit(), null);
+        return ret;
+    }
+
+    private void store(FileObject indexFolder, URL url, String resourceName, 
List<SymbolLocation> symbols) {
+        File cacheRoot = FileUtil.toFile(indexFolder);
+        File output = new File(cacheRoot, resourceName + ".mn"); //NOI18N
+        if (symbols.isEmpty()) {
+            if (output.exists()) {
+                output.delete();
+            }
+        } else {
+            output.getParentFile().mkdirs();
+            try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(new 
FileOutputStream(output), StandardCharsets.UTF_8))) {

Review Comment:
   There could be a vague chance that symbols are loaded while the file is 
being overwritten by the indexer. Consider to generate a temp file in the 
target dir + atomically move it to the `output` name at the end.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

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

Reply via email to