01: package net.sourceforge.squirrel_sql.fw.completion.util;
02:
03: class CompletionUtils {
04: private static final char[] SEPARATORS = { ' ', '\t', '\n', ',',
05: '(', '\'', '"', '=', '>', '<' };
06:
07: static String getStringToParse(String textTillCaret) {
08:
09: int lastIndexOfLineFeed = textTillCaret.lastIndexOf('\n');
10: String lineTillCaret;
11:
12: if (-1 == lastIndexOfLineFeed) {
13: lineTillCaret = textTillCaret;
14: } else {
15: lineTillCaret = textTillCaret
16: .substring(lastIndexOfLineFeed);
17: }
18:
19: String beginning = "";
20: if (0 != lineTillCaret.trim().length()
21: && !Character.isWhitespace(lineTillCaret
22: .charAt(lineTillCaret.length() - 1))) {
23: String trimmedLineTillCaret = lineTillCaret.trim();
24:
25: int lastSeparatorIndex = getLastSeparatorIndex(trimmedLineTillCaret);
26: if (-1 == lastSeparatorIndex) {
27: beginning = trimmedLineTillCaret;
28: } else {
29: beginning = trimmedLineTillCaret.substring(
30: lastSeparatorIndex + 1, trimmedLineTillCaret
31: .length());
32: }
33: }
34:
35: return beginning;
36: }
37:
38: static int getLastSeparatorIndex(String str) {
39: int lastSeparatorIndex = -1;
40: for (char separator : SEPARATORS) {
41: int buf = str.lastIndexOf(separator);
42: if (buf > lastSeparatorIndex) {
43: lastSeparatorIndex = buf;
44: }
45: }
46: return lastSeparatorIndex;
47: }
48:
49: static int getStringToParsePosition(String textTillCaret) {
50:
51: int lastIndexOfLineFeed = textTillCaret.lastIndexOf('\n');
52: String lineTillCaret;
53:
54: if (-1 == lastIndexOfLineFeed) {
55: lineTillCaret = textTillCaret;
56: } else {
57: lineTillCaret = textTillCaret
58: .substring(lastIndexOfLineFeed);
59: }
60:
61: int pos = lastIndexOfLineFeed + 1;
62: if (0 != lineTillCaret.length()
63: && !Character.isWhitespace(lineTillCaret
64: .charAt(lineTillCaret.length() - 1))) {
65: int lastSeparatorIndex = getLastSeparatorIndex(lineTillCaret);
66: if (-1 != lastSeparatorIndex) {
67: pos += lastSeparatorIndex;
68: }
69: }
70:
71: return pos;
72: }
73: }
|