-
-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathQueryEditorView.swift
More file actions
177 lines (152 loc) · 5.4 KB
/
QueryEditorView.swift
File metadata and controls
177 lines (152 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
// QueryEditorView.swift
// TablePro
//
// SQL query editor wrapper with toolbar
//
import CodeEditSourceEditor
import os
import SwiftUI
/// SQL query editor view with execute button
struct QueryEditorView: View {
private static let logger = Logger(subsystem: "com.TablePro", category: "QueryEditorView")
@Environment(AppState.self) private var appState
@Binding var queryText: String
@Binding var cursorPositions: [CursorPosition]
var onExecute: () -> Void
var schemaProvider: SQLSchemaProvider?
var databaseType: DatabaseType?
var onCloseTab: (() -> Void)?
var onExecuteQuery: (() -> Void)?
var onExplain: ((ClickHouseExplainVariant?) -> Void)?
var onAIExplain: ((String) -> Void)?
var onAIOptimize: ((String) -> Void)?
@State private var vimMode: VimMode = .normal
var body: some View {
let hasQuery = appState.hasQueryText
VStack(alignment: .leading, spacing: 0) {
// Editor header with toolbar (above editor, higher z-index)
editorToolbar(hasQueryText: hasQuery)
.zIndex(1)
Divider()
// SQL Editor (CodeEditSourceEditor-based with tree-sitter highlighting)
SQLEditorView(
text: $queryText,
cursorPositions: $cursorPositions,
schemaProvider: schemaProvider,
databaseType: databaseType,
vimMode: $vimMode,
onCloseTab: onCloseTab,
onExecuteQuery: onExecuteQuery,
onAIExplain: onAIExplain,
onAIOptimize: onAIOptimize
)
.frame(minHeight: 100)
.clipped()
}
.background(Color(nsColor: .textBackgroundColor))
}
// MARK: - Toolbar
private func editorToolbar(hasQueryText: Bool) -> some View {
HStack {
Text("Query")
.font(.headline)
.foregroundStyle(.secondary)
if AppSettingsManager.shared.editor.vimModeEnabled {
VimModeIndicatorView(mode: vimMode)
}
Spacer()
// Clear button
Button(action: { queryText = "" }) {
Image(systemName: "trash")
}
.buttonStyle(.borderless)
.help("Clear Query")
// Format button
Button(action: formatQuery) {
Image(systemName: "text.alignleft")
}
.buttonStyle(.borderless)
.help("Format Query (⌥⌘F)")
.keyboardShortcut("f", modifiers: [.option, .command])
Divider()
.frame(height: 16)
if databaseType == .clickhouse {
Menu {
ForEach(ClickHouseExplainVariant.allCases) { variant in
Button(variant.rawValue) {
onExplain?(variant)
}
}
} label: {
HStack(spacing: 4) {
Image(systemName: "chart.bar.doc.horizontal")
Text("Explain")
}
}
.menuStyle(.borderlessButton)
.fixedSize()
.disabled(!hasQueryText)
} else {
Button {
onExplain?(nil)
} label: {
HStack(spacing: 4) {
Image(systemName: "chart.bar.doc.horizontal")
Text("Explain")
}
}
.buttonStyle(.bordered)
.controlSize(.small)
.disabled(!hasQueryText)
}
// Execute button
Button(action: onExecute) {
HStack(spacing: 4) {
Image(systemName: "play.fill")
Text("Execute")
}
}
.buttonStyle(.borderedProminent)
.controlSize(.small)
.keyboardShortcut(.return, modifiers: .command)
}
.padding(.horizontal, 12)
.padding(.vertical, 8)
.background(Color(nsColor: .windowBackgroundColor))
}
// MARK: - Helpers
private func formatQuery() {
// Get current database type
let dbType = databaseType ?? .mysql
// Create formatter service
let formatter = SQLFormatterService()
let options = SQLFormatterOptions.default
let cursorOffset = cursorPositions.first?.range.location ?? 0
do {
// Format SQL with cursor preservation
let result = try formatter.format(
queryText,
dialect: dbType,
cursorOffset: cursorOffset,
options: options
)
// Update text and cursor position
queryText = result.formattedSQL
if let newCursor = result.cursorOffset {
cursorPositions = [CursorPosition(range: NSRange(location: newCursor, length: 0))]
}
} catch {
Self.logger.error("SQL Formatting error: \(error.localizedDescription, privacy: .public)")
}
}
}
#Preview {
QueryEditorView(
queryText: .constant("SELECT * FROM users\nWHERE active = true\nORDER BY created_at DESC;"),
cursorPositions: .constant([]),
onExecute: {},
databaseType: .mysql
)
.frame(width: 600, height: 200)
}