Search in sources :

Example 1 with SQLScriptElement

use of org.jkiss.dbeaver.model.sql.SQLScriptElement in project dbeaver by dbeaver.

the class NavigateQueryHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
    if (!(activeEditor instanceof SQLEditorBase)) {
        return null;
    }
    SQLEditorBase editor = (SQLEditorBase) activeEditor;
    String actionId = event.getCommand().getId();
    SQLScriptElement nextQuery;
    switch(actionId) {
        case CoreCommands.CMD_SQL_QUERY_NEXT:
            nextQuery = editor.extractNextQuery(true);
            break;
        case CoreCommands.CMD_SQL_QUERY_PREV:
            nextQuery = editor.extractNextQuery(false);
            break;
        default:
            nextQuery = null;
            break;
    }
    if (nextQuery != null) {
        editor.selectAndReveal(nextQuery.getOffset(), nextQuery.getLength());
    }
    return null;
}
Also used : SQLEditorBase(org.jkiss.dbeaver.ui.editors.sql.SQLEditorBase) SQLScriptElement(org.jkiss.dbeaver.model.sql.SQLScriptElement) IEditorPart(org.eclipse.ui.IEditorPart)

Example 2 with SQLScriptElement

use of org.jkiss.dbeaver.model.sql.SQLScriptElement in project dbeaver by dbeaver.

the class SQLReconcilingStrategy method calculatePositions.

protected void calculatePositions(IRegion partition) {
    if (!editor.isFoldingEnabled()) {
        return;
    }
    List<Annotation> removedAnnotations = null;
    Map<Annotation, Position> addedAnnotations = null;
    if (partition == null) {
        regionOffset = 0;
        regionLength = document.getLength();
    } else {
        regionOffset = partition.getOffset();
        regionLength = partition.getLength();
        for (int i = 0; i < parsedPositions.size(); i++) {
            SQLScriptPosition sp = parsedPositions.get(i);
            if (sp.getOffset() <= regionOffset + regionLength && sp.getOffset() + sp.getLength() >= regionOffset + regionLength) {
                SQLScriptPosition startPos = i > 0 ? parsedPositions.get(i - 1) : sp;
                SQLScriptPosition endPos = i < parsedPositions.size() - 1 ? parsedPositions.get(i + 1) : sp;
                regionOffset = i == 0 ? 0 : startPos.getOffset();
                regionLength = endPos.getOffset() + endPos.getLength() + regionLength;
                break;
            }
        }
    /*
            if (partition instanceof DirtyRegion) {
                // Modify positions
                DirtyRegion dirtyRegion = (DirtyRegion)partition;
                // Shift parsed positions
                boolean insert = DirtyRegion.INSERT.equals(dirtyRegion.getType());
                for (SQLScriptPosition sp : parsedPositions) {
                    if (dirtyRegion.getOffset() > sp.getOffset() && dirtyRegion.getOffset() < sp.getOffset() + sp.getLength()) {
                        // In this position
                        if (insert) {
                            sp.setLength(sp.getLength() + dirtyRegion.getLength());
                        } else {
                            sp.setLength(sp.getLength() - dirtyRegion.getLength());
                        }
                    } else if (sp.getOffset() >= dirtyRegion.getOffset()) {
                        // After this position
                        if (insert) {
                            sp.setOffset(sp.getOffset() + dirtyRegion.getLength());
                        } else {
                            sp.setOffset(sp.getOffset() - dirtyRegion.getLength());
                        }
                    }
                }
            }
*/
    }
    ProjectionAnnotationModel annotationModel = editor.getAnnotationModel();
    if (annotationModel == null) {
        return;
    }
    List<SQLScriptElement> queries = editor.extractScriptQueries(regionOffset, document.getLength() - regionOffset, false, true, false);
    {
        List<SQLScriptPosition> removedPositions = new ArrayList<>();
        for (SQLScriptPosition sp : parsedPositions) {
            if (sp.getOffset() >= regionOffset) /* && sp.getOffset() <= regionOffset + regionLength*/
            {
                removedPositions.add(sp);
            }
        }
        if (!removedPositions.isEmpty()) {
            parsedPositions.removeAll(removedPositions);
            removedAnnotations = new ArrayList<>();
            for (SQLScriptPosition removedPosition : removedPositions) {
                if (removedPosition.isMultiline()) {
                    removedAnnotations.add(removedPosition.getFoldingAnnotation());
                }
            }
        }
    }
    try {
        List<SQLScriptPosition> addedPositions = new ArrayList<>();
        int documentLength = document.getLength();
        for (SQLScriptElement se : queries) {
            int queryOffset = se.getOffset();
            int queryLength = se.getLength();
            boolean isMultiline = document.getLineOfOffset(queryOffset) != document.getLineOfOffset(queryOffset + queryLength);
            // Expand query to the end of line
            for (int i = queryOffset + queryLength; i < documentLength; i++) {
                char ch = document.getChar(i);
                if (Character.isWhitespace(ch)) {
                    queryLength++;
                }
                if (ch == '\n') {
                    break;
                }
            }
            addedPositions.add(new SQLScriptPosition(queryOffset, queryLength, isMultiline, new ProjectionAnnotation()));
        }
        if (!addedPositions.isEmpty()) {
            final int firstQueryPos = addedPositions.get(0).getOffset();
            int posBeforeFirst = 0;
            for (int i = 0; i < parsedPositions.size(); i++) {
                SQLScriptPosition sp = parsedPositions.get(i);
                if (sp.getOffset() >= firstQueryPos) {
                    break;
                }
                posBeforeFirst = i;
            }
            parsedPositions.addAll(posBeforeFirst, addedPositions);
            addedAnnotations = new HashMap<>();
            for (SQLScriptPosition pos : addedPositions) {
                if (pos.isMultiline()) {
                    addedAnnotations.put(pos.getFoldingAnnotation(), pos);
                }
            }
        }
    } catch (Exception e) {
        log.error(e);
    }
    if (removedAnnotations != null || !CommonUtils.isEmpty(addedAnnotations)) {
        annotationModel.modifyAnnotations(removedAnnotations == null ? null : removedAnnotations.toArray(new Annotation[removedAnnotations.size()]), addedAnnotations, null);
    }
}
Also used : ProjectionAnnotation(org.eclipse.jface.text.source.projection.ProjectionAnnotation) Position(org.eclipse.jface.text.Position) SQLScriptElement(org.jkiss.dbeaver.model.sql.SQLScriptElement) Annotation(org.eclipse.jface.text.source.Annotation) ProjectionAnnotation(org.eclipse.jface.text.source.projection.ProjectionAnnotation) ProjectionAnnotationModel(org.eclipse.jface.text.source.projection.ProjectionAnnotationModel)

