Search in sources :

Example 11 with ITextViewer

use of org.eclipse.jface.text.ITextViewer in project egit by eclipse.

the class SpellcheckableMessageArea method configureContextMenu.

private void configureContextMenu() {
    final boolean editable = isEditable(sourceViewer);
    TextViewerAction cutAction;
    TextViewerAction undoAction;
    TextViewerAction redoAction;
    TextViewerAction pasteAction;
    IAction quickFixAction;
    if (editable) {
        cutAction = createFromActionFactory(ActionFactory.CUT, ITextOperationTarget.CUT);
        undoAction = createFromActionFactory(ActionFactory.UNDO, ITextOperationTarget.UNDO);
        redoAction = createFromActionFactory(ActionFactory.REDO, ITextOperationTarget.REDO);
        pasteAction = createFromActionFactory(ActionFactory.PASTE, ITextOperationTarget.PASTE);
        quickFixAction = new QuickfixAction(sourceViewer);
    } else {
        cutAction = null;
        undoAction = null;
        redoAction = null;
        pasteAction = null;
        quickFixAction = null;
    }
    TextViewerAction copyAction = createFromActionFactory(ActionFactory.COPY, ITextOperationTarget.COPY);
    TextViewerAction selectAllAction = createFromActionFactory(ActionFactory.SELECT_ALL, ITextOperationTarget.SELECT_ALL);
    final TextEditorPropertyAction showWhitespaceAction = new TextEditorPropertyAction(UIText.SpellcheckableMessageArea_showWhitespace, sourceViewer, AbstractTextEditor.PREFERENCE_SHOW_WHITESPACE_CHARACTERS) {

        private IPainter whitespaceCharPainter;

        @Override
        public void propertyChange(PropertyChangeEvent event) {
            String property = event.getProperty();
            if (property.equals(getPreferenceKey()) || AbstractTextEditor.PREFERENCE_SHOW_LEADING_SPACES.equals(property) || AbstractTextEditor.PREFERENCE_SHOW_ENCLOSED_SPACES.equals(property) || AbstractTextEditor.PREFERENCE_SHOW_TRAILING_SPACES.equals(property) || AbstractTextEditor.PREFERENCE_SHOW_LEADING_IDEOGRAPHIC_SPACES.equals(property) || AbstractTextEditor.PREFERENCE_SHOW_ENCLOSED_IDEOGRAPHIC_SPACES.equals(property) || AbstractTextEditor.PREFERENCE_SHOW_TRAILING_IDEOGRAPHIC_SPACES.equals(property) || AbstractTextEditor.PREFERENCE_SHOW_LEADING_TABS.equals(property) || AbstractTextEditor.PREFERENCE_SHOW_ENCLOSED_TABS.equals(property) || AbstractTextEditor.PREFERENCE_SHOW_TRAILING_TABS.equals(property) || AbstractTextEditor.PREFERENCE_SHOW_CARRIAGE_RETURN.equals(property) || AbstractTextEditor.PREFERENCE_SHOW_LINE_FEED.equals(property) || AbstractTextEditor.PREFERENCE_WHITESPACE_CHARACTER_ALPHA_VALUE.equals(property)) {
                synchronizeWithPreference();
            }
        }

        @Override
        protected void toggleState(boolean checked) {
            if (checked)
                installPainter();
            else
                uninstallPainter();
        }

        /**
         * Installs the painter on the viewer.
         */
        private void installPainter() {
            Assert.isTrue(whitespaceCharPainter == null);
            ITextViewer v = getTextViewer();
            if (v instanceof ITextViewerExtension2) {
                IPreferenceStore store = getStore();
                whitespaceCharPainter = new WhitespaceCharacterPainter(v, store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_LEADING_SPACES), store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_ENCLOSED_SPACES), store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_TRAILING_SPACES), store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_LEADING_IDEOGRAPHIC_SPACES), store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_ENCLOSED_IDEOGRAPHIC_SPACES), store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_TRAILING_IDEOGRAPHIC_SPACES), store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_LEADING_TABS), store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_ENCLOSED_TABS), store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_TRAILING_TABS), store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_CARRIAGE_RETURN), store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_LINE_FEED), store.getInt(AbstractTextEditor.PREFERENCE_WHITESPACE_CHARACTER_ALPHA_VALUE));
                ((ITextViewerExtension2) v).addPainter(whitespaceCharPainter);
            }
        }

        /**
         * Remove the painter from the viewer.
         */
        private void uninstallPainter() {
            if (whitespaceCharPainter == null)
                return;
            ITextViewer v = getTextViewer();
            if (v instanceof ITextViewerExtension2)
                ((ITextViewerExtension2) v).removePainter(whitespaceCharPainter);
            whitespaceCharPainter.deactivate(true);
            whitespaceCharPainter = null;
        }
    };
    MenuManager contextMenu = new MenuManager();
    if (cutAction != null) {
        contextMenu.add(cutAction);
    }
    contextMenu.add(copyAction);
    if (pasteAction != null) {
        contextMenu.add(pasteAction);
    }
    contextMenu.add(selectAllAction);
    if (undoAction != null) {
        contextMenu.add(undoAction);
    }
    if (redoAction != null) {
        contextMenu.add(redoAction);
    }
    contextMenu.add(new Separator());
    contextMenu.add(showWhitespaceAction);
    contextMenu.add(new Separator());
    if (editable) {
        final SubMenuManager quickFixMenu = new SubMenuManager(contextMenu);
        quickFixMenu.setVisible(true);
        quickFixMenu.addMenuListener(new IMenuListener() {

            @Override
            public void menuAboutToShow(IMenuManager manager) {
                quickFixMenu.removeAll();
                addProposals(quickFixMenu);
            }
        });
    }
    final StyledText textWidget = getTextWidget();
    List<IAction> globalActions = new ArrayList<>();
    if (editable) {
        globalActions.add(cutAction);
        globalActions.add(pasteAction);
        globalActions.add(undoAction);
        globalActions.add(redoAction);
        globalActions.add(quickFixAction);
    }
    globalActions.add(copyAction);
    globalActions.add(selectAllAction);
    if (contentAssistAction != null) {
        globalActions.add(contentAssistAction);
    }
    ActionUtils.setGlobalActions(textWidget, globalActions, getHandlerService());
    textWidget.setMenu(contextMenu.createContextMenu(textWidget));
    sourceViewer.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            if (cutAction != null)
                cutAction.update();
            copyAction.update();
        }
    });
    if (editable) {
        sourceViewer.addTextListener(new ITextListener() {

            @Override
            public void textChanged(TextEvent event) {
                if (undoAction != null)
                    undoAction.update();
                if (redoAction != null)
                    redoAction.update();
            }
        });
    }
    textWidget.addDisposeListener(new DisposeListener() {

        @Override
        public void widgetDisposed(DisposeEvent disposeEvent) {
            showWhitespaceAction.dispose();
        }
    });
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) ITextListener(org.eclipse.jface.text.ITextListener) ArrayList(java.util.ArrayList) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) DisposeEvent(org.eclipse.swt.events.DisposeEvent) WhitespaceCharacterPainter(org.eclipse.jface.text.WhitespaceCharacterPainter) TextEvent(org.eclipse.jface.text.TextEvent) PropertyChangeEvent(org.eclipse.jface.util.PropertyChangeEvent) ITextViewerExtension2(org.eclipse.jface.text.ITextViewerExtension2) StyledText(org.eclipse.swt.custom.StyledText) IAction(org.eclipse.jface.action.IAction) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) IPainter(org.eclipse.jface.text.IPainter) IMenuListener(org.eclipse.jface.action.IMenuListener) ITextViewer(org.eclipse.jface.text.ITextViewer) SubMenuManager(org.eclipse.jface.action.SubMenuManager) SubMenuManager(org.eclipse.jface.action.SubMenuManager) MenuManager(org.eclipse.jface.action.MenuManager) IMenuManager(org.eclipse.jface.action.IMenuManager) IMenuManager(org.eclipse.jface.action.IMenuManager) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) Separator(org.eclipse.jface.action.Separator)

