-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support multiline tokens by considering source code string when encoding
- Loading branch information
Martin Azpillaga Aldalur
committed
Jan 2, 2025
1 parent
16825ff
commit 3e83718
Showing
2 changed files
with
19 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 17 additions & 12 deletions
29
...src/main/kotlin/com/strumenta/kolasu/languageserver/semanticHighlighting/SemanticToken.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,29 @@ | ||
package com.strumenta.kolasu.languageserver.semanticHighlighting | ||
|
||
import com.strumenta.kolasu.model.Point | ||
import com.strumenta.kolasu.model.Position | ||
import org.eclipse.lsp4j.SemanticTokens | ||
|
||
data class SemanticToken(val position: Position, val type: SemanticTokenType, val modifiers: List<SemanticTokenModifier>) | ||
|
||
fun encode(tokens: List<SemanticToken>): SemanticTokens { | ||
var lastLine = 1 // ? To offset that Kolasu lines start at 1 | ||
var lastColumn = 0 | ||
fun encode(tokens: List<SemanticToken>, code: String): SemanticTokens { | ||
// Create synthetic token at start to compute relative positions from | ||
var lastToken = SemanticToken(Position(Point(1, 0), Point(1, 0)), SemanticTokenType.TYPE, listOf()) | ||
|
||
val data = mutableListOf<Int>() | ||
for (token in tokens) { | ||
data.addAll(listOf( | ||
token.position.start.line - lastLine, | ||
if (token.position.start.line == lastLine) token.position.start.column - lastColumn else token.position.start.column, | ||
token.position.end.column - token.position.start.column, // ! assumes tokens are in a single line | ||
token.type.ordinal, | ||
token.modifiers.sumOf { it.bit } | ||
)) | ||
lastLine = token.position.start.line | ||
lastColumn = token.position.start.column | ||
val deltaLine = token.position.start.line - (lastToken.position.start.line) | ||
val deltaStart = if (token.position.start.line == lastToken.position.start.line) { | ||
token.position.start.column - lastToken.position.start.column | ||
} else { | ||
token.position.start.column | ||
} | ||
val length = token.position.length(code) | ||
val tokenType = token.type.ordinal | ||
val tokenModifiers = token.modifiers.sumOf { it.bit } | ||
|
||
data.addAll(listOf(deltaLine, deltaStart, length, tokenType, tokenModifiers)) | ||
lastToken = token | ||
} | ||
return SemanticTokens(data) | ||
} |