Files
sourcekit-lsp/Sources/SKTestSupport/Array+SyntaxHighlightingToken.swift
Alex Hoppen d0fc00ce98 Format using swift-format
Add `.swift-format` to the repo and format the repo with `swift-format`.

This commit does not add any automation to enforce formatting of sourcekit-lsp in CI. The goal of this commit is to get the majority of source changes out of the way so that the diff of actually enforcing formatting will have fewer changes or conflicts.
2023-10-10 13:44:47 -07:00

54 lines
1.7 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
}
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
)
)
}
}
}