Search in sources :

Example 66 with ITextViewer

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

the class ContentFormatHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
    if (activeEditor instanceof ContentEditor) {
        IEditorPart editorPart = ((ContentEditor) activeEditor).getActiveEditor();
        ITextViewer textViewer = editorPart.getAdapter(ITextViewer.class);
        if (textViewer instanceof TextViewer) {
            StyledText textWidget = textViewer.getTextWidget();
            boolean oldEditable = textWidget.getEditable();
            textWidget.setEditable(true);
            try {
                if (((TextViewer) textViewer).canDoOperation(SourceViewer.FORMAT)) {
                    ((TextViewer) textViewer).doOperation(SourceViewer.FORMAT);
                }
            } finally {
                textWidget.setEditable(oldEditable);
            }
        }
    }
    return null;
}
Also used : StyledText(org.eclipse.swt.custom.StyledText) IEditorPart(org.eclipse.ui.IEditorPart) ITextViewer(org.eclipse.jface.text.ITextViewer) TextViewer(org.eclipse.jface.text.TextViewer) ITextViewer(org.eclipse.jface.text.ITextViewer)

Example 67 with ITextViewer

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

the class ContentFormatHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
    if (activeEditor instanceof ContentEditor) {
        IEditorPart editorPart = ((ContentEditor) activeEditor).getActiveEditor();
        ITextViewer textViewer = editorPart.getAdapter(ITextViewer.class);
        if (textViewer instanceof TextViewer) {
            StyledText textWidget = textViewer.getTextWidget();
            boolean oldEditable = textWidget.getEditable();
            textWidget.setEditable(true);
            try {
                if (((TextViewer) textViewer).canDoOperation(SourceViewer.FORMAT)) {
                    ((TextViewer) textViewer).doOperation(SourceViewer.FORMAT);
                }
            } finally {
                textWidget.setEditable(oldEditable);
            }
        }
    }
    return null;
}
Also used : StyledText(org.eclipse.swt.custom.StyledText) IEditorPart(org.eclipse.ui.IEditorPart) ITextViewer(org.eclipse.jface.text.ITextViewer) TextViewer(org.eclipse.jface.text.TextViewer) ITextViewer(org.eclipse.jface.text.ITextViewer)

Example 68 with ITextViewer

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

the class SQLContextInformer method searchInformation.

public void searchInformation(IRegion region) {
    ITextViewer textViewer = editor.getTextViewer();
    final DBCExecutionContext executionContext = editor.getExecutionContext();
    if (region == null || textViewer == null || executionContext == null) {
        return;
    }
    IDocument document = textViewer.getDocument();
    if (document == null) {
        return;
    }
    SQLWordPartDetector wordDetector = new SQLWordPartDetector(document, syntaxManager, region.getOffset());
    wordRegion = wordDetector.detectIdentifier(document, region);
    if (wordRegion.word.length() == 0) {
        return;
    }
    String fullName = wordRegion.identifier;
    String tableName = wordRegion.word;
    boolean caseSensitive = false;
    if (wordDetector.isQuoted(tableName)) {
        tableName = DBUtils.getUnQuotedIdentifier(tableName, syntaxManager.getIdentifierQuoteStrings());
        caseSensitive = true;
    }
    String[] containerNames = null;
    if (!CommonUtils.equalObjects(fullName, tableName)) {
        int divPos = fullName.indexOf(syntaxManager.getStructSeparator());
        if (divPos != -1) {
            String[] parts = wordDetector.splitIdentifier(fullName);
            tableName = parts[parts.length - 1];
            containerNames = ArrayUtils.remove(String.class, parts, parts.length - 1);
            for (int i = 0; i < containerNames.length; i++) {
                if (wordDetector.isQuoted(containerNames[i])) {
                    containerNames[i] = DBUtils.getUnQuotedIdentifier(containerNames[i], syntaxManager.getIdentifierQuoteStrings());
                }
                containerNames[i] = DBObjectNameCaseTransformer.transformName(editor.getDataSource(), containerNames[i]);
            }
            if (wordDetector.isQuoted(tableName)) {
                tableName = DBUtils.getUnQuotedIdentifier(tableName, syntaxManager.getIdentifierQuoteStrings());
            }
        } else {
            // Full name could be quoted
            if (wordDetector.isQuoted(fullName)) {
                String unquotedName = DBUtils.getUnQuotedIdentifier(tableName, syntaxManager.getIdentifierQuoteStrings());
                if (unquotedName.equals(tableName)) {
                    caseSensitive = true;
                }
            }
        }
    }
    final SQLDialect dialect = syntaxManager.getDialect();
    keywordType = dialect.getKeywordType(fullName);
    if (keywordType == DBPKeywordType.KEYWORD && region.getLength() > 1) {
        // It is a keyword = let's use whole selection
        try {
            fullName = document.get(region.getOffset(), region.getLength());
        } catch (BadLocationException e) {
            log.warn(e);
        }
    }
    keywords = new String[] { fullName };
    if (keywordType == DBPKeywordType.KEYWORD || keywordType == DBPKeywordType.FUNCTION) {
        // Skip keywords
        return;
    }
    final Map<String, ObjectLookupCache> contextCache = getLinksCache();
    if (contextCache == null) {
        return;
    }
    ObjectLookupCache tlc = contextCache.get(fullName);
    if (tlc == null) {
        // Start new word finder job
        tlc = new ObjectLookupCache();
        contextCache.put(fullName, tlc);
        DBSStructureAssistant structureAssistant = DBUtils.getAdapter(DBSStructureAssistant.class, editor.getDataSource());
        TablesFinderJob job = new TablesFinderJob(executionContext, structureAssistant, containerNames, tableName, caseSensitive, tlc);
        job.schedule();
    }
    if (tlc.loading) {
        // Wait for 1000ms maximum
        for (int i = 0; i < 20; i++) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                // interrupted - just go further
                break;
            }
            if (!tlc.loading) {
                break;
            }
            Display.getCurrent().readAndDispatch();
        }
    }
    if (!tlc.loading) {
        synchronized (this) {
            objectReferences = tlc.references;
        }
    }
}
Also used : SQLWordPartDetector(org.jkiss.dbeaver.model.sql.parser.SQLWordPartDetector) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) ITextViewer(org.eclipse.jface.text.ITextViewer) SQLDialect(org.jkiss.dbeaver.model.sql.SQLDialect) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 69 with ITextViewer

