Skip to content

Commit e99a463

Browse files
committed
feat(completion): add file search from server api
1 parent 601cc82 commit e99a463

File tree

5 files changed

+47
-16
lines changed

5 files changed

+47
-16
lines changed

lua/opencode/config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ M.defaults = {
9999
completion = {
100100
file_sources = {
101101
enabled = true,
102-
preferred_cli_tool = 'fd',
102+
preferred_cli_tool = 'server',
103103
ignore_patterns = {
104104
'^%.git/',
105105
'^%.svn/',

lua/opencode/ui/completion/engines/blink_cmp.lua

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ function Source:get_trigger_characters()
1313
local mention_key = config.get_key_for_function('input_window', 'mention')
1414
local slash_key = config.get_key_for_function('input_window', 'slash_commands')
1515
local triggers = {}
16-
if mention_key then table.insert(triggers, mention_key) end
17-
if slash_key then table.insert(triggers, slash_key) end
16+
if mention_key then
17+
table.insert(triggers, mention_key)
18+
end
19+
if slash_key then
20+
table.insert(triggers, slash_key)
21+
end
1822
return triggers
1923
end
2024

@@ -48,13 +52,15 @@ function Source:get_completions(ctx, callback)
4852
local items = {}
4953
for _, completion_source in ipairs(completion_sources) do
5054
local source_items = completion_source.complete(context)
51-
for _, item in ipairs(source_items) do
55+
for i, item in ipairs(source_items) do
5256
table.insert(items, {
5357
label = item.label,
5458
kind = item.kind == 'file' and 17 or 1, -- 17: File, 1: Text
5559
detail = item.detail,
5660
documentation = item.documentation,
5761
insertText = item.insert_text or item.label,
62+
sortText = string.format('%02d_%02d_%s', completion_source.priority or 999, i, item.label),
63+
score_offset = -(completion_source.priority or 999) * 1000,
5864
data = {
5965
original_item = item,
6066
},

lua/opencode/ui/completion/engines/nvim_cmp.lua

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ function M.setup(completion_sources)
1616
local mention_key = config.get_key_for_function('input_window', 'mention')
1717
local slash_key = config.get_key_for_function('input_window', 'slash_commands')
1818
local triggers = {}
19-
if mention_key then table.insert(triggers, mention_key) end
20-
if slash_key then table.insert(triggers, slash_key) end
19+
if mention_key then
20+
table.insert(triggers, mention_key)
21+
end
22+
if slash_key then
23+
table.insert(triggers, slash_key)
24+
end
2125
return triggers
2226
end
2327

@@ -55,7 +59,7 @@ function M.setup(completion_sources)
5559
detail = item.detail,
5660
documentation = item.documentation,
5761
insertText = item.insert_text or item.label,
58-
sortText = string.format('%03d_%03d_%s', i, j, item.label),
62+
sortText = string.format('%02d_%02d_%s', completion_source.priority or 999, j, item.label),
5963
data = {
6064
original_item = item,
6165
},

lua/opencode/ui/completion/files.lua

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,18 @@ local function run_systemlist(cmd)
2020
end
2121

2222
local function try_tool(tool, args, pattern, max, ignore_patterns)
23+
if type(args) == 'function' then
24+
local promise = args(pattern, max)
25+
local result = promise and promise:wait()
26+
27+
if result and type(result) == 'table' then
28+
return vim.tbl_filter(should_keep(ignore_patterns), result)
29+
end
30+
end
31+
32+
-- Handle string-based commands (CLI tools)
2333
if vim.fn.executable(tool) then
34+
pattern = vim.fn.shellescape(pattern) or '.'
2435
local result = run_systemlist(tool .. string.format(args, pattern, max))
2536
if result then
2637
return vim.tbl_filter(should_keep(ignore_patterns), result)
@@ -32,18 +43,20 @@ end
3243
---@param pattern string
3344
---@return string[]
3445
local function find_files_fast(pattern)
35-
pattern = vim.fn.shellescape(pattern) or '.'
3646
local file_config = config.ui.completion.file_sources
3747
local cli_tool = last_successful_tool or file_config.preferred_cli_tool or 'fd'
3848
local max = file_config.max_files or 10
3949
local ignore_patterns = file_config.ignore_patterns or {}
4050

41-
local tools_order = { 'fd', 'fdfind', 'rg', 'git' }
51+
local tools_order = { 'fd', 'fdfind', 'rg', 'git', 'server' }
4252
local commands = {
4353
fd = ' --type f --type l --full-path --color=never -E .git -E node_modules -i %s --max-results %d 2>/dev/null',
4454
fdfind = ' --type f --type l --color=never -E .git -E node_modules --full-path -i %s --max-results %d 2>/dev/null',
4555
rg = ' --files --no-messages --color=never | grep -i %s 2>/dev/null | head -%d',
4656
git = ' ls-files --cached --others --exclude-standard | grep -i %s | head -%d',
57+
server = function(pattern, max)
58+
return require('opencode.state').api_client:find_files(pattern)
59+
end,
4760
}
4861

4962
if cli_tool and commands[cli_tool] then
@@ -66,7 +79,7 @@ end
6679

6780
---@param file string
6881
---@return CompletionItem
69-
local function create_file_item(file)
82+
local function create_file_item(file, suffix)
7083
local filename = vim.fn.fnamemodify(file, ':t')
7184
local dir = vim.fn.fnamemodify(file, ':h')
7285
local file_path = dir == '.' and filename or dir .. '/' .. filename
@@ -81,7 +94,7 @@ local function create_file_item(file)
8194
end
8295

8396
return {
84-
label = display_label,
97+
label = display_label .. (suffix or ''),
8598
kind = 'file',
8699
detail = detail,
87100
documentation = 'Path: ' .. detail,
@@ -94,7 +107,7 @@ end
94107
---@type CompletionSource
95108
local file_source = {
96109
name = 'files',
97-
priority = 0,
110+
priority = 5,
98111
complete = function(context)
99112
local sort_util = require('opencode.ui.completion.sort')
100113
local file_config = config.ui.completion.file_sources
@@ -136,10 +149,17 @@ local file_source = {
136149
function M.get_recent_files()
137150
local project = require('opencode.config_file').get_opencode_project()
138151
local max = config.ui.completion.file_sources.max_files
139-
local is_git = project and project.vcs == 'git'
140-
141-
local recent_files = is_git and M.get_git_changed_files() or M.get_old_files() or {}
142-
return vim.tbl_map(create_file_item, { unpack(recent_files, 1, max) })
152+
local api_client = require('opencode.state').api_client
153+
154+
local result = api_client:get_file_status():wait()
155+
local recent_files = {}
156+
if result then
157+
for _, file in ipairs(result) do
158+
local suffix = table.concat({ file.added and '+' .. file.added, file.removed and '-' .. file.removed }, ' ')
159+
table.insert(recent_files, create_file_item(file.path, ' ' .. suffix))
160+
end
161+
end
162+
return recent_files
143163
end
144164

145165
---Get the list of old files in the current working directory

lua/opencode/ui/completion/subagents.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local M = {}
33
---@type CompletionSource
44
local subagent_source = {
55
name = 'subagents',
6+
priority = 1,
67
complete = function(context)
78
local subagents = require('opencode.config_file').get_subagents()
89
local config = require('opencode.config')

0 commit comments

Comments
 (0)