mirror of
https://github.com/apple/sourcekit-lsp.git
synced 2026-03-02 18:23:24 +01:00
This adds a sourcekitd plugin that drives the code completion requests. It also includes a `CompletionScoring` module that’s used to rank code completion results based on their contextual match, allowing us to show more relevant code completion results at the top.
51 lines
1.6 KiB
Swift
51 lines
1.6 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2024 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// From SwiftPrivate.swift.
|
|
|
|
/// Compute the prefix sum of `seq`.
|
|
private func scan<S: Sequence, U>(
|
|
_ seq: S,
|
|
_ initial: U,
|
|
_ combine: (U, S.Element) -> U
|
|
) -> [U] {
|
|
var result: [U] = []
|
|
result.reserveCapacity(seq.underestimatedCount)
|
|
var runningResult = initial
|
|
for element in seq {
|
|
runningResult = combine(runningResult, element)
|
|
result.append(runningResult)
|
|
}
|
|
return result
|
|
}
|
|
|
|
func withArrayOfCStrings<R>(
|
|
_ args: [String],
|
|
_ body: ([UnsafeMutablePointer<CChar>?]) -> R
|
|
) -> R {
|
|
let argsCounts = Array(args.map { $0.utf8.count + 1 })
|
|
let argsOffsets = [0] + scan(argsCounts, 0, +)
|
|
let argsBufferSize = argsOffsets.last!
|
|
var argsBuffer: [UInt8] = []
|
|
argsBuffer.reserveCapacity(argsBufferSize)
|
|
for arg in args {
|
|
argsBuffer.append(contentsOf: arg.utf8)
|
|
argsBuffer.append(0)
|
|
}
|
|
return argsBuffer.withUnsafeMutableBufferPointer { (argsBuffer) in
|
|
let ptr = UnsafeMutableRawPointer(argsBuffer.baseAddress!).bindMemory(to: CChar.self, capacity: argsBuffer.count)
|
|
var cStrings: [UnsafeMutablePointer<CChar>?] = argsOffsets.map { ptr + $0 }
|
|
cStrings[cStrings.count - 1] = nil
|
|
return body(cStrings)
|
|
}
|
|
}
|