Example 3 with SQLScriptElement

use of org.jkiss.dbeaver.model.sql.SQLScriptElement in project dbeaver by serge-rider.

the class SQLEditorHandlerNavigateQuery method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
    if (!(activeEditor instanceof SQLEditorBase)) {
        return null;
    }
    SQLEditorBase editor = (SQLEditorBase) activeEditor;
    String actionId = event.getCommand().getId();
    SQLScriptElement nextQuery;
    switch(actionId) {
        case SQLEditorCommands.CMD_SQL_QUERY_NEXT:
            nextQuery = editor.extractNextQuery(true);
            break;
        case SQLEditorCommands.CMD_SQL_QUERY_PREV:
            nextQuery = editor.extractNextQuery(false);
            break;
        default:
            nextQuery = null;
            break;
    }
    if (nextQuery != null) {
        editor.selectAndReveal(nextQuery.getOffset(), nextQuery.getLength());
    }
    return null;
}
Also used : SQLEditorBase(org.jkiss.dbeaver.ui.editors.sql.SQLEditorBase) SQLScriptElement(org.jkiss.dbeaver.model.sql.SQLScriptElement) IEditorPart(org.eclipse.ui.IEditorPart)

Example 4 with SQLScriptElement

use of org.jkiss.dbeaver.model.sql.SQLScriptElement in project dbeaver by serge-rider.

the class SQLContentFormatter method format.

@Override
public void format(IDocument document, IFormattingContext context) {
    if (!editor.getActivePreferenceStore().getBoolean(SQLPreferenceConstants.SQL_FORMAT_ACTIVE_QUERY)) {
        IRegion region = (IRegion) context.getProperty(FormattingContextProperties.CONTEXT_REGION);
        if (region == null) {
            region = new Region(0, editor.getDocument().getLength());
        }
        if (region != null) {
            super.format(document, region);
            return;
        }
    }
    IFormattingStrategy strategy = getFormattingStrategy(IDocument.DEFAULT_CONTENT_TYPE);
    if (strategy != null) {
        SQLScriptElement activeQuery = editor.extractActiveQuery();
        if (activeQuery == null) {
            return;
        }
        super.format(document, new Region(activeQuery.getOffset(), activeQuery.getLength()));
    }
}
Also used : Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) SQLScriptElement(org.jkiss.dbeaver.model.sql.SQLScriptElement) IRegion(org.eclipse.jface.text.IRegion)

Example 5 with SQLScriptElement

use of org.jkiss.dbeaver.model.sql.SQLScriptElement in project dbeaver by dbeaver.

the class SQLCompletionProcessor method computeContextInformation.

/**
 * This method is incomplete in that it does not implement logic to produce
 * some context help relevant to SQL. It just hard codes two strings to
 * demonstrate the action
 *
 * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeContextInformation(ITextViewer,
 *      int)
 */
@Nullable
@Override
public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
    SQLScriptElement statementInfo = editor.extractQueryAtPos(documentOffset);
    if (statementInfo == null || CommonUtils.isEmpty(statementInfo.getText())) {
        return null;
    }
    IContextInformation[] result = new IContextInformation[1];
    result[0] = new ContextInformation(statementInfo.getText(), statementInfo.getText());
    return result;
}
Also used : SQLScriptElement(org.jkiss.dbeaver.model.sql.SQLScriptElement) Nullable(org.jkiss.code.Nullable)

Aggregations

SQLScriptElement (org.jkiss.dbeaver.model.sql.SQLScriptElement)10 Nullable (org.jkiss.code.Nullable)3 SQLEditorBase (org.jkiss.dbeaver.ui.editors.sql.SQLEditorBase)3 Annotation (org.eclipse.jface.text.source.Annotation)2 ProjectionAnnotation (org.eclipse.jface.text.source.projection.ProjectionAnnotation)2 ProjectionAnnotationModel (org.eclipse.jface.text.source.projection.ProjectionAnnotationModel)2 IEditorPart (org.eclipse.ui.IEditorPart)2 NotNull (org.jkiss.code.NotNull)2 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 org.eclipse.jface.text (org.eclipse.jface.text)1 IRegion (org.eclipse.jface.text.IRegion)1 Position (org.eclipse.jface.text.Position)1 Region (org.eclipse.jface.text.Region)1 TextSelection (org.eclipse.jface.text.TextSelection)1 DirtyRegion (org.eclipse.jface.text.reconciler.DirtyRegion)1 IReconcilingStrategy (org.eclipse.jface.text.reconciler.IReconcilingStrategy)1 IReconcilingStrategyExtension (org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension)1 ISelection (org.eclipse.jface.viewers.ISelection)1