use of org.eclipse.jface.text.ITextSelection in project xtext-eclipse by eclipse.
the class DefaultRenameElementHandler method isRefactoringEnabled.
protected boolean isRefactoringEnabled(IRenameElementContext renameElementContext, XtextResource resource) {
ResourceSet resourceSet = resource.getResourceSet();
if (renameElementContext != null && resourceSet != null) {
EObject targetElement = resourceSet.getEObject(renameElementContext.getTargetElementURI(), true);
if (targetElement != null && !targetElement.eIsProxy()) {
if (targetElement.eResource() == resource && renameElementContext.getTriggeringEditorSelection() instanceof ITextSelection) {
ITextSelection textSelection = (ITextSelection) renameElementContext.getTriggeringEditorSelection();
ITextRegion selectedRegion = new TextRegion(textSelection.getOffset(), textSelection.getLength());
INode crossReferenceNode = eObjectAtOffsetHelper.getCrossReferenceNode(resource, selectedRegion);
if (crossReferenceNode == null) {
// selection is on the declaration. make sure it's the name
ITextRegion significantRegion = locationInFileProvider.getSignificantTextRegion(targetElement);
if (!significantRegion.contains(selectedRegion))
return false;
}
}
IRenameStrategy.Provider renameStrategyProvider = globalServiceProvider.findService(targetElement, IRenameStrategy.Provider.class);
try {
if (renameStrategyProvider.get(targetElement, renameElementContext) != null) {
return true;
} else {
IRenameStrategy2 strategy2 = globalServiceProvider.findService(targetElement, IRenameStrategy2.class);
ISimpleNameProvider simpleNameProvider = globalServiceProvider.findService(targetElement, ISimpleNameProvider.class);
return strategy2 != null && simpleNameProvider.canRename(targetElement);
}
} catch (NoSuchStrategyException e) {
MessageDialog.openInformation(Display.getCurrent().getActiveShell(), "Cannot rename element", e.getMessage());
}
}
}
return false;
}
use of org.eclipse.jface.text.ITextSelection in project xtext-eclipse by eclipse.
the class AbstractJvmElementHandler method execute.
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
final XtextEditor editor = EditorUtils.getActiveXtextEditor(event);
if (editor != null) {
final ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
editor.getDocument().priorityReadOnly(new IUnitOfWork<Void, XtextResource>() {
@Override
public java.lang.Void exec(XtextResource resource) throws Exception {
JvmIdentifiableElement jvmIdentifiable = jvmElementAtOffsetHelper.getJvmIdentifiableElement(resource, selection.getOffset());
if (jvmIdentifiable != null) {
IJavaElement javaType = javaElementFinder.findElementFor(jvmIdentifiable);
if (javaType != null)
openPresentation(editor, javaType, jvmIdentifiable);
}
return null;
}
});
}
return null;
}
use of org.eclipse.jface.text.ITextSelection in project xtext-eclipse by eclipse.
the class ExpressionUtil method trimSelection.
protected ITextSelection trimSelection(XtextResource resource, ITextSelection selection) {
IParseResult parseResult = resource.getParseResult();
if (parseResult != null) {
String model = parseResult.getRootNode().getText();
String selectedText = model.substring(selection.getOffset(), selection.getOffset() + selection.getLength());
String trimmedSelection = selectedText.trim();
return new TextSelection(selection.getOffset() + selectedText.indexOf(trimmedSelection), trimmedSelection.length());
}
return null;
}
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 RailroadSelectionLinker method textSelectionChanged.
public void textSelectionChanged(SelectionChangedEvent event) {
ISelection selection = event.getSelection();
if (selection instanceof ITextSelection && !selection.isEmpty()) {
ITextSelection textSelection = (ITextSelection) selection;
int offset = textSelection.getOffset();
IFigure figureToBeSelected = findFigureForTextOffset(view.getContents(), offset, null);
if (figureToBeSelected != null) {
selectFigure(figureToBeSelected);
view.reveal(figureToBeSelected);
}
}
}
Aggregations