Skip to content

Commit

Permalink
SQL formatting
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.code.sf.net/p/jailer/code/trunk@1376 3dd849cd-670e-4645-a7cd-dd197c8d0e81
  • Loading branch information
rwisser committed Sep 27, 2017
1 parent c00f664 commit c537aaa
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ public class BasicFormatterImpl {
static final String initial = "\n ";

public String format(String source) {
return new FormatProcess( source ).perform();
try {
return new FormatProcess( source ).perform();
} catch (Throwable t) {
return source;
}
}

private static class FormatProcess {
Expand Down Expand Up @@ -122,11 +126,39 @@ public String perform() {

result.append( initial );

String nextToken = null;
boolean lastWasWS = false;
while ( tokens.hasMoreTokens() ) {
token = tokens.nextToken();
token = nextToken == null? tokens.nextToken() : nextToken;
nextToken = null;
lcToken = token.toLowerCase();

if ( "'".equals( token ) ) {
if ("-".equals(token) && tokens.hasMoreTokens()) {
nextToken = tokens.nextToken();
if ("-".equals(nextToken)) {
token += nextToken;
nextToken = null;
String t;
if (tokens.hasMoreTokens())
do {
t = tokens.nextToken();
token += t;
}
while ( !"\n".equals( t ) && tokens.hasMoreTokens() ); // cannot handle single quotes
}
} else if ("/".equals(token) && tokens.hasMoreTokens()) {
nextToken = tokens.nextToken();
if ("*".equals(nextToken)) {
token += nextToken;
nextToken = null;
String t;
do {
t = tokens.nextToken();
token += t;
}
while ( (!token.endsWith("*/") || !"/".equals( t )) && tokens.hasMoreTokens() ); // cannot handle single quotes
}
} else if ( "'".equals( token ) ) {
String t;
do {
t = tokens.nextToken();
Expand Down Expand Up @@ -194,10 +226,22 @@ else if ( isWhitespace( token ) ) {
white();
}

else if (" ".equals(token)) {
if (result.length() == 0 || !" ".equals(result.substring(result.length() - 1, result.length()))) {
misc();
}
}

else {
misc();
}


if (" ".equals(token)) {
lastWasWS = true;
} else {
lastWasWS = false;
}

if ( !isWhitespace( token ) ) {
lastToken = lcToken;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;
import javax.swing.KeyStroke;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
Expand All @@ -44,6 +50,8 @@
@SuppressWarnings("serial")
public class RSyntaxTextAreaWithSQLCompletion extends RSyntaxTextArea {

private int end;

public RSyntaxTextAreaWithSQLCompletion() {
setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_SQL);

Expand Down Expand Up @@ -78,6 +86,77 @@ public void caretUpdate(CaretEvent e) {
});
}

/**
* Overridden to add menu items related to formatting
*
* @return the popup menu
*/
@Override
protected JPopupMenu createPopupMenu() {
JPopupMenu menu = super.createPopupMenu();

JMenuItem item = new JMenuItem("Format SQL");
item.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String currentStatement = getCurrentStatement(true);
Pattern pattern = Pattern.compile("(.*?)(;\\s*(\\n\\r?|$))", Pattern.DOTALL);
Matcher matcher = pattern.matcher(currentStatement);
boolean result = matcher.find();
if (result) {
StringBuffer sb = new StringBuffer();
do {
matcher.appendReplacement(sb, Matcher.quoteReplacement(new BasicFormatterImpl().format(matcher.group(1))) + matcher.group(2));
result = matcher.find();
} while (result);
matcher.appendTail(sb);
replaceCurrentStatement(sb.toString(), true);
}
}
});
menu.add(item, 0);
menu.add(new JSeparator(), 1);
return menu;
}

/**
* Gets statement(s) at caret position.
*
* @param replacement the replacement
* @param singleStatement <code>true</code> to replace only one statement
*/
public void replaceCurrentStatement(String replacement, boolean singleStatement) {
Pair<Integer, Integer> loc = getCurrentStatementLocation(singleStatement);
if (loc != null) {
try {
int from = loc.a;
int to = loc.b;
if (to >= getLineCount()) {
to = getLineCount() - 1;
}
int start = getLineStartOffset(from);
int end = getLineEndOffset(to);
replaceRange(replacement, start, end);
} catch (BadLocationException e) {
e.printStackTrace();
}
}
}

/**
* Gets statement(s) at caret position.
*
* @param singleStatement <code>true</code> to get only one statement
* @return pair of start and end line number
*/
public String getCurrentStatement(boolean singleStatement) {
Pair<Integer, Integer> loc = getCurrentStatementLocation(singleStatement);
if (loc != null) {
return getText(loc.a, loc.b, true);
}
return "";
}

/**
* Gets text between two lines.
*
Expand All @@ -98,7 +177,7 @@ public String getText(int from, int to, boolean complete) {
}

/**
* Gets start- and end-line number of current caret position.
* Gets start- and end-line number of statement(s) at caret position.
*
* @param singleStatement <code>true</code> to get only one statement
* @return pair of start and end line number
Expand Down

0 comments on commit c537aaa

Please sign in to comment.