Search in sources :

Example 1 with CompletionData

use of org.erlide.engine.services.codeassist.CompletionData in project erlide_eclipse by erlang.

the class ErlangCompletionService method getImportedFunctions.

List<CompletionData> getImportedFunctions(final IOtpRpc backend, final int offset, final String prefix) {
    final List<CompletionData> result = new ArrayList<>();
    for (final IErlImport imp : module.getImports()) {
        final OtpErlangObject res = ErlangEngine.getInstance().getOtpDocService().getProposalsWithDoc(backend, imp.getImportModule(), prefix);
        addFunctionProposalsWithDoc(offset, prefix, result, res, imp, false);
    }
    return result;
}
Also used : CompletionData(org.erlide.engine.services.codeassist.CompletionData) FunctionCompletionData(org.erlide.engine.services.codeassist.FunctionCompletionData) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) ArrayList(java.util.ArrayList) IErlImport(org.erlide.engine.model.erlang.IErlImport)

Example 2 with CompletionData

use of org.erlide.engine.services.codeassist.CompletionData in project erlide_eclipse by erlang.

the class ErlangCompletionService method computeCompletions.

@Override
public List<CompletionData> computeCompletions(final IOtpRpc backend, IErlProject project0, IErlModule module0, String elementBefore0, final int offset, final String before0, final boolean inString) throws CoreException {
    // FIXME these should be passed on as parameters, where needed
    project = project0;
    module = module0;
    elementBefore = elementBefore0;
    String before = before0;
    final int commaPos = before.lastIndexOf(',');
    final int colonPos = before.lastIndexOf(':');
    final boolean doubleColon = colonPos >= 0 && before.charAt(colonPos - 1) == ':';
    final int hashMarkPos = before.lastIndexOf('#');
    final int dotPos = before.lastIndexOf('.');
    final int parenPos = before.lastIndexOf('(');
    final int leftBracketPos = before.lastIndexOf('{');
    final int interrogationMarkPos = before.lastIndexOf('?');
    final int arrowPos = before.lastIndexOf("->");
    final String prefix = getPrefix(before);
    List<String> fieldsSoFar = null;
    List<CompletionData> result;
    EnumSet<CompletionFlag> flags = EnumSet.noneOf(CompletionFlag.class);
    int pos;
    String moduleOrRecord = null;
    IErlElement element = getElementAt(offset);
    for (int i = 1; element == null && i <= 15; ++i) {
        element = getElementAt(offset - i);
    }
    RecordCompletion rc = null;
    if (hashMarkPos >= 0) {
        final IErlProject aproject = project;
        if (aproject != null) {
            rc = contextAssistService.checkRecordCompletion(backend, before);
        }
    }
    if (rc != null && rc.isNameWanted()) {
        flags = EnumSet.of(CompletionFlag.RECORD_DEFS);
        pos = hashMarkPos;
        before = rc.getPrefix();
    } else if (rc != null && rc.isFieldWanted()) {
        flags = EnumSet.of(CompletionFlag.RECORD_FIELDS);
        pos = hashMarkPos;
        if (dotPos > hashMarkPos) {
            pos = dotPos;
        } else if (leftBracketPos > hashMarkPos) {
            pos = leftBracketPos;
        } else {
            assert false;
        }
        before = rc.getPrefix();
        moduleOrRecord = rc.getName();
        fieldsSoFar = rc.getFields();
    } else if (colonPos > commaPos && colonPos > parenPos) {
        if (doubleColon) {
            flags = EnumSet.of(CompletionFlag.TYPES);
            pos = colonPos;
            before = before.substring(colonPos + 1);
        } else {
            moduleOrRecord = StringUtils.unquote(getPrefix(before.substring(0, colonPos)));
            flags = EnumSet.of(CompletionFlag.EXTERNAL_FUNCTIONS);
            pos = colonPos;
            before = before.substring(colonPos + 1);
        }
    } else if (interrogationMarkPos > hashMarkPos && interrogationMarkPos > commaPos && interrogationMarkPos > colonPos && interrogationMarkPos > arrowPos) {
        flags = EnumSet.of(CompletionFlag.MACRO_DEFS);
        pos = interrogationMarkPos;
        before = before.substring(interrogationMarkPos + 1);
    } else {
        pos = colonPos;
        before = prefix;
        if (element != null) {
            switch(element.getKind()) {
                case EXPORT:
                    flags = EnumSet.of(CompletionFlag.DECLARED_FUNCTIONS, CompletionFlag.ARITY_ONLY, CompletionFlag.UNEXPORTED_ONLY);
                    break;
                case IMPORT:
                    final IErlImport i = (IErlImport) element;
                    moduleOrRecord = i.getImportModule();
                    flags = EnumSet.of(CompletionFlag.EXTERNAL_FUNCTIONS, CompletionFlag.ARITY_ONLY);
                    break;
                case FUNCTION:
                case CLAUSE:
                    flags = EnumSet.of(CompletionFlag.MODULES);
                    if (module != null) {
                        flags.addAll(EnumSet.of(CompletionFlag.VARIABLES, CompletionFlag.DECLARED_FUNCTIONS, CompletionFlag.IMPORTED_FUNCTIONS, CompletionFlag.AUTO_IMPORTED_FUNCTIONS));
                    }
                    break;
                case ATTRIBUTE:
                    if ("include".equals(element.getName())) {
                        flags = EnumSet.of(CompletionFlag.INCLUDES);
                    } else if ("include_lib".equals(element.getName())) {
                        flags = EnumSet.of(CompletionFlag.INCLUDE_LIBS);
                    }
                    break;
                default:
                    break;
            }
        } else {
            if (doubleColon) {
                flags = EnumSet.of(CompletionFlag.TYPES);
            } else {
                flags = EnumSet.of(CompletionFlag.MODULES);
            }
        }
    }
    // TODO flags = filterFlags(flags);
    result = addCompletions(backend, flags, offset, before, moduleOrRecord, pos, fieldsSoFar, inString);
    return result;
}
Also used : IErlElement(org.erlide.engine.model.IErlElement) IErlProject(org.erlide.engine.model.root.IErlProject) CompletionData(org.erlide.engine.services.codeassist.CompletionData) FunctionCompletionData(org.erlide.engine.services.codeassist.FunctionCompletionData) OtpErlangString(com.ericsson.otp.erlang.OtpErlangString) IErlImport(org.erlide.engine.model.erlang.IErlImport)

