Skip to content

Commit a8ced84

Browse files
committed
Implement :CrazywallFollowRef
1 parent fbcab1c commit a8ced84

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
local Config = require("core.config")
2+
local plugin_utils = require("lua.crazywall.utils")
3+
4+
---@param plugin_state PluginState
5+
return function(plugin_state)
6+
vim.api.nvim_create_user_command("CrazywallFollowRef", function()
7+
local line = vim.api.nvim_get_current_line()
8+
local column = vim.api.nvim_win_get_cursor(0)[2]
9+
10+
if not plugin_state:get_current_config() then
11+
return vim.api.nvim_err_writeln(
12+
"crazywall: Could not find config "
13+
.. plugin_state.current_config_name
14+
.. "."
15+
)
16+
end
17+
18+
local config, err =
19+
Config:new(assert(plugin_state:get_current_config()))
20+
if not config then
21+
return plugin_utils.display_err(err)
22+
end
23+
24+
plugin_state.follow_ref(
25+
line,
26+
column,
27+
config,
28+
plugin_state.current_config_name
29+
)
30+
end, { nargs = 0 })
31+
end

lua/crazywall/init.lua

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,48 @@
11
local plugin_validate = require("crazywall.validate")
22
local default_config = require("core.defaults.config")
33
local PluginState = require("lua.crazywall.state")
4+
local Config = require("core.config")
45
local cmd_crazywall = require("lua.crazywall.commands.crazywall")
56
local cmd_crazywall_dry = require("lua.crazywall.commands.crazywall_dry")
67
local cmd_crazywall_quick = require("lua.crazywall.commands.crazywall_quick")
8+
local cmd_crazywall_follow_ref =
9+
require("lua.crazywall.commands.crazywall_follow_ref")
710
local M = {}
811

912
local plugin_state = PluginState:new()
1013
cmd_crazywall(plugin_state)
1114
cmd_crazywall_dry(plugin_state)
1215
cmd_crazywall_quick(plugin_state)
16+
cmd_crazywall_follow_ref(plugin_state)
1317

1418
M.setup = function(opts)
1519
opts = opts or {}
16-
local keys = { "configs", "default_config_name" }
20+
local keys = { "configs", "default_config_name", "follow_ref" }
1721
for key in pairs(opts) do
1822
local err = plugin_validate.string_in_list(key, keys)
1923
if err then
2024
error(err)
2125
end
2226
end
27+
if opts.configs then
28+
for name, config_table in pairs(opts.configs) do
29+
local config, err = Config:new(config_table)
30+
if not config then
31+
error(
32+
"Error occurred when trying to build config "
33+
.. name
34+
.. ": \n"
35+
.. err
36+
)
37+
end
38+
end
39+
end
2340
plugin_state.configs = opts.configs or plugin_state.configs
2441
plugin_state.configs["DEFAULT"] = plugin_state.configs.DEFAULT
2542
or default_config
2643
plugin_state.current_config_name = opts.default_config_name
2744
or plugin_state.current_config_name
45+
plugin_state.follow_ref = opts.follow_ref or plugin_state.follow_ref
2846
end
2947

3048
return M

lua/crazywall/state.lua

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---@class PluginState
22
---@field configs PartialConfigTable[]
33
---@field current_config_name string
4+
---@field follow_ref fun(line: string, column: integer, config: Config, config_name: string): any
45
local PluginState = {}
56
PluginState.__index = PluginState
67

@@ -11,6 +12,41 @@ function PluginState:new()
1112
---@cast self PluginState
1213
self.configs = {}
1314
self.current_config_name = "DEFAULT"
15+
self.follow_ref = function(line)
16+
---@param path string
17+
local file_exists_and_is_not_directory = function(path)
18+
local stat = vim.loop.fs_stat(path)
19+
return stat ~= nil and stat.type == "file"
20+
end
21+
--- Find text within [[]]s.
22+
local match = string.match(line, "%[%[(.-)%]%]")
23+
if not match then
24+
return
25+
end
26+
local current_file_dir = vim.fn.expand("%:p:h")
27+
local extension = '.' .. vim.fn.expand("%:e")
28+
---@param path string
29+
local expand_path = function(path)
30+
return vim.fn.fnamemodify(current_file_dir .. "/" .. path, ":p")
31+
end
32+
---@param path string
33+
local open_path = function(path)
34+
vim.cmd("edit " .. vim.fn.fnameescape(path))
35+
end
36+
for _, path in ipairs({
37+
expand_path(match .. extension),
38+
expand_path(match),
39+
expand_path(match .. "/_index" .. extension),
40+
}) do
41+
if file_exists_and_is_not_directory(path) then
42+
open_path(path)
43+
return
44+
end
45+
end
46+
print(
47+
"Default :CrazywallFollowRef failed. Read the docs to see how you can customize its behavior: https://github.com/gitpulljoe/crazywall.nvim"
48+
)
49+
end
1450
return self
1551
end
1652

0 commit comments

Comments
 (0)