|
2 | 2 |
|
3 | 3 | local Hooks = {} |
4 | 4 |
|
5 | | ---- Run post-mount action (tcd + optional hook) |
6 | | ---- @param mount_dir string Mount directory path |
7 | | ---- @param host string Host name |
8 | | ---- @param remote_path string|nil Remote path used for the mount |
9 | | ---- @param config table Plugin configuration |
10 | | -function Hooks.on_mount(mount_dir, host, remote_path, config) |
11 | | - local hook_cfg = (config.hooks and config.hooks.on_mount) or {} |
12 | | - local auto_change_to_dir = hook_cfg.auto_change_to_dir |
13 | | - local action = hook_cfg.auto_run |
14 | | - |
15 | | - -- Auto-change directory to mount point if configured |
16 | | - if auto_change_to_dir then |
17 | | - vim.cmd("tcd " .. vim.fn.fnameescape(mount_dir)) |
18 | | - end |
19 | | - |
| 5 | +-- Normalize hook config value into either a callable or a known preset string. |
| 6 | +--- @param action string|function|nil |
| 7 | +--- @return string|function|nil normalized |
| 8 | +local function normalize_action(action) |
20 | 9 | if action == nil or action == "none" then |
21 | | - return |
| 10 | + return nil |
22 | 11 | end |
23 | 12 |
|
24 | | - -- Allow custom handler |
25 | 13 | if type(action) == "function" then |
26 | | - local ok, err = pcall(action, { |
27 | | - mount_path = mount_dir, |
28 | | - host = host, |
29 | | - remote_path = remote_path, |
30 | | - }) |
31 | | - if not ok then |
32 | | - vim.notify("on_mount callback failed: " .. err, vim.log.levels.ERROR) |
33 | | - end |
34 | | - return |
| 14 | + return action |
35 | 15 | end |
36 | 16 |
|
37 | | - -- Normalize preset names |
38 | 17 | local preset = string.lower(action) |
39 | | - if preset == "livefiles" then |
| 18 | + if preset == "livefind" or preset == "live-find" then |
40 | 19 | preset = "live_find" |
41 | | - elseif preset == "livegrep" then |
| 20 | + elseif preset == "livegrep" or preset == "live-grep" then |
42 | 21 | preset = "live_grep" |
43 | | - elseif preset == "files" or preset == "live_find" or preset == "live_grep" or preset == "grep" or preset == "terminal" then |
44 | | - -- keep as is |
45 | | - else |
46 | | - preset = "files" |
47 | 22 | end |
48 | 23 |
|
49 | | - -- Resolve connection info for live_* actions |
50 | | - local function get_connection() |
51 | | - local Connections = require("sshfs.lib.connections") |
52 | | - for _, conn in ipairs(Connections.get_all()) do |
53 | | - if conn.mount_path == mount_dir then |
54 | | - return conn |
55 | | - end |
| 24 | + local allowed = { |
| 25 | + find = true, |
| 26 | + live_find = true, |
| 27 | + live_grep = true, |
| 28 | + grep = true, |
| 29 | + terminal = true, |
| 30 | + } |
| 31 | + |
| 32 | + if not allowed[preset] then |
| 33 | + preset = "find" |
| 34 | + end |
| 35 | + |
| 36 | + return preset |
| 37 | +end |
| 38 | + |
| 39 | +-- Fetch the connection associated with a mount directory. |
| 40 | +--- @param mount_dir string |
| 41 | +--- @return table|nil connection |
| 42 | +local function find_connection_by_mount(mount_dir) |
| 43 | + local Connections = require("sshfs.lib.connections") |
| 44 | + for _, conn in ipairs(Connections.get_all()) do |
| 45 | + if conn.mount_path == mount_dir then |
| 46 | + return conn |
56 | 47 | end |
57 | | - return nil |
58 | 48 | end |
| 49 | + return nil |
| 50 | +end |
59 | 51 |
|
60 | | - if preset == "files" then |
| 52 | +-- Execute one of the built-in preset actions. |
| 53 | +--- @param preset string |
| 54 | +--- @param mount_dir string |
| 55 | +--- @param config table |
| 56 | +--- @return nil |
| 57 | +local function run_preset_action(preset, mount_dir, config) |
| 58 | + if preset == "find" then |
61 | 59 | local Picker = require("sshfs.ui.picker") |
62 | | - local success, picker_name = Picker.open_file_picker(mount_dir, config, false) |
63 | | - if not success then |
| 60 | + local ok, picker_name = Picker.open_file_picker(mount_dir, config, false) |
| 61 | + if not ok then |
64 | 62 | vim.notify("Failed to open " .. picker_name .. " for new mount: " .. mount_dir, vim.log.levels.ERROR) |
65 | 63 | end |
66 | | - elseif preset == "grep" then |
| 64 | + return |
| 65 | + end |
| 66 | + |
| 67 | + if preset == "grep" then |
67 | 68 | local Picker = require("sshfs.ui.picker") |
68 | 69 | Picker.grep_remote_files(nil, { dir = mount_dir }) |
69 | | - elseif preset == "live_grep" or preset == "live_find" then |
70 | | - local conn = get_connection() |
| 70 | + return |
| 71 | + end |
| 72 | + |
| 73 | + if preset == "live_grep" or preset == "live_find" then |
| 74 | + local conn = find_connection_by_mount(mount_dir) |
71 | 75 | if not conn then |
72 | 76 | vim.notify("No connection found for on_mount action: " .. mount_dir, vim.log.levels.WARN) |
73 | 77 | return |
74 | 78 | end |
75 | 79 | local Picker = require("sshfs.ui.picker") |
76 | 80 | local fn = preset == "live_grep" and Picker.open_live_remote_grep or Picker.open_live_remote_find |
77 | | - local success, picker_name = fn(conn.host, conn.mount_path, conn.remote_path or ".", config) |
78 | | - if not success then |
| 81 | + local ok, picker_name = fn(conn.host, conn.mount_path, conn.remote_path or ".", config) |
| 82 | + if not ok then |
79 | 83 | vim.notify("Live action failed: " .. picker_name, vim.log.levels.ERROR) |
80 | 84 | end |
81 | | - elseif preset == "terminal" then |
| 85 | + return |
| 86 | + end |
| 87 | + |
| 88 | + if preset == "terminal" then |
82 | 89 | local Terminal = require("sshfs.ui.terminal") |
83 | 90 | Terminal.open_ssh() |
84 | 91 | end |
85 | 92 | end |
86 | 93 |
|
| 94 | +--- Run post-mount action (tcd + optional hook) |
| 95 | +--- @param mount_dir string Mount directory path |
| 96 | +--- @param host string Host name |
| 97 | +--- @param remote_path string|nil Remote path used for the mount |
| 98 | +--- @param config table Plugin configuration |
| 99 | +function Hooks.on_mount(mount_dir, host, remote_path, config) |
| 100 | + local hook_cfg = (config.hooks and config.hooks.on_mount) or {} |
| 101 | + local auto_change_to_dir = hook_cfg.auto_change_to_dir |
| 102 | + local action = normalize_action(hook_cfg.auto_run) |
| 103 | + |
| 104 | + -- Auto-change directory to mount point if configured |
| 105 | + if auto_change_to_dir then |
| 106 | + vim.cmd("tcd " .. vim.fn.fnameescape(mount_dir)) |
| 107 | + end |
| 108 | + |
| 109 | + if action == nil then |
| 110 | + return |
| 111 | + end |
| 112 | + |
| 113 | + -- Allow custom handler |
| 114 | + if type(action) == "function" then |
| 115 | + local ok, err = pcall(action, { |
| 116 | + mount_path = mount_dir, |
| 117 | + host = host, |
| 118 | + remote_path = remote_path, |
| 119 | + }) |
| 120 | + if not ok then |
| 121 | + vim.notify("on_mount callback failed: " .. err, vim.log.levels.ERROR) |
| 122 | + end |
| 123 | + return |
| 124 | + end |
| 125 | + |
| 126 | + run_preset_action(action, mount_dir, config) |
| 127 | +end |
| 128 | + |
87 | 129 | return Hooks |
0 commit comments