Example 12 with ITextViewer

use of org.eclipse.jface.text.ITextViewer in project egit by eclipse.

the class LogicalLineNumberRulerColumn method getLogicalLineNumberProvider.

/**
 * Retrieves the {@link ILogicalLineNumberProvider} to use. This
 * implementation returns a {@link LogicalLineNumberProvider}.
 *
 * @return the {@link ILogicalLineNumberProvider}
 */
protected ILogicalLineNumberProvider getLogicalLineNumberProvider() {
    if (logicalLineNumberProvider == null) {
        ITextViewer viewer = getParentRuler().getTextViewer();
        Assert.isNotNull(viewer);
        logicalLineNumberProvider = new LogicalLineNumberProvider(getSide(), viewer);
    }
    return logicalLineNumberProvider;
}
Also used : ITextViewer(org.eclipse.jface.text.ITextViewer)

Example 13 with ITextViewer

use of org.eclipse.jface.text.ITextViewer in project webtools.sourceediting by eclipse.

the class AbstractJSONCompletionProposalComputer method computeCompletionProposals.

@Override
public List computeCompletionProposals(CompletionProposalInvocationContext context, IProgressMonitor monitor) {
    ITextViewer textViewer = context.getViewer();
    int documentPosition = context.getInvocationOffset();
    setErrorMessage(null);
    fTextViewer = textViewer;
    IndexedRegion treeNode = ContentAssistUtils.getNodeAt(textViewer, documentPosition <= 0 ? 0 : documentPosition - 1);
    IJSONNode node = (IJSONNode) treeNode;
    while ((node != null) && (node.getNodeType() == Node.TEXT_NODE) && (node.getParentNode() != null)) {
        node = node.getParentNode();
    }
    ContentAssistRequest contentAssistRequest = null;
    IStructuredDocumentRegion sdRegion = getStructuredDocumentRegion(documentPosition);
    ITextRegion completionRegion = getCompletionRegion(documentPosition, node);
    // Fix completion region in case of JSON_OBJECT_CLOSE
    if (completionRegion != null && completionRegion.getType() == JSONRegionContexts.JSON_OBJECT_CLOSE && documentPosition > 0) {
        completionRegion = getCompletionRegion(documentPosition, node);
    }
    String matchString = EMPTY;
    if (completionRegion != null) {
        if (isPairValue(context, node)) {
            try {
                String nodeText = getNodeText(node);
                int colonIndex = nodeText.indexOf(COLON);
                int offset = documentPosition - node.getStartOffset();
                if (colonIndex >= 0 && offset >= 0) {
                    String str = nodeText.substring(colonIndex + 1, offset);
                    // $NON-NLS-1$
                    str = str.replaceAll(",", BLANK);
                    matchString = str;
                }
            } catch (BadLocationException e) {
            // ignore
            }
        } else {
            matchString = getMatchString(sdRegion, completionRegion, documentPosition);
        }
    }
    // compute normal proposals
    contentAssistRequest = computeCompletionProposals(matchString, completionRegion, (IJSONNode) treeNode, node != null ? node.getParentNode() : null, context);
    if (contentAssistRequest == null) {
        contentAssistRequest = new ContentAssistRequest((IJSONNode) treeNode, node != null ? node.getParentNode() : null, sdRegion, completionRegion, documentPosition, 0, EMPTY);
        setErrorMessage(JSONUIMessages.Content_Assist_not_availab_UI_);
    }
    /*
		 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=123892 Only set this
		 * error message if nothing else was already set
		 */
    if (contentAssistRequest.getProposals().size() == 0 && getErrorMessage() == null) {
        setErrorMessage(JSONUIMessages.Content_Assist_not_availab_UI_);
    }
    ICompletionProposal[] props = contentAssistRequest.getCompletionProposals();
    return (props != null) ? Arrays.asList(props) : new ArrayList(0);
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ArrayList(java.util.ArrayList) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode) BadLocationException(org.eclipse.jface.text.BadLocationException) ITextViewer(org.eclipse.jface.text.ITextViewer)

