mirror of
https://github.com/LazyVim/starter.git
synced 2026-07-25 14:01:03 +00:00
Fix up - relative file path not file name
This commit is contained in:
parent
7a05d8b99c
commit
d9a679204b
2 changed files with 17 additions and 22 deletions
|
|
@ -25,7 +25,7 @@ end, { desc = "Open next alternative file" })
|
||||||
local alternative_file = require("mj.alternate_file")
|
local alternative_file = require("mj.alternate_file")
|
||||||
|
|
||||||
map("n", "<leader>irc", function()
|
map("n", "<leader>irc", function()
|
||||||
require("mj.ruby_utils").insert_ruby_class_based_on_file_name()
|
require("mj.ruby_utils").insert_ruby_class_based_on_file_path()
|
||||||
end, { desc = "Insert ruby class" })
|
end, { desc = "Insert ruby class" })
|
||||||
|
|
||||||
-- this is not working correctly
|
-- this is not working correctly
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,20 @@
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
-- A Lua-native split function that splits a string by a given delimiter
|
||||||
|
local function lua_split(input_str, delimiter)
|
||||||
|
local result = {}
|
||||||
|
for match in (input_str .. delimiter):gmatch("(.-)" .. delimiter) do
|
||||||
|
table.insert(result, match)
|
||||||
|
end
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
|
||||||
M.snake_case_to_ruby_class = function(path)
|
M.snake_case_to_ruby_class = function(path)
|
||||||
-- Remove .rb suffix if present
|
-- Remove .rb suffix if present
|
||||||
path = path:gsub("%.rb$", "")
|
path = path:gsub("%.rb$", "")
|
||||||
|
|
||||||
-- Split by slash and convert each part to CamelCase
|
-- Split by slash and convert each part to CamelCase
|
||||||
local parts = vim.split(path, "/")
|
local parts = lua_split(path, "/")
|
||||||
for i, part in ipairs(parts) do
|
for i, part in ipairs(parts) do
|
||||||
parts[i] = part
|
parts[i] = part
|
||||||
:gsub("_(%a)", function(letter)
|
:gsub("_(%a)", function(letter)
|
||||||
|
|
@ -40,32 +49,18 @@ M.replace_selected_text_with_ruby_class = function()
|
||||||
vim.fn.setline(start_pos[2], ruby_class)
|
vim.fn.setline(start_pos[2], ruby_class)
|
||||||
end
|
end
|
||||||
|
|
||||||
M.insert_ruby_class_based_on_file_name = function()
|
M.insert_ruby_class_based_on_file_path = function()
|
||||||
-- Get the current file name (without path)
|
-- Get the full file path
|
||||||
local filename = vim.fn.expand("%:t")
|
local filepath = vim.fn.expand("%:.")
|
||||||
|
|
||||||
-- Remove the .rb suffix if present
|
-- Remove the .rb suffix if present
|
||||||
filename = filename:gsub("%.rb$", "")
|
filepath = filepath:gsub("%.rb$", "")
|
||||||
|
|
||||||
-- Convert the file name to Ruby class format
|
-- Convert the file path to Ruby class format
|
||||||
local ruby_class = M.snake_case_to_ruby_class(filename)
|
local ruby_class = M.snake_case_to_ruby_class(filepath)
|
||||||
|
|
||||||
-- Insert the class name at the current cursor position
|
-- Insert the class name at the current cursor position
|
||||||
vim.api.nvim_put({ ruby_class }, "c", true, true)
|
vim.api.nvim_put({ ruby_class }, "c", true, true)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Keybindings (adjust as necessary)
|
|
||||||
vim.api.nvim_set_keymap(
|
|
||||||
"v",
|
|
||||||
"<leader>r",
|
|
||||||
[[:lua require'module_name'.replace_selected_text_with_ruby_class()<CR>]],
|
|
||||||
{ noremap = true, silent = true }
|
|
||||||
)
|
|
||||||
vim.api.nvim_set_keymap(
|
|
||||||
"n",
|
|
||||||
"<leader>i",
|
|
||||||
[[:lua require'module_name'.insert_ruby_class_based_on_file_name()<CR>]],
|
|
||||||
{ noremap = true, silent = true }
|
|
||||||
)
|
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue