Search in sources :

Example 41 with ITextViewer

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

the class LineNumberColumn method createControl.

@Override
public Control createControl(CompositeRuler parentRuler, Composite parentControl) {
    Assert.isTrue(fDelegate != null);
    ITextViewer viewer = parentRuler.getTextViewer();
    Assert.isLegal(viewer instanceof ISourceViewer);
    fViewer = (ISourceViewer) viewer;
    initialize();
    Control control = fDelegate.createControl(parentRuler, parentControl);
    return control;
}
Also used : Control(org.eclipse.swt.widgets.Control) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) ITextViewer(org.eclipse.jface.text.ITextViewer)

Example 42 with ITextViewer

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

the class LinkedModeUI method leave.

void leave(final int flags) {
    if (!fIsActive)
        return;
    fIsActive = false;
    endCompoundChangeIfNeeded();
    Display display = null;
    if (fCurrentTarget.fWidget != null && !fCurrentTarget.fWidget.isDisposed())
        display = fCurrentTarget.fWidget.getDisplay();
    if (fCurrentTarget.fAnnotationModel != null)
        fCurrentTarget.fAnnotationModel.removeAllAnnotations();
    disconnect();
    for (LinkedModeUITarget fTarget : fTargets) {
        LinkedModeUITarget target = fTarget;
        ITextViewer viewer = target.getViewer();
        if (target.fKeyListener != null) {
            ((ITextViewerExtension) viewer).removeVerifyKeyListener(target.fKeyListener);
            target.fKeyListener = null;
        }
        viewer.removeTextInputListener(fCloser);
    }
    for (int i = 0; i < fTargets.length; i++) {
        if (fTargets[i].fAnnotationModel != null) {
            fTargets[i].fAnnotationModel.removeAllAnnotations();
            fTargets[i].fAnnotationModel.disconnect(fTargets[i].getViewer().getDocument());
            fTargets[i].fAnnotationModel = null;
        }
        uninstallAnnotationModel(fTargets[i]);
    }
    if ((flags & ILinkedModeListener.UPDATE_CARET) != 0 && fExitPosition != null && fFramePosition != fExitPosition && !fExitPosition.isDeleted())
        switchPosition(fExitPosition, true, false);
    final List<IDocument> docs = new ArrayList<>();
    for (LinkedModeUITarget fTarget : fTargets) {
        IDocument doc = fTarget.getViewer().getDocument();
        if (doc != null)
            docs.add(doc);
    }
    fModel.stopForwarding(flags);
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            if (fExitPosition != null)
                fExitPosition.getDocument().removePosition(fExitPosition);
            for (IDocument doc : docs) {
                doc.removePositionUpdater(fPositionUpdater);
                boolean uninstallCat = false;
                String[] cats = doc.getPositionCategories();
                for (String cat : cats) {
                    if (getCategory().equals(cat)) {
                        uninstallCat = true;
                        break;
                    }
                }
                if (uninstallCat)
                    try {
                        doc.removePositionCategory(getCategory());
                    } catch (BadPositionCategoryException e) {
                    // ignore
                    }
            }
            fModel.exit(flags);
        }
    };
    // gets correct document offsets.
    if (display != null)
        display.asyncExec(runnable);
    else
        runnable.run();
}
Also used : ArrayList(java.util.ArrayList) Point(org.eclipse.swt.graphics.Point) ITextViewer(org.eclipse.jface.text.ITextViewer) ITextViewerExtension(org.eclipse.jface.text.ITextViewerExtension) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) IDocument(org.eclipse.jface.text.IDocument) Display(org.eclipse.swt.widgets.Display)

Example 43 with ITextViewer

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

the class LinkedModeUI method connect.

private void connect() {
    Assert.isNotNull(fCurrentTarget);
    ITextViewer viewer = fCurrentTarget.getViewer();
    Assert.isNotNull(viewer);
    fCurrentTarget.fWidget = viewer.getTextWidget();
    if (fCurrentTarget.fWidget == null)
        leave(ILinkedModeListener.EXIT_ALL);
    if (fCurrentTarget.fKeyListener == null) {
        fCurrentTarget.fKeyListener = new KeyListener();
        ((ITextViewerExtension) viewer).prependVerifyKeyListener(fCurrentTarget.fKeyListener);
    } else
        fCurrentTarget.fKeyListener.setEnabled(true);
    registerAutoEditVetoer(viewer);
    ((IPostSelectionProvider) viewer).addPostSelectionChangedListener(fSelectionListener);
    createAnnotationModel();
    showSelection();
    fCurrentTarget.fShell = fCurrentTarget.fWidget.getShell();
    if (fCurrentTarget.fShell == null)
        leave(ILinkedModeListener.EXIT_ALL);
    fCurrentTarget.fShell.addShellListener(fCloser);
    fAssistant.install(viewer);
    viewer.addTextInputListener(fCloser);
    viewer.getDocument().addDocumentListener(fDocumentListener);
}
Also used : ITextViewerExtension(org.eclipse.jface.text.ITextViewerExtension) IPostSelectionProvider(org.eclipse.jface.viewers.IPostSelectionProvider) VerifyKeyListener(org.eclipse.swt.custom.VerifyKeyListener) ITextViewer(org.eclipse.jface.text.ITextViewer)

