mirror of
https://github.com/apple/sourcekit-lsp.git
synced 2026-03-02 18:23:24 +01:00
This is an implementation of LSP's semantic tokens for Swift. Both lexical and semantic tokens are provided by using the syntaxmap and the semantic annotations provided as part of SourceKit's open responses and document update notifications. While lexical tokens are parsed and stored in the DocumentManager synchronously, semantic tokens are provided asynchronously. If an edit occurs, tokens are automatically shifted by the DocumentManager. This is especially relevant for lexical tokens, which are updated in deltas. In addition, unit tests are added that assert that both lexical and semantic tokens are provided and shifted correctly upon edits.
52 lines
1.6 KiB
Swift
52 lines
1.6 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import SourceKitLSP
|
|
import LanguageServerProtocol
|
|
|
|
extension Array where Element == SyntaxHighlightingToken {
|
|
/// Decodes the LSP representation of syntax highlighting tokens
|
|
public init(lspEncodedTokens rawTokens: [UInt32]) {
|
|
self.init()
|
|
assert(rawTokens.count.isMultiple(of: 5))
|
|
reserveCapacity(rawTokens.count / 5)
|
|
|
|
var current = Position(line: 0, utf16index: 0)
|
|
|
|
for i in stride(from: 0, to: rawTokens.count, by: 5) {
|
|
let lineDelta = Int(rawTokens[i])
|
|
let charDelta = Int(rawTokens[i + 1])
|
|
let length = Int(rawTokens[i + 2])
|
|
let rawKind = rawTokens[i + 3]
|
|
let rawModifiers = rawTokens[i + 4]
|
|
|
|
current.line += lineDelta
|
|
|
|
if lineDelta == 0 {
|
|
current.utf16index += charDelta
|
|
} else {
|
|
current.utf16index = charDelta
|
|
}
|
|
|
|
guard let kind = SyntaxHighlightingToken.Kind(rawValue: rawKind) else { continue }
|
|
let modifiers = SyntaxHighlightingToken.Modifiers(rawValue: rawModifiers)
|
|
|
|
append(SyntaxHighlightingToken(
|
|
start: current,
|
|
utf16length: length,
|
|
kind: kind,
|
|
modifiers: modifiers
|
|
))
|
|
}
|
|
}
|
|
}
|