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;
}
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);
}
}
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;
}
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()));
}
}
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;
}
Aggregations