From 3224ee40da61ed7d5798c7e57602819e06807a66 Mon Sep 17 00:00:00 2001 From: Serge Rider Date: Sun, 8 Jan 2017 18:21:27 +0300 Subject: [PATCH] #1124 SQL templates formatting (indent fix) --- .../ui/editors/sql/templates/SQLContext.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) 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 5a1e504678..88b5c09e83 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 ""; + } + } } -- GitLab