Search in sources :

Example 26 with ITextViewer

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

the class SQLCompletionProposal method validate.

@Override
public boolean validate(IDocument document, int offset, DocumentEvent event) {
    if (event == null) {
        return false;
    }
    SQLSyntaxManager syntaxManager = getContext().getSyntaxManager();
    DBPDataSource dataSource = getContext().getDataSource();
    final SQLWordPartDetector wordDetector = new SQLWordPartDetector(document, syntaxManager, offset);
    String wordPart = wordDetector.getWordPart();
    int divPos = wordPart.lastIndexOf(syntaxManager.getStructSeparator());
    if (divPos != -1) {
        if (divPos == wordPart.length() - 1) {
            // It is valid only if full word matches (it should be the only proposal)
            if (replacementString.equals(wordPart.substring(0, divPos))) {
                {
                    // Call completion popup again
                    UIUtils.asyncExec(() -> {
                        IEditorPart activeEditor = UIUtils.getActiveWorkbenchWindow().getActivePage().getActiveEditor();
                        if (activeEditor != null) {
                            ITextViewer textViewer = activeEditor.getAdapter(ITextViewer.class);
                            if (textViewer != null) {
                                textViewer.getTextOperationTarget().doOperation(ISourceViewer.CONTENTASSIST_PROPOSALS);
                            }
                        }
                    });
                }
            }
            return false;
        }
        wordPart = wordPart.substring(divPos + 1);
    }
    String wordLower = wordPart.toLowerCase(Locale.ENGLISH);
    if (!CommonUtils.isEmpty(wordPart)) {
        boolean matchContains = dataSource != null && dataSource.getContainer().getPreferenceStore().getBoolean(SQLPreferenceConstants.PROPOSALS_MATCH_CONTAINS);
        boolean matched;
        if (getObject() == null || !matchContains) {
            // For keywords use strict matching
            matched = (matchContains ? replacementFull.contains(wordLower) : replacementFull.startsWith(wordLower)) && (CommonUtils.isEmpty(event.getText()) || replacementFull.contains(event.getText().toLowerCase(Locale.ENGLISH))) || (this.replacementLast != null && this.replacementLast.startsWith(wordLower));
        } else {
            // For objects use fuzzy matching
            int score = TextUtils.fuzzyScore(replacementFull, wordLower);
            matched = (score > 0 && (CommonUtils.isEmpty(event.getText()) || TextUtils.fuzzyScore(replacementFull, event.getText()) > 0)) || (this.replacementLast != null && TextUtils.fuzzyScore(this.replacementLast, wordLower) > 0);
            if (matched) {
                setProposalScore(score);
            }
        }
        if (matched) {
            setPosition(wordDetector);
            return true;
        }
    } else if (divPos != -1) {
        // Most likely it is a column name after an alias - all columns are valid
        if (getObject() != null) {
            return true;
        }
    }
    return false;
}
Also used : SQLWordPartDetector(org.jkiss.dbeaver.model.sql.parser.SQLWordPartDetector) SQLSyntaxManager(org.jkiss.dbeaver.model.sql.SQLSyntaxManager) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) StyledString(org.eclipse.jface.viewers.StyledString) IEditorPart(org.eclipse.ui.IEditorPart) Point(org.eclipse.swt.graphics.Point) ITextViewer(org.eclipse.jface.text.ITextViewer)

Example 27 with ITextViewer

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

the class AbstractCompositeHoverTest method testMultipleHovers.