Example 3 with CompletionData

use of org.erlide.engine.services.codeassist.CompletionData in project erlide_eclipse by erlang.

the class ErlangCompletionService method getDeclaredFunctions.

List<CompletionData> getDeclaredFunctions(final int offset, final String prefix, final boolean unexportedOnly, final boolean arityOnly) throws ErlModelException {
    final List<CompletionData> result = new ArrayList<>();
    for (final IErlElement e : module.getChildren()) {
        if (e instanceof IErlFunction) {
            final IErlFunction f = (IErlFunction) e;
            if (unexportedOnly && f.isExported()) {
                continue;
            }
            addFunctionCompletion(offset, prefix, result, f, arityOnly);
        }
    }
    return result;
}
Also used : IErlElement(org.erlide.engine.model.IErlElement) CompletionData(org.erlide.engine.services.codeassist.CompletionData) FunctionCompletionData(org.erlide.engine.services.codeassist.FunctionCompletionData) IErlFunction(org.erlide.engine.model.erlang.IErlFunction) ArrayList(java.util.ArrayList)

Example 4 with CompletionData

use of org.erlide.engine.services.codeassist.CompletionData in project erlide_eclipse by erlang.

the class ErlangCompletionService method addCompletions.

List<CompletionData> addCompletions(final IOtpRpc backend, final Set<CompletionFlag> flags, final int offset, final String prefix, final String moduleOrRecord, final int pos, final List<String> fieldsSoFar, final boolean inString) throws CoreException {
    final List<CompletionData> result = new ArrayList<>();
    final IErlProject aProject = project;
    if (aProject == null) {
        return result;
    }
    addSorted(prefix, result, addDeclaredFunctionsCompletions(flags, offset, prefix));
    if (flags.contains(CompletionFlag.VARIABLES)) {
        addSorted(prefix, result, getVariables(backend, offset, prefix));
    }
    if (flags.contains(CompletionFlag.IMPORTED_FUNCTIONS)) {
        addSorted(prefix, result, getImportedFunctions(backend, offset, prefix));
    }
    if (flags.contains(CompletionFlag.AUTO_IMPORTED_FUNCTIONS)) {
        addSorted(prefix, result, getAutoImportedFunctions(backend, offset, prefix));
    }
    if (flags.contains(CompletionFlag.MODULES)) {
        addSorted(prefix, result, getModules(backend, offset, prefix, CompletionFlag.MODULES, inString));
    }
    if (flags.contains(CompletionFlag.INCLUDES)) {
        addSorted(prefix, result, getModules(backend, offset, prefix, CompletionFlag.INCLUDES, inString));
    }
    if (flags.contains(CompletionFlag.INCLUDE_LIBS)) {
        addSorted(prefix, result, getModules(backend, offset, prefix, CompletionFlag.INCLUDE_LIBS, inString));
    }
    if (flags.contains(CompletionFlag.RECORD_DEFS)) {
        addSorted(prefix, result, getMacroOrRecordCompletions(offset, prefix, ErlElementKind.RECORD_DEF));
    }
    if (flags.contains(CompletionFlag.RECORD_FIELDS)) {
        addSorted(prefix, result, getRecordFieldCompletions(moduleOrRecord, offset, prefix, pos, fieldsSoFar));
    }
    if (flags.contains(CompletionFlag.MACRO_DEFS)) {
        addSorted(prefix, result, getMacroOrRecordCompletions(offset, prefix, ErlElementKind.MACRO_DEF));
    }
    if (flags.contains(CompletionFlag.EXTERNAL_FUNCTIONS)) {
        addSorted(prefix, result, getExternalCallCompletions(backend, moduleOrRecord, offset, prefix, flags.contains(CompletionFlag.ARITY_ONLY)));
    }
    if (flags.contains(CompletionFlag.TYPES)) {
        addSorted(prefix, result, getTypeCompletions(backend, moduleOrRecord, offset, prefix));
    }
    return result;
}
Also used : IErlProject(org.erlide.engine.model.root.IErlProject) CompletionData(org.erlide.engine.services.codeassist.CompletionData) FunctionCompletionData(org.erlide.engine.services.codeassist.FunctionCompletionData) ArrayList(java.util.ArrayList)

