Search in sources :

Example 16 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project xtext-eclipse by eclipse.

the class TextViewerJoinLinesAction method run.

@Override
public void run() {
    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;
    int startLine = selection.getStartLine();
    int endLine = selection.getEndLine();
    try {
        int caretOffset = joinLines(document, startLine, endLine);
        if (caretOffset > -1) {
            StyledText widget = viewer.getTextWidget();
            widget.setRedraw(false);
            adjustHighlightRange(viewer, caretOffset, 0);
            viewer.revealRange(caretOffset, 0);
            viewer.setSelectedRange(caretOffset, 0);
            widget.setRedraw(true);
        }
    } catch (BadLocationException e) {
    // should not happen
    }
}
Also used : StyledText(org.eclipse.swt.custom.StyledText) IDocument(org.eclipse.jface.text.IDocument) ITextSelection(org.eclipse.jface.text.ITextSelection) BadLocationException(org.eclipse.jface.text.BadLocationException) ITextViewer(org.eclipse.jface.text.ITextViewer)

Example 17 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project xtext-eclipse by eclipse.

the class TextViewerMoveLinesAction method runWithEvent.

@Override
public void runWithEvent(Event event) {
    ITextViewer viewer = getTextViewer();
    if (viewer == null)
        return;
    if (!canModifyViewer())
        return;
    // get involved objects
    IDocument document = viewer.getDocument();
    if (document == null)
        return;
    StyledText widget = viewer.getTextWidget();
    if (widget == null)
        return;
    // get selection
    ITextSelection sel = (ITextSelection) viewer.getSelectionProvider().getSelection();
    if (sel.isEmpty())
        return;
    ITextSelection skippedLine = getSkippedLine(document, sel);
    if (skippedLine == null)
        return;
    try {
        ITextSelection movingArea = getMovingSelection(document, sel, viewer);
        // visible area, bail out
        if (!containedByVisibleRegion(movingArea, viewer) || !containedByVisibleRegion(skippedLine, viewer))
            return;
        // get the content to be moved around: the moving (selected) area and the skipped line
        String moving = movingArea.getText();
        String skipped = skippedLine.getText();
        if (moving == null || skipped == null || document.getLength() == 0)
            return;
        String delim;
        String insertion;
        int offset, deviation;
        if (fUpwards) {
            delim = document.getLineDelimiter(skippedLine.getEndLine());
            if (fCopy) {
                delim = TextUtilities.getDefaultLineDelimiter(document);
                insertion = moving + delim;
                offset = movingArea.getOffset();
                deviation = 0;
            } else {
                Assert.isNotNull(delim);
                insertion = moving + delim + skipped;
                offset = skippedLine.getOffset();
                deviation = -skippedLine.getLength() - delim.length();
            }
        } else {
            delim = document.getLineDelimiter(movingArea.getEndLine());
            if (fCopy) {
                if (delim == null) {
                    delim = TextUtilities.getDefaultLineDelimiter(document);
                    insertion = delim + moving;
                } else {
                    insertion = moving + delim;
                }
                offset = skippedLine.getOffset();
                deviation = movingArea.getLength() + delim.length();
            } else {
                Assert.isNotNull(delim);
                insertion = skipped + delim + moving;
                offset = movingArea.getOffset();
                deviation = skipped.length() + delim.length();
            }
        }
        // modify the document
        beginCompoundEdit();
        if (fCopy) {
            document.replace(offset, 0, insertion);
        } else {
            document.replace(offset, insertion.length(), insertion);
        }
        // move the selection along
        int selOffset = movingArea.getOffset() + deviation;
        int selLength = movingArea.getLength() + (fAddDelimiter ? delim.length() : 0);
        if (!(viewer instanceof ITextViewerExtension5))
            selLength = Math.min(selLength, viewer.getVisibleRegion().getOffset() + viewer.getVisibleRegion().getLength() - selOffset);
        else {
        // TODO need to check what is necessary in the projection case
        }
        selectAndReveal(viewer, selOffset, selLength);
    } catch (BadLocationException x) {
        // won't happen without concurrent modification - bail out
        return;
    }
}
Also used : StyledText(org.eclipse.swt.custom.StyledText) ITextViewerExtension5(org.eclipse.jface.text.ITextViewerExtension5) IDocument(org.eclipse.jface.text.IDocument) ITextSelection(org.eclipse.jface.text.ITextSelection) BadLocationException(org.eclipse.jface.text.BadLocationException) ITextViewer(org.eclipse.jface.text.ITextViewer)

Example 18 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project xtext-eclipse by eclipse.

the class EditorCopyQualifiedNameHandler method getQualifiedName.

@Override
protected String getQualifiedName(ExecutionEvent event) {
    XtextEditor activeXtextEditor = EditorUtils.getActiveXtextEditor(event);
    if (activeXtextEditor == null) {
        return null;
    }
    final ITextSelection selection = getTextSelection(activeXtextEditor);
    return activeXtextEditor.getDocument().priorityReadOnly(new IUnitOfWork<String, XtextResource>() {

        @Override
        public String exec(XtextResource xTextResource) throws Exception {
            EObject context = getContext(selection, xTextResource);
            EObject selectedElement = getSelectedName(selection, xTextResource);
            return getQualifiedName(selectedElement, context);
        }
    });
}
Also used : XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) EObject(org.eclipse.emf.ecore.EObject) XtextResource(org.eclipse.xtext.resource.XtextResource) ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 19 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 20 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project eclipse.platform.text by eclipse.

the class QuickDiffRestoreAction method getSelection.

/**
 * Returns the selection of the editor this action belongs to.
 *
 * @return the editor's selection, or <code>null</code>
 */
protected ITextSelection getSelection() {
    if (getTextEditor() == null)
        return null;
    ISelectionProvider sp = getTextEditor().getSelectionProvider();
    if (sp == null)
        return null;
    ISelection s = sp.getSelection();
    if (s instanceof ITextSelection)
        return (ITextSelection) s;
    return null;
}
Also used : ISelectionProvider(org.eclipse.jface.viewers.ISelectionProvider) ISelection(org.eclipse.jface.viewers.ISelection) ITextSelection(org.eclipse.jface.text.ITextSelection)

Aggregations

ITextSelection (org.eclipse.jface.text.ITextSelection)200 ISelection (org.eclipse.jface.viewers.ISelection)64 IDocument (org.eclipse.jface.text.IDocument)50 BadLocationException (org.eclipse.jface.text.BadLocationException)45 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)34 IRegion (org.eclipse.jface.text.IRegion)33 Region (org.eclipse.jface.text.Region)29 IEditorPart (org.eclipse.ui.IEditorPart)29 ISelectionProvider (org.eclipse.jface.viewers.ISelectionProvider)26 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)20 ArrayList (java.util.ArrayList)18 TextSelection (org.eclipse.jface.text.TextSelection)16 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)14 IResource (org.eclipse.core.resources.IResource)12 IFile (org.eclipse.core.resources.IFile)11 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)11 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)11 Template (org.eclipse.jface.text.templates.Template)10 TemplateContext (org.eclipse.jface.text.templates.TemplateContext)10 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)10