use of org.eclipse.jface.text.ITextSelection in project erlide_eclipse by erlang.
the class ErlangAbstractHandler method getTextSelection.
/**
* Provide the text selection that is needed to execute the command. Default
* implementation, extend to Erlang elements selected.
*
* @param document
* text {@link IDocument}
* @param selection
* selection affected by command (extended by extendSelection)
* @return new {@link ITextSelection} with all text up to selection
*/
protected ITextSelection getTextSelection(final IDocument document, final ITextSelection selection, final ITextEditor editor) {
if (editor instanceof ErlangEditor) {
final AbstractErlangEditor erlangEditor = (AbstractErlangEditor) editor;
final IErlModule module = erlangEditor.getModule();
if (module != null) {
final int offset1 = selection.getOffset();
final int offset2 = offset1 + selection.getLength();
try {
final IErlElement e1 = module.getElementAt(offset1);
final IErlElement e2 = module.getElementAt(offset2);
if (e1 instanceof ISourceReference) {
final ISourceReference ref1 = (ISourceReference) e1;
final ISourceRange r1 = ref1.getSourceRange();
final int offset = r1.getOffset();
int length = r1.getLength();
if (e1.equals(e2)) {
final int docLength = document.getLength();
if (offset + length > docLength) {
length = docLength - offset;
}
return ErlangAbstractHandler.extendSelectionToWholeLines(document, new TextSelection(document, offset, length));
} else if (e2 == null) {
return ErlangAbstractHandler.extendSelectionToWholeLines(document, new TextSelection(document, offset, selection.getLength() + selection.getOffset() - offset));
} else if (e2 instanceof ISourceReference) {
final ISourceReference ref2 = (ISourceReference) e2;
final ISourceRange r2 = ref2.getSourceRange();
return ErlangAbstractHandler.extendSelectionToWholeLines(document, new TextSelection(document, offset, r2.getOffset() - offset + r2.getLength()));
}
}
} catch (final ErlModelException e) {
}
}
}
return ErlangAbstractHandler.extendSelectionToWholeLines(document, selection);
}
use of org.eclipse.jface.text.ITextSelection in project erlide_eclipse by erlang.
the class ErlangAbstractHandler method execute.
@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
final ITextEditor textEditor = (ITextEditor) HandlerUtil.getActiveEditor(event);
if (!validateEditorInputState(textEditor)) {
return null;
}
final ISelection sel = textEditor.getSelectionProvider().getSelection();
if (sel == null || sel.isEmpty() || !(sel instanceof ITextSelection)) {
return null;
}
ErlideEventTracer.getInstance().traceOperationStart(this);
try {
final IRunnableWithProgress myRunnableWithProgress = monitor0 -> {
final IProgressMonitor monitor = monitor0 != null ? monitor0 : new NullProgressMonitor();
try {
monitor.beginTask("Processing " + textEditor.getEditorInput().getName(), IProgressMonitor.UNKNOWN);
doAction(sel, textEditor);
} finally {
monitor.done();
}
};
try {
PlatformUI.getWorkbench().getProgressService().busyCursorWhile(myRunnableWithProgress);
} catch (final InvocationTargetException e) {
} catch (final InterruptedException e) {
}
} finally {
ErlideEventTracer.getInstance().traceOperationEnd(this);
}
return null;
}
use of org.eclipse.jface.text.ITextSelection in project erlide_eclipse by erlang.
the class FindAction method run.
/*
* Method declared on SelectionChangedAction.
*/
@Override
public void run(final ITextSelection selection) {
try {
final IErlModule module = fEditor.getModule();
if (module == null) {
return;
}
final ISelection sel = getSelection();
final ITextSelection textSel = (ITextSelection) sel;
final int offset = textSel.getOffset();
final OpenResult res = ErlangEngine.getInstance().getOpenService().open(module.getScannerName(), offset, ErlangEngine.getInstance().getModelUtilService().getImportsAsList(module), "", ErlangEngine.getInstance().getModel().getPathVars());
ErlLogger.debug("find " + res);
final ErlangSearchPattern ref = SearchUtil.getSearchPatternFromOpenResultAndLimitTo(module, offset, res, getLimitTo(), true);
if (ref != null) {
SearchUtil.runQuery(ref, getScope(), getScopeDescription(), getShell());
}
} catch (final Exception e) {
handleException(e);
}
}
use of org.eclipse.jface.text.ITextSelection in project erlide_eclipse by erlang.
the class ErlideSelection method getSelectedText.
/**
* @return the Selected text
*/
public String getSelectedText() {
final ITextSelection txtSel = getTextSelection();
final int start = txtSel.getOffset();
final int len = txtSel.getLength();
try {
return doc.get(start, len);
} catch (final BadLocationException e) {
throw new RuntimeException(e);
}
}
use of org.eclipse.jface.text.ITextSelection in project erlide_eclipse by erlang.
the class ErlangSearchPage method initSelections.
private void initSelections() throws ErlModelException {
final ISelection sel = getContainer().getSelection();
SearchPatternData initData = null;
if (sel instanceof IStructuredSelection) {
initData = tryStructuredSelection((IStructuredSelection) sel);
} else if (sel instanceof ITextSelection) {
final IEditorPart activePart = getActiveEditor();
if (activePart instanceof ErlangEditor) {
initData = tryErlangTextSelection(initData, activePart);
}
if (initData == null) {
initData = trySimpleTextSelection((ITextSelection) sel);
}
}
if (initData == null) {
initData = getDefaultInitValues();
}
fInitialData = initData;
fPattern.setText(initData.getPattern());
setSearchFor(initData.getSearchFor());
}
Aggregations