dbalek commented on code in PR #4071: URL: https://github.com/apache/netbeans/pull/4071#discussion_r864962685
########## java/java.editor/src/org/netbeans/modules/java/editor/javadoc/JavadocCompletionCollector.java: ########## @@ -0,0 +1,293 @@ +/* + * 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.editor.javadoc; + +import com.sun.source.tree.Scope; +import java.io.IOException; +import java.util.Collections; +import java.util.EnumSet; +import java.util.Iterator; +import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.function.Consumer; +import javax.lang.model.element.ElementKind; +import javax.lang.model.element.ExecutableElement; +import javax.lang.model.element.TypeElement; +import javax.lang.model.element.VariableElement; +import javax.lang.model.type.DeclaredType; +import javax.lang.model.type.ExecutableType; +import javax.lang.model.type.TypeKind; +import javax.lang.model.type.TypeMirror; +import javax.swing.text.Document; +import org.netbeans.api.editor.mimelookup.MimeRegistration; +import org.netbeans.api.java.source.CompilationController; +import org.netbeans.api.java.source.CompilationInfo; +import org.netbeans.api.java.source.ElementHandle; +import org.netbeans.api.java.source.JavaSource; +import org.netbeans.api.java.source.support.ReferencesCount; +import org.netbeans.api.lsp.Completion; +import org.netbeans.api.lsp.TextEdit; +import org.netbeans.modules.editor.java.JavaCompletionCollector; +import org.netbeans.modules.editor.java.Utilities; +import org.netbeans.modules.java.editor.base.javadoc.JavadocCompletionUtils; +import org.netbeans.modules.parsing.api.ParserManager; +import org.netbeans.modules.parsing.api.ResultIterator; +import org.netbeans.modules.parsing.api.Source; +import org.netbeans.modules.parsing.api.UserTask; +import org.netbeans.modules.parsing.spi.ParseException; +import org.netbeans.spi.lsp.CompletionCollector; +import org.openide.util.Exceptions; + +/** + * + * @author Dusan Balek + */ +@MimeRegistration(mimeType = "text/x-java", service = CompletionCollector.class) +public class JavadocCompletionCollector implements CompletionCollector { + + @Override + public boolean collectCompletions(Document doc, int offset, Completion.Context context, Consumer<Completion> consumer) { + AtomicBoolean ret = new AtomicBoolean(true); + if (JavadocCompletionUtils.isJavadocContext(doc, offset)) { + try { + ParserManager.parse(Collections.singletonList(Source.create(doc)), new UserTask() { + @Override + public void run(ResultIterator resultIterator) throws Exception { + CompilationController controller = CompilationController.get(resultIterator.getParserResult(offset)); + controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); + JavadocCompletionTask<Completion> task = JavadocCompletionTask.create(offset, new ItemFactoryImpl(controller, offset), + context.getTriggerKind() == Completion.TriggerKind.TriggerForIncompleteCompletions, () -> 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.hasAdditionalItems()) { + ret.set(false); + } + } + }); + } catch (ParseException ex) { + Exceptions.printStackTrace(ex); Review Comment: As the completion result set is usually collected form multiple collector instances, it is IMHO better (in case of failure of one of them) just to log the exception and show a partial result set collected from the other collectors (then notify users about the exception and show nothing). -- 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
