Files
helder/Sources/HelderUI/Editor/SymbolPopupView.swift
T
2026-10-10 14:54:39 +02:00

254 lines
11 KiB
Swift

import HelderCore
import SwiftUI
/// One row the keyboard can land on: a declaration or a reference line.
public struct SymbolTarget: Equatable, Sendable {
public var path: String
public var line: Int
}
public enum SymbolTargets {
/// Declarations first, then every reference line, as one flat list for ↑↓↵.
public static func flatten(_ lookup: SymbolLookup?) -> [SymbolTarget] {
guard let lookup else { return [] }
return lookup.definitions.map { SymbolTarget(path: $0.path, line: $0.line) }
+ lookup.references.flatMap { group in group.hits.map { SymbolTarget(path: group.path, line: $0.line) } }
}
/// One declaration is named in full, the way the file does; several share only the short name.
public static func title(_ name: String, _ lookup: SymbolLookup?) -> String {
guard let lookup, lookup.definitions.count == 1 else { return name }
return lookup.definitions[0].qualifiedName
}
}
/// ⌘-click on a PHP class name: where it is declared, then every reference. Centred on the window,
/// because the list is as long as the class is popular.
struct SymbolPopupView: View {
let editor: EditorStore
let name: String
let dismiss: () -> Void
/// Opens a file at a line (and reveals it in the Explorer).
let openAt: (String, Int) -> Void
@State private var highlighted = 0
@FocusState private var focused: Bool
private var lookup: SymbolLookup? { editor.symbolName == name ? editor.symbolLookup : nil }
var body: some View {
let targets = SymbolTargets.flatten(lookup)
GeometryReader { geometry in
ZStack {
Palette.scrim.onTapGesture(perform: dismiss)
panel(targets)
.frame(width: min(700, geometry.size.width * 0.82))
.frame(maxHeight: min(geometry.size.height * 0.72, 640))
.fixedSize(horizontal: false, vertical: true)
}
}
.focusable()
.focused($focused)
.focusEffectDisabled()
.onAppear { focused = true }
.onChange(of: lookup) { highlighted = 0 }
.onKeyPress(.downArrow) { highlighted = min(highlighted + 1, max(0, targets.count - 1)); return .handled }
.onKeyPress(.upArrow) { highlighted = max(highlighted - 1, 0); return .handled }
.onKeyPress(.return) {
if highlighted < targets.count { open(targets[highlighted]) }
return .handled
}
.onExitCommand(perform: dismiss)
}
private func panel(_ targets: [SymbolTarget]) -> some View {
VStack(spacing: 0) {
HStack(spacing: 10) {
Text(SymbolTargets.title(name, lookup)).typeRole(TypeRole(.monoBold, 13)).foregroundStyle(Palette.text1)
Spacer()
if let lookup {
Text("\(lookup.referenceCount) reference\(lookup.referenceCount == 1 ? "" : "s")")
.typeRole(TypeRole(.monoRegular, 10.5))
.foregroundStyle(Palette.text4)
}
}
.padding(.horizontal, 18)
.frame(height: 50)
.overlay(alignment: .bottom) { Rectangle().fill(Palette.raised).frame(height: Metrics.hairline) }
if let lookup {
ScrollViewReader { proxy in
ScrollView {
results(lookup)
.padding(.bottom, 6)
}
.onChange(of: highlighted) { _, index in proxy.scrollTo(index) }
}
} else {
empty("Searching…")
}
}
.background(Palette.panel, in: RoundedRectangle(cornerRadius: Metrics.radiusOverlay))
.overlay(alignment: .leading) { Rectangle().fill(Palette.amber).frame(width: Metrics.accentRule) }
.overlay(RoundedRectangle(cornerRadius: Metrics.radiusOverlay).stroke(Palette.raised, lineWidth: Metrics.hairline))
.clipShape(RoundedRectangle(cornerRadius: Metrics.radiusOverlay))
.shadow(color: Palette.overlayShadow, radius: Metrics.overlayShadowRadius, y: Metrics.overlayShadowY)
}
private func results(_ lookup: SymbolLookup) -> some View {
let definitionCount = lookup.definitions.count
return LazyVStack(alignment: .leading, spacing: 0) {
section("Defined in")
if lookup.definitions.isEmpty { empty("No declaration in this project") }
ForEach(Array(lookup.definitions.enumerated()), id: \.offset) { index, def in
SymbolRow(isHighlighted: highlighted == index) {
HStack(spacing: 8) {
TypeBadge(path: def.path)
Text(def.path).typeRole(Typography.path).foregroundStyle(Palette.text2).lineLimit(1).truncationMode(.head)
Text(def.kind)
.typeRole(TypeRole(.monoRegular, 10))
.foregroundStyle(Palette.text4)
.padding(.horizontal, 5)
.overlay(RoundedRectangle(cornerRadius: Metrics.radiusSmall).stroke(Palette.tooltipBorder, lineWidth: Metrics.hairline))
Spacer()
Text("\(def.line)").typeRole(Typography.meta).foregroundStyle(Palette.text4)
}
.padding(.horizontal, 13)
.padding(.vertical, 4)
}
.id(index)
.onHover { if $0 { highlighted = index } }
.pointerHand()
.onTapGesture { open(SymbolTarget(path: def.path, line: def.line)) }
}
ForEach(Array(lookup.definitions.enumerated()), id: \.offset) { _, def in relations(def) }
if !lookup.references.isEmpty { section("Used in") }
ForEach(Array(referenceRows(lookup, offset: definitionCount).enumerated()), id: \.offset) { _, row in
switch row {
case let .file(group):
HStack(spacing: 8) {
TypeBadge(path: group.path)
Text(group.path).typeRole(Typography.path).foregroundStyle(Palette.text3).lineLimit(1).truncationMode(.head)
Spacer()
Text("\(group.hits.count)").typeRole(Typography.meta).foregroundStyle(Palette.text4)
}
.padding(.horizontal, 13)
.padding(.top, 8)
.padding(.bottom, 2)
.contentShape(Rectangle())
.pointerHand()
.onTapGesture { if let first = group.hits.first { open(SymbolTarget(path: group.path, line: first.line)) } }
case let .hit(path, hit, index):
SymbolRow(isHighlighted: highlighted == index) {
HStack(spacing: 12) {
Text("\(hit.line)").typeRole(Typography.searchHit).foregroundStyle(highlighted == index ? Palette.amber : Palette.text5)
.frame(width: 40, alignment: .trailing)
Text(String(hit.text.prefix(200))).typeRole(Typography.searchHit).foregroundStyle(Palette.text3).lineLimit(1)
Spacer(minLength: 0)
}
.padding(.leading, 13)
.frame(height: Metrics.searchHitLine)
}
.id(index)
.onHover { if $0 { highlighted = index } }
.pointerHand()
.onTapGesture { open(SymbolTarget(path: path, line: hit.line)) }
}
}
}
}
private enum ReferenceRow {
case file(RefGroup)
case hit(path: String, ContentHit, index: Int)
}
private func referenceRows(_ lookup: SymbolLookup, offset: Int) -> [ReferenceRow] {
var rows: [ReferenceRow] = []
var index = offset
for group in lookup.references {
rows.append(.file(group))
for hit in group.hits {
rows.append(.hit(path: group.path, hit, index: index))
index += 1
}
}
return rows
}
/// What a declaration extends, implements and uses; a name this project declares is a chip you can follow.
@ViewBuilder
private func relations(_ def: SymbolDef) -> some View {
let groups = [("extends", def.parents), ("implements", def.interfaces), ("uses", def.traits)].filter { !$0.1.isEmpty }
if !groups.isEmpty {
VStack(alignment: .leading, spacing: 3) {
ForEach(groups, id: \.0) { key, names in
HStack(spacing: 6) {
Text(key.uppercased()).typeRole(TypeRole(.sansRegular, 10)).foregroundStyle(Palette.text4)
.frame(minWidth: 66, alignment: .leading)
ForEach(names, id: \.self) { chip($0) }
}
}
}
.padding(EdgeInsets(top: 2, leading: 34, bottom: 6, trailing: 13))
}
}
private func chip(_ name: String) -> some View {
SymbolChip(name: name, known: editor.knownSymbols.contains(name)) { editor.lookupSymbol(name) }
}
private func section(_ title: String) -> some View {
Text(title).typeRole(Typography.panelLabel).foregroundStyle(Palette.text4)
.padding(EdgeInsets(top: 14, leading: 16, bottom: 6, trailing: 16))
}
private func empty(_ text: String) -> some View {
Text(text).typeRole(Typography.tooltip).foregroundStyle(Palette.text4)
.padding(.horizontal, 16)
.padding(.vertical, 12)
.frame(maxWidth: .infinity, alignment: .leading)
}
private func open(_ target: SymbolTarget) {
dismiss()
openAt(target.path, target.line)
}
}
/// A selectable row: raised fill and the 2px amber rule when highlighted.
private struct SymbolRow<Content: View>: View {
let isHighlighted: Bool
@ViewBuilder let content: () -> Content
var body: some View {
content()
.frame(maxWidth: .infinity, alignment: .leading)
.background(isHighlighted ? Palette.raised : Color.clear)
.overlay(alignment: .leading) {
if isHighlighted { Rectangle().fill(Palette.amber).frame(width: Metrics.accentRule) }
}
.contentShape(Rectangle())
}
}
/// A related class name; one the project declares lights amber under the pointer and reloads the popup on it.
private struct SymbolChip: View {
let name: String
let known: Bool
let follow: () -> Void
@State private var hovering = false
var body: some View {
let lit = known && hovering
Text(name)
.typeRole(TypeRole(.monoRegular, 11))
.foregroundStyle(lit ? Palette.amber : known ? Palette.text2 : Palette.text4)
.padding(.horizontal, 7)
.background(lit ? Palette.amberTint : .clear, in: RoundedRectangle(cornerRadius: 3))
.overlay(RoundedRectangle(cornerRadius: 3).stroke(lit ? Palette.amberKeycapBorder : Palette.raised, lineWidth: Metrics.hairline))
.contentShape(Rectangle())
.onHover { hovering = $0 }
.pointerHand(known)
.onTapGesture { if known { follow() } }
}
}