Skip to content

Commit a7605f8

Browse files
committed
fix: address PR review findings from CodeRabbit
1 parent 96c5e3e commit a7605f8

7 files changed

Lines changed: 30 additions & 29 deletions

File tree

LocalPackages/CodeEditSourceEditor/Sources/CodeEditSourceEditor/CodeSuggestion/Window/SuggestionController.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ public final class SuggestionController: NSWindowController {
147147
}
148148
if textView.view.window == nil {
149149
self.close()
150-
} else if let firstResponder = window.firstResponder as? NSView,
150+
} else if textView.view.window === window,
151+
let firstResponder = window.firstResponder as? NSView,
151152
!firstResponder.isDescendant(of: textView.view) {
152153
self.close()
153154
}

TablePro/ContentView.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ struct ContentView: View {
1818
let payload: EditorTabPayload?
1919

2020
@State private var currentSession: ConnectionSession?
21+
@State private var closingSessionId: UUID?
2122
@State private var columnVisibility: NavigationSplitViewVisibility = .all
2223
@State private var showNewConnectionSheet = false
2324
@State private var showEditConnectionSheet = false
@@ -70,6 +71,7 @@ struct ContentView: View {
7071
// Right sidebar toggle is handled by MainContentView (has the binding)
7172
// Left sidebar toggle uses native NSSplitViewController.toggleSidebar via responder chain
7273
.onChange(of: DatabaseManager.shared.currentSessionId, initial: true) { _, newSessionId in
74+
guard closingSessionId == nil else { return }
7375
let ourConnectionId = payload?.connectionId
7476
if ourConnectionId != nil {
7577
guard newSessionId == ourConnectionId else { return }
@@ -331,6 +333,7 @@ struct ContentView: View {
331333
// MARK: - Connection Status
332334

333335
private func handleConnectionStatusChange() {
336+
guard closingSessionId == nil else { return }
334337
let sessions = DatabaseManager.shared.activeSessions
335338
let connectionId = payload?.connectionId ?? currentSession?.id ?? DatabaseManager.shared.currentSessionId
336339
guard let sid = connectionId else {
@@ -339,6 +342,7 @@ struct ContentView: View {
339342
}
340343
guard let newSession = sessions[sid] else {
341344
if currentSession?.id == sid {
345+
closingSessionId = sid
342346
rightPanelState?.teardown()
343347
rightPanelState = nil
344348
sessionState?.coordinator.teardown()

TablePro/Core/Services/Formatting/SQLFormatterService.swift

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,13 @@ struct SQLFormatterService: SQLFormatterProtocol {
108108
/// Get or create the keyword uppercasing regex for a given database type
109109
private static func keywordRegex(for dialect: DatabaseType) -> NSRegularExpression? {
110110
keywordRegexLock.lock()
111-
defer { keywordRegexLock.unlock() }
112-
113111
if let cached = keywordRegexCache[dialect] {
112+
keywordRegexLock.unlock()
114113
return cached
115114
}
115+
keywordRegexLock.unlock()
116116

117-
let provider: SQLDialectProvider
118-
if Thread.isMainThread {
119-
provider = MainActor.assumeIsolated { SQLDialectFactory.createDialect(for: dialect) }
120-
} else {
121-
provider = DispatchQueue.main.sync {
122-
MainActor.assumeIsolated { SQLDialectFactory.createDialect(for: dialect) }
123-
}
124-
}
117+
let provider = resolveDialectProvider(for: dialect)
125118
let allKeywords = provider.keywords.union(provider.functions).union(provider.dataTypes)
126119
let escapedKeywords = allKeywords.map { NSRegularExpression.escapedPattern(for: $0) }
127120
let pattern = "\\b(\(escapedKeywords.joined(separator: "|")))\\b"
@@ -130,10 +123,24 @@ struct SQLFormatterService: SQLFormatterProtocol {
130123
return nil
131124
}
132125

126+
keywordRegexLock.lock()
127+
defer { keywordRegexLock.unlock() }
128+
if let cached = keywordRegexCache[dialect] {
129+
return cached
130+
}
133131
keywordRegexCache[dialect] = regex
134132
return regex
135133
}
136134

135+
private static func resolveDialectProvider(for dialect: DatabaseType) -> SQLDialectProvider {
136+
if Thread.isMainThread {
137+
return MainActor.assumeIsolated { SQLDialectFactory.createDialect(for: dialect) }
138+
}
139+
return DispatchQueue.main.sync {
140+
MainActor.assumeIsolated { SQLDialectFactory.createDialect(for: dialect) }
141+
}
142+
}
143+
137144
// MARK: - Public API
138145

139146
func format(
@@ -159,15 +166,7 @@ struct SQLFormatterService: SQLFormatterProtocol {
159166
throw SQLFormatterError.invalidCursorPosition(cursor, max: sqlLength)
160167
}
161168

162-
// Get dialect provider
163-
let dialectProvider: SQLDialectProvider
164-
if Thread.isMainThread {
165-
dialectProvider = MainActor.assumeIsolated { SQLDialectFactory.createDialect(for: dialect) }
166-
} else {
167-
dialectProvider = DispatchQueue.main.sync {
168-
MainActor.assumeIsolated { SQLDialectFactory.createDialect(for: dialect) }
169-
}
170-
}
169+
let dialectProvider = Self.resolveDialectProvider(for: dialect)
171170

172171
// Format the SQL
173172
let formatted = formatSQL(sql, dialect: dialectProvider, databaseType: dialect, options: options)

TablePro/Extensions/String+HexDump.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@
88
import Foundation
99

1010
extension String {
11-
private static let hexDumpNumberFormatter: NumberFormatter = {
12-
let formatter = NumberFormatter()
13-
formatter.numberStyle = .decimal
14-
return formatter
15-
}()
1611

1712
/// Returns a classic hex dump representation of this string's bytes, or nil if empty.
1813
///
@@ -70,7 +65,7 @@ extension String {
7065
}
7166

7267
if totalCount > maxBytes {
73-
let formattedTotal = Self.hexDumpNumberFormatter.string(from: NSNumber(value: totalCount)) ?? "\(totalCount)"
68+
let formattedTotal = totalCount.formatted(.number)
7469
lines.append("... (truncated, \(formattedTotal) bytes total)")
7570
}
7671

TablePro/Views/AIChat/AIChatPanelView.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ struct AIChatPanelView: View {
4545
}
4646
.onAppear {
4747
viewModel.connection = connection
48-
viewModel.tables = tables
4948
}
5049
.task(id: tables) {
5150
viewModel.tables = tables

TablePro/Views/Editor/HistoryPanelView.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,8 @@ private extension HistoryPanelView {
303303
}()
304304

305305
func buildSecondaryMetadata(_ entry: QueryHistoryEntry) -> String {
306-
var text = "Executed: \(Self.metadataDateFormatter.string(from: entry.executedAt))"
306+
let executedAt = Self.metadataDateFormatter.string(from: entry.executedAt)
307+
var text = String(localized: "Executed: \(executedAt)")
307308

308309
if !entry.wasSuccessful, let error = entry.errorMessage {
309310
text += "\nError: \(error)"

TablePro/Views/Main/MainContentView.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,9 @@ struct MainContentView: View {
785785
let sessions = DatabaseManager.shared.activeSessions
786786
guard let session = sessions[connection.id] else { return }
787787
if session.isConnected && coordinator.needsLazyLoad {
788-
guard !changeManager.hasChanges else { return }
788+
let hasPendingEdits = changeManager.hasChanges
789+
|| (tabManager.selectedTab?.pendingChanges.hasChanges ?? false)
790+
guard !hasPendingEdits else { return }
789791
coordinator.needsLazyLoad = false
790792
if let selectedTab = tabManager.selectedTab,
791793
!selectedTab.databaseName.isEmpty,

0 commit comments

Comments
 (0)