Search in sources :

Example 96 with IErlElement

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

the class AbstractErlContentAssistProcessor method computeCompletionProposals.

@Override
public ICompletionProposal[] computeCompletionProposals(final ITextViewer viewer, final int offset) {
    final String id = Integer.toHexString(viewer.hashCode()) + "@" + offset;
    try {
        ErlideEventTracer.getInstance().traceOperationStart("completion", id);
        try {
            final IDocument doc = viewer.getDocument();
            final String before = getBefore(viewer, doc, offset);
            String elementBefore;
            final IErlElement el = getElementAt(offset);
            if (el instanceof ISourceReference) {
                final ISourceRange r = ((ISourceReference) el).getSourceRange();
                final int o = r.getOffset();
                elementBefore = doc.get(o, offset - o);
            } else {
                elementBefore = null;
            }
            if (restarted && offset > 0) {
                final char last = doc.get(offset - 1, 1).charAt(0);
                if (last == ',' || last == '.' || last == ';' || last == ')' || last == '(') {
                    return null;
                }
            }
            if (Objects.equal(oldDoc, doc) && oldBefore != null && before.startsWith(oldBefore) && oldSuggestions == 0) {
                return getNoCompletion(offset);
            }
            oldDoc = doc;
            oldBefore = before;
            final CompletionService completionService = ErlangEngine.getInstance().getCompletionService();
            List<ICompletionProposal> result = Lists.newArrayList();
            if (project != null) {
                final IOtpRpc backend = BackendCore.getBuildBackend(project);
                final List<CompletionData> resultData = completionService.computeCompletions(backend, project, module, elementBefore, offset, before, isInString());
                result = Lists.transform(resultData, new Function<CompletionData, ICompletionProposal>() {

                    @Override
                    public ICompletionProposal apply(final CompletionData data) {
                        return toProposal(data);
                    }
                });
            }
            final ErlTemplateCompletionProcessor t = new ErlTemplateCompletionProcessor(doc, offset - before.length(), before.length());
            result.addAll(Arrays.asList(t.computeCompletionProposals(viewer, offset)));
            oldSuggestions = result.size();
            if (result.isEmpty()) {
                ErlLogger.debug("no results");
                return getNoCompletion(offset);
            }
            // ErlLogger.debug("%d results", result.size());
            return result.toArray(new ICompletionProposal[result.size()]);
        } catch (final Exception e) {
            ErlLogger.warn(e);
            return null;
        }
    } finally {
        ErlideEventTracer.getInstance().traceOperationEnd("completion", id);
    }
}
Also used : ErlTemplateCompletionProcessor(org.erlide.ui.templates.ErlTemplateCompletionProcessor) ErlModelException(org.erlide.engine.model.ErlModelException) BadLocationException(org.eclipse.jface.text.BadLocationException) IOtpRpc(org.erlide.runtime.rpc.IOtpRpc) IErlElement(org.erlide.engine.model.IErlElement) CompletionData(org.erlide.engine.services.codeassist.CompletionData) FunctionCompletionData(org.erlide.engine.services.codeassist.FunctionCompletionData) Function(com.google.common.base.Function) CompletionService(org.erlide.engine.services.codeassist.CompletionService) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ISourceReference(org.erlide.engine.model.erlang.ISourceReference) IDocument(org.eclipse.jface.text.IDocument) ISourceRange(org.erlide.engine.model.erlang.ISourceRange)

Example 97 with IErlElement

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

the class AbstractErlContentAssistProcessor method getBefore.

String getBefore(final ITextViewer viewer, final IDocument doc, final int offset) {
    try {
        if (module != null) {
            try {
                final IErlElement element = module.getElementAt(offset);
                if (element instanceof ISourceReference) {
                    final ISourceReference sr = (ISourceReference) element;
                    final int start = sr.getSourceRange().getOffset();
                    if (start <= offset) {
                        return doc.get(start, offset - start);
                    }
                }
            } catch (final ErlModelException e) {
            }
        }
        for (int n = offset - 1; n >= 0; --n) {
            final char c = doc.getChar(n);
            final int type = Character.getType(c);
            if (type == Character.LINE_SEPARATOR || type == Character.PARAGRAPH_SEPARATOR || type == Character.CONTROL) {
                return doc.get(n + 1, offset - n - 1);
            }
        }
        return doc.get(0, offset);
    } catch (final BadLocationException e) {
        ErlLogger.warn(e);
    }
    return "";
}
Also used : IErlElement(org.erlide.engine.model.IErlElement) ErlModelException(org.erlide.engine.model.ErlModelException) ISourceReference(org.erlide.engine.model.erlang.ISourceReference) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 98 with IErlElement

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

