Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion TablePro/Core/Autocomplete/SQLSchemaProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ actor SQLSchemaProvider {
guard !tables.isEmpty, let connection = connectionInfo else { return nil }

var columnsByTable: [String: [ColumnInfo]] = [:]
for table in tables {
let tablesToFetch = Array(tables.prefix(settings.maxSchemaTables))
for table in tablesToFetch {
let columns = await getColumns(for: table.name)
if !columns.isEmpty {
columnsByTable[table.name.lowercased()] = columns
Expand Down
33 changes: 19 additions & 14 deletions TablePro/ViewModels/AIChatViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -445,22 +445,27 @@ final class AIChatViewModel {
var columns: [String: [ColumnInfo]] = [:]
var foreignKeys: [String: [ForeignKeyInfo]] = [:]

for table in tablesToFetch {
if let schemaProvider {
let cached = await schemaProvider.getColumns(for: table.name)
if !cached.isEmpty {
columns[table.name] = cached
await withTaskGroup(of: (String, [ColumnInfo]).self) { group in
for table in tablesToFetch {
group.addTask { [schemaProvider] in
if let schemaProvider {
let cached = await schemaProvider.getColumns(for: table.name)
if !cached.isEmpty {
return (table.name, cached)
}
}
do {
let cols = try await driver.fetchColumns(table: table.name)
return (table.name, cols)
} catch {
Self.logger.debug("Schema context: failed to fetch columns for '\(table.name)'")
return (table.name, [])
}
}
}

if columns[table.name] == nil {
do {
let cols = try await driver.fetchColumns(table: table.name)
columns[table.name] = cols
} catch {
Self.logger.warning(
"Failed to fetch columns for table '\(table.name)': \(error.localizedDescription)"
)
for await (tableName, cols) in group {
if !cols.isEmpty {
columns[tableName] = cols
}
}
}
Expand Down
24 changes: 18 additions & 6 deletions TablePro/Views/AIChat/AIChatCodeBlockView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ struct AIChatCodeBlockView: View {
let language: String?

@State private var isCopied: Bool = false
@State private var cachedHighlight: AttributedString?
@State private var cachedCodeLength: Int = 0
@FocusedValue(\.commandActions) private var actions

var body: some View {
Expand Down Expand Up @@ -76,12 +78,8 @@ struct AIChatCodeBlockView: View {

private var codeContent: some View {
ScrollView(.horizontal, showsIndicators: false) {
if isSQL {
Text(highlightedSQL(code))
.textSelection(.enabled)
.padding(10)
} else if isMongoDB {
Text(highlightedJavaScript(code))
if isSQL || isMongoDB {
Text(cachedHighlight ?? AttributedString(code))
.textSelection(.enabled)
.padding(10)
} else if isRedis {
Expand All @@ -96,6 +94,20 @@ struct AIChatCodeBlockView: View {
.padding(10)
}
}
.onAppear {
recomputeHighlight()
}
.onChange(of: code) {
let newLen = (code as NSString).length
if newLen - cachedCodeLength >= 50 {
recomputeHighlight()
}
}
}

private func recomputeHighlight() {
cachedHighlight = isSQL ? highlightedSQL(code) : highlightedJavaScript(code)
cachedCodeLength = (code as NSString).length
}

private var isSQL: Bool {
Expand Down
Loading