Example 5 with CompletionData

use of org.erlide.engine.services.codeassist.CompletionData in project erlide_eclipse by erlang.

the class ErlangCompletionService method addFunctionCompletion.

protected void addFunctionCompletion(final int offset, final List<CompletionData> result, final String funWithArity, final String docStr, final String funWithParameters, final List<Location> offsetsAndLengths) {
    int cursorPosition = funWithParameters.length();
    if (!offsetsAndLengths.isEmpty()) {
        cursorPosition = offsetsAndLengths.get(0).getOffset();
    }
    // first check if it's already there...
    for (final CompletionData c : result) {
        if (c.getDisplayString().equals(funWithArity)) {
            return;
        }
    }
    final CompletionData c = new FunctionCompletionData(offsetsAndLengths, funWithArity, funWithParameters, offset, 0, cursorPosition, docStr);
    result.add(c);
}
Also used : CompletionData(org.erlide.engine.services.codeassist.CompletionData) FunctionCompletionData(org.erlide.engine.services.codeassist.FunctionCompletionData) FunctionCompletionData(org.erlide.engine.services.codeassist.FunctionCompletionData)

Aggregations

CompletionData (org.erlide.engine.services.codeassist.CompletionData)12 FunctionCompletionData (org.erlide.engine.services.codeassist.FunctionCompletionData)12 ArrayList (java.util.ArrayList)8 OtpErlangString (com.ericsson.otp.erlang.OtpErlangString)6 IErlElement (org.erlide.engine.model.IErlElement)4 OtpErlangObject (com.ericsson.otp.erlang.OtpErlangObject)3 CoreException (org.eclipse.core.runtime.CoreException)2 ErlModelException (org.erlide.engine.model.ErlModelException)2 IErlImport (org.erlide.engine.model.erlang.IErlImport)2 IErlPreprocessorDef (org.erlide.engine.model.erlang.IErlPreprocessorDef)2 IErlProject (org.erlide.engine.model.root.IErlProject)2 Function (com.google.common.base.Function)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IDocument (org.eclipse.jface.text.IDocument)1 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)1 IErlFunction (org.erlide.engine.model.erlang.IErlFunction)1 IErlRecordDef (org.erlide.engine.model.erlang.IErlRecordDef)1 IErlRecordField (org.erlide.engine.model.erlang.IErlRecordField)1 ISourceRange (org.erlide.engine.model.erlang.ISourceRange)1 ISourceReference (org.erlide.engine.model.erlang.ISourceReference)1