Skip to content

Commit

Permalink
refact: model FoldingRules class closer to upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Feb 14, 2024
1 parent 25e7fbe commit 38bd03e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,29 @@
*/
public final class FoldingRules {

/**
* Describes language specific folding markers such as '#region' and '#endregion'.
* The start and end regexes will be tested against the contents of all lines and must be designed efficiently:
* - the regex should start with '^'
* - regexp flags (i, g) are ignored
*/
public final class FoldingMarkers {
public final RegExPattern start;
public final RegExPattern end;

public FoldingMarkers(RegExPattern start, RegExPattern end) {
this.start = start;
this.end = end;
}

@Override
public String toString() {
return StringUtils.toString(this, sb -> sb
.append("start=").append(start).append(", ")
.append("end=").append(end));
}
}

/**
* Used by the indentation based strategy to decide whether empty lines belong to the previous or the next block.
* A language adheres to the off-side rule if blocks in that language are expressed by their indentation.
Expand All @@ -30,20 +53,17 @@ public final class FoldingRules {
* If not set, `false` is used and empty lines belong to the previous block.
*/
public final boolean offSide;
public final RegExPattern markersStart;
public final RegExPattern markersEnd;
public final FoldingMarkers markers;

public FoldingRules(final boolean offSide, final RegExPattern markersStart, final RegExPattern markersEnd) {
this.offSide = offSide;
this.markersStart = markersStart;
this.markersEnd = markersEnd;
this.markers = new FoldingMarkers(markersStart, markersEnd);
}

@Override
public String toString() {
return StringUtils.toString(this, sb -> sb
.append("markersStart=").append(markersStart).append(", ")
.append("markersEnd=").append(markersEnd).append(", ")
.append("markers=").append(", ")
.append("offSide=").append(offSide));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ private static String removeTrailingCommas(final String jsonString) {
if (element.isJsonObject()) {
// ex : { "pattern": "^<\\/([_:\\w][_:\\w-.\\d]*)\\s*>", "flags": "i" }
final var pattern = getAsString(((JsonObject) element).get("pattern"));
if (pattern == null) {
if (pattern == null || pattern.isEmpty()) {
return null;
}
final var flags = getAsString(((JsonObject) element).get("flags"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ public void refresh(final @Nullable LanguageConfiguration configuration) {
final FoldingRules folding = configuration.getFolding();
if (folding != null) {
foldingOffsideText.setText(Boolean.toString(folding.offSide));
foldingMarkersStartText.setText(folding.markersStart.pattern());
foldingMarkersEndText.setText(folding.markersEnd.pattern());
foldingMarkersStartText.setText(folding.markers.start.pattern());
foldingMarkersEndText.setText(folding.markers.end.pattern());
}

final String wordPattern = configuration.getWordPattern();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void testOniguramaFallback() throws Exception {
"folding": {
"markers": {
"start": "{%\\\\s*(block|filter|for|if|macro|raw)",
"end": ""
"end": "{%\\\\s*end(block|filter|for|if|macro|raw)\\\\s*%}"
}
}
}"""));
Expand All @@ -59,7 +59,7 @@ void testOniguramaFallback() throws Exception {

final var folding = languageConfiguration.getFolding();
assert folding != null;
assertTrue(folding.markersStart.getClass().getSimpleName().endsWith("OnigRegExPattern"));
assertTrue(folding.markers.start.getClass().getSimpleName().endsWith("OnigRegExPattern"));
}

@Test
Expand Down

0 comments on commit 38bd03e

Please sign in to comment.