Skip to content

Commit

Permalink
Align onEnterRule#indent JSON loading with vscode specification
Browse files Browse the repository at this point in the history
  • Loading branch information
angelozerr committed Sep 7, 2022
1 parent 972061a commit 40893cb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ public enum IndentAction {
* indentation).
*/
Outdent;

public static IndentAction get(@Nullable String value) {
// see
// https://github.com/microsoft/vscode/blob/13ba7bb446a638d37ebccb1a7d74e31c32bb9790/src/vs/workbench/contrib/codeEditor/browser/languageConfigurationExtensionPoint.ts#L341
if (value == null) {
return IndentAction.None;
}
switch (value) {
case "none":
return IndentAction.None;
case "indent":
return IndentAction.Indent;
case "indentOutdent":
return IndentAction.IndentOutdent;
case "outdent":
return IndentAction.Outdent;
default:
return IndentAction.None;
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

/**
* The language configuration interface defines the contract between extensions and various editor features, like
Expand Down Expand Up @@ -62,10 +63,10 @@ public static LanguageConfiguration load(@NonNull final Reader reader) {
final var actionElem = jsonObj.get("action"); //$NON-NLS-1$
if (actionElem != null && actionElem.isJsonObject()) {
final var actionJsonObj = actionElem.getAsJsonObject();
final var indentActionString = getAsString(actionJsonObj.get("indentAction")); //$NON-NLS-1$
final var indentActionString = getAsString(actionJsonObj.get("indent")); //$NON-NLS-1$
if (indentActionString != null) {
final var afterText = getAsPattern(jsonObj.get("afterText")); //$NON-NLS-1$
final var indentAction = IndentAction.valueOf(indentActionString);
final var indentAction = IndentAction.get(indentActionString);
final var removeText = getAsInteger(actionJsonObj.get("removeText")); //$NON-NLS-1$
final var appendText = getAsString(actionJsonObj.get("appendText")); //$NON-NLS-1$
final var action = new EnterAction(indentAction);
Expand Down Expand Up @@ -205,11 +206,29 @@ public static LanguageConfiguration load(@NonNull final Reader reader) {
}

@Nullable
private static Pattern getAsPattern(@Nullable final JsonElement element) {
final var pattern = getAsString(element);
private static Pattern getAsPattern(@Nullable final JsonElement element) {
final var pattern = getPattern(element);
return pattern == null ? null : RegExpUtils.create(pattern);
}

@Nullable
private static String getPattern(@Nullable final JsonElement element) {
if (element == null) {
return null;
}
if (element.isJsonObject()) {
// ex : { "pattern": "^<\\/([_:\\w][_:\\w-.\\d]*)\\s*>", "flags": "i" }
var pattern = getAsString(((JsonObject) element).get("pattern"));
if (pattern == null) {
return null;
}
var flags = getAsString(((JsonObject) element).get("flags"));
return flags != null ? pattern + "(?" + flags + ")" : pattern;
}
// ex : "^<\\/([_:\\w][_:\\w-.\\d]*)\\s*>"
return getAsString(element);
}

@Nullable
private static String getAsString(@Nullable final JsonElement element) {
if (element == null) {
Expand Down

0 comments on commit 40893cb

Please sign in to comment.