private Iterable bodyCompletion()

in boot/src/main/java/org/netbeans/html/boot/impl/JavaScriptProcesor.java [259:359]


    private Iterable<? extends Completion> bodyCompletion(Element e, String userText) {
        ExecutableElement ee = (ExecutableElement) e;
        String identifier = endsWithIdentifier(userText);
        int preIdentifierAt = userText.length() - identifier.length() - 1;
        if (preIdentifierAt >= 0) {
            char preId = userText.charAt(preIdentifierAt);
            if (preId == '.' || preId == '@') {
                int lastAt = userText.lastIndexOf('@', preIdentifierAt + 1);
                String maybePackageName = preIdentifierAt > lastAt ? userText.substring(lastAt + 1, preIdentifierAt) : "";
                if (!maybePackageName.isEmpty()) {
                    PackageElement maybePackage = processingEnv.getElementUtils().getPackageElement(maybePackageName);
                    if (maybePackage != null) {
                        List<Completion> offer = new ArrayList<>();
                        for (Element ch : maybePackage.getEnclosedElements()) {
                            final String elemName = ch.getSimpleName().toString();
                            if (elemName.startsWith(identifier)) {
                                final String userCompl = userText.substring(0, lastAt + 1) + maybePackageName + "." + elemName + "::"; // NOI18N
                                offer.add(Completions.of(userCompl));
                            }
                        }
                        return offer;
                    }
                } else {
                    String instanceName = null;
                    if (lastAt > 0 && userText.charAt(lastAt - 1) == '.') {
                        instanceName = endsWithIdentifier(userText, lastAt - 1);
                        if (instanceName.isEmpty()) {
                            instanceName = null;
                        }
                    }
                    if (instanceName != null) {
                        for (VariableElement p : ee.getParameters()) {
                            if (p.getSimpleName().contentEquals(instanceName)) {
                                Element et = processingEnv.getTypeUtils().asElement(p.asType());
                                if (et.getKind().isClass() || et.getKind().isInterface()) {
                                    String fqn = processingEnv.getElementUtils().getBinaryName((TypeElement) et).toString();
                                    String type = userText.substring(0, lastAt + 1) + fqn + "::";
                                    return Collections.nCopies(1, Completions.of(type));
                                }
                            }
                        }
                    }
                }
            } else if (preId == ':') {
                int lastAt = userText.lastIndexOf('@', preIdentifierAt + 1);
                String maybeClassName = userText.substring(lastAt + 1, preIdentifierAt - 1);
                TypeElement maybeClass = findClass(maybeClassName);
                String instanceName = null;
                if (lastAt > 0 && userText.charAt(lastAt - 1) == '.') {
                    instanceName = endsWithIdentifier(userText, lastAt - 1);
                    if (instanceName.isEmpty()) {
                        instanceName = null;
                    }
                }
                if (maybeClass != null) {
                    List<Completion> offer = new ArrayList<>();
                    for (Element ch : maybeClass.getEnclosedElements()) {
                        if (ch.getKind() != ElementKind.METHOD) {
                            continue;
                        }
                        final String elemName = ch.getSimpleName().toString();
                        if (elemName.startsWith(identifier)) {
                            ExecutableElement m = (ExecutableElement) ch;
                            boolean isStatic = m.getModifiers().contains(Modifier.STATIC);
                            if (instanceName != null) {
                                if (isStatic) {
                                    continue;
                                }
                            } else {
                                if (!isStatic) {
                                    continue;
                                }
                            }

                            StringBuilder invokeMethod = new StringBuilder().
                                    append(userText.substring(0, lastAt + 1)).
                                    append(maybeClassName).
                                    append("::").
                                    append(elemName).
                                    append(findParamTypes(m)).
                                    append("(");
                            String sep = "";
                            for (VariableElement p : m.getParameters()) {
                                invokeMethod.append(sep);
                                if (p.asType().getKind().isPrimitive()) {
                                    invokeMethod.append("0");
                                } else {
                                    invokeMethod.append("null");
                                }
                                sep = ", ";
                            }
                            invokeMethod.append(")");
                            offer.add(Completions.of(invokeMethod.toString()));
                        }
                    }
                    return offer;
                }
            }
        }
        return Collections.emptyList();
    }