Skip to content

Commit 73afc72

Browse files
committed
Fix getting parent by ignoring injected langs and add new func to module
1 parent 4887379 commit 73afc72

3 files changed

Lines changed: 54 additions & 7 deletions

File tree

lua/elixir_dev/module.lua

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,31 @@ function M.get_module_public_function_nodes(bufnr, node)
6363
end, do_block_node)
6464
end
6565

66+
function M.module_name_by_path(path)
67+
local file_path = path or vim.fn.expand("%")
68+
69+
if file_path and file_path ~= "" then
70+
local mod_name = file_path
71+
:gsub(".ex$", "")
72+
:gsub(".exs$", "")
73+
:gsub("/", ".")
74+
:gsub("(%l)(%w*)", function(a, b)
75+
return string.upper(a) .. b
76+
end)
77+
:gsub("_", "")
78+
:gsub("Api", "API")
79+
:gsub("Json", "JSON")
80+
:gsub(".Controllers", "")
81+
:gsub(".Views", "")
82+
:gsub("Lib.", "")
83+
:gsub("Test.", "")
84+
85+
return mod_name
86+
end
87+
88+
return "Module"
89+
end
90+
6691
function M.yank_module_name()
6792
local bufnr = treesitter_utils.get_current_elixir_buf()
6893

lua/elixir_dev/utils/treesitter.lua

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,19 @@ local function hard_stop_for_inner_pipes(node)
2929
return false
3030
end
3131

32+
function M.get_node_at_cursor()
33+
-- Ignore injected langs
34+
return require("nvim-treesitter.ts_utils").get_node_at_cursor(0, true)
35+
end
36+
3237
function M.is_ts_elixir_parser_enabled(bufnr)
3338
local buff_active = highlighter.active[bufnr]
3439

3540
return buff_active and buff_active.tree._lang == "elixir" or false
3641
end
3742

3843
function M.get_master_node(initial_node)
39-
local ts_utils = require("nvim-treesitter.ts_utils")
40-
41-
local node = initial_node or ts_utils.get_node_at_cursor()
44+
local node = initial_node or M.get_node_at_cursor()
4245

4346
local parent = node:parent()
4447
local start_row = node:start()
@@ -65,15 +68,13 @@ function M.get_master_node(initial_node)
6568
end
6669

6770
function M.get_parent_node(types, validation, initial_node)
68-
local ts_utils = require("nvim-treesitter.ts_utils")
69-
7071
if not validation or type(validation) ~= "function" then
7172
validation = function(_)
7273
return true
7374
end
7475
end
7576

76-
local node = initial_node or ts_utils.get_node_at_cursor()
77+
local node = initial_node or M.get_node_at_cursor()
7778

7879
while node do
7980
if vim.tbl_contains(types, node:type()) and validation(node) then
@@ -87,7 +88,7 @@ function M.get_parent_node(types, validation, initial_node)
8788
end
8889

8990
function M.get_all_child(validation, initial_node)
90-
local node = initial_node or ts_utils.get_node_at_cursor()
91+
local node = initial_node or M.get_node_at_cursor()
9192

9293
if not validation or type(validation) ~= "function" then
9394
validation = function(_)

test/spec/elixir_dev/module_spec.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,24 @@ describe("format_node_function", function()
122122
vim.api.nvim_buf_delete(bufnr, { force = true })
123123
end)
124124
end)
125+
126+
describe("module_name_by_path", function()
127+
it("return the module name by file path", function()
128+
assert.are.equal("LiveRetro.Schema", module.module_name_by_path("lib/live_retro/schema.ex"))
129+
130+
assert.are.equal(
131+
"LiveRetroWeb.UserAuthTest",
132+
module.module_name_by_path("test/live_retro_web/user_auth_test.exs")
133+
)
134+
135+
assert.are.equal(
136+
"LiveRetroWeb.PageController",
137+
module.module_name_by_path("lib/live_retro_web/controllers/page_controller.ex")
138+
)
139+
140+
assert.are.equal(
141+
"LiveRetroWeb.PageControllerTest",
142+
module.module_name_by_path("test/live_retro_web/controllers/page_controller_test.exs")
143+
)
144+
end)
145+
end)

0 commit comments

Comments
 (0)