Search in sources :

Example 1 with SQLTemplatesPage

use of org.jkiss.dbeaver.ui.editors.sql.templates.SQLTemplatesPage in project dbeaver by dbeaver.

the class SQLSymbolInserter method verifyKey.

@Override
public void verifyKey(VerifyEvent event) {
    if (!event.doit) {
        return;
    }
    IDocument document = sourceViewer.getDocument();
    final Point selection = sourceViewer.getSelectedRange();
    final int offset = selection.x;
    final int length = selection.y;
    switch(event.character) {
        case '(':
        case '[':
            if (!closeBrackets) {
                return;
            }
            if (hasCharacterToTheRight(document, offset + length, event.character)) {
                return;
            }
        case '\'':
            if (event.character == '\'') {
                if (!closeSingleQuotes) {
                    return;
                }
                if (hasIdentifierToTheLeft(document, offset) || hasIdentifierToTheRight(document, offset + length)) {
                    return;
                }
            }
        case '"':
            if (event.character == '"') {
                if (!closeDoubleQuotes) {
                    return;
                }
                if (hasIdentifierToTheLeft(document, offset) || hasIdentifierToTheRight(document, offset + length)) {
                    return;
                }
            }
            try {
                ITypedRegion partition = TextUtilities.getPartition(document, SQLPartitionScanner.SQL_PARTITIONING, offset, true);
                if (!IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType()) && partition.getOffset() != offset) {
                    return;
                }
                if (!editor.validateEditorInputState()) {
                    return;
                }
                final char character = event.character;
                final char closingCharacter = getPeerCharacter(character);
                document.replace(offset, length, String.valueOf(character) + closingCharacter);
                SymbolLevel level = new SymbolLevel();
                bracketLevelStack.add(level);
                LinkedPositionGroup group = new LinkedPositionGroup();
                group.addPosition(new LinkedPosition(document, offset + 1, 0, LinkedPositionGroup.NO_STOP));
                LinkedModeModel model = new LinkedModeModel();
                model.addLinkingListener(this);
                model.addGroup(group);
                model.forceInstall();
                level.offset = offset;
                level.length = 2;
                // set up position tracking for our magic peers
                if (bracketLevelStack.size() == 1) {
                    document.addPositionCategory(CATEGORY);
                    document.addPositionUpdater(positionUpdater);
                }
                level.firstPosition = new Position(offset, 1);
                level.secondPosition = new Position(offset + 1, 1);
                document.addPosition(CATEGORY, level.firstPosition);
                document.addPosition(CATEGORY, level.secondPosition);
                level.uI = new EditorLinkedModeUI(model, sourceViewer);
                level.uI.setSimpleMode(true);
                level.uI.setExitPolicy(new ExitPolicy(closingCharacter, getEscapeCharacter(closingCharacter), bracketLevelStack));
                level.uI.setExitPosition(sourceViewer, offset + 2, 0, Integer.MAX_VALUE);
                level.uI.setCyclingMode(LinkedModeUI.CYCLE_NEVER);
                level.uI.enter();
                IRegion newSelection = level.uI.getSelectedRegion();
                sourceViewer.setSelectedRange(newSelection.getOffset(), newSelection.getLength());
                event.doit = false;
            } catch (BadLocationException | BadPositionCategoryException e) {
                log.debug(e);
            }
            break;
        case SWT.TAB:
            {
                try {
                    int curOffset = offset;
                    // }
                    while (curOffset > 0) {
                        if (!Character.isJavaIdentifierPart(document.getChar(curOffset - 1))) {
                            break;
                        }
                        curOffset--;
                    }
                    if (curOffset != offset) {
                        String templateName = document.get(curOffset, offset - curOffset);
                        SQLTemplatesPage templatesPage = editor.getTemplatesPage();
                        Template template = templatesPage.getTemplateStore().findTemplate(templateName);
                        if (template != null && template.isAutoInsertable()) {
                            sourceViewer.setSelectedRange(curOffset, offset - curOffset);
                            templatesPage.insertTemplate(template, document);
                            event.doit = false;
                        }
                    }
                } catch (BadLocationException e) {
                    log.debug(e);
                }
                break;
            }
    }
}
Also used : SQLTemplatesPage(org.jkiss.dbeaver.ui.editors.sql.templates.SQLTemplatesPage) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) Template(org.eclipse.jface.text.templates.Template) IExitPolicy(org.eclipse.jface.text.link.LinkedModeUI.IExitPolicy)

Example 2 with SQLTemplatesPage

use of org.jkiss.dbeaver.ui.editors.sql.templates.SQLTemplatesPage in project dbeaver by serge-rider.

the class SQLSymbolInserter method verifyKey.

