dbalek commented on code in PR #4071:
URL: https://github.com/apache/netbeans/pull/4071#discussion_r864994761
##########
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) {
+ switch (kind) {
+ case PACKAGE:
+ return Completion.Kind.Folder;
+ case ENUM:
+ return Completion.Kind.Enum;
+ case CLASS:
+ return Completion.Kind.Class;
+ case ANNOTATION_TYPE:
+ return Completion.Kind.Interface;
+ case INTERFACE:
+ return Completion.Kind.Interface;
+ case ENUM_CONSTANT:
+ return Completion.Kind.EnumMember;
+ case FIELD:
+ return Completion.Kind.Field;
+ case PARAMETER:
+ return Completion.Kind.Variable;
+ case LOCAL_VARIABLE:
+ return Completion.Kind.Variable;
+ case EXCEPTION_PARAMETER:
+ return Completion.Kind.Variable;
+ case METHOD:
+ return Completion.Kind.Method;
+ case CONSTRUCTOR:
+ return Completion.Kind.Constructor;
+ case TYPE_PARAMETER:
+ return Completion.Kind.TypeParameter;
+ case RESOURCE_VARIABLE:
+ return Completion.Kind.Variable;
+ case MODULE:
+ return Completion.Kind.Module;
+ case STATIC_INIT:
+ case INSTANCE_INIT:
+ case OTHER:
+ default:
+ return Completion.Kind.Text;
+ }
+ }
+
+ public static Supplier<List<TextEdit>> addImport(Document doc,
ElementHandle<?> handle) {
+ return () -> {
+ try {
+ TextEdit textEdit =
modify2TextEdit(JavaSource.forDocument(doc), copy -> {
+ copy.toPhase(JavaSource.Phase.RESOLVED);
+ Element e = handle.resolve(copy);
+ if (e != null) {
+ copy.rewrite(copy.getCompilationUnit(),
GeneratorUtilities.get(copy).addImports(copy.getCompilationUnit(),
Collections.singleton(e)));
+ }
+ });
+ return textEdit != null ? Collections.singletonList(textEdit)
: null;
+ } catch (IOException ex) {
+ throw new RuntimeException(ex);
+ }
+ };
+ }
+
+ public static boolean isOfKind(Element e, EnumSet<ElementKind> kinds) {
+ if (kinds.contains(e.getKind())) {
+ return true;
+ }
+ for (Element ee : e.getEnclosedElements()) {
+ if (isOfKind(ee, kinds)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public static boolean isInDefaultPackage(Element e) {
+ while (e != null && e.getKind() != ElementKind.PACKAGE) {
+ e = e.getEnclosingElement();
+ }
+ return e != null && e.getSimpleName().length() == 0;
+ }
+
+ private static TextEdit modify2TextEdit(JavaSource js, Task<WorkingCopy>
task) throws IOException {
Review Comment:
On the other hand, the replicated piece of code is short and simple. If we
decide to create a common utility, it should be placed somewhere without
changing module dependencies.
--
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