-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New feature: split file paths into its components (#110)
* ✨ (maps.lua): Add new key mappings for RSplitPathPaste and RSplitPathHere ✨ (path.lua): Create new path.lua file to handle path splitting functionality for paste and here commands * ✨ (maps.lua): Add new commands RSplitPathPaste and RSplitPathHere for splitting file path and pasting or opening it respectively * ✨ (path.lua): Add support for 's3://' in path matching to handle S3 URLs * 📝 (README.md): Remove unnecessary blank lines and correct the formatting of the assignment operator ✨ (README.md): Add documentation for two new commands to separate a file path into its components * 📝 (README.md): Add example usage for <localleader>sp command to improve documentation clarity
- Loading branch information
1 parent
55f03f8
commit 630c16a
Showing
3 changed files
with
111 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
local M = {} | ||
|
||
local unquote_and_split_file_path = function(file_path) | ||
local quote = file_path:match("^[\"']") | ||
if quote then | ||
file_path = file_path:sub(2, -2) -- Remove surrounding quotes | ||
end | ||
|
||
-- Split the path into components | ||
local components = {} | ||
for component in file_path:gmatch("[^/]+") do | ||
table.insert(components, component) | ||
end | ||
|
||
-- If path starts with a /, add it to the first component | ||
if file_path:sub(1, 1) == "/" then components[1] = "/" .. components[1] end | ||
|
||
-- Join the components with the detected quote | ||
local result = table.concat(components, quote .. ", " .. quote) | ||
|
||
return result | ||
end | ||
|
||
local function replace_path(node, formatted_path) | ||
local bufnr = vim.api.nvim_get_current_buf() | ||
local start_row, start_col, end_row, end_col = node:range() | ||
|
||
vim.api.nvim_buf_set_text( | ||
bufnr, | ||
start_row, | ||
start_col, | ||
end_row, | ||
end_col, | ||
{ formatted_path } | ||
) | ||
end | ||
|
||
M.separate = function(prefix) | ||
local node = vim.treesitter.get_node() | ||
|
||
if node and node:type() == "string" then | ||
local path = vim.treesitter.get_node_text(node, 0) | ||
|
||
-- Check if the path is a URL or doesn't contain slashes | ||
if | ||
path:match("https?://") | ||
or path:match("ftp://") | ||
or path:match("s3://") | ||
or not path:match("/") | ||
then | ||
return | ||
end | ||
|
||
-- Traverse up the syntax tree until we find a call_expression node | ||
local parent = node:parent() | ||
while parent do | ||
if parent:type() == "call" then | ||
local function_name = vim.treesitter.get_node_text(parent, 0) | ||
if function_name:match("paste") or function_name:match("here") then | ||
return | ||
end | ||
end | ||
parent = parent:parent() | ||
end | ||
|
||
-- Format the path | ||
local formatted_path = unquote_and_split_file_path(path) | ||
|
||
if prefix == "paste" then | ||
formatted_path = 'paste("' .. formatted_path .. '", sep = "/")' | ||
elseif prefix == "here" then | ||
formatted_path = 'here("' .. formatted_path .. '")' | ||
end | ||
|
||
replace_path(node, formatted_path) | ||
end | ||
end | ||
|
||
return M |