Created
October 18, 2024 18:22
-
-
Save thomasv314/b3fdcafb4ce7ea0fe1c5597f49796ed8 to your computer and use it in GitHub Desktop.
Revisions
-
thomasv314 created this gist
Oct 18, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,52 @@ vim.g.terraform_module_path = '/Users/%s/new/code/terraform-modules/%s' local function extract_and_replace_source() local line_number = vim.api.nvim_win_get_cursor(0)[1] local line = vim.api.nvim_get_current_line() local git_match = line:match 'source%s*=%s*"[email protected]:[^/]+/[^/]+.git//([^?"]+)' if git_match then local module_path = git_match local current_user = vim.loop.os_getenv 'USER' local new_path = string.format(vim.g.terraform_module_path, current_user, module_path) local new_line = line:gsub('source%s*=%s*".-"', 'source = "' .. new_path .. '"') local original_comment = string.format('# original: %s', line) vim.api.nvim_buf_set_lines(0, line_number - 1, line_number - 1, false, { original_comment }) vim.api.nvim_set_current_line(new_line) print('Updated source to:', new_line) else print 'No valid Git URL found on the current line.' end end local function restore_original_source() local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false) for i, line in ipairs(lines) do local original_match = line:match '# original:%s*(.+)' if original_match then local next_line = lines[i + 1] if next_line and next_line:match 'source%s*=%s*"' then local start_pos = string.find(next_line, '%S') local updated_line = string.rep(' ', start_pos - 1) .. original_match vim.api.nvim_buf_set_lines(0, i, i + 1, false, { updated_line }) vim.api.nvim_buf_set_lines(0, i - 1, i, false, {}) -- remove comment print 'Restored original' return end end end print 'No valid "# original:" comment found.' end vim.api.nvim_create_user_command('Tfunpin', extract_and_replace_source, {}) vim.api.nvim_create_user_command('Tfpin', restore_original_source, {}) return {}