Describe the bug
Effected Code:
|
class DefaultSyntaxHighlighter( |
|
override val mode: LanguageMode, |
|
private val provider: LanguageModeProvider, |
|
) : SyntaxHighlighter { |
|
|
|
override fun highlight(text: String): AnnotatedString { |
|
if (mode == LanguageMode.PLAIN_TEXT) return AnnotatedString(text) |
|
return try { |
|
val doc = EditorDocument.create(text) |
|
buildAnnotatedString { |
|
append(text) |
|
var tokenState: Any? = null |
|
var charOffset = 0 |
|
for (lineNum in 1..doc.lineCount) { |
|
val lineText = doc.lineText(lineNum) |
|
val (tokens, newState) = provider.tokenizeLine(lineText, lineNum, tokenState) |
|
tokenState = newState |
|
for (token in tokens) { |
|
val start = charOffset + token.startOffset |
|
val end = charOffset + token.endOffset |
|
if (start < end && end <= text.length) { |
|
val color = TokenColorRegistry.colorFor(mode, token.type) |
|
val style = if (token.type == TokenType.KEYWORD) |
|
SpanStyle(color = color, fontWeight = FontWeight.SemiBold) |
|
else |
|
SpanStyle(color = color) |
|
addStyle(style, start, end.coerceAtMost(text.length)) |
|
} |
|
} |
|
charOffset += lineText.length + if (lineNum < doc.lineCount) 1 else 0 |
|
} |
|
} |
|
} catch (_: Throwable) { |
|
AnnotatedString(text) |
|
} |
|
} |
|
} |
- As there is not check inside the class that the
mode and provider should match with each other, anyone could mistakenly or intentionally provide mismatched LanguageMode and LanguageModeProvider which might wrongly show Ui or even crash.
Expected behavior
- Class should reject objects with mismatched
LanguageMode and LanguageModeProvider.
Environment (please complete the following information):
- OS: Windows 11
- Java: 11
- App version / commit SHA: 1.16.0
- Module/area:
editor-ui
- Run mode: None
Describe the bug
Effected Code:
req-lab/editor-ui/src/commonMain/kotlin/com/reqlab/editor/ui/SyntaxHighlighter.kt
Lines 55 to 91 in 99a261b
modeandprovidershould match with each other, anyone could mistakenly or intentionally provide mismatchedLanguageModeandLanguageModeProviderwhich might wrongly show Ui or even crash.Expected behavior
LanguageModeandLanguageModeProvider.Environment (please complete the following information):
editor-ui