Skip to content

Commit fe4a92a

Browse files
committed
fix parsing of urls
1 parent dc25136 commit fe4a92a

File tree

5 files changed

+58
-22
lines changed

5 files changed

+58
-22
lines changed

README.md

+35-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,35 @@
1-
# gx.nvim
1+
# 🔗 gx.nvim
2+
3+
## ✨ Features
4+
5+
* open links without `netrw`
6+
* more to come (github, jira, ..)
7+
8+
## ⚡️ Requirements
9+
10+
* Neovim >= 0.5.0
11+
* macOS (`open`) or linux (`xdg-open`)
12+
13+
## 📦 Installation
14+
15+
### [lazy.nvim](https://github.com/folke/lazy.nvim)
16+
17+
```lua
18+
19+
require("lazy").setup({
20+
{
21+
"chrishrb/gx.nvim",
22+
config = function()
23+
require("gx").setup()
24+
end,
25+
},
26+
})
27+
```
28+
29+
## ⌨️ Mappings
30+
31+
* `gx` is overriden by default
32+
33+
## 🚀 Usage
34+
35+
When your cursor is over a link or you mark a link or part of a link with the visual mode, you can press `gx` to open the link in the browser.

after/plugin/gx.lua

-7
This file was deleted.

lua/gx/helper.lua

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ local function visual_selection_range()
1313
end
1414

1515
-- check that cursor on uri in normal mode
16-
function M.checkIfCursorOnUri(mode, i, j)
16+
function M.checkIfCursorOnUrl(mode, i, j)
1717
if mode ~= "n" then
1818
return true
1919
end
2020

2121
local col = vim.api.nvim_win_get_cursor(0)[2]
2222
if i <= (col + 1) and j >= (col + 1) then
23-
print(i, col, j)
2423
return true
2524
end
2625

lua/gx/init.lua

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
local notfier = require "lua.gx.notfier"
2-
local helper = require "lua.gx.helper"
3-
local parser = require "lua.gx.parser"
1+
local notfier = require("gx.notfier")
2+
local helper = require("gx.helper")
3+
local parser = require("gx.parser")
44

55
local keymap = vim.keymap.set
66
local sysname = vim.loop.os_uname().sysname
@@ -35,9 +35,9 @@ local function searchForUrl()
3535
line = helper.cutWithVisualMode(mode, line)
3636

3737
-- search for url
38-
local i, j, url = parser.getUrl(line)
38+
local url = parser.getUrl(mode, line)
3939

40-
if not helper.checkIfCursorOnUri(mode, i, j) then
40+
if not url then
4141
return
4242
end
4343

lua/gx/parser.lua

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
1-
local pattern = "[%a]*://.*%.[/?_%d%a]*"
1+
local helper = require("gx.helper")
22

33
local M = {}
44

5-
-- get uri from line
6-
function M.getUrl(line)
7-
local i, j = string.find(line, pattern)
5+
-- find pattern in line and check if cursor on it
6+
local function find(line, mode, pattern, startIndex)
7+
startIndex = startIndex or 1
8+
local i, j = string.find(line, pattern, startIndex)
89

9-
if not i then
10-
return i, j, nil
10+
if helper.checkIfCursorOnUrl(mode, i, j) then
11+
return string.sub(line, i, j)
12+
elseif not i then
13+
return nil
14+
else
15+
return find(line, mode, pattern, j+1)
1116
end
17+
end
1218

13-
return i, j, string.sub(line, i, j)
19+
-- get url from line
20+
function M.getUrl(mode, line)
21+
local pattern = "[%a]*://[^)%]%[\"'`˚:,!:;{}]*%.[/?_%-%d%a]*"
22+
local url = find(line, mode, pattern)
23+
return url
1424
end
1525

1626
-- TODO: add more parser

0 commit comments

Comments
 (0)