Example 44 with ITextViewer

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

the class AsyncCompletionProposalPopup method showProposals.

/**
 * This methods differs from its super as it will show the list of proposals that
 * gets augmented as the {@link IContentAssistProcessor#computeCompletionProposals(ITextViewer, int)}
 * complete. All computations operation happen in a non-UI Thread so they're not blocking UI.
 */
@Override
public String showProposals(boolean autoActivated) {
    if (fKeyListener == null)
        fKeyListener = new ProposalSelectionListener();
    final Control control = fContentAssistSubjectControlAdapter.getControl();
    if (!Helper.okToUse(fProposalShell) && control != null && !control.isDisposed()) {
        // add the listener before computing the proposals so we don't move the caret
        // when the user types fast.
        fContentAssistSubjectControlAdapter.addKeyListener(fKeyListener);
        fInvocationOffset = fContentAssistSubjectControlAdapter.getSelectedRange().x;
        fFilterOffset = fInvocationOffset;
        fLastCompletionOffset = fFilterOffset;
        // start invocation of processors as Futures, and make them populate the proposals upon completion
        List<ICompletionProposal> computedProposals = Collections.synchronizedList(new ArrayList<>());
        fFutures = buildCompletionFuturesOrJobs(fInvocationOffset);
        List<CompletableFuture<Void>> populateFutures = new ArrayList<>(fFutures.size());
        for (CompletableFuture<List<ICompletionProposal>> future : fFutures) {
            populateFutures.add(future.thenAccept(proposals -> computedProposals.addAll(proposals)));
        }
        long requestBeginningTimestamp = System.currentTimeMillis();
        long stillRemainingThreeshold = MAX_WAIT_IN_MS;
        for (CompletableFuture<?> future : populateFutures) {
            try {
                future.get(stillRemainingThreeshold, TimeUnit.MILLISECONDS);
            } catch (TimeoutException | ExecutionException | InterruptedException ex) {
            // future failed or took more time than we want to wait
            }
            stillRemainingThreeshold = MAX_WAIT_IN_MS - (System.currentTimeMillis() - requestBeginningTimestamp);
            if (stillRemainingThreeshold < 0) {
                // we already spent too much time (more than MAX_WAIT_IN_MS), stop waiting.
                break;
            }
        }
        fComputedProposals = computedProposals;
        if (stillRemainingThreeshold > 0) {
            // everything ready in time, go synchronous
            int count = computedProposals.size();
            if (count == 0 && hideWhenNoProposals(autoActivated))
                return null;
            if (count == 1 && !autoActivated && canAutoInsert(computedProposals.get(0))) {
                insertProposal(computedProposals.get(0), (char) 0, 0, fInvocationOffset);
                hide();
            } else {
                createProposalSelector();
                setProposals(computedProposals, false);
                displayProposals();
            }
        } else {
            // processors took too much time, go asynchronous
            createProposalSelector();
            ComputingProposal computingProposal = new ComputingProposal(fInvocationOffset, fFutures.size());
            computedProposals.add(0, computingProposal);
            fComputedProposals = computedProposals;
            setProposals(fComputedProposals, false);
            Set<CompletableFuture<Void>> remaining = Collections.synchronizedSet(new HashSet<>(populateFutures));
            for (CompletableFuture<Void> populateFuture : populateFutures) {
                populateFuture.thenRun(() -> {
                    remaining.removeIf(CompletableFuture::isDone);
                    computingProposal.setRemaining(remaining.size());
                    if (remaining.isEmpty()) {
                        computedProposals.remove(computingProposal);
                    }
                    List<ICompletionProposal> newProposals = new ArrayList<>(computedProposals);
                    fComputedProposals = newProposals;
                    Display.getDefault().asyncExec(() -> {
                        setProposals(newProposals, false);
                        displayProposals();
                    });
                });
            }
            displayProposals();
        }
    } else {
        fLastCompletionOffset = fFilterOffset;
        handleRepeatedInvocation();
    }
    return getErrorMessage();
}
Also used : Arrays(java.util.Arrays) NLS(org.eclipse.osgi.util.NLS) Set(java.util.Set) TimeoutException(java.util.concurrent.TimeoutException) Image(org.eclipse.swt.graphics.Image) CompletableFuture(java.util.concurrent.CompletableFuture) Display(org.eclipse.swt.widgets.Display) DocumentEvent(org.eclipse.jface.text.DocumentEvent) Point(org.eclipse.swt.graphics.Point) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Future(java.util.concurrent.Future) IDocument(org.eclipse.jface.text.IDocument) IContentAssistSubjectControl(org.eclipse.jface.contentassist.IContentAssistSubjectControl) BadLocationException(org.eclipse.jface.text.BadLocationException) ITextViewer(org.eclipse.jface.text.ITextViewer) TextUtilities(org.eclipse.jface.text.TextUtilities) Collections(java.util.Collections) Control(org.eclipse.swt.widgets.Control) ArrayList(java.util.ArrayList) Point(org.eclipse.swt.graphics.Point) IContentAssistSubjectControl(org.eclipse.jface.contentassist.IContentAssistSubjectControl) Control(org.eclipse.swt.widgets.Control) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 45 with ITextViewer

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

