Search in sources :

Example 16 with IErlProject

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

the class ErlProjectTest method findFunctionInExternalFilesTest.

@Test
public void findFunctionInExternalFilesTest() throws Exception {
    // given
    // a module with calls to the lists module
    final IErlProject project = ErlProjectTest.projects[0];
    final IErlModule moduleE = ErlideTestUtils.createModule(project, "e.erl", "-module(e).\n-export([f/0]).\nf() ->\n    lists:reverse([1, 0]),\n    lists:reverse([1, 0], [2]).\n");
    final ScannerService scanner = moduleE.getScanner();
    try {
        moduleE.open(null);
        // when
        // looking for lists:reverse/2 and lists:reverse/1
        final IErlModel model = ErlangEngine.getInstance().getModel();
        final OpenResult res = ErlangEngine.getInstance().getOpenService().open(moduleE.getScannerName(), 49, ErlangEngine.getInstance().getModelUtilService().getImportsAsList(moduleE), project.getProperties().getExternalModules(), model.getPathVars());
        final IErlFunction function = ErlangEngine.getInstance().getModelFindService().findFunction(model, project, moduleE, res.getName(), res.getPath(), res.getFunction(), IErlElementLocator.Scope.PROJECT_ONLY);
        assertNotNull(function);
        final IErlElement module = model.findModuleFromProject(project, function.getModuleName(), res.getPath(), IErlElementLocator.Scope.PROJECT_ONLY);
        // then
        // the function should be returned and the module, in External Files
        assertNotNull(module);
        assertEquals(function.getParent(), module);
        assertEquals(ErlangEngine.getInstance().getModelUtilService().getProject(function), project);
    } finally {
        scanner.dispose();
    }
}
Also used : IErlProject(org.erlide.engine.model.root.IErlProject) IErlElement(org.erlide.engine.model.IErlElement) IErlModel(org.erlide.engine.model.root.IErlModel) OpenResult(org.erlide.engine.services.search.OpenResult) IErlModule(org.erlide.engine.model.root.IErlModule) ScannerService(org.erlide.engine.services.parsing.ScannerService) Test(org.junit.Test)

Example 17 with IErlProject

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

the class ErlProjectTest method findIncludeFile.

@Test
public void findIncludeFile() throws Exception {
    // given
    // a project with a module and an include including file.hrl
    final IErlProject project = ErlProjectTest.projects[0];
    final String includeName = "a.hrl";
    final IErlModule include = ErlideTestUtils.createModule(project, includeName, "-include_lib(\"kernel/include/file.hrl\").\n-record(rec1, {field, another=def}).\n-define(MACRO(A), lists:reverse(A)).\n");
    include.open(null);
    final IErlModule module = ErlideTestUtils.createModule(project, "f.erl", "-module(f).\n-include(\"a.hrl\").\n-export([f/0]).\n-record(rec2, {a, b}).\n" + "f() ->\n    lists:reverse([1, 0]),\n    lists:reverse([1, 0], [2]).\n");
    module.open(null);
    final IErlElementLocator model = ErlangEngine.getInstance().getModel();
    // when
    // looking for the include
    final IErlModule include1 = model.findIncludeFromModule(module, includeName, null, IErlElementLocator.Scope.PROJECT_ONLY);
    final IErlModule include2 = model.findIncludeFromProject(project, "file.hrl", null, IErlElementLocator.Scope.PROJECT_ONLY);
    // then
    // it should be found
    assertEquals(include, include1);
    assertNotNull(include2);
}
Also used : IErlProject(org.erlide.engine.model.root.IErlProject) IErlModule(org.erlide.engine.model.root.IErlModule) IErlElementLocator(org.erlide.engine.model.root.IErlElementLocator) Test(org.junit.Test)

Example 18 with IErlProject

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

the class ErlProjectTest method findExternalModule.

@Test
public void findExternalModule() throws Exception {
    File externalFile = null;
    IErlProject project = null;
    try {
        // given
        // an erlang project with an external file
        project = ErlideTestUtils.createErlProject("testproject");
        final String externalFileName = "external5.erl";
        externalFile = ErlideTestUtils.createTmpFile(externalFileName, "-module(external5).\nf([_ | _]=L ->\n    atom_to_list(L).\n");
        final String absolutePath = externalFile.getAbsolutePath();
        final String externalsFileName = "x.erlidex";
        final File externalsFile = ErlideTestUtils.createTmpFile(externalsFileName, absolutePath);
        ((ErlProject) project).setExternalModulesFile(externalsFile.getAbsolutePath());
        project.open(null);
        final IErlElementLocator model = ErlangEngine.getInstance().getModel();
        // when
        // looking for it
        final IErlModule externalModule = model.findModuleFromProject(project, externalFileName, null, IErlElementLocator.Scope.PROJECT_ONLY);
        // then
        // we should find it
        assertThat(externalModule).isNotNull();
        assertThat(FilePathUtils.equalFilePaths(absolutePath, externalModule.getFilePath(), EFS.getLocalFileSystem().isCaseSensitive())).isTrue();
    } finally {
        if (externalFile != null && externalFile.exists()) {
            externalFile.delete();
        }
        if (project != null) {
            ErlideTestUtils.deleteProject(project);
        }
    }
}
Also used : IErlProject(org.erlide.engine.model.root.IErlProject) ErlProject(org.erlide.engine.internal.model.root.ErlProject) IErlProject(org.erlide.engine.model.root.IErlProject) IErlModule(org.erlide.engine.model.root.IErlModule) IErlElementLocator(org.erlide.engine.model.root.IErlElementLocator) File(java.io.File) Test(org.junit.Test)

