Skip to content

Commit

Permalink
[SQLLINE-432] Highlight decimal numbers when highlighting is on
Browse files Browse the repository at this point in the history
  • Loading branch information
snuyanzin committed May 13, 2021
1 parent 281b240 commit fc48250
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
38 changes: 35 additions & 3 deletions src/main/java/sqlline/SqlLineHighlighter.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ void handleSqlSyntax(String buffer,
wordStart = pos;
continue;
}
if (wordStart == -1 && Character.isDigit(ch)
if (wordStart == -1
&& (Character.isDigit(ch) || ch == '.' || ch == 'e' || ch == 'E')
&& (pos == 0
|| (buffer.length() > pos - 1
&& !Character.isLetterOrDigit(buffer.charAt(pos - 1))
Expand Down Expand Up @@ -426,8 +427,39 @@ int handleSqlIdentifierQuotes(String line, String openSqlIdentifier,
* @return position where number finished
*/
int handleNumbers(String line, BitSet numberBitSet, int startingPoint) {
int end = startingPoint + 1;
while (end < line.length() && Character.isDigit(line.charAt(end))) {
int end = startingPoint;
int dotCount = 0;
int eCount = 0;
char c;
while (end < line.length()
&& (Character.isDigit(line.charAt(end))
|| line.charAt(end) == '.'
|| line.charAt(end) == 'e'
|| line.charAt(end) == 'E')) {
c = line.charAt(end);
switch (c) {
case '.':
if (dotCount == 1 || eCount > 0) {
return startingPoint;
}
dotCount++;
break;
case 'e':
case 'E':
if (eCount == 1
|| end == startingPoint
|| end >= line.length() - 2) {
return startingPoint;
}
char sign = line.charAt(end + 1);
if (sign != '-' && sign != '+'
|| !Character.isDigit(line.charAt(end + 2))) {
return startingPoint;
}
eCount++;
end++;
break;
}
end++;
}
if (end == line.length()) {
Expand Down
8 changes: 7 additions & 1 deletion src/test/java/sqlline/SqlLineHighlighterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,13 @@ public void testNumberStrings() {
String[] linesRequiredToBeNumbers = {
"123456789",
"0123",
"1"
"1",
"2.1",
".1",
".2341",
"1e+1",
"1e-12",
"1.2E+12"
};

for (String line : linesRequiredToBeNumbers) {
Expand Down

0 comments on commit fc48250

Please sign in to comment.