v2.4.1 release: scratchpad polish + demo cleanup
Maintenance release, no framework API changes. Scratchpad: Kill All Sound (⌘K) rename, mode-switch desync fix, code-completion prefix filtering restored (⌃Space Show Completions). Demos: GUIDemo voices + CLI EventPattern BlipVoice moved into Synthesizers/, EventPattern demo split into makeLead/makeBass, GUIDemo doc drift fixed (conductor default). LyrebirdScratch gains a SwiftPM package so the workspace shows it like GUIDemo (.xcodeproj kept for distribution). Linux live-coding scratchpad documented.
This commit is contained in:
@@ -182,7 +182,78 @@ public actor SnippetCompletionService {
|
||||
// User line 0 sits at wrapped line `userBodyLineOffset - 1` (0-based);
|
||||
// the column is unchanged (the preamble adds only whole lines).
|
||||
let wrappedLine: Int = line + (_userBodyLineOffset - 1)
|
||||
return try await _client.completion(uri: _fileURI, line: wrappedLine, character: character)
|
||||
let items: [LSPCompletionItem] = try await _client.completion(
|
||||
uri: _fileURI, line: wrappedLine, character: character)
|
||||
// sourcekit-lsp returns a bare-identifier completion as the *whole*
|
||||
// visible-symbol universe (hundreds of SDK/stdlib globals), unsorted and
|
||||
// unfiltered — LSP expects the client to narrow it by the typed prefix.
|
||||
// Member completion (after `.`) is already server-scoped, so its prefix
|
||||
// is empty and this leaves it untouched. Without this, typing `OscS` and
|
||||
// asking for completion floods the window with Darwin/IOKit globals and
|
||||
// buries (or truncates away) the real `Osc*` types.
|
||||
let prefix: String = SnippetCompletionService.partialWord(
|
||||
in: userBody, line: line, character: character)
|
||||
return SnippetCompletionService.filterAndRank(items, prefix: prefix)
|
||||
}
|
||||
|
||||
/// The identifier fragment immediately before the caret — letters, digits and
|
||||
/// underscores — on the user-body `line` up to `character` (zero-based,
|
||||
/// UTF-16). Empty when the character before the caret is not an identifier
|
||||
/// character (e.g. a `.` member completion, or start of line).
|
||||
static func partialWord(in userBody: String, line: Int, character: Int) -> String {
|
||||
let lines: [Substring] = userBody.split(separator: "\n", omittingEmptySubsequences: false)
|
||||
guard line >= 0, line < lines.count else { return "" }
|
||||
let units: [UInt16] = Array(lines[line].utf16)
|
||||
let end: Int = min(character, units.count)
|
||||
var start: Int = end
|
||||
while start > 0, isIdentifierUTF16(units[start - 1]) {
|
||||
start -= 1
|
||||
}
|
||||
guard start < end else { return "" }
|
||||
return String(decoding: units[start..<end], as: UTF16.self)
|
||||
}
|
||||
|
||||
/// ASCII letter / digit / underscore — the same identifier class the editor's
|
||||
/// `rangeForUserCompletion` uses to pick the word the completion replaces.
|
||||
private static func isIdentifierUTF16(_ u: UInt16) -> Bool {
|
||||
let underscore: UInt16 = 95
|
||||
let zero: UInt16 = 48, nine: UInt16 = 57
|
||||
let upperA: UInt16 = 65, upperZ: UInt16 = 90
|
||||
let lowerA: UInt16 = 97, lowerZ: UInt16 = 122
|
||||
return u == underscore
|
||||
|| (u >= zero && u <= nine)
|
||||
|| (u >= upperA && u <= upperZ)
|
||||
|| (u >= lowerA && u <= lowerZ)
|
||||
}
|
||||
|
||||
/// Narrow `items` to those matching `prefix` (case-insensitive, by `label`)
|
||||
/// and order best-first. An empty `prefix` keeps every item (member
|
||||
/// completion is already scoped by the server) and only sorts it. With a
|
||||
/// prefix, exact case-insensitive prefix matches come first, then anything
|
||||
/// that merely contains the fragment (so a mistyped case or an internal match
|
||||
/// still surfaces), each group ordered by the server's `sortText` then label.
|
||||
static func filterAndRank(_ items: [LSPCompletionItem], prefix: String) -> [LSPCompletionItem] {
|
||||
func ordered(_ group: [LSPCompletionItem]) -> [LSPCompletionItem] {
|
||||
group.sorted { a, b in
|
||||
let ka: String = a.sortText ?? a.label
|
||||
let kb: String = b.sortText ?? b.label
|
||||
if ka != kb { return ka < kb }
|
||||
return a.label < b.label
|
||||
}
|
||||
}
|
||||
guard !prefix.isEmpty else { return ordered(items) }
|
||||
let needle: String = prefix.lowercased()
|
||||
var prefixed: [LSPCompletionItem] = []
|
||||
var contained: [LSPCompletionItem] = []
|
||||
for item: LSPCompletionItem in items {
|
||||
let hay: String = item.label.lowercased()
|
||||
if hay.hasPrefix(needle) {
|
||||
prefixed.append(item)
|
||||
} else if hay.contains(needle) {
|
||||
contained.append(item)
|
||||
}
|
||||
}
|
||||
return ordered(prefixed) + ordered(contained)
|
||||
}
|
||||
|
||||
/// Shut down the server and remove the workspace. Safe to call more than once.
|
||||
|
||||
Reference in New Issue
Block a user