MCPcopy Create free account
hub / github.com/ajitpratap0/GoSQLX / encodeSemanticTokens

Function encodeSemanticTokens

pkg/lsp/handler.go:1724–1755  ·  view source on GitHub ↗

encodeSemanticTokens converts token positions to LSP semantic token encoding. Each token is encoded as 5 uint32 values: [deltaLine, deltaStartChar, length, tokenType, tokenModifiers]

(tokens []models.TokenWithSpan)

Source from the content-addressed store, hash-verified

1722//
1723// [deltaLine, deltaStartChar, length, tokenType, tokenModifiers]
1724func encodeSemanticTokens(tokens []models.TokenWithSpan) []uint32 {
1725 data := make([]uint32, 0, len(tokens)*5)
1726 prevLine := uint32(0)
1727 prevStartChar := uint32(0)
1728
1729 for _, tok := range tokens {
1730 tokenType := classifyToken(tok)
1731 if tokenType < 0 {
1732 continue // skip tokens we don't classify
1733 }
1734
1735 // Location is 1-based; convert to 0-based for LSP
1736 line := uint32(tok.Start.Line - 1)
1737 startChar := uint32(tok.Start.Column - 1)
1738 length := uint32(tok.End.Column - tok.Start.Column)
1739 if length == 0 {
1740 // Fallback: use text length
1741 length = uint32(len(tok.Token.Value))
1742 }
1743
1744 deltaLine := line - prevLine
1745 deltaStartChar := startChar
1746 if deltaLine == 0 {
1747 deltaStartChar = startChar - prevStartChar
1748 }
1749
1750 data = append(data, deltaLine, deltaStartChar, length, uint32(tokenType), 0)
1751 prevLine = line
1752 prevStartChar = startChar
1753 }
1754 return data
1755}
1756
1757// Token type indices matching the legend declared in handleInitialize:
1758//

Callers 1

Calls 1

classifyTokenFunction · 0.85

Tested by

no test coverage detected