Skip to content

Extend custom mode API with start/end and keyword mapping #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/de/f0rce/ace/enums/AceCustomModeTokens.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public enum AceCustomModeTokens {
/** the name of an attribute (mainly in tags) */
ENTITY_OTHER_ATTRIBUTE_NAME("entity.other.attribute-name"),

IDENTIFIER("identifier"),

/** stuff which is "invalid" */
INVALID("invalid"),

Expand Down
32 changes: 32 additions & 0 deletions src/main/java/de/f0rce/ace/util/AceCustomModeRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;
import de.f0rce.ace.enums.AceCustomModeTokens;

Expand All @@ -19,6 +20,8 @@ public class AceCustomModeRule {

private Object token;
private String regex;
private String start;
private String end;
private String next;
private String defaultToken;

Expand Down Expand Up @@ -49,6 +52,15 @@ public void setTokens(AceCustomModeTokens... tokens) {
.collect(Collectors.toCollection(ArrayList<String>::new));
}

public void setKeywordMapper(Map<AceCustomModeTokens, String> map, AceCustomModeTokens defaultToken, boolean ignoreCase, String splitChar) {
this.token = Map.of(
"map", map.entrySet().stream().collect(Collectors.toMap(e -> e.getKey().getToken(), Map.Entry::getValue)),
"defaultToken", defaultToken.getToken(),
"ignoreCase", ignoreCase,
"splitChar", splitChar
);
}

/** @return {@link ArrayList} */
public ArrayList<AceCustomModeTokens> getTokens() {
if (this.token instanceof ArrayList<?>) {
Expand Down Expand Up @@ -76,6 +88,26 @@ public String getRegex() {
return this.regex;
}

/** @param start {@link String} */
public void setStart(String start) {
this.start = start;
}

/** @return {@link String} */
public String getStart() {
return this.start;
}

/** @param end {@link String} */
public void setEnd(String end) {
this.end = end;
}

/** @return {@link String} */
public String getEnd() {
return this.end;
}

/** @param next {@link String} */
public void setNext(String next) {
this.next = next;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,23 @@ class LitAce extends LitElement {
var customModeFunction = function () {
this.$rules = parsed.states;

// Convert objects to keyword mappers
for (var key in this.$rules) {
var state = this.$rules[key];
for (var i = 0; i < state.length; i++) {
var rule = state[i];
let token = rule.token;
if (typeof token === "object") {
rule.token = this.createKeywordMapper(
token.map,
token.defaultToken,
token.ignoreCase,
token.splitChar
)
}
}
}

this.normalizeRules();
}

Expand Down
45 changes: 42 additions & 3 deletions src/test/java/de/f0rce/View.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package de.f0rce;

import com.vaadin.flow.component.Key;
import com.vaadin.flow.component.Shortcuts;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.checkbox.Checkbox;
import com.vaadin.flow.component.combobox.ComboBox;
Expand All @@ -16,12 +14,17 @@
import com.vaadin.flow.theme.Theme;
import com.vaadin.flow.theme.lumo.Lumo;
import de.f0rce.ace.AceEditor;
import de.f0rce.ace.enums.AceCustomModeTokens;
import de.f0rce.ace.enums.AceExportType;
import de.f0rce.ace.enums.AceMode;
import de.f0rce.ace.enums.AceTheme;
import de.f0rce.ace.util.AceCustomMode;
import de.f0rce.ace.util.AceCustomModeRule;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Theme(themeClass = Lumo.class, variant = Lumo.DARK)
@Route("")
Expand Down Expand Up @@ -126,6 +129,37 @@ public View() {
// aceEditor.addCustomAutocompletion(l3, "food", true);
aceEditor.addDynamicAutocompletion(map, ".", "not dynmaic", true);

AceCustomMode customMode = new AceCustomMode();

AceCustomModeRule keywords = new AceCustomModeRule();
keywords.setRegex("[a-zA-Z_$][a-zA-Z0-9_$]*\\b");
keywords.setKeywordMapper(
Map.of(
AceCustomModeTokens.KEYWORD, String.join("|", custom)
),
AceCustomModeTokens.IDENTIFIER,
true,
"|"
);

AceCustomModeRule lineComment = new AceCustomModeRule();
lineComment.setRegex("--.*$");
lineComment.setToken(AceCustomModeTokens.COMMENT);

AceCustomModeRule blockComment = new AceCustomModeRule();
blockComment.setStart("/\\*");
blockComment.setEnd("\\*/");
blockComment.setToken(AceCustomModeTokens.COMMENT);

customMode.addState(
"start",
lineComment,
blockComment,
keywords
);

aceEditor.addCustomMode("custom", customMode);

aceEditor.addFocusListener(
evt -> {
// aceEditor.openAutocompletion();
Expand Down Expand Up @@ -185,7 +219,12 @@ public View() {
modesComboBox.addValueChangeListener(
event -> {
if (event.getValue() != null) {
aceEditor.setMode(event.getValue());
AceMode mode = event.getValue();
if (mode == AceMode.custom) {
aceEditor.setCustomMode("custom");
} else {
aceEditor.setMode(mode);
}
}
});

Expand Down