Added XML syntax highlighting
This commit is contained in:
parent
7f18e0f6ae
commit
6316c1d5f9
6 changed files with 113 additions and 24 deletions
|
@ -87,7 +87,7 @@ public class BodyTabController implements Initializable {
|
|||
mode = HighlightMode.HTML;
|
||||
break;
|
||||
default:
|
||||
mode = HighlightMode.NONE;
|
||||
mode = HighlightMode.PLAIN;
|
||||
}
|
||||
rawInputArea.setMode(mode);
|
||||
});
|
||||
|
@ -217,7 +217,7 @@ public class BodyTabController implements Initializable {
|
|||
mode = HighlightMode.HTML;
|
||||
break;
|
||||
default:
|
||||
mode = HighlightMode.NONE;
|
||||
mode = HighlightMode.PLAIN;
|
||||
}
|
||||
|
||||
rawInputArea.setText(dashboardState.getBody(), mode);
|
||||
|
|
|
@ -400,15 +400,15 @@ public class DashboardController implements Initializable {
|
|||
break;
|
||||
case "text/html":
|
||||
responseType.setText("HTML");
|
||||
responseArea.setText(responseBody, HighlightMode.NONE);
|
||||
responseArea.setText(responseBody, HighlightMode.HTML);
|
||||
break;
|
||||
default:
|
||||
responseType.setText("PLAIN TEXT");
|
||||
responseArea.setText(responseBody, HighlightMode.NONE);
|
||||
responseArea.setText(responseBody, HighlightMode.PLAIN);
|
||||
}
|
||||
} else {
|
||||
responseType.setText("NONE");
|
||||
responseArea.setText("No body found in the response.", HighlightMode.NONE);
|
||||
responseType.setText("PLAIN");
|
||||
responseArea.setText("No body found in the response.", HighlightMode.PLAIN);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
snackbar.show("Response could not be parsed.", 5000);
|
||||
|
|
|
@ -2,26 +2,32 @@ package com.rohitawate.everest.controllers.codearea;
|
|||
|
||||
import com.rohitawate.everest.controllers.codearea.highlighters.Highlighter;
|
||||
import com.rohitawate.everest.controllers.codearea.highlighters.JSONHighlighter;
|
||||
import com.rohitawate.everest.controllers.codearea.highlighters.XMLHighlighter;
|
||||
import com.rohitawate.everest.util.settings.Settings;
|
||||
import javafx.geometry.Insets;
|
||||
import org.fxmisc.richtext.CodeArea;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
public class EverestCodeArea extends CodeArea {
|
||||
public enum HighlightMode {
|
||||
JSON, XML, HTML, NONE
|
||||
JSON, XML, HTML, PLAIN
|
||||
}
|
||||
|
||||
private Highlighter highlighter;
|
||||
private JSONHighlighter jsonHighlighter;
|
||||
private XMLHighlighter xmlHighlighter;
|
||||
|
||||
public EverestCodeArea() {
|
||||
this.getStylesheets().add(getClass().getResource("/css/syntax/Moondust.css").toString());
|
||||
this.getStyleClass().add("everest-code-area");
|
||||
this.setWrapText(Settings.editorWrapText);
|
||||
this.setPadding(new Insets(5));
|
||||
|
||||
jsonHighlighter = new JSONHighlighter();
|
||||
setMode(HighlightMode.NONE);
|
||||
xmlHighlighter = new XMLHighlighter();
|
||||
|
||||
setMode(HighlightMode.PLAIN);
|
||||
|
||||
this.multiPlainChanges()
|
||||
.successionEnds(Duration.ofMillis(1))
|
||||
|
@ -29,7 +35,17 @@ public class EverestCodeArea extends CodeArea {
|
|||
}
|
||||
|
||||
public void setMode(HighlightMode mode) {
|
||||
highlighter = mode == HighlightMode.JSON ? jsonHighlighter : jsonHighlighter;
|
||||
switch (mode) {
|
||||
case JSON:
|
||||
highlighter = jsonHighlighter;
|
||||
break;
|
||||
default:
|
||||
highlighter = xmlHighlighter;
|
||||
break;
|
||||
}
|
||||
|
||||
// Re-computes the highlighting for the new mode
|
||||
this.setStyleSpans(0, highlighter.computeHighlighting(getText()));
|
||||
}
|
||||
|
||||
public void setText(String text, HighlightMode mode) {
|
||||
|
|
|
@ -0,0 +1,68 @@
|
|||
package com.rohitawate.everest.controllers.codearea.highlighters;
|
||||
|
||||
import org.fxmisc.richtext.model.StyleSpans;
|
||||
import org.fxmisc.richtext.model.StyleSpansBuilder;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class XMLHighlighter implements Highlighter {
|
||||
|
||||
private static final Pattern XML_TAG = Pattern.compile("(?<ELEMENT>(</?\\h*)(\\w+)([^<>]*)(\\h*/?>))"
|
||||
+ "|(?<COMMENT><!--[^<>]+-->)");
|
||||
|
||||
private static final Pattern ATTRIBUTES = Pattern.compile("(\\w+\\h*)(=)(\\h*\"[^\"]+\")");
|
||||
|
||||
private static final int GROUP_OPEN_BRACKET = 2;
|
||||
private static final int GROUP_ELEMENT_NAME = 3;
|
||||
private static final int GROUP_ATTRIBUTES_SECTION = 4;
|
||||
private static final int GROUP_CLOSE_BRACKET = 5;
|
||||
private static final int GROUP_ATTRIBUTE_NAME = 1;
|
||||
private static final int GROUP_EQUAL_SYMBOL = 2;
|
||||
private static final int GROUP_ATTRIBUTE_VALUE = 3;
|
||||
|
||||
@Override
|
||||
public StyleSpans<Collection<String>> computeHighlighting(String text) {
|
||||
Matcher matcher = XML_TAG.matcher(text);
|
||||
int lastKwEnd = 0;
|
||||
StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
|
||||
while (matcher.find()) {
|
||||
|
||||
spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
|
||||
if (matcher.group("COMMENT") != null) {
|
||||
spansBuilder.add(Collections.singleton("xml_comment"), matcher.end() - matcher.start());
|
||||
} else {
|
||||
if (matcher.group("ELEMENT") != null) {
|
||||
String attributesText = matcher.group(GROUP_ATTRIBUTES_SECTION);
|
||||
|
||||
spansBuilder.add(Collections.singleton("xml_bracket"), matcher.end(GROUP_OPEN_BRACKET) - matcher.start(GROUP_OPEN_BRACKET));
|
||||
spansBuilder.add(Collections.singleton("xml_tag"), matcher.end(GROUP_ELEMENT_NAME) - matcher.end(GROUP_OPEN_BRACKET));
|
||||
|
||||
if (!attributesText.isEmpty()) {
|
||||
|
||||
lastKwEnd = 0;
|
||||
|
||||
Matcher amatcher = ATTRIBUTES.matcher(attributesText);
|
||||
while (amatcher.find()) {
|
||||
spansBuilder.add(Collections.emptyList(), amatcher.start() - lastKwEnd);
|
||||
spansBuilder.add(Collections.singleton("xml_attribute"), amatcher.end(GROUP_ATTRIBUTE_NAME) - amatcher.start(GROUP_ATTRIBUTE_NAME));
|
||||
spansBuilder.add(Collections.singleton("xml_bracket"), amatcher.end(GROUP_EQUAL_SYMBOL) - amatcher.end(GROUP_ATTRIBUTE_NAME));
|
||||
spansBuilder.add(Collections.singleton("xml_attribute_value"), amatcher.end(GROUP_ATTRIBUTE_VALUE) - amatcher.end(GROUP_EQUAL_SYMBOL));
|
||||
lastKwEnd = amatcher.end();
|
||||
}
|
||||
if (attributesText.length() > lastKwEnd)
|
||||
spansBuilder.add(Collections.emptyList(), attributesText.length() - lastKwEnd);
|
||||
}
|
||||
|
||||
lastKwEnd = matcher.end(GROUP_ATTRIBUTES_SECTION);
|
||||
spansBuilder.add(Collections.singleton("xml_bracket"), matcher.end(GROUP_CLOSE_BRACKET) - lastKwEnd);
|
||||
}
|
||||
}
|
||||
lastKwEnd = matcher.end();
|
||||
}
|
||||
spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd);
|
||||
return spansBuilder.create();
|
||||
}
|
||||
}
|
|
@ -79,20 +79,6 @@
|
|||
-fx-background-color: #822f2f;
|
||||
}
|
||||
|
||||
.everest-code-area {
|
||||
-fx-font-family: "Liberation Mono", monospace;
|
||||
-fx-text-fill: #a1a1a1;
|
||||
-fx-background-radius: 0;
|
||||
-fx-background-insets: 0;
|
||||
-fx-background-color: transparent;
|
||||
-fx-border-width: 0px;
|
||||
}
|
||||
|
||||
.everest-code-area .content {
|
||||
-fx-background-radius: 0;
|
||||
-fx-background-color: #2a2a2a;
|
||||
}
|
||||
|
||||
#loadingLayer {
|
||||
-fx-background-color: #bf8829;
|
||||
}
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
/*General rules for EverestCodeArea*/
|
||||
|
||||
.everest-code-area {
|
||||
-fx-background-color: #282828;
|
||||
}
|
||||
|
||||
.everest-code-area .text {
|
||||
-fx-fill: azure;
|
||||
-fx-fill: #ababab;
|
||||
-fx-font-family: "Liberation Mono", monospace;
|
||||
-fx-font-size: 17px;
|
||||
}
|
||||
|
@ -12,19 +14,26 @@
|
|||
-fx-stroke: white;
|
||||
}
|
||||
|
||||
/*Common to JSON and XML*/
|
||||
|
||||
.xml_bracket,
|
||||
.json_curly {
|
||||
-fx-fill: coral !important;
|
||||
}
|
||||
|
||||
.xml_tag,
|
||||
.json_property {
|
||||
-fx-fill: orange !important;
|
||||
-fx-font-weight: bold;
|
||||
}
|
||||
|
||||
.xml_attribute_value,
|
||||
.json_value {
|
||||
-fx-fill: cornflowerblue !important;
|
||||
}
|
||||
|
||||
/*JSON-specific*/
|
||||
|
||||
.json_array {
|
||||
-fx-fill: limegreen !important;
|
||||
}
|
||||
|
@ -38,3 +47,13 @@
|
|||
-fx-fill: greenyellow !important;
|
||||
-fx-font-weight: bold;
|
||||
}
|
||||
|
||||
/*XML-specific*/
|
||||
|
||||
.xml_attribute {
|
||||
-fx-fill: darkviolet !important;
|
||||
}
|
||||
|
||||
.xml_comment {
|
||||
-fx-fill: teal !important;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue