use of org.erlide.engine.model.erlang.ISourceRange in project erlide_eclipse by erlang.
the class ErlangEditor method setSelection.
public void setSelection(final ISourceReference reference, final boolean moveCursor) {
if (getSelectionProvider() == null) {
return;
}
final ISelection selection = getSelectionProvider().getSelection();
if (selection instanceof TextSelection) {
final TextSelection textSelection = (TextSelection) selection;
if (moveCursor && (textSelection.getOffset() != 0 || textSelection.getLength() != 0)) {
markInNavigationHistory();
}
}
if (reference != null) {
StyledText textWidget = null;
final ISourceViewer sourceViewer = getSourceViewer();
if (sourceViewer == null) {
return;
}
textWidget = sourceViewer.getTextWidget();
if (textWidget == null) {
return;
}
try {
ISourceRange range = null;
range = reference.getSourceRange();
if (range == null) {
return;
}
int offset = range.getOffset();
int length = range.getLength();
if (offset < 0 || length < 0) {
return;
}
setHighlightRange(offset, length, moveCursor);
if (!moveCursor) {
return;
}
offset = -1;
length = -1;
if (reference instanceof IErlMember) {
range = ((IErlMember) reference).getNameRange();
if (range != null) {
offset = range.getOffset();
length = range.getLength();
}
} else if (reference instanceof IErlAttribute) {
range = ((IErlAttribute) reference).getNameRange();
if (range != null) {
offset = range.getOffset();
length = range.getLength();
}
} else if (reference instanceof IErlFunctionClause) {
range = ((IErlFunctionClause) reference).getNameRange();
if (range != null) {
offset = range.getOffset();
length = range.getLength();
}
}
if (offset > -1 && length > 0) {
try {
textWidget.setRedraw(false);
sourceViewer.revealRange(offset, length);
sourceViewer.setSelectedRange(offset, length);
} finally {
textWidget.setRedraw(true);
}
markInNavigationHistory();
}
} catch (final IllegalArgumentException x) {
}
} else if (moveCursor) {
resetHighlightRange();
markInNavigationHistory();
}
}
use of org.erlide.engine.model.erlang.ISourceRange 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.erlang.ISourceRange in project erlide_eclipse by erlang.
the class OpenUtils method findOpenResult.
public Object findOpenResult(final ITextEditor editor, final IErlModule module, final IErlProject project, final OpenResult openResult, final IErlElement element) throws CoreException, ErlModelException, OtpErlangRangeException, RpcException, BadLocationException {
final IErlElementLocator.Scope scope = NavigationPreferencePage.getCheckAllProjects() ? IErlElementLocator.Scope.ALL_PROJECTS : IErlElementLocator.Scope.REFERENCED_PROJECTS;
final IErlElementLocator model = ErlangEngine.getInstance().getModel();
Object found = null;
if (openResult.isExternalCall()) {
found = findExternalCallOrType(module, openResult, project, element, scope);
} else if (openResult.isInclude()) {
found = modelFindService.findInclude(model, project, module, openResult.getName(), openResult.getPath());
} else if (openResult.isLocalCall()) {
found = findLocalCall(module, project, openResult, element, scope);
} else if (openResult.isVariable() && element instanceof ISourceReference) {
final ISourceReference sref = (ISourceReference) element;
final ISourceRange range = sref.getSourceRange();
final String elementText = editor.getDocumentProvider().getDocument(editor.getEditorInput()).get(range.getOffset(), range.getLength());
found = modelFindService.findVariable(range, openResult.getName(), elementText);
} else if (openResult.isRecord() || openResult.isMacro()) {
final ErlElementKind kind = openResult.isMacro() ? ErlElementKind.MACRO_DEF : ErlElementKind.RECORD_DEF;
found = modelFindService.findPreprocessorDef(module, openResult.getName(), kind);
} else if (openResult.isField()) {
final IErlRecordDef def = (IErlRecordDef) modelFindService.findPreprocessorDef(module, openResult.getFun(), ErlElementKind.RECORD_DEF);
if (def != null) {
found = def.getFieldNamed(openResult.getName());
}
}
return found;
}
Aggregations