the class OpenAction method run.

@Override
public void run(final ITextSelection selection) {
    try {
        final IEditorPart activeEditor = getSite().getPage().getActiveEditor();
        final int offset = selection.getOffset();
        ITextEditor textEditor = null;
        OpenResult openResult = null;
        IErlElement element = null;
        IErlProject project = null;
        IErlModule module = null;
        final IErlModel model = ErlangEngine.getInstance().getModel();
        if (activeEditor instanceof AbstractErlangEditor) {
            final AbstractErlangEditor editor = (AbstractErlangEditor) activeEditor;
            textEditor = editor;
            editor.reconcileNow();
            final String scannerName = editor.getScannerName();
            module = editor.getModule();
            project = editor.getProject();
            openResult = ErlangEngine.getInstance().getOpenService().open(scannerName, offset, ErlangEngine.getInstance().getModelUtilService().getImportsAsList(module), project.getProperties().getExternalModules(), model.getPathVars());
            ErlLogger.debug("open " + openResult);
            element = editor.getElementAt(offset, true);
        } else if (activeEditor instanceof ITextEditor) {
            textEditor = (ITextEditor) activeEditor;
            final String text = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput()).get();
            openResult = ErlangEngine.getInstance().getOpenService().openText(text, offset);
            final IFile file = textEditor.getEditorInput().getAdapter(IFile.class);
            if (file != null) {
                final IProject p = file.getProject();
                if (p != null) {
                    project = model.findProject(p);
                }
            }
        }
        if (openResult != null) {
            helper.openOpenResult(textEditor, module, offset, project, openResult, element);
        }
    } catch (final Exception e) {
        ErlLogger.error(e);
    }
}
Also used : IErlProject(org.erlide.engine.model.root.IErlProject) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IFile(org.eclipse.core.resources.IFile) IErlModel(org.erlide.engine.model.root.IErlModel) IEditorPart(org.eclipse.ui.IEditorPart) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) AbstractErlangEditor(org.erlide.ui.editors.erl.AbstractErlangEditor) IErlElement(org.erlide.engine.model.IErlElement) OpenResult(org.erlide.engine.services.search.OpenResult) IErlModule(org.erlide.engine.model.root.IErlModule)

Example 99 with IErlElement

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

the class CallHierarchyAction method run.

@Override
public void run() {
    if (module == null) {
        return;
    }
    final IErlElement el = editor.getElementAt(editor.getViewer().getSelectedRange().x, false);
    IErlFunction f = null;
    if (el instanceof IErlFunction) {
        f = (IErlFunction) el;
    } else if (el instanceof IErlFunctionClause) {
        f = (IErlFunction) el.getParent();
    }
    if (f == null) {
        return;
    }
    String name = module.getName();
    final int i = name.lastIndexOf('.');
    if (i > 0) {
        name = name.substring(0, i);
    }
    final FunctionRef ref = new FunctionRef(name, f.getFunctionName(), f.getArity());
    final IWorkbenchWindow dw = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    final IWorkbenchPage page = dw.getActivePage();
    final AsyncCaller<CallHierarchyView> ac = new AsyncCaller<CallHierarchyView>(100) {

        @Override
        protected CallHierarchyView prepare() {
            try {
                final IViewPart p = page.showView("org.erlide.ui.callhierarchy");
                final CallHierarchyView cvh = p.getAdapter(CallHierarchyView.class);
                if (cvh == null) {
                    return null;
                }
                cvh.setMessage("<searching... project " + ErlangEngine.getInstance().getModelUtilService().getProject(module).getName() + ">");
                return cvh;
            } catch (final PartInitException e) {
                ErlLogger.error("could not open Call hierarchy view: ", e.getMessage());
                return null;
            }
        }

        @Override
        protected RpcFuture call() throws BackendException {
            final RpcFuture result = xrefService.addProject(ErlangEngine.getInstance().getModelUtilService().getProject(module));
            return result;
        }

        @Override
        protected void handleResult(final CallHierarchyView context, final RpcFuture result) {
            page.activate(context);
            try {
                context.setRoot(ErlangEngine.getInstance().getModel().findFunction(ref));
            } catch (final ErlModelException e) {
                ErlLogger.error(e);
            }
        }
    };
    ac.run();
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IViewPart(org.eclipse.ui.IViewPart) IErlFunction(org.erlide.engine.model.erlang.IErlFunction) IErlFunctionClause(org.erlide.engine.model.erlang.IErlFunctionClause) IErlElement(org.erlide.engine.model.IErlElement) CallHierarchyView(org.erlide.ui.views.CallHierarchyView) ErlModelException(org.erlide.engine.model.ErlModelException) AsyncCaller(org.erlide.ui.jinterface.AsyncCaller) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) RpcFuture(org.erlide.runtime.rpc.RpcFuture) PartInitException(org.eclipse.ui.PartInitException) FunctionRef(org.erlide.engine.model.erlang.FunctionRef)

