提交 e3077f12 编写于 作者: S serge-rider

#3579 SQL indent fix (use preferences in default indenter)


Former-commit-id: 75eb042f
上级 6ab69e67
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
package org.jkiss.dbeaver.ui.editors.sql.indent; package org.jkiss.dbeaver.ui.editors.sql.indent;
import org.eclipse.jface.text.*; import org.eclipse.jface.text.*;
import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
import org.jkiss.dbeaver.core.DBeaverCore; import org.jkiss.dbeaver.core.DBeaverCore;
import org.jkiss.dbeaver.model.preferences.DBPPreferenceStore; import org.jkiss.dbeaver.model.preferences.DBPPreferenceStore;
import org.jkiss.dbeaver.ui.editors.sql.SQLPreferenceConstants; import org.jkiss.dbeaver.ui.editors.sql.SQLPreferenceConstants;
...@@ -30,9 +31,6 @@ import java.text.BreakIterator; ...@@ -30,9 +31,6 @@ import java.text.BreakIterator;
*/ */
public class SQLCommentAutoIndentStrategy extends DefaultIndentLineAutoEditStrategy { public class SQLCommentAutoIndentStrategy extends DefaultIndentLineAutoEditStrategy {
private static final int DEFAULT_MARGIN = 5;
private static final int DEFAULT_TAB_WIDTH = 4;
private String partitioning; private String partitioning;
/** /**
...@@ -157,12 +155,7 @@ public class SQLCommentAutoIndentStrategy extends DefaultIndentLineAutoEditStrat ...@@ -157,12 +155,7 @@ public class SQLCommentAutoIndentStrategy extends DefaultIndentLineAutoEditStrat
} }
String comment = document.get(partition.getOffset(), partition.getLength()); String comment = document.get(partition.getOffset(), partition.getLength());
if (comment.indexOf("/*", 2) != -1) //$NON-NLS-1$ return comment.indexOf("/*", 2) != -1;
{
return true; // enclosed another comment -> probably a new comment
}
return false;
} }
catch (BadLocationException e) { catch (BadLocationException e) {
...@@ -550,9 +543,8 @@ public class SQLCommentAutoIndentStrategy extends DefaultIndentLineAutoEditStrat ...@@ -550,9 +543,8 @@ public class SQLCommentAutoIndentStrategy extends DefaultIndentLineAutoEditStrat
private static int calculateDisplayedWidth(String string) private static int calculateDisplayedWidth(String string)
{ {
final int tabWidth = DEFAULT_TAB_WIDTH; final int tabWidth = getPreferenceStore().getInt(
/*getPreferenceStore().getInt( AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH);
AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH);*/
int column = 0; int column = 0;
for (int i = 0; i < string.length(); i++) { for (int i = 0; i < string.length(); i++) {
...@@ -630,7 +622,7 @@ public class SQLCommentAutoIndentStrategy extends DefaultIndentLineAutoEditStrat ...@@ -630,7 +622,7 @@ public class SQLCommentAutoIndentStrategy extends DefaultIndentLineAutoEditStrat
private static int getMargin() private static int getMargin()
{ {
return DEFAULT_MARGIN;//getPreferenceStore().getInt(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_PRINT_MARGIN_COLUMN); return getPreferenceStore().getInt(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_PRINT_MARGIN_COLUMN);
} }
/** /**
......
...@@ -17,9 +17,12 @@ ...@@ -17,9 +17,12 @@
package org.jkiss.dbeaver.ui.editors.sql.indent; package org.jkiss.dbeaver.ui.editors.sql.indent;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion; import org.eclipse.jface.text.IRegion;
import org.eclipse.ui.internal.editors.text.EditorsPlugin;
import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
public class SQLIndenter { public class SQLIndenter {
...@@ -55,10 +58,7 @@ public class SQLIndenter { ...@@ -55,10 +58,7 @@ public class SQLIndenter {
* @param scanner the {@link SQLHeuristicScanner}to be used for scanning the document. It must be installed on the * @param scanner the {@link SQLHeuristicScanner}to be used for scanning the document. It must be installed on the
* same <code>IDocument</code>. * same <code>IDocument</code>.
*/ */
public SQLIndenter(IDocument document, SQLHeuristicScanner scanner) public SQLIndenter(IDocument document, SQLHeuristicScanner scanner) {
{
assert (document != null);
assert (scanner != null);
this.document = document; this.document = document;
this.scanner = scanner; this.scanner = scanner;
} }
...@@ -70,8 +70,7 @@ public class SQLIndenter { ...@@ -70,8 +70,7 @@ public class SQLIndenter {
* @return a String which reflects the indentation at the line in which the reference position to * @return a String which reflects the indentation at the line in which the reference position to
* <code>offset</code> resides, or <code>null</code> if it cannot be determined * <code>offset</code> resides, or <code>null</code> if it cannot be determined
*/ */
public String getReferenceIndentation(int offset) public String getReferenceIndentation(int offset) {
{
int unit; int unit;
unit = findReferencePosition(offset); unit = findReferencePosition(offset);
// if we were unable to find anything, return null // if we were unable to find anything, return null
...@@ -89,8 +88,7 @@ public class SQLIndenter { ...@@ -89,8 +88,7 @@ public class SQLIndenter {
* @return a String which reflects the correct indentation for the line in which offset resides, or * @return a String which reflects the correct indentation for the line in which offset resides, or
* <code>null</code> if it cannot be determined * <code>null</code> if it cannot be determined
*/ */
public String computeIndentation(int offset) public String computeIndentation(int offset) {
{
return computeIndentation(offset, false); return computeIndentation(offset, false);
} }
...@@ -103,8 +101,7 @@ public class SQLIndenter { ...@@ -103,8 +101,7 @@ public class SQLIndenter {
* @return a String which reflects the correct indentation for the line in which offset resides, or * @return a String which reflects the correct indentation for the line in which offset resides, or
* <code>null</code> if it cannot be determined * <code>null</code> if it cannot be determined
*/ */
public String computeIndentation(int offset, boolean assumeOpening) public String computeIndentation(int offset, boolean assumeOpening) {
{
indent = 1; indent = 1;
...@@ -132,8 +129,7 @@ public class SQLIndenter { ...@@ -132,8 +129,7 @@ public class SQLIndenter {
* @param offset the offset in the document * @param offset the offset in the document
* @return the indentation (leading whitespace) of the line in which <code>offset</code> is located * @return the indentation (leading whitespace) of the line in which <code>offset</code> is located
*/ */
private String getLeadingWhitespace(int offset) private String getLeadingWhitespace(int offset) {
{
try { try {
IRegion line = document.getLineInformationOfOffset(offset); IRegion line = document.getLineInformationOfOffset(offset);
int lineOffset = line.getOffset(); int lineOffset = line.getOffset();
...@@ -148,8 +144,7 @@ public class SQLIndenter { ...@@ -148,8 +144,7 @@ public class SQLIndenter {
} }
return document.get(lineOffset, nonWS - lineOffset); return document.get(lineOffset, nonWS - lineOffset);
} } catch (BadLocationException e) {
catch (BadLocationException e) {
// _log.debug(EditorMessages.error_badLocationException, e); // _log.debug(EditorMessages.error_badLocationException, e);
return ""; return "";
} }
...@@ -160,8 +155,7 @@ public class SQLIndenter { ...@@ -160,8 +155,7 @@ public class SQLIndenter {
* *
* @param indent the indentation to be modified * @param indent the indentation to be modified
*/ */
private void unindent(StringBuilder indent) private void unindent(StringBuilder indent) {
{
CharSequence oneIndent = createIndent(); CharSequence oneIndent = createIndent();
int i = indent.lastIndexOf(oneIndent.toString()); //$NON-NLS-1$ int i = indent.lastIndexOf(oneIndent.toString()); //$NON-NLS-1$
if (i != -1) { if (i != -1) {
...@@ -175,8 +169,7 @@ public class SQLIndenter { ...@@ -175,8 +169,7 @@ public class SQLIndenter {
* @param indent the requested indentation level. * @param indent the requested indentation level.
* @return the indentation specified by <code>indent</code> * @return the indentation specified by <code>indent</code>
*/ */
private StringBuilder createIndent(int indent) private StringBuilder createIndent(int indent) {
{
StringBuilder oneIndent = createIndent(); StringBuilder oneIndent = createIndent();
StringBuilder ret = new StringBuilder(); StringBuilder ret = new StringBuilder();
...@@ -192,11 +185,19 @@ public class SQLIndenter { ...@@ -192,11 +185,19 @@ public class SQLIndenter {
* *
* @return one indentation * @return one indentation
*/ */
private StringBuilder createIndent() private StringBuilder createIndent() {
{ IPreferenceStore preferenceStore = EditorsPlugin.getDefault().getPreferenceStore();
// get a sensible default when running without the infrastructure for testing boolean useSpaces = preferenceStore.getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SPACES_FOR_TABS);
StringBuilder oneIndent = new StringBuilder(); StringBuilder oneIndent = new StringBuilder();
if (!useSpaces) {
oneIndent.append('\t'); oneIndent.append('\t');
} else {
int tabWidth = preferenceStore.getInt(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH);
for (int i = 0; i < tabWidth; i++) {
oneIndent.append(' ');
}
}
return oneIndent; return oneIndent;
} }
...@@ -207,8 +208,7 @@ public class SQLIndenter { ...@@ -207,8 +208,7 @@ public class SQLIndenter {
* @return the reference statement relative to which <code>offset</code> should be indented, or * @return the reference statement relative to which <code>offset</code> should be indented, or
* {@link SQLHeuristicScanner#NOT_FOUND} * {@link SQLHeuristicScanner#NOT_FOUND}
*/ */
public int findReferencePosition(int offset) public int findReferencePosition(int offset) {
{
indent = 0; // the indentation modification indent = 0; // the indentation modification
position = offset; position = offset;
nextToken(); nextToken();
...@@ -224,8 +224,7 @@ public class SQLIndenter { ...@@ -224,8 +224,7 @@ public class SQLIndenter {
* @return the reference position for a list item: either a previous list item that has its own indentation, or the * @return the reference position for a list item: either a previous list item that has its own indentation, or the
* list introduction start. * list introduction start.
*/ */
private int skipToPreviousListItemOrListStart() private int skipToPreviousListItemOrListStart() {
{
int startLine = line; int startLine = line;
int startPosition = position; int startPosition = position;
while (true) { while (true) {
...@@ -237,8 +236,7 @@ public class SQLIndenter { ...@@ -237,8 +236,7 @@ public class SQLIndenter {
int lineOffset = document.getLineOffset(startLine); int lineOffset = document.getLineOffset(startLine);
int bound = Math.min(document.getLength(), startPosition + 1); int bound = Math.min(document.getLength(), startPosition + 1);
int align = scanner.findNonWhitespaceForwardInAnyPartition(lineOffset, bound); int align = scanner.findNonWhitespaceForwardInAnyPartition(lineOffset, bound);
} } catch (BadLocationException e) {
catch (BadLocationException e) {
// _log.debug(EditorMessages.error_badLocationException, e); // _log.debug(EditorMessages.error_badLocationException, e);
// ignore and return just the position // ignore and return just the position
} }
...@@ -257,19 +255,16 @@ public class SQLIndenter { ...@@ -257,19 +255,16 @@ public class SQLIndenter {
* Reads the next token in backward direction from the heuristic scanner and sets the fields * Reads the next token in backward direction from the heuristic scanner and sets the fields
* <code>fToken, fPreviousPosition</code> and <code>fPosition</code> accordingly. * <code>fToken, fPreviousPosition</code> and <code>fPosition</code> accordingly.
*/ */
private void nextToken() private void nextToken() {
{
nextToken(position); nextToken(position);
} }
public void nextToken(int start) public void nextToken(int start) {
{
token = scanner.previousToken(start - 1, SQLHeuristicScanner.UNBOUND); token = scanner.previousToken(start - 1, SQLHeuristicScanner.UNBOUND);
position = scanner.getPosition() + 1; position = scanner.getPosition() + 1;
try { try {
line = document.getLineOfOffset(position); line = document.getLineOfOffset(position);
} } catch (BadLocationException e) {
catch (BadLocationException e) {
line = -1; line = -1;
} }
} }
......
...@@ -138,7 +138,7 @@ public class SQLSyntaxManager { ...@@ -138,7 +138,7 @@ public class SQLSyntaxManager {
this.quoteStrings = sqlDialect.getIdentifierQuoteStrings(); this.quoteStrings = sqlDialect.getIdentifierQuoteStrings();
this.structSeparator = sqlDialect.getStructSeparator(); this.structSeparator = sqlDialect.getStructSeparator();
this.catalogSeparator = sqlDialect.getCatalogSeparator(); this.catalogSeparator = sqlDialect.getCatalogSeparator();
this.escapeChar = dialect.getStringEscapeCharacter();; this.escapeChar = dialect.getStringEscapeCharacter();
if (!preferenceStore.getBoolean(ModelPreferences.SCRIPT_IGNORE_NATIVE_DELIMITER)) { if (!preferenceStore.getBoolean(ModelPreferences.SCRIPT_IGNORE_NATIVE_DELIMITER)) {
this.statementDelimiters = new String[] { sqlDialect.getScriptDelimiter().toLowerCase() }; this.statementDelimiters = new String[] { sqlDialect.getScriptDelimiter().toLowerCase() };
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册