Skip to content

Commit 19e484a

Browse files
committed
Add documentation to CodeSuggestionEntry
1 parent 066857a commit 19e484a

File tree

5 files changed

+64
-10
lines changed

5 files changed

+64
-10
lines changed

Example/CodeEditSourceEditorExample/CodeEditSourceEditorExample/Views/MockCompletionDelegate.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class MockCompletionDelegate: CodeSuggestionDelegate, ObservableObject {
4949
var label: String
5050
var detail: String?
5151
var pathComponents: [String]?
52-
var targetPosition: CursorPosition? { nil }
52+
var targetPosition: CursorPosition? = CursorPosition(line: 10, column: 20)
5353
var sourcePreview: String?
5454
var image: Image = Image(systemName: "dot.square.fill")
5555
var imageColor: Color = .gray
@@ -75,7 +75,7 @@ class MockCompletionDelegate: CodeSuggestionDelegate, ObservableObject {
7575
text: randomString,
7676
detail: text.randomElement()!,
7777
sourcePreview: randomString,
78-
pathComponents: (0..<Int.random(in: 1..<5)).map { text[$0] }
78+
pathComponents: (0..<Int.random(in: 0..<10)).map { text[$0] }
7979
)
8080
)
8181
}

Sources/CodeEditSourceEditor/CodeSuggestion/Model/CodeSuggestionEntry.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import SwiftUI
1212
public protocol CodeSuggestionEntry {
1313
var label: String { get }
1414
var detail: String? { get }
15+
var documentation: String? { get }
1516

1617
/// Leave as `nil` if the link is in the same document.
1718
var pathComponents: [String]? { get }

Sources/CodeEditSourceEditor/CodeSuggestion/TableView/CodeSuggestionPreviewView.swift

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ final class CodeSuggestionPreviewView: NSVisualEffectView {
3939
var font: NSFont = .systemFont(ofSize: 12) {
4040
didSet {
4141
sourcePreviewLabel.font = font
42-
pathComponentsLabel.font = font
42+
pathComponentsLabel.font = .systemFont(ofSize: font.pointSize)
4343
}
4444
}
4545
var documentationFont: NSFont = .systemFont(ofSize: 12) {
@@ -76,6 +76,10 @@ final class CodeSuggestionPreviewView: NSVisualEffectView {
7676
styleStaticLabel(documentationLabel)
7777
styleStaticLabel(pathComponentsLabel)
7878

79+
pathComponentsLabel.maximumNumberOfLines = 1
80+
pathComponentsLabel.lineBreakMode = .byTruncatingMiddle
81+
pathComponentsLabel.usesSingleLineMode = true
82+
7983
stackView.addArrangedSubview(sourcePreviewLabel)
8084
stackView.addArrangedSubview(documentationLabel)
8185
stackView.addArrangedSubview(pathComponentsLabel)
@@ -97,6 +101,10 @@ final class CodeSuggestionPreviewView: NSVisualEffectView {
97101
fatalError("init(coder:) has not been implemented")
98102
}
99103

104+
func hideIfEmpty() {
105+
isHidden = sourcePreview == nil && documentation == nil && pathComponents.isEmpty
106+
}
107+
100108
func setPreferredMaxLayoutWidth(width: CGFloat) {
101109
sourcePreviewLabel.preferredMaxLayoutWidth = width
102110
documentationLabel.preferredMaxLayoutWidth = width
@@ -105,6 +113,7 @@ final class CodeSuggestionPreviewView: NSVisualEffectView {
105113

106114
private func styleStaticLabel(_ label: NSTextField) {
107115
label.isEditable = false
116+
label.isSelectable = true
108117
label.allowsDefaultTighteningForTruncation = false
109118
label.isBezeled = false
110119
label.isBordered = false
@@ -113,7 +122,48 @@ final class CodeSuggestionPreviewView: NSVisualEffectView {
113122
}
114123

115124
private func configurePathComponentsLabel() {
116-
// TODO: This
117-
pathComponentsLabel.isHidden = true
125+
pathComponentsLabel.isHidden = pathComponents.isEmpty
126+
127+
let folder = NSTextAttachment()
128+
folder.image = NSImage(systemSymbolName: "folder.fill", accessibilityDescription: nil)?
129+
.withSymbolConfiguration(
130+
.init(paletteColors: [NSColor.systemBlue]).applying(.init(pointSize: font.pointSize, weight: .regular))
131+
)
132+
133+
let string: NSMutableAttributedString = NSMutableAttributedString(attachment: folder)
134+
string.append(NSAttributedString(string: " "))
135+
136+
let separator = NSTextAttachment()
137+
separator.image = NSImage(systemSymbolName: "chevron.compact.right", accessibilityDescription: nil)?
138+
.withSymbolConfiguration(
139+
.init(paletteColors: [NSColor.labelColor])
140+
.applying(.init(pointSize: font.pointSize + 1, weight: .regular))
141+
)
142+
143+
for (idx, component) in pathComponents.enumerated() {
144+
string.append(NSAttributedString(string: component, attributes: [.foregroundColor: NSColor.labelColor]))
145+
if idx != pathComponents.count - 1 {
146+
string.append(NSAttributedString(string: " "))
147+
string.append(NSAttributedString(attachment: separator))
148+
string.append(NSAttributedString(string: " "))
149+
}
150+
}
151+
152+
if let targetRange {
153+
string.append(NSAttributedString(string: ":\(targetRange.start.line)"))
154+
if targetRange.start.column > 1 {
155+
string.append(NSAttributedString(string: ":\(targetRange.start.column)"))
156+
}
157+
}
158+
if let paragraphStyle = NSMutableParagraphStyle.default.mutableCopy() as? NSMutableParagraphStyle {
159+
paragraphStyle.lineBreakMode = .byTruncatingMiddle
160+
string.addAttribute(
161+
.paragraphStyle,
162+
value: paragraphStyle,
163+
range: NSRange(location: 0, length: string.length)
164+
)
165+
}
166+
167+
pathComponentsLabel.attributedStringValue = string
118168
}
119169
}

Sources/CodeEditSourceEditor/CodeSuggestion/TableView/SuggestionViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,10 @@ extension SuggestionViewController: NSTableViewDataSource, NSTableViewDelegate {
334334
let selectedItem = model.items[tableView.selectedRow]
335335

336336
previewView.sourcePreview = selectedItem.sourcePreview
337-
// TODO: Add Documentation To Completion Items
338-
previewView.documentation = selectedItem.detail
337+
previewView.documentation = selectedItem.documentation
339338
previewView.pathComponents = selectedItem.pathComponents ?? []
340339
previewView.targetRange = selectedItem.targetPosition
340+
previewView.hideIfEmpty()
341341
updateSize(using: nil)
342342

343343
model.didSelect(item: selectedItem)

Sources/CodeEditSourceEditor/JumpToDefinition/JumpToDefinitionLink.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,29 @@ public struct JumpToDefinitionLink: Identifiable, Sendable, CodeSuggestionEntry
1818
public let targetRange: CursorPosition
1919

2020
public let label: String
21-
public let sourcePreview: String?
21+
public var detail: String? { url?.lastPathComponent }
22+
public var documentation: String?
2223

24+
public let sourcePreview: String?
2325
public let image: Image
2426
public let imageColor: Color
2527

26-
public var detail: String? { url?.lastPathComponent }
27-
public var pathComponents: [String]? { url?.pathComponents ?? [] }
28+
public var pathComponents: [String]? { url?.relativePath.components(separatedBy: "/") ?? [] }
2829
public var deprecated: Bool { false }
2930

3031
public init(
3132
url: URL?,
3233
targetRange: CursorPosition,
3334
typeName: String,
3435
sourcePreview: String,
36+
documentation: String?,
3537
image: Image = Image(systemName: "dot.square.fill"),
3638
imageColor: Color = Color(NSColor.lightGray)
3739
) {
3840
self.url = url
3941
self.targetRange = targetRange
4042
self.label = typeName
43+
self.documentation = documentation
4144
self.sourcePreview = sourcePreview
4245
self.image = image
4346
self.imageColor = imageColor

0 commit comments

Comments
 (0)