Example 19 with IErlProject

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

the class ErlTextHover method internalGetHoverInfo.

private static ErlangBrowserInformationControlInput internalGetHoverInfo(final AbstractErlangEditor editor, final ITextViewer textViewer, final IRegion hoverRegion) {
    if (editor == null) {
        return null;
    }
    final StringBuffer result = new StringBuffer();
    OpenResult element = null;
    // TODO our model is too coarse, here we need access to expressions
    final Collection<OtpErlangObject> fImports = ErlangEngine.getInstance().getModelUtilService().getImportsAsList(editor.getModule());
    final int offset = hoverRegion.getOffset();
    final int length = hoverRegion.getLength();
    final String debuggerVar = ErlTextHover.makeDebuggerVariableHover(textViewer, offset, length);
    if (!debuggerVar.isEmpty()) {
        result.append(debuggerVar);
    }
    final IErlProject erlProject = editor.getProject();
    String docPath = "";
    String anchor = "";
    try {
        if (erlProject == null) {
            return null;
        }
        final IOtpRpc backend = BackendCore.getBuildBackend(erlProject);
        if (backend == null) {
            return null;
        }
        final IErlModel model = ErlangEngine.getInstance().getModel();
        final String externalModulesString = erlProject.getProperties().getExternalModules();
        final OtpErlangTuple t = (OtpErlangTuple) ErlangEngine.getInstance().getOtpDocService().getOtpDoc(backend, offset, editor.getScannerName(), fImports, externalModulesString, model.getPathVars());
        if (Util.isOk(t)) {
            element = new OpenResult(t.elementAt(2));
            final String docStr = Util.stringValue(t.elementAt(1));
            result.append(docStr);
            if (t.arity() > 4) {
                docPath = Util.stringValue(t.elementAt(3));
                anchor = Util.stringValue(t.elementAt(4));
            }
        } else {
            element = new OpenResult(t);
            final Object found = new OpenUtils().findOpenResult(editor, editor.getModule(), erlProject, element, editor.getElementAt(offset, false));
            if (found instanceof IErlFunction) {
                final IErlFunction function = (IErlFunction) found;
                final String comment = DocumentationFormatter.getDocumentationString(function.getComments(), function.getTypespec());
                if (comment.isEmpty()) {
                    return null;
                }
                result.append(comment);
            } else if (found instanceof IErlPreprocessorDef) {
                final IErlPreprocessorDef preprocessorDef = (IErlPreprocessorDef) found;
                result.append(preprocessorDef.getExtra());
            }
        }
    } catch (final Exception e) {
        ErlLogger.warn(e);
        return null;
    }
    final String strResult = HoverUtil.getHTML(result);
    return new ErlangBrowserInformationControlInput(null, editor, element, strResult, 20, HoverUtil.getDocumentationURL(docPath, anchor));
}
Also used : IErlProject(org.erlide.engine.model.root.IErlProject) IErlModel(org.erlide.engine.model.root.IErlModel) IErlFunction(org.erlide.engine.model.erlang.IErlFunction) IErlPreprocessorDef(org.erlide.engine.model.erlang.IErlPreprocessorDef) Point(org.eclipse.swt.graphics.Point) DebugException(org.eclipse.debug.core.DebugException) BadLocationException(org.eclipse.jface.text.BadLocationException) IOtpRpc(org.erlide.runtime.rpc.IOtpRpc) ErlangBrowserInformationControlInput(org.erlide.ui.internal.information.ErlangBrowserInformationControlInput) OpenResult(org.erlide.engine.services.search.OpenResult) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) OpenUtils(org.erlide.ui.actions.OpenUtils) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) OtpErlangTuple(com.ericsson.otp.erlang.OtpErlangTuple)

Example 20 with IErlProject

use of org.erlide.engine.model.root.IErlProject 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)

Aggregations

IErlProject (org.erlide.engine.model.root.IErlProject)123 IErlModule (org.erlide.engine.model.root.IErlModule)57 Test (org.junit.Test)47 IPath (org.eclipse.core.runtime.IPath)30 IProject (org.eclipse.core.resources.IProject)29 ErlProject (org.erlide.engine.internal.model.root.ErlProject)25 File (java.io.File)19 Path (org.eclipse.core.runtime.Path)16 CoreException (org.eclipse.core.runtime.CoreException)13 ErlModelException (org.erlide.engine.model.ErlModelException)13 IErlElementLocator (org.erlide.engine.model.root.IErlElementLocator)13 IErlModel (org.erlide.engine.model.root.IErlModel)13 ArrayList (java.util.ArrayList)11 IErlElement (org.erlide.engine.model.IErlElement)10 IFile (org.eclipse.core.resources.IFile)9 IResource (org.eclipse.core.resources.IResource)9 HashSet (java.util.HashSet)7 IProjectDescription (org.eclipse.core.resources.IProjectDescription)7 OtpErlangString (com.ericsson.otp.erlang.OtpErlangString)6 IErlPreprocessorDef (org.erlide.engine.model.erlang.IErlPreprocessorDef)6