Files
sourcekit-lsp/Sources/SKTestSupport/Array+SyntaxHighlightingToken.swift
2024-01-08 23:51:09 +01:00

54 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 LanguageServerProtocol
import SourceKitLSP
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
}
let kind = SemanticTokenTypes.all[Int(rawKind)]
let modifiers = SemanticTokenModifiers(rawValue: rawModifiers)
append(
SyntaxHighlightingToken(
start: current,
utf16length: length,
kind: kind,
modifiers: modifiers
)
)
}
}
}