autosnippet 3.2.22 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dashboard/dist/assets/{icons-C1dUryS-.js → icons-BofcEZ3f.js} +1 -1
- package/dashboard/dist/assets/index-SiN1GChm.js +128 -0
- package/dashboard/dist/index.html +2 -2
- package/dist/bin/cli.d.ts +0 -1
- package/dist/bin/cli.js +0 -133
- package/dist/lib/cli/SetupService.d.ts +46 -2
- package/dist/lib/cli/SetupService.js +2 -27
- package/dist/lib/{platform/ios/spm → core/discovery}/SpmDiscoverer.d.ts +2 -5
- package/dist/lib/{platform/ios/spm → core/discovery}/SpmDiscoverer.js +159 -44
- package/dist/lib/core/discovery/index.d.ts +1 -1
- package/dist/lib/core/discovery/index.js +2 -2
- package/dist/lib/external/mcp/handlers/guard.js +6 -3
- package/dist/lib/http/HttpServer.js +0 -6
- package/dist/lib/http/routes/commands.d.ts +1 -1
- package/dist/lib/http/routes/commands.js +1 -66
- package/dist/lib/http/routes/remote.js +0 -5
- package/dist/lib/injection/ServiceMap.d.ts +0 -9
- package/dist/lib/injection/modules/AppModule.d.ts +2 -3
- package/dist/lib/injection/modules/AppModule.js +3 -30
- package/dist/lib/service/module/ModuleService.js +3 -13
- package/dist/lib/service/search/SearchEngine.js +1 -1
- package/dist/lib/shared/constants.d.ts +0 -15
- package/dist/lib/shared/constants.js +0 -10
- package/dist/scripts/release.js +2 -10
- package/package.json +4 -19
- package/dashboard/dist/assets/index-DdvZE4Yd.js +0 -128
- package/dist/lib/http/routes/snippets.d.ts +0 -6
- package/dist/lib/http/routes/snippets.js +0 -49
- package/dist/lib/platform/ClipboardManager.d.ts +0 -24
- package/dist/lib/platform/ClipboardManager.js +0 -142
- package/dist/lib/platform/NativeUi.d.ts +0 -53
- package/dist/lib/platform/NativeUi.js +0 -284
- package/dist/lib/platform/ios/index.d.ts +0 -38
- package/dist/lib/platform/ios/index.js +0 -42
- package/dist/lib/platform/ios/routes/spm.d.ts +0 -9
- package/dist/lib/platform/ios/routes/spm.js +0 -371
- package/dist/lib/platform/ios/snippet/PlaceholderConverter.d.ts +0 -21
- package/dist/lib/platform/ios/snippet/PlaceholderConverter.js +0 -48
- package/dist/lib/platform/ios/snippet/XcodeCodec.d.ts +0 -23
- package/dist/lib/platform/ios/snippet/XcodeCodec.js +0 -96
- package/dist/lib/platform/ios/spm/DependencyGraph.d.ts +0 -56
- package/dist/lib/platform/ios/spm/DependencyGraph.js +0 -195
- package/dist/lib/platform/ios/spm/PackageSwiftParser.d.ts +0 -69
- package/dist/lib/platform/ios/spm/PackageSwiftParser.js +0 -231
- package/dist/lib/platform/ios/spm/PathFinder.d.ts +0 -28
- package/dist/lib/platform/ios/spm/PathFinder.js +0 -117
- package/dist/lib/platform/ios/spm/PolicyEngine.d.ts +0 -44
- package/dist/lib/platform/ios/spm/PolicyEngine.js +0 -79
- package/dist/lib/platform/ios/spm/SpmHelper.d.ts +0 -102
- package/dist/lib/platform/ios/spm/SpmHelper.js +0 -464
- package/dist/lib/platform/ios/xcode/HeaderResolver.d.ts +0 -33
- package/dist/lib/platform/ios/xcode/HeaderResolver.js +0 -90
- package/dist/lib/platform/ios/xcode/SaveEventFilter.d.ts +0 -66
- package/dist/lib/platform/ios/xcode/SaveEventFilter.js +0 -142
- package/dist/lib/platform/ios/xcode/XcodeAutomation.d.ts +0 -71
- package/dist/lib/platform/ios/xcode/XcodeAutomation.js +0 -327
- package/dist/lib/platform/ios/xcode/XcodeImportResolver.d.ts +0 -130
- package/dist/lib/platform/ios/xcode/XcodeImportResolver.js +0 -404
- package/dist/lib/platform/ios/xcode/XcodeIntegration.d.ts +0 -89
- package/dist/lib/platform/ios/xcode/XcodeIntegration.js +0 -588
- package/dist/lib/platform/ios/xcode/XcodeWriteUtils.d.ts +0 -99
- package/dist/lib/platform/ios/xcode/XcodeWriteUtils.js +0 -190
- package/dist/lib/service/automation/ActionPipeline.d.ts +0 -34
- package/dist/lib/service/automation/ActionPipeline.js +0 -53
- package/dist/lib/service/automation/AutomationOrchestrator.d.ts +0 -86
- package/dist/lib/service/automation/AutomationOrchestrator.js +0 -57
- package/dist/lib/service/automation/ContextCollector.d.ts +0 -24
- package/dist/lib/service/automation/ContextCollector.js +0 -35
- package/dist/lib/service/automation/DirectiveDetector.d.ts +0 -51
- package/dist/lib/service/automation/DirectiveDetector.js +0 -112
- package/dist/lib/service/automation/FileWatcher.d.ts +0 -51
- package/dist/lib/service/automation/FileWatcher.js +0 -366
- package/dist/lib/service/automation/TriggerResolver.d.ts +0 -36
- package/dist/lib/service/automation/TriggerResolver.js +0 -62
- package/dist/lib/service/automation/handlers/AlinkHandler.d.ts +0 -7
- package/dist/lib/service/automation/handlers/AlinkHandler.js +0 -80
- package/dist/lib/service/automation/handlers/CreateHandler.d.ts +0 -11
- package/dist/lib/service/automation/handlers/CreateHandler.js +0 -170
- package/dist/lib/service/automation/handlers/GuardHandler.d.ts +0 -17
- package/dist/lib/service/automation/handlers/GuardHandler.js +0 -218
- package/dist/lib/service/automation/handlers/HeaderHandler.d.ts +0 -2
- package/dist/lib/service/automation/handlers/HeaderHandler.js +0 -32
- package/dist/lib/service/automation/handlers/SearchHandler.d.ts +0 -11
- package/dist/lib/service/automation/handlers/SearchHandler.js +0 -278
- package/dist/lib/service/snippet/SnippetFactory.d.ts +0 -101
- package/dist/lib/service/snippet/SnippetFactory.js +0 -145
- package/dist/lib/service/snippet/SnippetInstaller.d.ts +0 -91
- package/dist/lib/service/snippet/SnippetInstaller.js +0 -276
- package/dist/lib/service/snippet/codecs/SnippetCodec.d.ts +0 -44
- package/dist/lib/service/snippet/codecs/SnippetCodec.js +0 -35
- package/dist/lib/service/snippet/codecs/VSCodeCodec.d.ts +0 -27
- package/dist/lib/service/snippet/codecs/VSCodeCodec.js +0 -82
- package/dist/scripts/build-native-ui.d.ts +0 -3
- package/dist/scripts/build-native-ui.js +0 -62
- package/dist/scripts/init-snippets.d.ts +0 -30
- package/dist/scripts/init-snippets.js +0 -298
- package/dist/scripts/install-full.d.ts +0 -7
- package/dist/scripts/install-full.js +0 -38
- package/resources/native-ui/README.md +0 -29
- package/resources/native-ui/combined-window.swift +0 -494
- package/resources/native-ui/main.swift +0 -598
- package/scripts/postinstall-safe.mjs +0 -89
|
@@ -1,598 +0,0 @@
|
|
|
1
|
-
import AppKit
|
|
2
|
-
import Foundation
|
|
3
|
-
import Darwin
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* AutoSnippet Native UI Helper (macOS)
|
|
7
|
-
* 提供高级原生列表和预览窗口,支持代码高亮。
|
|
8
|
-
* 注意:SyntaxHighlighter 和 SearchItem 定义在 combined-window.swift 中,避免重复定义
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
// MARK: - List Selection Window
|
|
12
|
-
|
|
13
|
-
class ListSelectionWindowController: NSObject, NSTableViewDataSource, NSTableViewDelegate {
|
|
14
|
-
var items: [String] = []
|
|
15
|
-
var selectedIndex: Int = -1
|
|
16
|
-
private var tableView: NSTableView!
|
|
17
|
-
private var panel: NSPanel!
|
|
18
|
-
|
|
19
|
-
var filteredItems: [String] = []
|
|
20
|
-
var searchField: NSSearchField!
|
|
21
|
-
|
|
22
|
-
func show(items: [String], title: String, prompt: String) -> Int {
|
|
23
|
-
self.items = items
|
|
24
|
-
self.filteredItems = items
|
|
25
|
-
self.selectedIndex = 0
|
|
26
|
-
|
|
27
|
-
// 创建面板
|
|
28
|
-
let panelWidth: CGFloat = 650
|
|
29
|
-
let panelHeight: CGFloat = min(CGFloat(items.count * 32 + 200), 600)
|
|
30
|
-
|
|
31
|
-
panel = NSPanel(
|
|
32
|
-
contentRect: NSRect(x: 0, y: 0, width: panelWidth, height: panelHeight),
|
|
33
|
-
styleMask: [.titled, .closable, .resizable, .fullSizeContentView],
|
|
34
|
-
backing: .buffered,
|
|
35
|
-
defer: false
|
|
36
|
-
)
|
|
37
|
-
panel.title = title
|
|
38
|
-
panel.level = .floating
|
|
39
|
-
panel.center()
|
|
40
|
-
panel.titlebarAppearsTransparent = true
|
|
41
|
-
panel.isMovableByWindowBackground = true
|
|
42
|
-
|
|
43
|
-
// 模糊背景
|
|
44
|
-
let visualEffect = NSVisualEffectView(frame: NSRect(x: 0, y: 0, width: panelWidth, height: panelHeight))
|
|
45
|
-
visualEffect.material = .hudWindow
|
|
46
|
-
visualEffect.blendingMode = .behindWindow
|
|
47
|
-
visualEffect.state = .active
|
|
48
|
-
visualEffect.autoresizingMask = [.width, .height]
|
|
49
|
-
panel.contentView = visualEffect
|
|
50
|
-
|
|
51
|
-
let contentView = NSView(frame: NSRect(x: 0, y: 0, width: panelWidth, height: panelHeight))
|
|
52
|
-
contentView.autoresizingMask = [.width, .height]
|
|
53
|
-
visualEffect.addSubview(contentView)
|
|
54
|
-
|
|
55
|
-
// 提示文本 - 改为标题样式
|
|
56
|
-
let promptLabel = NSTextField(labelWithString: prompt)
|
|
57
|
-
promptLabel.font = NSFont.systemFont(ofSize: 13, weight: .medium)
|
|
58
|
-
promptLabel.textColor = .secondaryLabelColor
|
|
59
|
-
promptLabel.frame = NSRect(x: 20, y: panelHeight - 55, width: panelWidth - 40, height: 20)
|
|
60
|
-
promptLabel.autoresizingMask = [.width, .minYMargin]
|
|
61
|
-
contentView.addSubview(promptLabel)
|
|
62
|
-
|
|
63
|
-
// 搜索框
|
|
64
|
-
searchField = NSSearchField(frame: NSRect(x: 20, y: panelHeight - 95, width: panelWidth - 40, height: 28))
|
|
65
|
-
searchField.placeholderString = "搜索..."
|
|
66
|
-
searchField.autoresizingMask = [.width, .minYMargin]
|
|
67
|
-
searchField.target = self
|
|
68
|
-
searchField.action = #selector(searchFieldDidChange)
|
|
69
|
-
searchField.font = NSFont.systemFont(ofSize: 13)
|
|
70
|
-
contentView.addSubview(searchField)
|
|
71
|
-
|
|
72
|
-
// 创建表格
|
|
73
|
-
let scrollView = NSScrollView(frame: NSRect(x: 20, y: 60, width: panelWidth - 40, height: panelHeight - 175))
|
|
74
|
-
scrollView.hasVerticalScroller = true
|
|
75
|
-
scrollView.borderType = .noBorder
|
|
76
|
-
scrollView.autoresizingMask = [.width, .height]
|
|
77
|
-
scrollView.wantsLayer = true
|
|
78
|
-
scrollView.layer?.cornerRadius = 8
|
|
79
|
-
scrollView.layer?.masksToBounds = true
|
|
80
|
-
// 使用 overlay 滚动条
|
|
81
|
-
scrollView.scrollerStyle = .overlay
|
|
82
|
-
scrollView.autohidesScrollers = true
|
|
83
|
-
scrollView.scrollerKnobStyle = .light
|
|
84
|
-
|
|
85
|
-
tableView = NSTableView()
|
|
86
|
-
tableView.headerView = nil
|
|
87
|
-
tableView.rowHeight = 52 // 增加行高以容纳两行标题
|
|
88
|
-
tableView.intercellSpacing = NSSize(width: 0, height: 1)
|
|
89
|
-
// Xcode 深色背景:#1F1F24
|
|
90
|
-
tableView.backgroundColor = NSColor(calibratedRed: 0.12, green: 0.12, blue: 0.14, alpha: 1.0)
|
|
91
|
-
tableView.usesAlternatingRowBackgroundColors = false
|
|
92
|
-
tableView.selectionHighlightStyle = .regular
|
|
93
|
-
tableView.doubleAction = #selector(doubleClicked)
|
|
94
|
-
tableView.target = self
|
|
95
|
-
|
|
96
|
-
let column = NSTableColumn(identifier: NSUserInterfaceItemIdentifier("item"))
|
|
97
|
-
column.width = panelWidth - 60
|
|
98
|
-
tableView.addTableColumn(column)
|
|
99
|
-
|
|
100
|
-
tableView.dataSource = self
|
|
101
|
-
tableView.delegate = self
|
|
102
|
-
|
|
103
|
-
scrollView.documentView = tableView
|
|
104
|
-
contentView.addSubview(scrollView)
|
|
105
|
-
|
|
106
|
-
// 按钮
|
|
107
|
-
let cancelButton = NSButton(title: "取消", target: self, action: #selector(cancelClicked))
|
|
108
|
-
cancelButton.bezelStyle = .rounded
|
|
109
|
-
cancelButton.frame = NSRect(x: panelWidth - 200, y: 15, width: 90, height: 32)
|
|
110
|
-
cancelButton.keyEquivalent = "\u{1b}" // Escape
|
|
111
|
-
cancelButton.wantsLayer = true
|
|
112
|
-
cancelButton.layer?.cornerRadius = 6
|
|
113
|
-
cancelButton.autoresizingMask = [.minXMargin, .maxYMargin]
|
|
114
|
-
contentView.addSubview(cancelButton)
|
|
115
|
-
|
|
116
|
-
let okButton = NSButton(title: "确定", target: self, action: #selector(okClicked))
|
|
117
|
-
okButton.bezelStyle = .rounded
|
|
118
|
-
okButton.frame = NSRect(x: panelWidth - 100, y: 15, width: 90, height: 32)
|
|
119
|
-
okButton.keyEquivalent = "\r" // Enter
|
|
120
|
-
okButton.wantsLayer = true
|
|
121
|
-
okButton.layer?.cornerRadius = 6
|
|
122
|
-
okButton.autoresizingMask = [.minXMargin, .maxYMargin]
|
|
123
|
-
contentView.addSubview(okButton)
|
|
124
|
-
|
|
125
|
-
// 显示并运行
|
|
126
|
-
tableView.reloadData()
|
|
127
|
-
if !filteredItems.isEmpty {
|
|
128
|
-
tableView.selectRowIndexes(IndexSet(integer: 0), byExtendingSelection: false)
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// 淡入动画
|
|
132
|
-
panel.alphaValue = 0
|
|
133
|
-
NSApp.activate(ignoringOtherApps: true)
|
|
134
|
-
panel.makeKeyAndOrderFront(nil)
|
|
135
|
-
panel.makeFirstResponder(searchField)
|
|
136
|
-
|
|
137
|
-
NSAnimationContext.runAnimationGroup({ context in
|
|
138
|
-
context.duration = 0.2
|
|
139
|
-
panel.animator().alphaValue = 1.0
|
|
140
|
-
})
|
|
141
|
-
|
|
142
|
-
NSApp.runModal(for: panel)
|
|
143
|
-
|
|
144
|
-
return selectedIndex
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
// MARK: - 搜索功能
|
|
148
|
-
|
|
149
|
-
@objc func searchFieldDidChange() {
|
|
150
|
-
let searchText = searchField.stringValue.lowercased()
|
|
151
|
-
if searchText.isEmpty {
|
|
152
|
-
filteredItems = items
|
|
153
|
-
} else {
|
|
154
|
-
filteredItems = items.filter { $0.lowercased().contains(searchText) }
|
|
155
|
-
}
|
|
156
|
-
tableView.reloadData()
|
|
157
|
-
if !filteredItems.isEmpty {
|
|
158
|
-
tableView.selectRowIndexes(IndexSet(integer: 0), byExtendingSelection: false)
|
|
159
|
-
selectedIndex = 0
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
// MARK: - NSTableViewDataSource
|
|
164
|
-
|
|
165
|
-
func numberOfRows(in tableView: NSTableView) -> Int {
|
|
166
|
-
return filteredItems.count
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// MARK: - NSTableViewDelegate
|
|
170
|
-
|
|
171
|
-
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
|
|
172
|
-
let cellIdentifier = NSUserInterfaceItemIdentifier("ItemCell")
|
|
173
|
-
|
|
174
|
-
var cellView = tableView.makeView(withIdentifier: cellIdentifier, owner: nil) as? NSTableCellView
|
|
175
|
-
if cellView == nil {
|
|
176
|
-
cellView = NSTableCellView()
|
|
177
|
-
cellView?.identifier = cellIdentifier
|
|
178
|
-
|
|
179
|
-
let textField = NSTextField(labelWithString: "")
|
|
180
|
-
textField.font = NSFont.systemFont(ofSize: 13)
|
|
181
|
-
textField.lineBreakMode = .byWordWrapping // 改为自动换行
|
|
182
|
-
textField.maximumNumberOfLines = 2 // 最多显示两行
|
|
183
|
-
textField.translatesAutoresizingMaskIntoConstraints = false
|
|
184
|
-
cellView?.addSubview(textField)
|
|
185
|
-
cellView?.textField = textField
|
|
186
|
-
|
|
187
|
-
NSLayoutConstraint.activate([
|
|
188
|
-
textField.leadingAnchor.constraint(equalTo: cellView!.leadingAnchor, constant: 12),
|
|
189
|
-
textField.trailingAnchor.constraint(equalTo: cellView!.trailingAnchor, constant: -12),
|
|
190
|
-
textField.topAnchor.constraint(equalTo: cellView!.topAnchor, constant: 8),
|
|
191
|
-
textField.bottomAnchor.constraint(lessThanOrEqualTo: cellView!.bottomAnchor, constant: -8)
|
|
192
|
-
])
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
cellView?.textField?.stringValue = filteredItems[row]
|
|
196
|
-
return cellView
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
func tableViewSelectionDidChange(_ notification: Notification) {
|
|
200
|
-
selectedIndex = tableView.selectedRow
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
@objc func doubleClicked() {
|
|
204
|
-
if tableView.clickedRow >= 0 {
|
|
205
|
-
selectedIndex = tableView.clickedRow
|
|
206
|
-
panel.close()
|
|
207
|
-
NSApp.stopModal()
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
@objc func okClicked() {
|
|
212
|
-
selectedIndex = tableView.selectedRow
|
|
213
|
-
panel.close()
|
|
214
|
-
NSApp.stopModal()
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
@objc func cancelClicked() {
|
|
218
|
-
selectedIndex = -1
|
|
219
|
-
panel.close()
|
|
220
|
-
NSApp.stopModal()
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
// MARK: - Code Preview Window
|
|
225
|
-
|
|
226
|
-
class CodePreviewWindowController {
|
|
227
|
-
enum PreviewResult {
|
|
228
|
-
case confirmed // 用户点击了“立即插入”
|
|
229
|
-
case cancelled // 用户点击了“取消”
|
|
230
|
-
case returnToList // 用户点击了“返回”
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
func show(title: String, code: String) -> PreviewResult {
|
|
234
|
-
let panelWidth: CGFloat = 900
|
|
235
|
-
let panelHeight: CGFloat = 520
|
|
236
|
-
|
|
237
|
-
let panel = NSPanel(
|
|
238
|
-
contentRect: NSRect(x: 0, y: 0, width: panelWidth, height: panelHeight),
|
|
239
|
-
styleMask: [.titled, .closable, .resizable, .fullSizeContentView],
|
|
240
|
-
backing: .buffered,
|
|
241
|
-
defer: false
|
|
242
|
-
)
|
|
243
|
-
panel.title = "代码预览"
|
|
244
|
-
panel.level = .floating
|
|
245
|
-
panel.center()
|
|
246
|
-
panel.minSize = NSSize(width: 400, height: 300)
|
|
247
|
-
panel.titlebarAppearsTransparent = true
|
|
248
|
-
panel.isMovableByWindowBackground = true
|
|
249
|
-
|
|
250
|
-
// 模糊背景
|
|
251
|
-
let visualEffect = NSVisualEffectView(frame: NSRect(x: 0, y: 0, width: panelWidth, height: panelHeight))
|
|
252
|
-
visualEffect.material = .hudWindow
|
|
253
|
-
visualEffect.blendingMode = .behindWindow
|
|
254
|
-
visualEffect.state = .active
|
|
255
|
-
visualEffect.autoresizingMask = [.width, .height]
|
|
256
|
-
panel.contentView = visualEffect
|
|
257
|
-
|
|
258
|
-
let contentView = NSView(frame: NSRect(x: 0, y: 0, width: panelWidth, height: panelHeight))
|
|
259
|
-
contentView.autoresizingMask = [.width, .height]
|
|
260
|
-
visualEffect.addSubview(contentView)
|
|
261
|
-
|
|
262
|
-
// 标题 - 相似度百分比前置
|
|
263
|
-
let titleText: String
|
|
264
|
-
// 使用正则提取相似度百分比,例如 "xxx.md (47%)" -> "(47%) xxx.md"
|
|
265
|
-
if let regex = try? NSRegularExpression(pattern: "^(.+?)\\s*\\((\\d+%)\\)$", options: []),
|
|
266
|
-
let match = regex.firstMatch(in: title, options: [], range: NSRange(title.startIndex..., in: title)),
|
|
267
|
-
let nameRange = Range(match.range(at: 1), in: title),
|
|
268
|
-
let percentRange = Range(match.range(at: 2), in: title) {
|
|
269
|
-
let name = String(title[nameRange])
|
|
270
|
-
let percent = String(title[percentRange])
|
|
271
|
-
titleText = "即将插入: (\(percent)) \(name)"
|
|
272
|
-
} else {
|
|
273
|
-
titleText = "即将插入: \(title)"
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
let titleLabel = NSTextField(labelWithString: titleText)
|
|
277
|
-
titleLabel.font = NSFont.systemFont(ofSize: 14, weight: .semibold)
|
|
278
|
-
titleLabel.textColor = .labelColor
|
|
279
|
-
titleLabel.frame = NSRect(x: 20, y: panelHeight - 65, width: panelWidth - 40, height: 40) // 增加高度到40,调整y位置
|
|
280
|
-
titleLabel.autoresizingMask = [.width, .minYMargin]
|
|
281
|
-
titleLabel.lineBreakMode = .byWordWrapping // 改为自动换行
|
|
282
|
-
titleLabel.maximumNumberOfLines = 2 // 最多显示两行
|
|
283
|
-
contentView.addSubview(titleLabel)
|
|
284
|
-
|
|
285
|
-
// 代码区域(带语法高亮)
|
|
286
|
-
let scrollView = NSScrollView(frame: NSRect(x: 20, y: 60, width: panelWidth - 40, height: panelHeight - 135))
|
|
287
|
-
scrollView.hasVerticalScroller = true
|
|
288
|
-
scrollView.hasHorizontalScroller = true
|
|
289
|
-
scrollView.borderType = .noBorder
|
|
290
|
-
scrollView.autoresizingMask = [.width, .height]
|
|
291
|
-
scrollView.wantsLayer = true
|
|
292
|
-
scrollView.layer?.cornerRadius = 8
|
|
293
|
-
scrollView.layer?.masksToBounds = true
|
|
294
|
-
// 设置 scrollView 背景色为 Xcode 深色
|
|
295
|
-
scrollView.backgroundColor = NSColor(calibratedRed: 0.12, green: 0.12, blue: 0.14, alpha: 1.0)
|
|
296
|
-
// 使用 overlay 滚动条
|
|
297
|
-
scrollView.scrollerStyle = .overlay
|
|
298
|
-
scrollView.autohidesScrollers = true
|
|
299
|
-
scrollView.scrollerKnobStyle = .light
|
|
300
|
-
scrollView.horizontalScrollElasticity = .automatic
|
|
301
|
-
scrollView.verticalScrollElasticity = .automatic
|
|
302
|
-
|
|
303
|
-
let textView = NSTextView(frame: scrollView.bounds)
|
|
304
|
-
textView.isEditable = false
|
|
305
|
-
textView.isSelectable = true
|
|
306
|
-
// Xcode 深色背景:#1F1F24
|
|
307
|
-
textView.backgroundColor = NSColor(calibratedRed: 0.12, green: 0.12, blue: 0.14, alpha: 1.0)
|
|
308
|
-
textView.textContainerInset = NSSize(width: 16, height: 16)
|
|
309
|
-
textView.isHorizontallyResizable = true
|
|
310
|
-
textView.isVerticallyResizable = true
|
|
311
|
-
textView.textContainer?.widthTracksTextView = false
|
|
312
|
-
textView.textContainer?.heightTracksTextView = false
|
|
313
|
-
textView.textContainer?.containerSize = NSSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
|
|
314
|
-
textView.autoresizingMask = []
|
|
315
|
-
|
|
316
|
-
// 增加右侧边距,避免滚动条遮挡代码
|
|
317
|
-
textView.textContainer?.lineFragmentPadding = 10 // 左右各 10px
|
|
318
|
-
if let textContainer = textView.textContainer {
|
|
319
|
-
// 设置右侧额外边距
|
|
320
|
-
textContainer.size = NSSize(width: scrollView.bounds.width - 40, height: CGFloat.greatestFiniteMagnitude)
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
// 应用语法高亮
|
|
324
|
-
let processedCode = code.replacingOccurrences(of: "\\n", with: "\n")
|
|
325
|
-
let highlightedCode = SyntaxHighlighter.highlight(processedCode)
|
|
326
|
-
textView.textStorage?.setAttributedString(highlightedCode)
|
|
327
|
-
|
|
328
|
-
scrollView.documentView = textView
|
|
329
|
-
contentView.addSubview(scrollView)
|
|
330
|
-
|
|
331
|
-
// 按钮
|
|
332
|
-
let backButton = NSButton(title: "← 返回", target: nil, action: nil)
|
|
333
|
-
backButton.bezelStyle = .rounded
|
|
334
|
-
backButton.frame = NSRect(x: 20, y: 15, width: 90, height: 32)
|
|
335
|
-
backButton.autoresizingMask = [.maxXMargin, .maxYMargin]
|
|
336
|
-
backButton.wantsLayer = true
|
|
337
|
-
backButton.layer?.cornerRadius = 6
|
|
338
|
-
contentView.addSubview(backButton)
|
|
339
|
-
|
|
340
|
-
let cancelButton = NSButton(title: "取消", target: nil, action: nil)
|
|
341
|
-
cancelButton.bezelStyle = .rounded
|
|
342
|
-
cancelButton.frame = NSRect(x: panelWidth - 310, y: 15, width: 90, height: 32)
|
|
343
|
-
cancelButton.keyEquivalent = "\u{1b}"
|
|
344
|
-
cancelButton.autoresizingMask = [.minXMargin, .maxYMargin]
|
|
345
|
-
cancelButton.wantsLayer = true
|
|
346
|
-
cancelButton.layer?.cornerRadius = 6
|
|
347
|
-
contentView.addSubview(cancelButton)
|
|
348
|
-
|
|
349
|
-
let copyButton = NSButton(title: "复制代码", target: nil, action: nil)
|
|
350
|
-
copyButton.bezelStyle = .rounded
|
|
351
|
-
copyButton.frame = NSRect(x: panelWidth - 210, y: 15, width: 90, height: 32)
|
|
352
|
-
copyButton.autoresizingMask = [.minXMargin, .maxYMargin]
|
|
353
|
-
copyButton.wantsLayer = true
|
|
354
|
-
copyButton.layer?.cornerRadius = 6
|
|
355
|
-
contentView.addSubview(copyButton)
|
|
356
|
-
|
|
357
|
-
let okButton = NSButton(title: "立即插入", target: nil, action: nil)
|
|
358
|
-
okButton.bezelStyle = .rounded
|
|
359
|
-
okButton.frame = NSRect(x: panelWidth - 110, y: 15, width: 90, height: 32)
|
|
360
|
-
okButton.keyEquivalent = "\r"
|
|
361
|
-
okButton.autoresizingMask = [.minXMargin, .maxYMargin]
|
|
362
|
-
okButton.wantsLayer = true
|
|
363
|
-
okButton.layer?.cornerRadius = 6
|
|
364
|
-
contentView.addSubview(okButton)
|
|
365
|
-
|
|
366
|
-
class ButtonHandler {
|
|
367
|
-
var result = false
|
|
368
|
-
var shouldReturnToList = false // 新增:标记是否需要返回列表
|
|
369
|
-
var panel: NSPanel
|
|
370
|
-
var codeText: String
|
|
371
|
-
|
|
372
|
-
init(panel: NSPanel, codeText: String) {
|
|
373
|
-
self.panel = panel
|
|
374
|
-
self.codeText = codeText
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
@objc func okClicked() {
|
|
378
|
-
// 插入时也只使用纯代码
|
|
379
|
-
let pureCode = extractPureCode(codeText)
|
|
380
|
-
let pasteboard = NSPasteboard.general
|
|
381
|
-
pasteboard.clearContents()
|
|
382
|
-
pasteboard.setString(pureCode, forType: .string)
|
|
383
|
-
result = true
|
|
384
|
-
panel.close()
|
|
385
|
-
NSApp.stopModal()
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
@objc func cancelClicked() {
|
|
389
|
-
result = false
|
|
390
|
-
panel.close()
|
|
391
|
-
NSApp.stopModal()
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
@objc func backClicked() {
|
|
395
|
-
// 返回功能:设置标志并关闭窗口
|
|
396
|
-
result = false
|
|
397
|
-
shouldReturnToList = true
|
|
398
|
-
panel.close()
|
|
399
|
-
NSApp.stopModal()
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
@objc func copyClicked() {
|
|
403
|
-
let pasteboard = NSPasteboard.general
|
|
404
|
-
pasteboard.clearContents()
|
|
405
|
-
// 只复制纯代码,去掉 markdown 代码块标记
|
|
406
|
-
let pureCode = extractPureCode(codeText)
|
|
407
|
-
pasteboard.setString(pureCode, forType: .string)
|
|
408
|
-
print("✅ 代码已复制到剪贴板")
|
|
409
|
-
// 复制后关闭窗口
|
|
410
|
-
panel.close()
|
|
411
|
-
NSApp.stopModal()
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
private func extractPureCode(_ code: String) -> String {
|
|
415
|
-
var lines = code.split(separator: "\n", omittingEmptySubsequences: false).map(String.init)
|
|
416
|
-
|
|
417
|
-
// 移除首尾的代码块标记(```swift、```等)
|
|
418
|
-
while !lines.isEmpty && (lines.first?.trimmingCharacters(in: .whitespaces).starts(with: "```") ?? false) {
|
|
419
|
-
lines.removeFirst()
|
|
420
|
-
}
|
|
421
|
-
while !lines.isEmpty && (lines.last?.trimmingCharacters(in: .whitespaces).starts(with: "```") ?? false) {
|
|
422
|
-
lines.removeLast()
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
return lines.joined(separator: "\n")
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
let handler = ButtonHandler(panel: panel, codeText: processedCode)
|
|
430
|
-
backButton.target = handler
|
|
431
|
-
backButton.action = #selector(ButtonHandler.backClicked)
|
|
432
|
-
cancelButton.target = handler
|
|
433
|
-
cancelButton.action = #selector(ButtonHandler.cancelClicked)
|
|
434
|
-
copyButton.target = handler
|
|
435
|
-
copyButton.action = #selector(ButtonHandler.copyClicked)
|
|
436
|
-
okButton.target = handler
|
|
437
|
-
okButton.action = #selector(ButtonHandler.okClicked)
|
|
438
|
-
|
|
439
|
-
// 淡入动画
|
|
440
|
-
panel.alphaValue = 0
|
|
441
|
-
NSApp.activate(ignoringOtherApps: true)
|
|
442
|
-
panel.makeKeyAndOrderFront(nil)
|
|
443
|
-
|
|
444
|
-
NSAnimationContext.runAnimationGroup({ context in
|
|
445
|
-
context.duration = 0.2
|
|
446
|
-
panel.animator().alphaValue = 1.0
|
|
447
|
-
})
|
|
448
|
-
|
|
449
|
-
NSApp.runModal(for: panel)
|
|
450
|
-
|
|
451
|
-
// 根据状态返回不同的结果
|
|
452
|
-
if handler.shouldReturnToList {
|
|
453
|
-
return .returnToList
|
|
454
|
-
} else if handler.result {
|
|
455
|
-
return .confirmed
|
|
456
|
-
} else {
|
|
457
|
-
return .cancelled
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
// MARK: - Main
|
|
463
|
-
|
|
464
|
-
func printUsage() {
|
|
465
|
-
print("Usage:")
|
|
466
|
-
print(" native-ui list \"Title 1\" \"Title 2\" ...")
|
|
467
|
-
print(" native-ui preview \"Title\" \"Code Content\"")
|
|
468
|
-
print(" native-ui combined <keyword> <json_data> # 新:组合窗口")
|
|
469
|
-
fflush(stdout)
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
// MARK: - Combined Handler
|
|
473
|
-
// 注意:SearchItem 结构体定义在 combined-window.swift 中
|
|
474
|
-
|
|
475
|
-
func handleCombined(_ args: [String]) {
|
|
476
|
-
guard args.count >= 2 else {
|
|
477
|
-
fputs("native-ui combined: 需要 keyword 和 json_data 两个参数\n", stderr)
|
|
478
|
-
fflush(stderr)
|
|
479
|
-
exit(1)
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
let keyword = args[0]
|
|
483
|
-
let jsonData = args[1]
|
|
484
|
-
|
|
485
|
-
// 解析 JSON 数据
|
|
486
|
-
guard let data = jsonData.data(using: .utf8),
|
|
487
|
-
let json = try? JSONSerialization.jsonObject(with: data) as? [[String: Any]] else {
|
|
488
|
-
fputs("native-ui combined: JSON 解析失败\n", stderr)
|
|
489
|
-
fflush(stderr)
|
|
490
|
-
exit(1)
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
var items: [SearchItem] = []
|
|
494
|
-
for item in json {
|
|
495
|
-
if let title = item["title"] as? String, let code = item["code"] as? String {
|
|
496
|
-
let explanation = item["explanation"] as? String ?? ""
|
|
497
|
-
let groupSize = (item["groupSize"] as? NSNumber)?.intValue ?? 0
|
|
498
|
-
items.append(SearchItem(title: title, code: code, explanation: explanation, groupSize: groupSize))
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
if items.isEmpty {
|
|
503
|
-
fputs("native-ui combined: 无有效数据\n", stderr)
|
|
504
|
-
fflush(stderr)
|
|
505
|
-
exit(1)
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
let app = NSApplication.shared
|
|
509
|
-
app.setActivationPolicy(.accessory)
|
|
510
|
-
|
|
511
|
-
let controller = CombinedSearchWindowController()
|
|
512
|
-
let result = controller.show(items: items, keyword: keyword)
|
|
513
|
-
|
|
514
|
-
switch result {
|
|
515
|
-
case .confirmed(let index):
|
|
516
|
-
print(index)
|
|
517
|
-
exit(0)
|
|
518
|
-
case .cancelled:
|
|
519
|
-
exit(1)
|
|
520
|
-
}
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
func handleList(_ items: [String]) {
|
|
524
|
-
if items.isEmpty {
|
|
525
|
-
fputs("native-ui list: 至少需要一个选项\n", stderr)
|
|
526
|
-
fflush(stderr)
|
|
527
|
-
exit(1)
|
|
528
|
-
}
|
|
529
|
-
|
|
530
|
-
let app = NSApplication.shared
|
|
531
|
-
app.setActivationPolicy(.accessory)
|
|
532
|
-
|
|
533
|
-
let controller = ListSelectionWindowController()
|
|
534
|
-
let selectedIndex = controller.show(
|
|
535
|
-
items: items,
|
|
536
|
-
title: "AutoSnippet 搜索结果",
|
|
537
|
-
prompt: "找到以下匹配项,请选择一个进行插入:"
|
|
538
|
-
)
|
|
539
|
-
|
|
540
|
-
if selectedIndex >= 0 {
|
|
541
|
-
print(selectedIndex)
|
|
542
|
-
exit(0)
|
|
543
|
-
} else {
|
|
544
|
-
exit(1)
|
|
545
|
-
}
|
|
546
|
-
}
|
|
547
|
-
|
|
548
|
-
func handlePreview(_ args: [String]) {
|
|
549
|
-
guard args.count >= 2 else {
|
|
550
|
-
fputs("native-ui preview: 需要 title 和 code 两个参数\n", stderr)
|
|
551
|
-
fflush(stderr)
|
|
552
|
-
exit(1)
|
|
553
|
-
}
|
|
554
|
-
|
|
555
|
-
let title = args[0]
|
|
556
|
-
let code = args[1]
|
|
557
|
-
|
|
558
|
-
let app = NSApplication.shared
|
|
559
|
-
app.setActivationPolicy(.accessory)
|
|
560
|
-
|
|
561
|
-
let controller = CodePreviewWindowController()
|
|
562
|
-
let result = controller.show(title: title, code: code)
|
|
563
|
-
|
|
564
|
-
switch result {
|
|
565
|
-
case .confirmed:
|
|
566
|
-
exit(0) // 用户确认插入
|
|
567
|
-
case .returnToList:
|
|
568
|
-
exit(2) // 用户要求返回列表
|
|
569
|
-
case .cancelled:
|
|
570
|
-
exit(1) // 用户取消
|
|
571
|
-
}
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
// Main logic
|
|
575
|
-
let args = CommandLine.arguments
|
|
576
|
-
guard args.count > 1 else {
|
|
577
|
-
printUsage()
|
|
578
|
-
exit(1)
|
|
579
|
-
}
|
|
580
|
-
|
|
581
|
-
let command = args[1]
|
|
582
|
-
if command == "--help" || command == "-h" {
|
|
583
|
-
printUsage()
|
|
584
|
-
exit(0)
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
switch command {
|
|
588
|
-
case "list":
|
|
589
|
-
handleList(Array(args.dropFirst(2)))
|
|
590
|
-
case "preview":
|
|
591
|
-
handlePreview(Array(args.dropFirst(2)))
|
|
592
|
-
case "combined":
|
|
593
|
-
handleCombined(Array(args.dropFirst(2)))
|
|
594
|
-
default:
|
|
595
|
-
fputs("未知子命令: \(command)\n", stderr)
|
|
596
|
-
printUsage()
|
|
597
|
-
exit(1)
|
|
598
|
-
}
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 安全的 postinstall 脚本
|
|
5
|
-
*
|
|
6
|
-
* 在 macOS 上如果 native-ui 二进制缺失但 Swift 源码存在,
|
|
7
|
-
* 尝试自动编译(需要 Xcode Command Line Tools)。
|
|
8
|
-
* 编译失败不阻塞安装流程。
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import { execSync } from 'node:child_process';
|
|
12
|
-
|
|
13
|
-
const __dirname = import.meta.dirname;
|
|
14
|
-
|
|
15
|
-
import fs from 'node:fs';
|
|
16
|
-
import path from 'node:path';
|
|
17
|
-
|
|
18
|
-
const root = path.resolve(__dirname, '..');
|
|
19
|
-
|
|
20
|
-
const NATIVE_UI_BIN = path.join(root, 'resources', 'native-ui', 'native-ui');
|
|
21
|
-
const NATIVE_UI_SRC = path.join(root, 'resources', 'native-ui', 'main.swift');
|
|
22
|
-
const NATIVE_UI_COMBINED = path.join(root, 'resources', 'native-ui', 'combined-window.swift');
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* 在 macOS 上尝试编译 native-ui(如果二进制缺失)
|
|
26
|
-
*/
|
|
27
|
-
function tryBuildNativeUi() {
|
|
28
|
-
// 非 macOS 跳过
|
|
29
|
-
if (process.platform !== 'darwin') {
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// 已有二进制,跳过
|
|
34
|
-
if (fs.existsSync(NATIVE_UI_BIN)) {
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// 检查源码是否存在
|
|
39
|
-
if (!fs.existsSync(NATIVE_UI_SRC) || !fs.existsSync(NATIVE_UI_COMBINED)) {
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// 检查 swiftc 是否可用
|
|
44
|
-
try {
|
|
45
|
-
execSync('which swiftc', { stdio: 'pipe' });
|
|
46
|
-
} catch {
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// 尝试编译
|
|
51
|
-
try {
|
|
52
|
-
execSync(
|
|
53
|
-
`swiftc "${NATIVE_UI_SRC}" "${NATIVE_UI_COMBINED}" -o "${NATIVE_UI_BIN}" -framework AppKit`,
|
|
54
|
-
{ cwd: root, stdio: 'pipe', timeout: 120_000 }
|
|
55
|
-
);
|
|
56
|
-
if (fs.existsSync(NATIVE_UI_BIN)) {
|
|
57
|
-
}
|
|
58
|
-
} catch {}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// 检查预构建的二进制文件
|
|
62
|
-
function checkBinaries() {
|
|
63
|
-
const checks = [
|
|
64
|
-
{
|
|
65
|
-
name: 'Native UI',
|
|
66
|
-
path: NATIVE_UI_BIN,
|
|
67
|
-
optional: true,
|
|
68
|
-
platform: 'darwin',
|
|
69
|
-
},
|
|
70
|
-
];
|
|
71
|
-
|
|
72
|
-
checks.forEach(({ name, path: binPath, optional, platform }) => {
|
|
73
|
-
// 跳过非目标平台
|
|
74
|
-
if (platform && process.platform !== platform) {
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (fs.existsSync(binPath)) {
|
|
79
|
-
const stat = fs.statSync(binPath);
|
|
80
|
-
const _sizeKB = (stat.size / 1024).toFixed(1);
|
|
81
|
-
} else if (optional) {
|
|
82
|
-
} else {
|
|
83
|
-
console.warn(`⚠️ ${name}: 未找到`);
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
tryBuildNativeUi();
|
|
89
|
-
checkBinaries();
|