Skip to content

Commit 36dc6e6

Browse files
committed
sql keywords highlighting done
1 parent 3ac5b31 commit 36dc6e6

File tree

5 files changed

+84
-4
lines changed

5 files changed

+84
-4
lines changed

.idea/inspectionProfiles/Project_Default.xml

+9-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/edu/upvictoria/javasqlide/App.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package edu.upvictoria.javasqlide;
22

3-
import edu.upvictoria.javasqlide.controllers.IDEController;
43
import javafx.application.Application;
54
import javafx.fxml.FXMLLoader;
65
import javafx.scene.Scene;
76
import javafx.stage.Stage;
87

8+
import java.util.Objects;
9+
910
public class App extends Application {
1011
@Override
1112
public void start(Stage stage) {
@@ -14,6 +15,7 @@ public void start(Stage stage) {
1415

1516
Scene scene = new Scene(fxmlLoader.load(), 1280, 800);
1617
stage.setTitle("Java SQL Developer");
18+
scene.getStylesheets().add(Objects.requireNonNull(App.class.getResource("/edu/upvictoria/javasqlide/sql-keywords.css")).toExternalForm());
1719
stage.setScene(scene);
1820
stage.show();
1921
} catch (Exception e){

src/main/java/edu/upvictoria/javasqlide/controllers/IDEController.java

+41-2
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@
1515
import javafx.stage.FileChooser;
1616
import org.fxmisc.richtext.CodeArea;
1717
import org.fxmisc.richtext.LineNumberFactory;
18+
import org.fxmisc.richtext.model.StyleSpans;
19+
import org.fxmisc.richtext.model.StyleSpansBuilder;
1820

1921
import java.io.BufferedReader;
2022
import java.io.File;
2123
import java.io.FileReader;
2224
import java.nio.charset.Charset;
2325
import java.nio.charset.StandardCharsets;
2426
import java.util.ArrayList;
27+
import java.util.Collection;
28+
import java.util.Collections;
2529
import java.util.Objects;
30+
import java.util.regex.Matcher;
2631
import java.util.regex.Pattern;
2732

2833
public class IDEController {
@@ -42,15 +47,17 @@ public class IDEController {
4247
private static final String CONSTRAINTS_PATTERN = "\\b(" + String.join("|", Analyzer.getConstraints()) + ")\\b";
4348
private static final String FUNCTIONS_PATTERN = "\\b(" + String.join("|", Analyzer.getFunctions()) + ")\\b";
4449
private static final String PAREN_PATTERN = "\\(|\\)";
50+
private static final String COMPARATORS_PATTERN = "\\b(" + String.join("|", Analyzer.getComparators()) + ")\\b";
4551
private static final String SEMICOLON_PATTERN = "\\;";
46-
private static final String STRING_PATTERN = "\"([^'\\\\]|\\\\.)*'";
52+
private static final String STRING_PATTERN = "'([^'\\\\]|\\\\.)*'";
4753

4854
private static final Pattern PATTERN = Pattern.compile(
4955
"(?<KEYWORD>" + KEYWORD_PATTERN + ")"
56+
+ "|(?<PAREN>" + PAREN_PATTERN + ")"
5057
+ "|(?<DATATYPE>" + DATATYPES_PATTERN + ")"
5158
+ "|(?<CONSTRAINT>" + CONSTRAINTS_PATTERN + ")"
5259
+ "|(?<FUNCTION>" + FUNCTIONS_PATTERN + ")"
53-
+ "|(?<PAREN>" + PAREN_PATTERN + ")"
60+
+ "|(?<COMPARATOR>" + COMPARATORS_PATTERN + ")"
5461
+ "|(?<SEMICOLON>" + SEMICOLON_PATTERN + ")"
5562
+ "|(?<STRING>" + STRING_PATTERN + ")"
5663
);
@@ -67,6 +74,12 @@ protected void initialize() {
6774
}
6875
});
6976

77+
codeArea.richChanges()
78+
.filter(ch -> !ch.getInserted().equals(ch.getRemoved()))
79+
.subscribe(change -> {
80+
codeArea.setStyleSpans(0, computeHighlighting(codeArea.getText()));
81+
});
82+
codeArea.getStyleClass().add("area");
7083
}
7184

7285
@FXML
@@ -163,6 +176,32 @@ protected void executeSelectedStatement(ActionEvent event) {
163176
}
164177
}
165178

179+
private StyleSpans<Collection<String>> computeHighlighting(String text) {
180+
text = text.toUpperCase();
181+
Matcher matcher = PATTERN.matcher(text);
182+
int lastKwEnd = 0;
183+
StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
184+
185+
while(matcher.find()) {
186+
String styleClass =
187+
matcher.group("KEYWORD") != null ? "keyword" :
188+
matcher.group("DATATYPE") != null ? "datatype" :
189+
matcher.group("CONSTRAINT") != null ? "constraint" :
190+
matcher.group("FUNCTION") != null ? "function" :
191+
matcher.group("COMPARATOR") != null ? "comparator" :
192+
matcher.group("PAREN") != null ? "paren" :
193+
matcher.group("SEMICOLON") != null ? "semicolon" :
194+
matcher.group("STRING") != null ? "string" :
195+
null; /* never happens */ assert styleClass != null;
196+
spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
197+
spansBuilder.add(Collections.singleton(styleClass), matcher.end() - matcher.start());
198+
lastKwEnd = matcher.end();
199+
}
200+
spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd);
201+
return spansBuilder.create();
202+
}
203+
204+
166205
/**
167206
* Aquí mando toda la basura de mensajes de excepciones para que se impriman en el tab de logs
168207
* @param errorMessage

src/main/java/module-info.java

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
requires ExceptionFramework;
66
requires java.sql;
77
requires org.fxmisc.richtext;
8+
requires reactfx;
89

910
opens edu.upvictoria.javasqlide.controllers to javafx.fxml;
1011
exports edu.upvictoria.javasqlide;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
.keyword {
2+
-fx-fill: purple;
3+
-fx-font-weight: bold;
4+
}
5+
.semicolon {
6+
-fx-font-weight: bold;
7+
}
8+
.comparator {
9+
-fx-fill: firebrick;
10+
-fx-font-weight: bold;
11+
}
12+
.paren {
13+
-fx-fill: firebrick;
14+
-fx-font-weight: bold;
15+
}
16+
.datatype {
17+
-fx-fill: darkgreen;
18+
-fx-font-weight: bold;
19+
}
20+
.constraint {
21+
-fx-fill: teal;
22+
-fx-font-weight: bold;
23+
}
24+
.string {
25+
-fx-fill: blue;
26+
}
27+
28+
.function {
29+
-fx-fill: cadetblue;
30+
}

0 commit comments

Comments
 (0)