diff --git a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/editors/sql/templates/SQLContext.java b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/editors/sql/templates/SQLContext.java index 5a1e5046781f9c8759a8f505b9527e4f82ac6d00..88b5c09e83b68122c63e537546a0e4e6fa73c755 100644 --- a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/editors/sql/templates/SQLContext.java +++ b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/editors/sql/templates/SQLContext.java @@ -19,6 +19,7 @@ package org.jkiss.dbeaver.ui.editors.sql.templates; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IRegion; import org.eclipse.jface.text.Position; import org.eclipse.jface.text.templates.*; import org.jkiss.dbeaver.model.DBPContextProvider; @@ -70,6 +71,7 @@ public class SQLContext extends DocumentTemplateContext implements DBPContextPro } }; TemplateBuffer buffer = translator.translate(template); + formatTemplate(buffer); /* // Reorder variables TemplateVariable[] bufferVariables = buffer.getVariables(); @@ -89,6 +91,21 @@ public class SQLContext extends DocumentTemplateContext implements DBPContextPro return buffer; } + private void formatTemplate(TemplateBuffer buffer) { + TemplateVariable[] variables= buffer.getVariables(); + final String indentation = getIndentation(); + String content = buffer.getString(); + if (!indentation.isEmpty() && content.indexOf('\n') != -1) { + StringBuilder result = new StringBuilder(); + for (int i = 0; i < content.length(); i++) { + char c = content.charAt(i); + result.append(c); + if (c == '\n') result.append(indentation); + } + buffer.setContent(result.toString(), variables); + } + } + /* private static final String[] VAR_ORDER = { "table", @@ -119,4 +136,24 @@ public class SQLContext extends DocumentTemplateContext implements DBPContextPro { return variables.values(); } + + private String getIndentation() { + int start = this.getStart(); + IDocument document = this.getDocument(); + + try { + IRegion region = document.getLineInformationOfOffset(start); + int lineIndent = start - region.getOffset(); + if (lineIndent <= 0) { + return ""; + } + char[] buf = new char[lineIndent]; + for (int i = 0; i < lineIndent; i++) { + buf[i] = ' '; + } + return String.valueOf(buf); + } catch (Exception var6) { + return ""; + } + } }