Example 14 with ITextViewer

use of org.eclipse.jface.text.ITextViewer in project webtools.sourceediting by eclipse.

the class DocumentRegionProcessor method getFoldingStrategy.

/**
 * <p>Get the folding strategy for this processor. Retrieved from the
 * extended configuration builder.  The processor chosen is set by the plugin.</p>
 *
 * <p>EX:<br />
 * <code>&lt;extension point="org.eclipse.wst.sse.ui.editorConfiguration"&gt;<br />
 *  &lt;provisionalConfiguration<br />
 *			type="foldingstrategy"<br />
 *			class="org.eclipse.wst.xml.ui.internal.projection.XMLFoldingStrategy"<br />
 *			target="org.eclipse.core.runtime.xml, org.eclipse.wst.xml.core.xmlsource" /&gt;<br />
 *	&lt;/extension&gt;</code></p>
 *
 * <p>The type must be equal to <code>AbstractFoldingStrategy.ID</code> (AKA: foldingstrategy)
 * and the class must extend <code>org.eclipse.wst.sse.ui.internal.projection.AbstractFoldingStrategy</code>
 * and the target must be a structured editor content type ID</p>
 *
 * @return the requested folding strategy or null if none can be found
 */
protected IReconcilingStrategy getFoldingStrategy() {
    if (fFoldingStrategy == null && getDocument() != null) {
        String contentTypeId = getContentType(getDocument());
        if (contentTypeId == null) {
            contentTypeId = IContentTypeManager.CT_TEXT;
        }
        ITextViewer viewer = getTextViewer();
        if (viewer instanceof ProjectionViewer) {
            ExtendedConfigurationBuilder builder = ExtendedConfigurationBuilder.getInstance();
            IContentType type = Platform.getContentTypeManager().getContentType(contentTypeId);
            while (fFoldingStrategy == null && type != null) {
                fFoldingStrategy = (AbstractStructuredFoldingStrategy) builder.getConfiguration(AbstractStructuredFoldingStrategy.ID, type.getId());
                type = type.getBaseType();
            }
            if (fFoldingStrategy != null) {
                fFoldingStrategy.setViewer((ProjectionViewer) viewer);
                fFoldingStrategy.setDocument(getDocument());
            }
        }
    }
    return fFoldingStrategy;
}
Also used : IContentType(org.eclipse.core.runtime.content.IContentType) ProjectionViewer(org.eclipse.jface.text.source.projection.ProjectionViewer) ITextViewer(org.eclipse.jface.text.ITextViewer) ExtendedConfigurationBuilder(org.eclipse.wst.sse.ui.internal.ExtendedConfigurationBuilder)