Example 100 with IErlElement

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

the class ModelUtilsTest method findExternalTypeTest.

@Test
public void findExternalTypeTest() throws Exception {
    // given
    // an Erlang module with typedef
    final IErlModule moduleB = ErlideTestUtils.createModule(ModelUtilsTest.projects[0], "bx.erl", "-module(bx).\n-type concat_thing() :: atom() | integer() | float() | string().\n");
    // final IErlModule moduleC =
    // ErlideTestUtils.createErlModule(projects[1],
    // "c.erl", "-module(c).\n-type cc() :: b:concat_thing().\n");
    final ScannerService scanner = moduleB.getScanner();
    try {
        moduleB.open(null);
        ModelUtilsTest.projects[0].open(null);
        // moduleC.open(null);
        // when
        // looking for it
        // within project
        final IErlElementLocator model = ErlangEngine.getInstance().getModel();
        final IErlElement element1 = modelFindService.findTypeDef(model, ModelUtilsTest.projects[0], moduleB, "bx", "concat_thing", moduleB.getResource().getLocation().toPortableString(), IErlElementLocator.Scope.PROJECT_ONLY);
        // in other project but path given
        final IErlElement element2 = modelFindService.findTypeDef(model, ModelUtilsTest.projects[1], moduleB, "bx", "concat_thing", moduleB.getResource().getLocation().toPortableString(), IErlElementLocator.Scope.PROJECT_ONLY);
        // in other project no path given, search all projects true
        final IErlElement element3 = modelFindService.findTypeDef(model, ModelUtilsTest.projects[1], moduleB, "bx", "concat_thing", null, IErlElementLocator.Scope.ALL_PROJECTS);
        // in other project no path given, search all projects false, ->
        // null
        final IErlElement element4 = modelFindService.findTypeDef(model, ModelUtilsTest.projects[1], moduleB, "bx", "concat_thing", null, IErlElementLocator.Scope.PROJECT_ONLY);
        // then
        // it should be returned if found
        assertTrue(element1 instanceof IErlTypespec);
        assertNull(element2);
        assertTrue(element3 instanceof IErlTypespec);
        assertNull(element4);
    } finally {
        scanner.dispose();
    }
}
Also used : IErlElement(org.erlide.engine.model.IErlElement) IErlTypespec(org.erlide.engine.model.erlang.IErlTypespec) IErlModule(org.erlide.engine.model.root.IErlModule) IErlElementLocator(org.erlide.engine.model.root.IErlElementLocator) ScannerService(org.erlide.engine.services.parsing.ScannerService) Test(org.junit.Test)

Aggregations

IErlElement (org.erlide.engine.model.IErlElement)123 Test (org.junit.Test)36 IErlModule (org.erlide.engine.model.root.IErlModule)35 ErlModelException (org.erlide.engine.model.ErlModelException)28 IParent (org.erlide.engine.model.IParent)17 IFile (org.eclipse.core.resources.IFile)16 ArrayList (java.util.ArrayList)12 OtpErlangString (com.ericsson.otp.erlang.OtpErlangString)11 IResource (org.eclipse.core.resources.IResource)11 IErlProject (org.erlide.engine.model.root.IErlProject)10 IOpenable (org.erlide.engine.model.root.IOpenable)10 IProject (org.eclipse.core.resources.IProject)9 OtpErlangObject (com.ericsson.otp.erlang.OtpErlangObject)8 IErlModel (org.erlide.engine.model.root.IErlModel)8 WranglerException (org.erlide.wrangler.refactoring.exception.WranglerException)8 OtpErlangRangeException (com.ericsson.otp.erlang.OtpErlangRangeException)7 IFolder (org.eclipse.core.resources.IFolder)7 CoreException (org.eclipse.core.runtime.CoreException)7 OtpErlangAtom (com.ericsson.otp.erlang.OtpErlangAtom)6 OtpErlangTuple (com.ericsson.otp.erlang.OtpErlangTuple)6