Skip to content

Commit 81b2bf6

Browse files
committed
refactor: replace raw int priority with InjectionPriority enum
1 parent 1462492 commit 81b2bf6

6 files changed

Lines changed: 32 additions & 21 deletions

File tree

core/src/main/kotlin/dev/textmate/grammar/Grammar.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class Grammar(
7979
}
8080
}
8181

82-
result.sortBy { it.priority }
82+
result.sortBy { it.priority.value }
8383
return result
8484
}
8585

core/src/main/kotlin/dev/textmate/grammar/InjectionRule.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@ package dev.textmate.grammar
22

33
import dev.textmate.grammar.rule.RuleId
44

5+
/** Injection priority parsed from the selector prefix (`L:` / `R:`). */
6+
internal enum class InjectionPriority(val value: Int) {
7+
/** `L:` prefix — wins ties against normal rules at the same position. */
8+
HIGH(-1),
9+
/** No prefix — normal priority. */
10+
DEFAULT(0),
11+
/** `R:` prefix — loses ties against normal rules at the same position. */
12+
LOW(1)
13+
}
14+
515
/**
616
* A compiled injection rule: a scope selector matcher paired with a compiled rule and priority.
717
* Plain class (not data class) — function-typed [matcher] has identity-based equals/hashCode on JVM.
@@ -11,8 +21,8 @@ internal class InjectionRule(
1121
val debugSelector: String,
1222
/** Returns true when the current scope stack matches this injection's target. */
1323
val matcher: ScopeMatcher,
14-
/** Priority: -1 = L: (high, wins ties), 0 = default, 1 = R: (low). */
15-
val priority: Int,
24+
/** Injection priority parsed from selector prefix. */
25+
val priority: InjectionPriority,
1626
/** Rule compiled into the host grammar's rule space. */
1727
val ruleId: RuleId
1828
)

core/src/main/kotlin/dev/textmate/grammar/InjectionSelectorParser.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ internal typealias ScopeMatcher = (List<String>) -> Boolean
99
*/
1010
internal class MatcherWithPriority(
1111
val matcher: ScopeMatcher,
12-
val priority: Int // -1 = L: (high), 0 = default, 1 = R: (low)
12+
val priority: InjectionPriority
1313
)
1414

1515
/**
@@ -21,8 +21,8 @@ internal class MatcherWithPriority(
2121
* "text, string, comment" — OR: any of the three
2222
* "source.js comment" — AND: both must appear in order
2323
* "source.js -comment" — AND NOT
24-
* "L:comment" — high priority (priority = -1)
25-
* "R:comment" — low priority (priority = 1)
24+
* "L:comment" — high priority
25+
* "R:comment" — low priority
2626
*/
2727
internal object InjectionSelectorParser {
2828

@@ -47,13 +47,13 @@ internal object InjectionSelectorParser {
4747
fun parse(): List<MatcherWithPriority> {
4848
val results = mutableListOf<MatcherWithPriority>()
4949
while (token != null) {
50-
var priority = 0
50+
var priority = InjectionPriority.DEFAULT
5151
val t = token
5252
if (t != null && t.length == 2 && t[1] == ':') {
5353
priority = when (t[0]) {
54-
'L' -> -1
55-
'R' -> 1
56-
else -> 0
54+
'L' -> InjectionPriority.HIGH
55+
'R' -> InjectionPriority.LOW
56+
else -> InjectionPriority.DEFAULT
5757
}
5858
advance()
5959
}

core/src/main/kotlin/dev/textmate/grammar/tokenize/Tokenizer.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.textmate.grammar.tokenize
22

33
import dev.textmate.grammar.Grammar
4+
import dev.textmate.grammar.InjectionPriority
45
import dev.textmate.grammar.InjectionRule
56
import dev.textmate.grammar.rule.BeginEndRule
67
import dev.textmate.grammar.rule.BeginWhileRule
@@ -283,7 +284,7 @@ private fun matchInjections(
283284
): MatchInjectionsResult? {
284285
var bestMatchStart = Int.MAX_VALUE
285286
var bestResult: MatchRuleResult? = null
286-
var bestPriority = 0
287+
var bestPriority = InjectionPriority.DEFAULT
287288

288289
val scopes = stack.contentNameScopesList?.getScopeNames() ?: return null
289290

@@ -312,7 +313,7 @@ private fun matchInjections(
312313

313314
val result = bestResult ?: return null
314315
return MatchInjectionsResult(
315-
priorityMatch = bestPriority == -1,
316+
priorityMatch = bestPriority == InjectionPriority.HIGH,
316317
matchRuleResult = result
317318
)
318319
}

core/src/test/kotlin/dev/textmate/grammar/InjectionGrammarTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class InjectionGrammarTest {
4545

4646
assertEquals(1, injections.size)
4747
assertEquals("comment", injections[0].debugSelector)
48-
assertEquals(0, injections[0].priority)
48+
assertEquals(InjectionPriority.DEFAULT, injections[0].priority)
4949
}
5050

5151
@Test
@@ -72,7 +72,7 @@ class InjectionGrammarTest {
7272
}
7373

7474
@Test
75-
fun `getInjections L-priority injector has priority minus one`() {
75+
fun `getInjections L-priority injector has HIGH priority`() {
7676
val hostRaw = RawGrammar(scopeName = "source.test", patterns = emptyList())
7777
val injectorRaw = RawGrammar(
7878
scopeName = "text.injector",
@@ -81,7 +81,7 @@ class InjectionGrammarTest {
8181
)
8282
val grammar = createRegistry(hostRaw, injectorRaw).loadGrammar("source.test")
8383
?: error("Grammar 'source.test' not found")
84-
assertEquals(-1, grammar.getInjections()[0].priority)
84+
assertEquals(InjectionPriority.HIGH, grammar.getInjections()[0].priority)
8585
}
8686

8787
@Test

core/src/test/kotlin/dev/textmate/grammar/InjectionSelectorParserTest.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,23 @@ class InjectionSelectorParserTest {
6363
// --- Priority ---
6464

6565
@Test
66-
fun `L prefix gives priority minus one`() {
66+
fun `L prefix gives HIGH priority`() {
6767
val matchers = InjectionSelectorParser.createMatchers("L:comment")
6868
assertEquals(1, matchers.size)
69-
assertEquals(-1, matchers[0].priority)
69+
assertEquals(InjectionPriority.HIGH, matchers[0].priority)
7070
}
7171

7272
@Test
73-
fun `R prefix gives priority one`() {
73+
fun `R prefix gives LOW priority`() {
7474
val matchers = InjectionSelectorParser.createMatchers("R:comment")
7575
assertEquals(1, matchers.size)
76-
assertEquals(1, matchers[0].priority)
76+
assertEquals(InjectionPriority.LOW, matchers[0].priority)
7777
}
7878

7979
@Test
80-
fun `no prefix gives priority zero`() {
80+
fun `no prefix gives DEFAULT priority`() {
8181
val matchers = InjectionSelectorParser.createMatchers("comment")
82-
assertEquals(0, matchers[0].priority)
82+
assertEquals(InjectionPriority.DEFAULT, matchers[0].priority)
8383
}
8484

8585
// --- Edge cases ---

0 commit comments

Comments
 (0)