@SuppressWarnings("deprecation")
@Test
public void testMultipleHovers() {
    final ITextHover hover1 = new ITextHover() {

        @Override
        public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
            // TODO Auto-generated method stub
            if (offset == 0)
                return new Region(offset, 0);
            return null;
        }

        @Override
        public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
            return "hover1";
        }
    };
    final ITextHover hover2 = new ITextHover() {

        @Override
        public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
            // TODO Auto-generated method stub
            if (offset == 1)
                return new Region(offset, 0);
            return null;
        }

        @Override
        public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
            return "hover2";
        }
    };
    AbstractCompositeHover hover = new AbstractCompositeHover() {

        @Override
        protected List<ITextHover> createHovers() {
            List<ITextHover> hovers = Lists.newArrayList();
            hovers.add(hover1);
            hovers.add(hover2);
            return hovers;
        }
    };
    assertEquals(2, hover.getHovers().size());
    assertNotNull(hover.getHoverRegion(editor.getInternalSourceViewer(), 0));
    assertEquals("hover1", hover.getHoverInfo(editor.getInternalSourceViewer(), new Region(0, 0)));
    assertNotNull(hover.getHoverRegion(editor.getInternalSourceViewer(), 1));
    assertEquals("hover2", hover.getHoverInfo(editor.getInternalSourceViewer(), new Region(1, 0)));
}
Also used : ITextHover(org.eclipse.jface.text.ITextHover) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) IRegion(org.eclipse.jface.text.IRegion) ITextViewer(org.eclipse.jface.text.ITextViewer) AbstractCompositeHover(org.eclipse.xtext.ui.editor.hover.AbstractCompositeHover) Test(org.junit.Test) AbstractEditorTest(org.eclipse.xtext.ui.testing.AbstractEditorTest)

Example 28 with ITextViewer

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

the class CrossReferenceProposalTest method testBug276742_08b.

@Test
public void testBug276742_08b() throws Exception {
    String modelAsString = "Foo {}";
    ContentAssistProcessorTestBuilder builder = newBuilder();
    XtextContentAssistProcessor processor = get(XtextContentAssistProcessor.class);
    XtextResource resource = getResourceFromString(modelAsString);
    ITextViewer viewer = builder.getSourceViewer(modelAsString, builder.getDocument(resource, modelAsString));
    ContentAssistContext[] contexts = processor.getContextFactory().create(viewer, modelAsString.length(), resource);
    assertEquals(2, contexts.length);
    Set<EClass> contextTypes = Sets.newHashSet(CrossReferenceProposalTestPackage.Literals.MODEL, CrossReferenceProposalTestPackage.Literals.CLASS);
    CrossReferenceProposalTestLanguageGrammarAccess grammarAccess = get(CrossReferenceProposalTestLanguageGrammarAccess.class);
    for (ContentAssistContext context : contexts) {
        EObject model = context.getCurrentModel();
        assertTrue(contextTypes.remove(model.eClass()));
        if (context.getFirstSetGrammarElements().contains(grammarAccess.getClassAccess().getRightCurlyBracketKeyword_3())) {
            assertEquals(CrossReferenceProposalTestPackage.Literals.CLASS, model.eClass());
        } else {
            assertEquals(CrossReferenceProposalTestPackage.Literals.MODEL, model.eClass());
        }
    }
}
Also used : XtextContentAssistProcessor(org.eclipse.xtext.ui.editor.contentassist.XtextContentAssistProcessor) CrossReferenceProposalTestLanguageGrammarAccess(org.eclipse.xtext.ui.tests.editor.contentassist.services.CrossReferenceProposalTestLanguageGrammarAccess) ContentAssistProcessorTestBuilder(org.eclipse.xtext.ui.testing.ContentAssistProcessorTestBuilder) EClass(org.eclipse.emf.ecore.EClass) ContentAssistContext(org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext) EObject(org.eclipse.emf.ecore.EObject) XtextResource(org.eclipse.xtext.resource.XtextResource) ITextViewer(org.eclipse.jface.text.ITextViewer) Test(org.junit.Test)

Example 29 with ITextViewer

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

the class TextViewerDeleteLineAction method run.

/*
	 * @see IAction#run()
	 */
@Override
public void run() {
    if (fTarget == null)
        return;
    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;
    try {
        fTarget.deleteLine(document, selection, fType, fCopyToClipboard);
    } 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) ITextViewer(org.eclipse.jface.text.ITextViewer)

Example 30 with ITextViewer

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

the class TextViewerDeleteLineAction method update.

/*
	 * @see IUpdate#update()
	 */
@Override
public void update() {
    super.update();
    if (!isEnabled())
        return;
    if (!canModifyViewer()) {
        setEnabled(false);
        return;
    }
    ITextViewer viewer = getTextViewer();
    if (viewer != null) {
        if (fTarget == null) {
            fTarget = new TextViewerDeleteLineTarget(viewer);
        }
    } else {
        fTarget = null;
    }
    setEnabled(fTarget != null);
}
Also used : ITextViewer(org.eclipse.jface.text.ITextViewer) TextViewerDeleteLineTarget(org.eclipse.ui.texteditor.TextViewerDeleteLineTarget)

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