Skip to content

Commit 54595e8

Browse files
committed
feat: new textobj filepath
1 parent bf2133a commit 54595e8

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

README.md

+24-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Bundle of more than 30 new text objects for Neovim.
1919
* [Go to next occurrence of a text object](#go-to-next-occurrence-of-a-text-object)
2020
* [Dynamically switch text object settings](#dynamically-switch-text-object-settings)
2121
* [`ii` on unindented line should select entire buffer](#ii-on-unindented-line-should-select-entire-buffer)
22-
* [Smarter `gx`](#smarter-gx)
22+
* [Smarter `gx` & `gf`](#smarter-gx--gf)
2323
* [Delete surrounding indentation](#delete-surrounding-indentation)
2424
* [Yank surrounding indentation](#yank-surrounding-indentation)
2525
* [Indent last paste](#indent-last-paste)
@@ -60,7 +60,8 @@ Bundle of more than 30 new text objects for Neovim.
6060
| `lastChange` | last non-deletion-change, yank, or paste (paste-manipulation plugins may interfere) | \- | \- | `g;` |
6161
| `notebookCell` | cell delimited by [double percent comment][jupytext], such as `# %%` | outer includes the top cell border | \- | `iN`/`aN` |
6262
| `emoji` | single emoji (or Nerdfont glyph) | \- | small | `.` |
63-
| `argument` | comma-separated argument (not as accurate as the treesitter-textobjects, use as fallback) | \outer includes one `.` (or `:`) | small | `i,`/`a,` |
63+
| `argument` | comma-separated argument (not as accurate as the treesitter-textobjects, use as fallback) | outer includes the `,` | small | `i,`/`a,` |
64+
| `filepath` | unix-filepath; supports `~` or `$HOME`, but not spaces in the filepath. | inner is only the filename | big | `iF`/`aF` |
6465

6566
[jupytext]: https://jupytext.readthedocs.io/en/latest/formats-scripts.html#the-percent-format
6667

@@ -301,7 +302,7 @@ vim.keymap.set("o", "ii", function()
301302
end)
302303
```
303304

304-
### Smarter `gx`
305+
### Smarter `gx` & `gf`
305306
The code below retrieves the next URL (within the amount of lines configured in
306307
the `setup` call), and opens it in your browser. As opposed to vim's built-in
307308
`gx`, this is **forward-seeking**, meaning your cursor does not have to stand on
@@ -320,6 +321,26 @@ vim.keymap.set("n", "gx", function()
320321
end, { desc = "URL Opener" })
321322
```
322323

324+
Similarly, we can also create a forward-looking version of `gf`:
325+
326+
```lua
327+
vim.keymap.set("n", "gf", function()
328+
require("various-textobjs").filepath("outer") -- select filepath
329+
330+
local foundPath = vim.fn.mode() == "v" -- only switches to visual mode when textobj found
331+
if not foundPath then return end
332+
333+
local path = vim.fn.getregion(vim.fn.getpos("."), vim.fn.getpos("v"), { type = "v" })[1]
334+
335+
local exists = vim.uv.fs_stat(vim.fs.normalize(path)) ~= nil
336+
if exists then
337+
vim.ui.open(path)
338+
else
339+
vim.notify("Path does not exist.", vim.log.levels.WARN)
340+
end
341+
end, { desc = "URL Opener" })
342+
```
343+
323344
### Delete surrounding indentation
324345
Using the indentation text object, you can also create custom indentation-related
325346
utilities. A common operation is to remove the line before and after an

lua/various-textobjs/config/default-keymaps.lua

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ local innerOuterMaps = {
66
value = "v",
77
key = "k",
88
subword = "S", -- lowercase taken for sentence textobj
9+
filepath = "F", -- `f` is usually used for outer/inner function textobjs
910
notebookCell = "N",
1011
closedFold = "z", -- z is the common prefix for folds
1112
chainMember = "m",

lua/various-textobjs/textobjs/charwise.lua

+15-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ local function smallForward()
2222
return require("various-textobjs.config.config").config.forwardLooking.small
2323
end
2424

25+
---@return integer
26+
---@nodiscard
27+
local function bigForward()
28+
return require("various-textobjs.config.config").config.forwardLooking.big
29+
end
30+
2531
--------------------------------------------------------------------------------
2632

2733
function M.toNextClosingBracket()
@@ -127,8 +133,15 @@ end
127133

128134
function M.url()
129135
local urlPatterns = require("various-textobjs.config.config").config.textobjs.url.patterns
130-
local bigForward = require("various-textobjs.config.config").config.forwardLooking.big
131-
core.selectClosestTextobj(urlPatterns, "outer", bigForward)
136+
core.selectClosestTextobj(urlPatterns, "outer", bigForward())
137+
end
138+
139+
---@param scope "inner"|"outer" inner is only the filename
140+
function M.filepath(scope)
141+
local pattern = {
142+
unixPath = "([.~]?/?[%w_%-.$/]+/)[%w_%-.]+()",
143+
}
144+
core.selectClosestTextobj(pattern, scope, bigForward())
132145
end
133146

134147
---@param scope "inner"|"outer" inner excludes the leading dot

0 commit comments

Comments
 (0)