Search in sources :

Example 31 with ITextSelection

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

the class AddMarkerAction method getInitialAttributes.

/**
 * Returns the attributes the new marker will be initialized with.
 * <p>
 * Subclasses may extend or replace this method.</p>
 *
 * @return the attributes the new marker will be initialized with
 */
protected Map<String, Object> getInitialAttributes() {
    Map<String, Object> attributes = new HashMap<>(11);
    ITextSelection selection = (ITextSelection) getTextEditor().getSelectionProvider().getSelection();
    if (!selection.isEmpty()) {
        int start = selection.getOffset();
        int length = selection.getLength();
        if (length < 0) {
            length = -length;
            start -= length;
        }
        MarkerUtilities.setCharStart(attributes, start);
        MarkerUtilities.setCharEnd(attributes, start + length);
        // marker line numbers are 1-based
        int line = selection.getStartLine();
        MarkerUtilities.setLineNumber(attributes, line == -1 ? -1 : line + 1);
        IDocument document = getTextEditor().getDocumentProvider().getDocument(getTextEditor().getEditorInput());
        MarkerUtilities.setMessage(attributes, getLabelProposal(document, start, length));
    }
    return attributes;
}
Also used : HashMap(java.util.HashMap) ITextSelection(org.eclipse.jface.text.ITextSelection) IDocument(org.eclipse.jface.text.IDocument)

Example 32 with ITextSelection

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

the class JoinLinesAction method run.

@Override
public void run() {
    ITextEditor editor = getTextEditor();
    if (editor == null)
        return;
    if (!validateEditorInputState())
        return;
    IDocument document = getDocument(editor);
    if (document == null)
        return;
    ITextSelection selection = getSelection(editor);
    if (selection == null)
        return;
    int startLine = selection.getStartLine();
    int endLine = selection.getEndLine();
    try {
        int caretOffset = joinLines(document, startLine, endLine);
        if (caretOffset > -1)
            editor.selectAndReveal(caretOffset, 0);
    } catch (BadLocationException e) {
    // should not happen
    }
}
Also used : IDocument(org.eclipse.jface.text.IDocument) ITextSelection(org.eclipse.jface.text.ITextSelection) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 33 with ITextSelection

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

the class JoinLinesAction method getSelection.

/**
 * Returns the editor's selection.
 *
 * @param editor the editor
 * @return the editor's selection
 */
private static ITextSelection getSelection(ITextEditor editor) {
    ISelectionProvider selectionProvider = editor.getSelectionProvider();
    if (selectionProvider == null)
        return null;
    ISelection selection = selectionProvider.getSelection();
    if (!(selection instanceof ITextSelection))
        return null;
    return (ITextSelection) selection;
}
Also used : ISelectionProvider(org.eclipse.jface.viewers.ISelectionProvider) ISelection(org.eclipse.jface.viewers.ISelection) ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 34 with ITextSelection

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

the class TextSelectionNavigationLocation method update.

/**
 * Updates the this location.
 */
@Override
public void update() {
    IEditorPart part = getEditorPart();
    if (part instanceof ITextEditor) {
        ITextEditor textEditor = (ITextEditor) getEditorPart();
        if (equalsLocationOf(textEditor))
            return;
        ISelection s = textEditor.getSelectionProvider().getSelection();
        if (s == null || s.isEmpty())
            return;
        ITextSelection selection = (ITextSelection) s;
        if (selection.getOffset() == 0 && selection.getLength() == 0)
            return;
        fPosition.offset = selection.getOffset();
        fPosition.length = selection.getLength();
        fPosition.isDeleted = false;
        if (!part.isDirty())
            fSavedPosition = new Position(fPosition.offset, fPosition.length);
    }
}
Also used : Position(org.eclipse.jface.text.Position) ISelection(org.eclipse.jface.viewers.ISelection) IEditorPart(org.eclipse.ui.IEditorPart) ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 35 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