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);
}
}
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 "";
}
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);
}
}
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();
}
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();
}
}
Aggregations