Sources/OrgIndex/CompletionQueries.swift
cmc Completion from the index: id links by title, property values, Denote notes
30fb15c9bc 13 hours ago65 lines · 3815 bytes
1import Foundation
2import GRDB
3import OrgCore
4
5/// What completion asks the index: link targets, property values and words. The open file's
6/// own come from its buffer, so `excluding` leaves its rows out.
7extension IndexStore {
8 /// Every heading in org files, and every file with an ID, with its ID if it has one:
9 /// what `[[id:` completes.
10 public func linkTargets(excluding path: String? = nil) throws -> [CompletionItem] {
11 try database.read { db in
12 let files = try Row.fetchAll(db, sql: """
13 SELECT path, org_id, title FROM files WHERE org_id IS NOT NULL AND kind = 'org' AND path IS NOT ? ORDER BY path
14 """, arguments: [path]).map { row in
15 let path: String = row["path"]
16 let title = (row["title"] as String?) ?? ((path as NSString).lastPathComponent as NSString).deletingPathExtension
17 return CompletionItem(title: title, id: row["org_id"], path: path, start: 0, heading: false)
18 }
19 let headings = try Row.fetchAll(db, sql: """
20 SELECT f.path, h.title, h.org_id, h.start_offset FROM headings h JOIN files f ON f.id = h.file_id
21 WHERE f.kind = 'org' AND f.path IS NOT ? ORDER BY f.path, h.ordinal
22 """, arguments: [path]).map { row in
23 CompletionItem(title: row["title"], id: row["org_id"], path: row["path"], start: row["start_offset"], heading: true)
24 }
25 return files + headings
26 }
27 }
28
29 /// Values headings give `name` themselves, most used first, and the values its `_ALL`
30 /// properties allow, in order.
31 public func propertyValues(_ name: String, excluding path: String? = nil) throws -> (used: [String], allowed: [String]) {
32 try database.read { db in
33 let used = try String.fetchAll(db, sql: """
34 SELECT p.value FROM properties p JOIN headings h ON h.id = p.heading_id JOIN files f ON f.id = h.file_id
35 WHERE upper(p.key) = upper(?) AND p.inherited = 0 AND f.path IS NOT ? AND p.value != ''
36 GROUP BY p.value ORDER BY count(*) DESC, p.value
37 """, arguments: [name, path])
38 let allowed = try String.fetchAll(db, sql: """
39 SELECT p.value FROM properties p JOIN headings h ON h.id = p.heading_id JOIN files f ON f.id = h.file_id
40 WHERE upper(p.key) = upper(?) AND p.inherited = 0 AND f.path IS NOT ?
41 """, arguments: [name + "_ALL", path])
42 return (used, allowed.flatMap(PropertyValues.readList).filter { $0 != ":ETC" })
43 }
44 }
45
46 /// Words starting with `prefix`, ignoring case, in headings and text before first
47 /// headings that the full-text index finds for it.
48 public func words(_ prefix: String, excluding path: String? = nil, limit: Int = 50) throws -> [String] {
49 let term = prefix.lowercased().split { !$0.isLetter && !$0.isNumber }.first.map(String.init) ?? ""
50 guard !term.isEmpty else { return [] }
51 let match = "\"" + term + "\"*"
52 let texts = try database.read { db in
53 try String.fetchAll(db, sql: """
54 SELECT headings_fts.title || char(10) || headings_fts.body FROM headings_fts
55 JOIN headings h ON h.id = headings_fts.rowid JOIN files f ON f.id = h.file_id
56 WHERE headings_fts MATCH ? AND f.path IS NOT ? LIMIT 200
57 """, arguments: [match, path])
58 + String.fetchAll(db, sql: """
59 SELECT files_fts.body FROM files_fts JOIN files f ON f.id = files_fts.rowid
60 WHERE files_fts MATCH ? AND f.path IS NOT ? LIMIT 50
61 """, arguments: [match, path])
62 }
63 return Dabbrev.words(prefix, in: texts, limit: limit)
64 }
65}