Search in sources :

Example 21 with ITextViewer

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

the class OpenFileHyperlinkTracker method mouseMove.

/*
	 * @see org.eclipse.swt.events.MouseMoveListener#mouseMove(org.eclipse.swt.events.MouseEvent)
	 */
public void mouseMove(MouseEvent event) {
    if (event.widget instanceof Control && !((Control) event.widget).isFocusControl()) {
        deactivate();
        return;
    }
    if (!fActive) {
        if (event.stateMask != fKeyModifierMask)
            return;
        // modifier was already pressed
        fActive = true;
    }
    ITextViewer viewer = getTextViewer();
    if (viewer == null) {
        deactivate();
        return;
    }
    StyledText text = viewer.getTextWidget();
    if (text == null || text.isDisposed()) {
        deactivate();
        return;
    }
    if ((event.stateMask & SWT.BUTTON1) != 0 && text.getSelectionCount() != 0) {
        deactivate();
        return;
    }
    IRegion region = null;
    int offset = getCurrentTextOffset();
    IOpenOn openOn = OpenOnProvider.getInstance().getOpenOn(getTextViewer().getDocument(), offset);
    if (openOn != null) {
        region = openOn.getOpenOnRegion(getTextViewer().getDocument(), offset);
    }
    if (region == null || region.getLength() == 0) {
        repairRepresentation();
        return;
    }
    highlightRegion(viewer, region);
    activateCursor(viewer);
}
Also used : Control(org.eclipse.swt.widgets.Control) StyledText(org.eclipse.swt.custom.StyledText) IRegion(org.eclipse.jface.text.IRegion) Point(org.eclipse.swt.graphics.Point) ITextViewer(org.eclipse.jface.text.ITextViewer)

Example 22 with ITextViewer

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

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 23 with ITextViewer

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

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 24 with ITextViewer

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

the class GenerateUUIDHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
    if (activePart == null) {
        return null;
    }
    IResultSetController rsc = activePart.getAdapter(IResultSetController.class);
    if (rsc != null && UIUtils.hasFocus(rsc.getControl())) {
        IResultSetSelection selection = rsc.getSelection();
        if (selection != null && !selection.isEmpty()) {
            for (Object cell : selection.toArray()) {
                DBDAttributeBinding attr = selection.getElementAttribute(cell);
                ResultSetRow row = selection.getElementRow(cell);
                if (row != null && attr != null) {
                    ResultSetValueController valueController = new ResultSetValueController(rsc, attr, row, IValueController.EditType.NONE, null);
                    DBDValueHandler valueHandler = valueController.getValueHandler();
                    String uuid = generateUUID();
                    valueController.updateValue(uuid, false);
                }
            }
            rsc.redrawData(false, false);
            rsc.updateEditControls();
        }
    } else {
        ITextViewer textViewer = activePart.getAdapter(ITextViewer.class);
        if (textViewer != null) {
            ISelection selection = textViewer.getSelectionProvider().getSelection();
            if (selection instanceof TextSelection) {
                try {
                    int offset = ((TextSelection) selection).getOffset();
                    int length = ((TextSelection) selection).getLength();
                    String uuid = generateUUID();
                    textViewer.getDocument().replace(offset, length, uuid);
                    textViewer.getSelectionProvider().setSelection(new TextSelection(offset + uuid.length(), 0));
                } catch (BadLocationException e) {
                    DBWorkbench.getPlatformUI().showError("Insert UUID", "Error inserting UUID in text editor", e);
                }
            }
        }
    }
    return null;
}
Also used : TextSelection(org.eclipse.jface.text.TextSelection) IResultSetSelection(org.jkiss.dbeaver.ui.controls.resultset.IResultSetSelection) DBDValueHandler(org.jkiss.dbeaver.model.data.DBDValueHandler) ResultSetRow(org.jkiss.dbeaver.ui.controls.resultset.ResultSetRow) IResultSetController(org.jkiss.dbeaver.ui.controls.resultset.IResultSetController) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) ITextViewer(org.eclipse.jface.text.ITextViewer) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) ISelection(org.eclipse.jface.viewers.ISelection) ResultSetValueController(org.jkiss.dbeaver.ui.controls.resultset.ResultSetValueController) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 25 with ITextViewer

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

the class ToggleBreakpointHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
    if (activeEditor != null) {
        SQLEditorBase sqlEditor = activeEditor.getAdapter(SQLEditorBase.class);
        if (sqlEditor != null) {
            ITextViewer textViewer = sqlEditor.getAdapter(ITextViewer.class);
            if (textViewer != null) {
                IVerticalRulerInfo rulerInfo = sqlEditor.getAdapter(IVerticalRulerInfo.class);
                ToggleBreakpointAction action = new ToggleBreakpointAction(sqlEditor, textViewer.getDocument(), rulerInfo);
                action.runWithEvent(new Event());
            }
        }
    }
    return null;
}
Also used : SQLEditorBase(org.jkiss.dbeaver.ui.editors.sql.SQLEditorBase) ExecutionEvent(org.eclipse.core.commands.ExecutionEvent) Event(org.eclipse.swt.widgets.Event) IEditorPart(org.eclipse.ui.IEditorPart) IVerticalRulerInfo(org.eclipse.jface.text.source.IVerticalRulerInfo) ToggleBreakpointAction(org.eclipse.debug.ui.actions.ToggleBreakpointAction) 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