File search (trame:filesearch)
trame:filesearch finds files and folders by name as the user types, with typo tolerance, background watching, and
frecency ranking backed by LMDB (fff-search in native/filesearch). A program that enables
toolbox.Feature.Filesearch links it in (-define:GT_FILESEARCH=true); without that define, filesearch.available()
is false and filesearch.open returns .Unavailable.
None of these calls belong on the interface thread: call filesearch.open, filesearch.search,
filesearch.note_opened and filesearch.close on a worker (such as ui.task) and post the page back.
#import "trame:filesearch"
idx, err := filesearch.open({root = project_dir, frecency_dir = data_dir, watch = true})
if err == .None {
defer filesearch.close(idx)
_ = filesearch.wait_ready(idx, 5000)
page, _ := filesearch.search(idx, "shcema", {limit = 50})
defer filesearch.page_destroy(&page)
for h in page.hits {
// h.path (relative to root), h.dir, h.score, h.ranges (byte spans in h.path)
}
filesearch.note_opened(idx, "models/schema.fm")
}| Symbol | What it does |
|---|---|
filesearch.available() | true when built with GT_FILESEARCH=true |
filesearch.open(o) | opens an filesearch.Index over o.root (filesearch.Open_Options), starting background scan and optional watcher |
filesearch.close(idx) | stops the watcher and frees the index (safe on 0 or twice) |
filesearch.wait_ready(idx, timeout_ms) | waits up to timeout_ms for the initial scan |
filesearch.state(idx) | returns filesearch.State (scanning, watching, scanned) |
filesearch.search(idx, query, o, allocator) | returns filesearch.Page of filesearch.Hit (with filesearch.Match_Range spans when o.dirs == false, or files + folders interleaved when o.dirs == true) |
filesearch.page_destroy(p, allocator) | frees a Page returned by search |
filesearch.note_opened(idx, path) | records a file open in the LMDB frecency store and updates the live index |
Command palette source (ui.Palette_Files)
ui.palette_files_add(&p, &pf, o) registers a filesearch.Index as a searching / (Palette_Prefix.Paths) source
on ui.Command_Palette (Palette_Files_Options, PALETTE_FILES_LIMIT_DEFAULT). Both search and frecency updates
(ui.palette_files_opened) run on background chores so the interface thread never touches disk or LMDB.
The index of a folder, opened and closed by the source:
ui.palette_files_add(&app.palette, &app.files)
// each time the program's folder changes ("": none), on the interface's thread
ui.palette_files_root(&app.files, &app.ui, folder, {frecency_dir = ui.palette_files_frecency_dir(), watch = true})
// at the end: closes the index it opened
ui.palette_files_destroy(&app.files)ui.palette_files_root(&pf, s, root, o)opensroot's index on a chore (Palette_Files_Root_Options:frecency_dir,watch,allow_home,wait_msfor the first scan on the chore). A new root closes the old index on a chore; an answer for an older root, or for a source destroyed meanwhile, is closed and never taken. The same root again does nothing;""closes the index. Readpf.index,pf.opening,pf.scanningandpf.scannedto show its state or to search the same index yourself.ui.palette_files_frecency_dir()is the suite's frecency folder (PALETTE_FILES_FRECENCY_FOLDERunder the user's data folder): a file opened in one program ranks higher in the others.ui.palette_files_set(&pf, idx, root)gives an index the caller opened and closes itself.ui.palette_files_destroy(&pf)frees what the source owns and closes the indexpalette_files_rootopened.
Design and platform measurements: src/md/features/file-search.md.