use of org.eclipse.jface.text.ITextViewer in project mylyn.docs by eclipse.

the class FileRefHyperlinkDetectorTest method testFileDoesNotExist.

@Test
public void testFileDoesNotExist() throws CoreException {
    ImmutableList<String> fileRefPatterns = ImmutableList.of("include::(.+)\\[\\]", "image::(.+)\\[\\]");
    FileRefHyperlinkDetector fileRefHyperlinkDetector = new FileRefHyperlinkDetector(project, fileRefPatterns);
    ITextViewer mockTextViewer = mock(ITextViewer.class);
    when(mockTextViewer.getDocument()).thenReturn(new Document("image::file-that-does-not-exist.png[]"));
    IHyperlink[] detectHyperlinks = fileRefHyperlinkDetector.detectHyperlinks(mockTextViewer, new Region(7, 15), false);
    assertThat(detectHyperlinks, is(nullValue()));
}
Also used : FileRefHyperlinkDetector(org.eclipse.mylyn.internal.wikitext.ui.editor.syntax.FileRefHyperlinkDetector) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) Region(org.eclipse.jface.text.Region) Document(org.eclipse.jface.text.Document) ITextViewer(org.eclipse.jface.text.ITextViewer) Test(org.junit.Test)

Example 70 with ITextViewer

use of org.eclipse.jface.text.ITextViewer in project mylyn.docs by eclipse.

the class FileRefHyperlinkDetectorTest method testFindFileRefAndOpenHyperlink.

@Test
public void testFindFileRefAndOpenHyperlink() throws CoreException {
    String asciidocFileName = "simon-scholz.adoc";
    IFile file = project.getFile(asciidocFileName);
    file.create(new ByteArrayInputStream("== Writing tests is kinda documentation".getBytes()), true, new NullProgressMonitor());
    ImmutableList<String> fileRefPatterns = ImmutableList.of("include::(.+)\\[\\]", "image::(.+)\\[\\]");
    FileRefHyperlinkDetector fileRefHyperlinkDetector = new FileRefHyperlinkDetector(file.getParent(), fileRefPatterns);
    ITextViewer mockTextViewer = mock(ITextViewer.class);
    when(mockTextViewer.getDocument()).thenReturn(new Document("include::" + asciidocFileName + "[]"));
    IHyperlink[] detectHyperlinks = fileRefHyperlinkDetector.detectHyperlinks(mockTextViewer, new Region(9, 17), false);
    assertThat(detectHyperlinks.length, is(1));
    IHyperlink hyperlink = detectHyperlinks[0];
    hyperlink.open();
    IEditorPart activeEditor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
    IEditorInput editorInput = activeEditor.getEditorInput();
    if (editorInput instanceof IFileEditorInput) {
        IFile editorFile = ((IFileEditorInput) editorInput).getFile();
        assertThat(file, equalTo(editorFile));
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) IEditorPart(org.eclipse.ui.IEditorPart) Document(org.eclipse.jface.text.Document) ITextViewer(org.eclipse.jface.text.ITextViewer) FileRefHyperlinkDetector(org.eclipse.mylyn.internal.wikitext.ui.editor.syntax.FileRefHyperlinkDetector) ByteArrayInputStream(java.io.ByteArrayInputStream) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) IFileEditorInput(org.eclipse.ui.IFileEditorInput) Region(org.eclipse.jface.text.Region) IEditorInput(org.eclipse.ui.IEditorInput) Test(org.junit.Test)

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