Search in sources :

Example 96 with ITextSelection

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;
}
Also used : ISimpleNameProvider(org.eclipse.xtext.ui.refactoring2.rename.ISimpleNameProvider) INode(org.eclipse.xtext.nodemodel.INode) IRenameStrategy(org.eclipse.xtext.ui.refactoring.IRenameStrategy) TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) IRenameStrategy2(org.eclipse.xtext.ide.refactoring.IRenameStrategy2) ITextRegion(org.eclipse.xtext.util.ITextRegion) EObject(org.eclipse.emf.ecore.EObject) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) ITextSelection(org.eclipse.jface.text.ITextSelection) NoSuchStrategyException(org.eclipse.xtext.ui.refactoring.IRenameStrategy.Provider.NoSuchStrategyException)

Example 97 with ITextSelection

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;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) XtextResource(org.eclipse.xtext.resource.XtextResource) ITextSelection(org.eclipse.jface.text.ITextSelection) ExecutionException(org.eclipse.core.commands.ExecutionException)

Example 98 with ITextSelection

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;
}
Also used : ITextSelection(org.eclipse.jface.text.ITextSelection) TextSelection(org.eclipse.jface.text.TextSelection) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 99 with ITextSelection

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;
}
Also used : XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) XtextResource(org.eclipse.xtext.resource.XtextResource) ITextSelection(org.eclipse.jface.text.ITextSelection) ExecutionException(org.eclipse.core.commands.ExecutionException) RefactoringWizardOpenOperation_NonForking(org.eclipse.xtext.ui.refactoring.ui.RefactoringWizardOpenOperation_NonForking) ITextRegion(org.eclipse.xtext.util.ITextRegion) XExpression(org.eclipse.xtext.xbase.XExpression) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 100 with ITextSelection

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);
        }
    }
}
Also used : ISelection(org.eclipse.jface.viewers.ISelection) ITextSelection(org.eclipse.jface.text.ITextSelection) IFigure(org.eclipse.draw2d.IFigure)

Aggregations

ITextSelection (org.eclipse.jface.text.ITextSelection)205 ISelection (org.eclipse.jface.viewers.ISelection)65 IDocument (org.eclipse.jface.text.IDocument)52 BadLocationException (org.eclipse.jface.text.BadLocationException)46 IRegion (org.eclipse.jface.text.IRegion)34 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)34 Region (org.eclipse.jface.text.Region)31 IEditorPart (org.eclipse.ui.IEditorPart)29 ISelectionProvider (org.eclipse.jface.viewers.ISelectionProvider)25 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)21 ArrayList (java.util.ArrayList)18 TextSelection (org.eclipse.jface.text.TextSelection)17 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)14 Point (org.eclipse.swt.graphics.Point)13 IResource (org.eclipse.core.resources.IResource)12 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)12 IFile (org.eclipse.core.resources.IFile)11 StyledText (org.eclipse.swt.custom.StyledText)11 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)11 Template (org.eclipse.jface.text.templates.Template)10