use of org.eclipse.jface.text.ITextSelection in project xtext-eclipse by eclipse.
the class ExtractVariableHandler method execute.
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
try {
syncUtil.totalSync(false);
final XtextEditor editor = EditorUtils.getActiveXtextEditor(event);
if (editor != null) {
final ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
final IXtextDocument document = editor.getDocument();
XtextResource resource = document.priorityReadOnly(new IUnitOfWork<XtextResource, XtextResource>() {
@Override
public XtextResource exec(XtextResource state) throws Exception {
return resourceCopier.loadIntoNewResourceSet(state);
}
});
XExpression expression = expressionUtil.findSelectedExpression(resource, selection);
if (expression != null) {
ExtractVariableRefactoring introduceVariableRefactoring = refactoringProvider.get();
if (introduceVariableRefactoring.initialize(editor, expression)) {
ITextRegion region = locationInFileProvider.getFullTextRegion(expression);
editor.selectAndReveal(region.getOffset(), region.getLength());
ExtractVariableWizard wizard = new ExtractVariableWizard(introduceVariableRefactoring);
RefactoringWizardOpenOperation_NonForking openOperation = new RefactoringWizardOpenOperation_NonForking(wizard);
openOperation.run(editor.getSite().getShell(), "Extract Local Variable");
}
}
}
} catch (InterruptedException e) {
return null;
} catch (Exception exc) {
LOG.error("Error during refactoring", exc);
MessageDialog.openError(Display.getCurrent().getActiveShell(), "Error during refactoring", exc.getMessage() + "\nSee log for details");
}
return null;
}
use of org.eclipse.jface.text.ITextSelection in project xtext-eclipse by eclipse.
the class JavaElementDelegate method getJavaElementForXtextEditor.
protected IJavaElement getJavaElementForXtextEditor(IEditorPart editor) {
if (!(editor instanceof XtextEditor))
return null;
IJavaProject jp = null;
if (editor.getEditorInput() instanceof IFileEditorInput) {
IFile file = ((IFileEditorInput) editor.getEditorInput()).getFile();
if (!fileExtensionProvider.isValid(file.getFileExtension())) {
return null;
}
jp = JavaCore.create(file.getProject());
if (jp == null || !jp.exists() || !jp.isOpen())
return null;
} else {
return null;
}
XtextEditor xtextEditor = (XtextEditor) editor;
ISelection selection = xtextEditor.getSelectionProvider().getSelection();
if (!(selection instanceof ITextSelection))
return null;
final int offset = ((ITextSelection) selection).getOffset();
IJavaElement func = xtextEditor.getDocument().readOnly(new IUnitOfWork<IJavaElement, XtextResource>() {
@Override
public IJavaElement exec(XtextResource state) throws Exception {
return findJavaElement(state, offset);
}
});
return func;
}
use of org.eclipse.jface.text.ITextSelection in project xtext-eclipse by eclipse.
the class AbstractCursorHandlingTest method assertState.
protected void assertState(String string, XtextEditor editor) {
int cursor = string.indexOf('|');
assertEquals(string.replace("|", ""), editor.getDocument().get());
ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
assertEquals("unexpected cursor position:", cursor, selection.getOffset());
}
use of org.eclipse.jface.text.ITextSelection in project xtext-eclipse by eclipse.
the class AbstractAutoEditTest method selectText.
protected void selectText(XtextEditor editor, int relativeToCurrentOffset, int length) throws Exception {
ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
editor.getInternalSourceViewer().setSelectedRange(selection.getOffset() + relativeToCurrentOffset, length);
}
use of org.eclipse.jface.text.ITextSelection in project xtext-eclipse by eclipse.
the class TextViewerDeleteLineAction method run.
/*
* @see IAction#run()
*/
@Override
public void run() {
if (fTarget == null)
return;
ITextViewer viewer = getTextViewer();
if (viewer == null)
return;
if (!canModifyViewer())
return;
IDocument document = viewer.getDocument();
if (document == null)
return;
ITextSelection selection = getSelection(viewer);
if (selection == null)
return;
try {
fTarget.deleteLine(document, selection, fType, fCopyToClipboard);
} catch (BadLocationException e) {
// should not happen
}
}
Aggregations