Example 15 with ITextViewer

use of org.eclipse.jface.text.ITextViewer in project webtools.sourceediting by eclipse.

the class JSPELCompletionProposalComputer method computeCompletionProposals.

/**
 * @see org.eclipse.jst.jsp.ui.internal.contentassist.JSPJavaCompletionProposalComputer#computeCompletionProposals(org.eclipse.wst.sse.ui.contentassist.CompletionProposalInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
 */
public List computeCompletionProposals(CompletionProposalInvocationContext context, IProgressMonitor monitor) {
    ITextViewer viewer = context.getViewer();
    int documentPosition = context.getInvocationOffset();
    // get results from JSP completion processor
    // 3 for the "get" at the beginning of the java proposal
    List results = new ArrayList(computeJavaCompletionProposals(viewer, documentPosition, 3));
    // get the function proposals for syntax like: ${ fn:| }
    IStructuredDocumentRegion flat = ContentAssistUtils.getStructuredDocumentRegion(viewer, documentPosition);
    if (flat != null) {
        ITextRegion cursorRegion = flat.getRegionAtCharacterOffset(documentPosition);
        String elText;
        int startOffset;
        // else can use flat region
        if (cursorRegion instanceof ITextRegionContainer) {
            ITextRegionContainer container = (ITextRegionContainer) cursorRegion;
            cursorRegion = container.getRegionAtCharacterOffset(documentPosition);
            elText = container.getText(cursorRegion);
            startOffset = container.getStartOffset(cursorRegion);
        } else {
            elText = flat.getText(cursorRegion);
            startOffset = flat.getStartOffset(cursorRegion);
        }
        // sanity check that we are actually in EL region
        if (cursorRegion.getType() == DOMJSPRegionContexts.JSP_EL_CONTENT) {
            String prefix = getPrefix(documentPosition - startOffset, elText);
            if (null != prefix) {
                List proposals = getFunctionProposals(prefix, viewer, documentPosition);
                results.addAll(proposals);
            }
        }
    }
    return results;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ITextRegionContainer(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer) ITextViewer(org.eclipse.jface.text.ITextViewer)

Aggregations

ITextViewer (org.eclipse.jface.text.ITextViewer)75 BadLocationException (org.eclipse.jface.text.BadLocationException)13 IDocument (org.eclipse.jface.text.IDocument)13 Region (org.eclipse.jface.text.Region)13 StyledText (org.eclipse.swt.custom.StyledText)13 IRegion (org.eclipse.jface.text.IRegion)11 ArrayList (java.util.ArrayList)9 Point (org.eclipse.swt.graphics.Point)9 Test (org.junit.Test)9 ITextSelection (org.eclipse.jface.text.ITextSelection)8 IEditorPart (org.eclipse.ui.IEditorPart)8 ITextViewerExtension5 (org.eclipse.jface.text.ITextViewerExtension5)6 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)6 Document (org.eclipse.jface.text.Document)5 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)5 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)5 Display (org.eclipse.swt.widgets.Display)5 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)5 List (java.util.List)4 ITextViewerExtension (org.eclipse.jface.text.ITextViewerExtension)4