the class AbstractXMLCompletionProposalComputer method computeCompletionProposals.

/**
 * <p>Return a list of proposed code completions based on the specified
 * location within the document that corresponds to the current cursor
 * position within the text-editor control.</p>
 *
 * @see org.eclipse.wst.sse.ui.contentassist.ICompletionProposalComputer#computeCompletionProposals(org.eclipse.wst.sse.ui.contentassist.CompletionProposalInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
 */
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);
    Node node = (Node) treeNode;
    while ((node != null) && (node.getNodeType() == Node.TEXT_NODE) && (node.getParentNode() != null)) {
        node = node.getParentNode();
    }
    IDOMNode xmlnode = (IDOMNode) node;
    ContentAssistRequest contentAssistRequest = null;
    IStructuredDocumentRegion sdRegion = getStructuredDocumentRegion(documentPosition);
    ITextRegion completionRegion = getCompletionRegion(documentPosition, node);
    String matchString = getMatchString(sdRegion, completionRegion, documentPosition);
    // Handle empty Documents
    if (completionRegion == null) {
        if (((treeNode == null) || (((Node) treeNode).getNodeType() == Node.DOCUMENT_NODE)) && (completionRegion == null) && ((xmlnode == null) || (xmlnode.getChildNodes() == null) || (xmlnode.getChildNodes().getLength() == 0))) {
            IStructuredModel sModel = null;
            try {
                if (textViewer.getDocument() instanceof IStructuredDocument) {
                    sModel = StructuredModelManager.getModelManager().getModelForRead((IStructuredDocument) textViewer.getDocument());
                }
                if (sModel != null) {
                    IDOMDocument docNode = ((IDOMModel) sModel).getDocument();
                    contentAssistRequest = new ContentAssistRequest(docNode, docNode, sdRegion, completionRegion, documentPosition, 0, null);
                    addEmptyDocumentProposals(contentAssistRequest, context);
                }
            } finally {
                if (sModel != null) {
                    sModel.releaseFromRead();
                }
            }
            if (contentAssistRequest == null) {
                // $NON-NLS-1$
                Logger.logException(new IllegalStateException("problem getting model"));
                return new ArrayList(0);
            }
            ICompletionProposal[] props = contentAssistRequest.getCompletionProposals();
            return (props != null) ? Arrays.asList(props) : new ArrayList(0);
        }
        // MASSIVE ERROR CONDITION
        // $NON-NLS-1$
        Logger.logException(new IllegalStateException("completion region was null"));
        setErrorMessage(XMLUIMessages.SEVERE_internal_error_occu_UI_);
        // $NON-NLS-1$
        contentAssistRequest = new ContentAssistRequest((Node) treeNode, node.getParentNode(), sdRegion, completionRegion, documentPosition, 0, "");
        ICompletionProposal[] props = contentAssistRequest.getCompletionProposals();
        return (props != null) ? Arrays.asList(props) : new ArrayList(0);
    }
    // catch documents where no region can be determined
    if ((xmlnode.getNodeType() == Node.DOCUMENT_NODE) && ((completionRegion == null) || (xmlnode.getChildNodes() == null) || (xmlnode.getChildNodes().getLength() == 0))) {
        contentAssistRequest = computeStartDocumentProposals(matchString, completionRegion, (IDOMNode) treeNode, xmlnode, context);
        ICompletionProposal[] props = contentAssistRequest.getCompletionProposals();
        return (props != null) ? Arrays.asList(props) : new ArrayList(0);
    }
    // compute normal proposals
    contentAssistRequest = computeCompletionProposals(matchString, completionRegion, (IDOMNode) treeNode, xmlnode, context);
    if (contentAssistRequest == null) {
        // $NON-NLS-1$
        contentAssistRequest = new ContentAssistRequest((Node) treeNode, node.getParentNode(), sdRegion, completionRegion, documentPosition, 0, "");
        setErrorMessage(XMLUIMessages.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(XMLUIMessages.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) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) ITextViewer(org.eclipse.jface.text.ITextViewer) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)

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