dbalek commented on code in PR #4071:
URL: https://github.com/apache/netbeans/pull/4071#discussion_r864953476


##########
java/java.editor/src/org/netbeans/modules/editor/java/JavaCompletionCollector.java:
##########
@@ -110,57 +110,178 @@ public class JavaCompletionCollector implements 
CompletionCollector {
     @Override
     public boolean collectCompletions(Document doc, int offset, 
Completion.Context context, Consumer<Completion> consumer) {
         AtomicBoolean ret = new AtomicBoolean(true);
-        try {
-            ParserManager.parse(Collections.singletonList(Source.create(doc)), 
new UserTask() {
-                @Override
-                public void run(ResultIterator resultIterator) throws 
Exception {
-                    TokenSequence<JavaTokenId> ts = null;
-                    for (TokenSequence<?> cand : 
resultIterator.getSnapshot().getTokenHierarchy().embeddedTokenSequences(offset, 
false)) {
-                        if (cand.language() == JavaTokenId.language()) {
-                            ts = (TokenSequence<JavaTokenId>) cand;
-                            break;
-                        }
-                    }
-                    if (ts == null) {//No Java on this offset
-                        return ;
-                    }
-                    if (ts.move(offset) == 0 || !ts.moveNext()) {
-                        if (!ts.movePrevious()) {
-                            ts.moveNext();
+        if (Utilities.isJavaContext(doc, offset, true)) {
+            try {
+                
ParserManager.parse(Collections.singletonList(Source.create(doc)), new 
UserTask() {
+                    @Override
+                    public void run(ResultIterator resultIterator) throws 
Exception {
+                        TokenSequence<JavaTokenId> ts = 
SourceUtils.getJavaTokenSequence(resultIterator.getSnapshot().getTokenHierarchy(),
 offset);
+                        if (ts.move(offset) == 0 || !ts.moveNext()) {
+                            if (!ts.movePrevious()) {
+                                ts.moveNext();
+                            }
                         }
-                    }
-                    int len = offset - ts.offset();
-                    boolean combinedCompletion = context != null && 
context.getTriggerKind() == 
Completion.TriggerKind.TriggerForIncompleteCompletions
-                            || len > 0 && ts.token().length() >= len && 
ts.token().id() == JavaTokenId.IDENTIFIER;
-                    CompilationController controller = 
CompilationController.get(resultIterator.getParserResult(ts.offset()));
-                    controller.toPhase(JavaSource.Phase.RESOLVED);
-                    JavaCompletionTask<Completion> task = 
JavaCompletionTask.create(offset, new ItemFactoryImpl(controller, offset), 
combinedCompletion ? EnumSet.of(JavaCompletionTask.Options.COMBINED_COMPLETION) 
: EnumSet.noneOf(JavaCompletionTask.Options.class), () -> false);
-                    task.run(resultIterator);
-                    List<Completion> results = task.getResults();
-                    if (results != null) {
-                        for (Iterator<Completion> it = results.iterator(); 
it.hasNext();) {
-                            Completion item = it.next();
-                            if (item == null) {
-                                it.remove();
+                        int len = offset - ts.offset();
+                        boolean combinedCompletion = context != null && 
context.getTriggerKind() == 
Completion.TriggerKind.TriggerForIncompleteCompletions
+                                || len > 0 && ts.token().length() >= len && 
ts.token().id() == JavaTokenId.IDENTIFIER;
+                        CompilationController controller = 
CompilationController.get(resultIterator.getParserResult(ts.offset()));
+                        controller.toPhase(JavaSource.Phase.RESOLVED);
+                        JavaCompletionTask<Completion> task = 
JavaCompletionTask.create(offset, new ItemFactoryImpl(controller, offset), 
combinedCompletion ? EnumSet.of(JavaCompletionTask.Options.COMBINED_COMPLETION) 
: EnumSet.noneOf(JavaCompletionTask.Options.class), () -> false);
+                        task.run(resultIterator);
+                        List<Completion> results = task.getResults();
+                        if (results != null) {
+                            for (Iterator<Completion> it = results.iterator(); 
it.hasNext();) {
+                                Completion item = it.next();
+                                if (item == null) {
+                                    it.remove();
+                                }
                             }
+                            results.forEach(consumer);
+                        }
+                        if (task.hasAdditionalClasses() || 
task.hasAdditionalMembers()) {
+                            ret.set(false);
                         }
-                        results.forEach(consumer);
-                    }
-                    if (task.hasAdditionalClasses() || 
task.hasAdditionalMembers()) {
-                        ret.set(false);
                     }
-                }
-            });
-        } catch (ParseException ex) {
-            Exceptions.printStackTrace(ex);
+                });
+            } catch (ParseException ex) {
+                Exceptions.printStackTrace(ex);
+            }
         }
         return ret.get();
     }
 
+    public static final Set<String> SUPPORTED_ELEMENT_KINDS = new 
HashSet<>(Arrays.asList("PACKAGE", "CLASS", "INTERFACE", "ENUM", 
"ANNOTATION_TYPE", "METHOD", "CONSTRUCTOR", "INSTANCE_INIT", "STATIC_INIT", 
"FIELD", "ENUM_CONSTANT", "TYPE_PARAMETER", "MODULE"));
+
+    public static Supplier<String> getDocumentation(Document doc, int offset, 
ElementHandle handle) {
+        return () -> {
+            try {
+                JavaDocumentationTask<Future<String>> task = 
JavaDocumentationTask.create(offset, handle, new 
JavaDocumentationTask.DocumentationFactory<Future<String>>() {
+                    @Override
+                    public Future<String> create(CompilationInfo 
compilationInfo, Element element, Callable<Boolean> cancel) {
+                        return ElementJavadoc.create(compilationInfo, element, 
cancel).getTextAsync();
+                    }
+                }, () -> false);
+                
ParserManager.parse(Collections.singletonList(Source.create(doc)), new 
UserTask() {
+                    @Override
+                    public void run(ResultIterator resultIterator) throws 
Exception {
+                        task.run(resultIterator);
+                    }
+                });
+                return task.getDocumentation().get();
+            } catch (Exception ex) {
+                throw new RuntimeException(ex);
+            }
+        };
+    }
+
+    public static Completion.Kind elementKind2CompletionItemKind(ElementKind 
kind) {

Review Comment:
   Unfortunately, `ElementHeaders.javaKind2Structure` returns 
`StructureElement.Kind` that corresponds to the 
[SymbolKind](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#symbolKind)
 defined by LSP which however differs from the 
[CompletionItemKind](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#completionItemKind)
 required by code completion (`Completion.Kind`).



-- 
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