Search in sources :

Example 1 with IErlImport

use of org.erlide.engine.model.erlang.IErlImport 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 IErlImport

use of org.erlide.engine.model.erlang.IErlImport 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 IErlImport

use of org.erlide.engine.model.erlang.IErlImport in project erlide_eclipse by erlang.

the class OpenUtils method findLocalCall.

private IErlElement findLocalCall(final IErlModule module, final IErlProject erlProject, final OpenResult res, final IErlElement element, final IErlElementLocator.Scope scope) throws RpcException, CoreException {
    if (isTypeDefOrRecordDef(element, res)) {
        return modelFindService.findTypespec(module, res.getFun());
    }
    final IErlFunction foundElement = module.findFunction(res.getFunction());
    if (foundElement != null) {
        return foundElement;
    }
    // imported functions
    OtpErlangObject res2 = null;
    String moduleName = null;
    final IErlImport ei = module.findImport(res.getFunction());
    if (ei != null) {
        final IErlModel model = ErlangEngine.getInstance().getModel();
        moduleName = ei.getImportModule();
        res2 = ErlangEngine.getInstance().getOpenService().getSourceFromModule(model.getPathVars(), moduleName, erlProject.getProperties().getExternalModules());
    }
    if (res2 instanceof OtpErlangString && moduleName != null) {
        // imported from otp module
        final OtpErlangString otpErlangString = (OtpErlangString) res2;
        final String modulePath = otpErlangString.stringValue();
        final IErlElementLocator model = ErlangEngine.getInstance().getModel();
        return modelFindService.findFunction(model, erlProject, module, moduleName, modulePath, res.getFunction(), scope);
    }
    // functions defined in include files
    final Collection<IErlModule> allIncludedFiles = ErlangEngine.getInstance().getModelFindService().findAllIncludedFiles(module);
    for (final IErlModule includedModule : allIncludedFiles) {
        final IErlFunction function = includedModule.findFunction(res.getFunction());
        if (function != null) {
            return function;
        }
    }
    return null;
}
Also used : IErlModel(org.erlide.engine.model.root.IErlModel) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) IErlFunction(org.erlide.engine.model.erlang.IErlFunction) IErlModule(org.erlide.engine.model.root.IErlModule) IErlElementLocator(org.erlide.engine.model.root.IErlElementLocator) OtpErlangString(com.ericsson.otp.erlang.OtpErlangString) IErlImport(org.erlide.engine.model.erlang.IErlImport) OtpErlangString(com.ericsson.otp.erlang.OtpErlangString)

Example 4 with IErlImport

use of org.erlide.engine.model.erlang.IErlImport in project erlide_eclipse by erlang.

the class ModelUtilsTest method getImportsAsListTest.

@Test
public void getImportsAsListTest() throws Exception {
    // given
    // an Erlang module with imports
    final IErlModule moduleA = ErlideTestUtils.createModule(ModelUtilsTest.projects[0], "ax.erl", "-module(ax).\n-import(lists, [reverse/1, foldl/3].\n");
    moduleA.open(null);
    // when
    // fetching imports as list of OtpErlangTuple
    final Collection<IErlElement> children = moduleA.getChildren();
    final Collection<IErlImport> imports2 = moduleA.getImports();
    final List<OtpErlangObject> imports = modelUtilService.getImportsAsList(moduleA);
    // then
    // they should be returned
    assertEquals(2, children.size());
    assertEquals(1, imports2.size());
    assertEquals(1, imports.size());
    final OtpErlangAtom listAtom = new OtpErlangAtom("lists");
    assertEquals(new OtpErlangTuple(new OtpErlangObject[] { listAtom, new OtpErlangList(new OtpErlangObject[] { makeTuple2("reverse", 1), makeTuple2("foldl", 3) }) }), imports.get(0));
}
Also used : IErlElement(org.erlide.engine.model.IErlElement) OtpErlangList(com.ericsson.otp.erlang.OtpErlangList) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) IErlModule(org.erlide.engine.model.root.IErlModule) IErlImport(org.erlide.engine.model.erlang.IErlImport) OtpErlangAtom(com.ericsson.otp.erlang.OtpErlangAtom) OtpErlangTuple(com.ericsson.otp.erlang.OtpErlangTuple) Test(org.junit.Test)

Example 5 with IErlImport

use of org.erlide.engine.model.erlang.IErlImport in project erlide_eclipse by erlang.

the class ErlModule method getImports.

@Override
public Collection<IErlImport> getImports() {
    final List<IErlImport> result = new ArrayList<>();
    synchronized (getModelLock()) {
        for (final IErlElement e : internalGetChildren()) {
            if (e instanceof IErlImport) {
                final IErlImport ei = (IErlImport) e;
                result.add(ei);
            }
        }
    }
    return result;
}
Also used : IErlElement(org.erlide.engine.model.IErlElement) ArrayList(java.util.ArrayList) IErlImport(org.erlide.engine.model.erlang.IErlImport)

Aggregations

IErlImport (org.erlide.engine.model.erlang.IErlImport)7 OtpErlangObject (com.ericsson.otp.erlang.OtpErlangObject)4 OtpErlangAtom (com.ericsson.otp.erlang.OtpErlangAtom)3 OtpErlangList (com.ericsson.otp.erlang.OtpErlangList)3 OtpErlangTuple (com.ericsson.otp.erlang.OtpErlangTuple)3 ArrayList (java.util.ArrayList)3 IErlElement (org.erlide.engine.model.IErlElement)3 OtpErlangString (com.ericsson.otp.erlang.OtpErlangString)2 IErlModule (org.erlide.engine.model.root.IErlModule)2 CompletionData (org.erlide.engine.services.codeassist.CompletionData)2 FunctionCompletionData (org.erlide.engine.services.codeassist.FunctionCompletionData)2 ErlImport (org.erlide.engine.internal.model.erlang.ErlImport)1 ErlangFunction (org.erlide.engine.model.erlang.ErlangFunction)1 IErlFunction (org.erlide.engine.model.erlang.IErlFunction)1 IErlElementLocator (org.erlide.engine.model.root.IErlElementLocator)1 IErlModel (org.erlide.engine.model.root.IErlModel)1 IErlProject (org.erlide.engine.model.root.IErlProject)1 Test (org.junit.Test)1