Skip to content

Commit

Permalink
Implement DistantOpen autocomplete (#143)
Browse files Browse the repository at this point in the history
* Implement 'complete' field for DistantCommand
* Implement autocomplete for DistantOpen
  • Loading branch information
asoderman authored Oct 8, 2024
1 parent f3a4a1c commit cf8f00d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
48 changes: 48 additions & 0 deletions lua/distant/commands/distant_open.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
local plugin = require('distant')
local utils = require('distant.commands.utils')

local parent_path = require('distant-core.utils').parent_path

--- DistantOpen path [opt1=... opt2=...]
--- @param cmd NvimCommand
local function command(cmd)
Expand Down Expand Up @@ -38,12 +40,58 @@ local function command(cmd)
plugin.editor.open(opts)
end

local function completion(ArgLead,_,_)
local seperator = require('distant-core.utils').seperator()

-- Helper: Get the last component of a path
local function last_component(path)
local parts = vim.split(path, seperator)
return parts[#parts]
end

local path = ArgLead
if path == nil or path == '' then
path = "."
end

local component
if not vim.endswith(path, seperator) and path ~= "." then
component = last_component(path)
path = parent_path(path)
end

local err, payload = plugin.api().read_dir({
path = path,
depth = 1,
absolute = true,
canonicalize = true,
})

assert(not err, err)
assert(payload)

local results = {}

for _, entry in ipairs(payload.entries) do
if component == nil or string.match(entry.path, component) then
local ending_sep = ""
if entry.file_type == 'dir' then
ending_sep = seperator
end
table.insert(results, entry.path .. ending_sep)
end
end

return results
end

--- @type DistantCommand
local COMMAND = {
name = 'DistantOpen',
description = 'Open a file or directory on the remote machine',
command = command,
bang = true,
nargs = '*',
complete = completion,
}
return COMMAND
14 changes: 14 additions & 0 deletions lua/distant/commands/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
--- @field bang? boolean
--- @field force? boolean
--- @field nargs? integer|'*'
--- @field complete? fun(string,string,_):string[]

local log = require('distant-core').log

Expand Down Expand Up @@ -58,11 +59,24 @@ local function _initialize()
table.insert(names, name)
end

local complete
if cmd.complete then
-- Register a global function
local func_name = "DistantCommandComplete_" .. cmd.name

_G[func_name] = function(ArgLead, CmdLine, CursorPos)
return table.concat(cmd.complete(ArgLead, CmdLine, CursorPos), "\n")
end
-- completion config is a string with name of the global function
complete = "custom,v:lua." .. func_name
end

for _, name in ipairs(names) do
vim.api.nvim_create_user_command(name, cmd.command, {
desc = cmd.description,
bang = cmd.bang,
nargs = cmd.nargs,
complete = complete,
})
end
end
Expand Down

0 comments on commit cf8f00d

Please sign in to comment.