@Override
public void verifyKey(VerifyEvent event) {
    if (!event.doit) {
        return;
    }
    if (editor.isBlockSelectionModeEnabled()) {
        return;
    }
    IDocument document = sourceViewer.getDocument();
    final Point selection = sourceViewer.getSelectedRange();
    final int offset = selection.x;
    final int length = selection.y;
    switch(event.character) {
        case '(':
        case '[':
            if (!closeBrackets) {
                return;
            }
            try {
                if (offset < document.getLength() && !Character.isWhitespace(document.getChar(offset + length))) {
                    return;
                }
            } catch (BadLocationException e) {
                log.debug(e);
                return;
            }
            if (hasCharacterToTheRight(document, offset + length, event.character)) {
                return;
            }
        case '\'':
            if (event.character == '\'') {
                if (!closeSingleQuotes) {
                    return;
                }
                if (hasIdentifierToTheLeft(document, offset) || hasIdentifierToTheRight(document, offset + length)) {
                    return;
                }
            }
        case '"':
            if (event.character == '"') {
                if (!closeDoubleQuotes) {
                    return;
                }
                if (hasIdentifierToTheLeft(document, offset) || hasIdentifierToTheRight(document, offset + length)) {
                    return;
                }
            }
            try {
                ITypedRegion partition = TextUtilities.getPartition(document, SQLParserPartitions.SQL_PARTITIONING, offset, true);
                if (!IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType()) && partition.getOffset() != offset) {
                    return;
                }
                if (!editor.validateEditorInputState()) {
                    return;
                }
                final char character = event.character;
                final char closingCharacter = getPeerCharacter(character);
                document.replace(offset, length, String.valueOf(character) + getSelection() + closingCharacter);
                SymbolLevel level = new SymbolLevel();
                bracketLevelStack.add(level);
                LinkedPositionGroup group = new LinkedPositionGroup();
                group.addPosition(new LinkedPosition(document, offset + 1, 0, LinkedPositionGroup.NO_STOP));
                LinkedModeModel model = new LinkedModeModel();
                model.addLinkingListener(this);
                model.addGroup(group);
                model.forceInstall();
                level.offset = offset;
                level.length = 2;
                // set up position tracking for our magic peers
                if (bracketLevelStack.size() == 1) {
                    document.addPositionCategory(CATEGORY);
                    document.addPositionUpdater(positionUpdater);
                }
                level.firstPosition = new Position(offset, 1);
                level.secondPosition = new Position(offset + 1, 1);
                document.addPosition(CATEGORY, level.firstPosition);
                document.addPosition(CATEGORY, level.secondPosition);
                level.uI = new EditorLinkedModeUI(model, sourceViewer);
                level.uI.setSimpleMode(true);
                level.uI.setExitPolicy(new ExitPolicy(closingCharacter, getEscapeCharacter(closingCharacter), bracketLevelStack));
                level.uI.setExitPosition(sourceViewer, offset + 2, 0, Integer.MAX_VALUE);
                level.uI.setCyclingMode(LinkedModeUI.CYCLE_NEVER);
                level.uI.enter();
                IRegion newSelection = level.uI.getSelectedRegion();
                sourceViewer.setSelectedRange(newSelection.getOffset(), newSelection.getLength());
                event.doit = false;
            } catch (BadLocationException | BadPositionCategoryException e) {
                log.debug(e);
            }
            break;
        case SWT.TAB:
            {
                try {
                    int curOffset = offset;
                    // }
                    while (curOffset > 0) {
                        if (!Character.isJavaIdentifierPart(document.getChar(curOffset - 1))) {
                            break;
                        }
                        curOffset--;
                    }
                    if (curOffset != offset) {
                        String templateName = document.get(curOffset, offset - curOffset);
                        SQLTemplatesPage templatesPage = editor.getTemplatesPage();
                        Template template = templatesPage.getTemplateStore().findTemplate(templateName);
                        if (template != null && template.isAutoInsertable()) {
                            sourceViewer.setSelectedRange(curOffset, offset - curOffset);
                            templatesPage.insertTemplate(template, document);
                            event.doit = false;
                        }
                    }
                } catch (BadLocationException e) {
                    log.debug(e);
                }
                break;
            }
    }
}
Also used : SQLTemplatesPage(org.jkiss.dbeaver.ui.editors.sql.templates.SQLTemplatesPage) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) Template(org.eclipse.jface.text.templates.Template) IExitPolicy(org.eclipse.jface.text.link.LinkedModeUI.IExitPolicy)

Aggregations

IExitPolicy (org.eclipse.jface.text.link.LinkedModeUI.IExitPolicy)2 Template (org.eclipse.jface.text.templates.Template)2 Point (org.eclipse.swt.graphics.Point)2 SQLTemplatesPage (org.jkiss.dbeaver.ui.editors.sql.templates.SQLTemplatesPage)2