restores Helder in all its glory

This commit is contained in:
2026-09-28 11:34:49 +02:00
parent c2b38b853c
commit 1d29d0952d
280 changed files with 29205 additions and 7 deletions
@@ -0,0 +1,97 @@
import Foundation
public struct PHPDeclaration: Sendable, Equatable {
public var kind: String
public var name: String
public var parents: [String]
public var interfaces: [String]
public init(kind: String, name: String, parents: [String], interfaces: [String]) {
self.kind = kind
self.name = name
self.parents = parents
self.interfaces = interfaces
}
}
public enum PHPSymbols {
/// ripgrep pattern for declarations and `namespace X;` lines in one pass.
public static let declarationPattern = #"^\s*(namespace\s+[A-Za-z_\\][\w\\]*\s*;|(final\s+|abstract\s+|readonly\s+)*(class|interface|trait|enum)\s+\w+)"#
/// ripgrep pattern for an indented `use` inside a class body.
public static let traitUsePattern = #"^\s+use\s+[A-Za-z_\\][\w\\ \t,]*[;{]"#
/// The declaration on one line, or nil. `final`/`abstract`/`readonly` may precede the keyword.
///
/// Only this line is read, so an `implements` list wrapped onto the next line yields
/// fewer interfaces, never wrong ones. Namespaced parents are reduced to short names.
public static func parseDeclaration(_ line: String) -> PHPDeclaration? {
let declaration = /^[ \t]*(?:(?:final|abstract|readonly)[ \t]+)*(class|interface|trait|enum)[ \t]+([A-Za-z_]\w*)(.*)$/
guard let match = line.firstMatch(of: declaration) else { return nil }
let rest = match.3
let extends = rest.firstMatch(of: /\bextends\s+([^{]*?)(?:\s+implements\b|\s*\{|$)/)
let implements = rest.firstMatch(of: /\bimplements\s+([^{]*?)(?:\s*\{|$)/)
return PHPDeclaration(
kind: String(match.1), name: String(match.2),
parents: extends.map { clauseNames($0.1) } ?? [],
interfaces: implements.map { clauseNames($0.1) } ?? []
)
}
/// The namespace a `namespace X;` line declares, without a leading backslash, or nil.
public static func parseNamespace(_ line: String) -> String? {
guard let match = line.firstMatch(of: /^[ \t]*namespace[ \t]+([A-Za-z_\\][\w\\]*)[ \t]*;/) else { return nil }
let name = match.1
return String(name.hasPrefix("\\") ? name.dropFirst() : name)
}
/// The traits an indented `use A, B;` mixes in; `use function`, `use const` and anything else give [].
public static func parseTraitUse(_ line: String) -> [String] {
guard let match = line.firstMatch(of: /^[ \t]+use[ \t]+([A-Za-z_\\][\w\\ \t,]*?)[ \t]*[;{]/),
line.firstMatch(of: /^\s*use\s+(function|const)\b/) == nil
else { return [] }
return clauseNames(match.1)
}
/// Short class names out of an `extends`/`implements`/`use` clause.
private static func clauseNames(_ clause: Substring) -> [String] {
clause.split(separator: ",", omittingEmptySubsequences: false).compactMap { part in
let last = part.trimmingCharacters(in: .whitespaces).split(separator: "\\", omittingEmptySubsequences: false).last ?? ""
let name = last.trimmingCharacters(in: .whitespaces)
return isIdentifier(name) ? name : nil
}
}
/// A column-0 `use` line: a namespace import, which reference search drops as noise.
public static func isImportLine(_ line: String) -> Bool {
line.range(of: #"^use[ \t]+[^;]*;?[ \t\r]*$"#, options: .regularExpression) != nil
}
/// True for a bare PHP identifier, the only thing a lookup accepts.
public static func isIdentifier(_ name: String) -> Bool {
name.range(of: #"^[A-Za-z_]\w*$"#, options: .regularExpression) != nil
}
/// Project declarations before `vendor/` ones, then by path, capped at `SymbolLimits.maxDefinitions`.
public static func sortDefinitions(_ defs: [SymbolDef]) -> [SymbolDef] {
let sorted = defs.sorted {
let lv = $0.path.hasPrefix("vendor/"), rv = $1.path.hasPrefix("vendor/")
return lv != rv ? !lv : $0.path < $1.path
}
return Array(sorted.prefix(SymbolLimits.maxDefinitions))
}
/// The identifier around a UTF-16 offset, or "" when the caret is not on one.
public static func word(in text: String, at offset: Int) -> String {
let units = Array(text.utf16)
guard offset >= 0, offset <= units.count else { return "" }
func isWord(_ u: UInt16) -> Bool {
(u >= 48 && u <= 57) || (u >= 65 && u <= 90) || (u >= 97 && u <= 122) || u == 95
}
var start = offset
while start > 0 && isWord(units[start - 1]) { start -= 1 }
var end = offset
while end < units.count && isWord(units[end]) { end += 1 }
let word = String(decoding: units[start..<end], as: UTF16.self)
return isIdentifier(word) ? word : ""
}
}
@@ -0,0 +1,66 @@
import Foundation
/// One class, interface, trait or enum declaration.
public struct SymbolDef: Sendable, Equatable, Codable {
public var name: String
public var path: String
public var line: Int
/// `class`, `interface`, `trait` or `enum`.
public var kind: String
/// The file's namespace, for the fully qualified name; "" when none.
public var namespace: String
/// `extends` on the declaration line, short names.
public var parents: [String]
/// `implements` on the declaration line, short names.
public var interfaces: [String]
/// Traits mixed in by an indented `use` in the body.
public var traits: [String]
public init(name: String, path: String, line: Int, kind: String, namespace: String = "",
parents: [String] = [], interfaces: [String] = [], traits: [String] = []) {
self.name = name
self.path = path
self.line = line
self.kind = kind
self.namespace = namespace
self.parents = parents
self.interfaces = interfaces
self.traits = traits
}
public var qualifiedName: String { namespace.isEmpty ? name : namespace + "\\" + name }
}
/// References to one name within one file.
public struct RefGroup: Sendable, Equatable {
public var path: String
public var hits: [ContentHit]
public init(path: String, hits: [ContentHit]) {
self.path = path
self.hits = hits
}
}
/// What a ⌘-click on a class name returns: where it is declared and where it is used.
public struct SymbolLookup: Sendable, Equatable {
public var name: String
public var definitions: [SymbolDef]
public var references: [RefGroup]
public var referenceCount: Int
public init(name: String, definitions: [SymbolDef] = [], references: [RefGroup] = [], referenceCount: Int = 0) {
self.name = name
self.definitions = definitions
self.references = references
self.referenceCount = referenceCount
}
public static func empty(_ name: String) -> SymbolLookup { SymbolLookup(name: name) }
}
public enum SymbolLimits {
public static let maxDefinitions = 25
public static let maxReferenceFiles = 300
public static let maxLineLength = 1000
}