This repository has been archived by the owner on Aug 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: handle multiple zettel IDs on 1 line
- Loading branch information
Damien Rajon
committed
Jul 1, 2021
1 parent
3dbbf29
commit 14b1efd
Showing
5 changed files
with
199 additions
and
93 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
test: | ||
@nvim --headless -c "PlenaryBustedDirectory tests/nerveux {minimal_init = 'tests/minimal_init.vim'}" |
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 |
---|---|---|
@@ -1,74 +1,81 @@ | ||
local utils = {} | ||
local Job = require "plenary.job" | ||
|
||
local DOUBLE_LINK_RE = "%[%[([ A-Za-z0-9|]+)%]%]#?" | ||
local DOUBLE_LINK_RE = "%[%[(%w+)|?[%g%s]-%]%]#?" | ||
local DOUBLE_LINK_RE_NOCAP = "%[%[[%g%s]-%]%]#?" | ||
|
||
---@param s string | ||
function utils.match_link(s) | ||
return s:match(DOUBLE_LINK_RE) | ||
end | ||
function utils.match_link(s) return s:match(DOUBLE_LINK_RE) end | ||
|
||
--- Returns a table containing all the IDs and they positions and if | ||
-- they are folgezettels | ||
function utils.get_all_link_indices(line) | ||
local function get_all_ids_from_line(line_) | ||
return line_:gmatch(DOUBLE_LINK_RE_NOCAP) | ||
end | ||
|
||
local function remove_link_decorations(match_) | ||
return match_:gsub(DOUBLE_LINK_RE, "%1") | ||
end | ||
|
||
function utils.find_link(s) | ||
return s:find(DOUBLE_LINK_RE) | ||
local matches = get_all_ids_from_line(line) | ||
local start = 1 | ||
local indices = {} | ||
for match in matches do | ||
local start_ix, end_ix = line:find(match, start, true) | ||
local is_folgezettel = string.sub(line, end_ix, end_ix) == "#" | ||
local id = remove_link_decorations(match) | ||
|
||
table.insert(indices, {start_ix, end_ix, id, is_folgezettel }) | ||
start = end_ix | ||
end | ||
|
||
return indices | ||
end | ||
|
||
function utils.find_link(s) return s:find(DOUBLE_LINK_RE) end | ||
|
||
--- Stolen from https://github.com/blitmap/lua-snippets/blob/master/string-pad.lua | ||
function utils.lpad(s, l, is_eol) | ||
local short_or_eq = #s <= l | ||
local ss = (is_eol or short_or_eq) and s or (string.sub(s, 0, l) .. "…") | ||
local res = string.rep(" ", l - #ss) .. s | ||
local short_or_eq = #s <= l | ||
local ss = (is_eol or short_or_eq) and s or (string.sub(s, 0, l) .. "…") | ||
local res = string.rep(" ", l - #ss) .. s | ||
|
||
return res | ||
return res | ||
end | ||
|
||
--- Stolen from https://github.com/blitmap/lua-snippets/blob/master/string-pad.lua | ||
function utils.pad(s, l, is_eol) | ||
local res1 = utils.rpad(s, (l / 2) + #s, is_eol) -- pad to half-length + the length of s | ||
local res2 = utils.lpad(res1, l, is_eol) -- right-pad our left-padded string to the full length | ||
local res1 = utils.rpad(s, (l / 2) + #s, is_eol) -- pad to half-length + the length of s | ||
local res2 = utils.lpad(res1, l, is_eol) -- right-pad our left-padded string to the full length | ||
|
||
return res2 | ||
return res2 | ||
end | ||
|
||
--- Stolen from https://github.com/blitmap/lua-snippets/blob/master/string-pad.lua | ||
function utils.rpad(s, l, is_eol) | ||
local short_or_eq = #s <= l | ||
local ss = (is_eol or short_or_eq) and s or (string.sub(s, 1, l - 1) .. "…") | ||
local res = ss .. string.rep(" ", l - #ss) | ||
local short_or_eq = #s <= l | ||
local ss = (is_eol or short_or_eq) and s or (string.sub(s, 1, l - 1) .. "…") | ||
local res = ss .. string.rep(" ", l - #ss) | ||
|
||
return res | ||
return res | ||
end | ||
|
||
function utils.map(tbl, f) | ||
local t = {} | ||
for k, v in pairs(tbl) do | ||
t[k] = f(v) | ||
end | ||
return t | ||
local t = {} | ||
for k, v in pairs(tbl) do t[k] = f(v) end | ||
return t | ||
end | ||
|
||
utils.is_process_running = function(process_name, cb) | ||
local j = | ||
Job:new( | ||
{ | ||
command = "pgrep", | ||
args = {"-c", process_name} | ||
} | ||
) | ||
j:start() | ||
j:after_failure( | ||
function() | ||
return cb(nil, false) | ||
end | ||
) | ||
j:after_success( | ||
function(_, ret_code) | ||
return cb(nil, tonumber(ret_code) == 0) | ||
end | ||
) | ||
local j = Job:new({command = "pgrep", args = {"-c", process_name}}) | ||
j:start() | ||
j:after_failure(function() return cb(nil, false) end) | ||
j:after_success( | ||
function(_, ret_code) return cb(nil, tonumber(ret_code) == 0) end) | ||
end | ||
|
||
function utils.get_zettel_id_from_fname() | ||
return vim.fn.fnamemodify(vim.api.nvim_buf_get_name(0), ":t:r") | ||
return vim.fn.fnamemodify(vim.api.nvim_buf_get_name(0), ":t:r") | ||
end | ||
|
||
return utils |
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,4 @@ | ||
set rtp+=. | ||
runtime plugin/plenary.vim | ||
|
||
nnoremap ,,x :luafile %<CR> |
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,55 @@ | ||
local u = require "nerveux.utils" | ||
|
||
describe("When extracting links", function() | ||
|
||
it("should work on the simplest links", function() | ||
do | ||
local line = "[[12345]]" | ||
assert.same({{1, 9, "12345", false}}, u.get_all_link_indices(line)) | ||
end | ||
|
||
do | ||
local line = "[[1234567890abcdefghijklmnopqrstuvwxyzABCDDE]]" | ||
assert.same( | ||
{{1, 46, "1234567890abcdefghijklmnopqrstuvwxyzABCDDE", false}}, | ||
u.get_all_link_indices(line)) | ||
end | ||
end) | ||
|
||
it("should work when a the link is in the middle of the line", function() | ||
local line = "ok ca marche [[12345]] salut merci" | ||
assert.same({{14, 22, "12345", false}}, u.get_all_link_indices(line)) | ||
end) | ||
|
||
it("should work with multiple links on the same line ", function() | ||
local line = "ok ca marche [[12345]] salut merci [[abcdef123]]" | ||
assert.same({{14, 22, "12345", false}, {36, 48, "abcdef123", false}}, | ||
u.get_all_link_indices(line)) | ||
end) | ||
|
||
it("should work with folgezettels", function() | ||
local line = "ok ca marche [[12345]]#" | ||
assert.same({{14, 23, "12345", true}}, u.get_all_link_indices(line)) | ||
end) | ||
|
||
it("should work with a simple aliases", function() | ||
do | ||
local line = "ok ca marche [[12345|alias]]#" | ||
assert.same({{14, #line, "12345", true}}, u.get_all_link_indices(line)) | ||
end | ||
|
||
do | ||
local line = | ||
"ok ca marche [[12345zxcv|alias with spaces wow ! SPACES!@#%]]#" | ||
assert.same({{14, #line, "12345zxcv", true}}, u.get_all_link_indices(line)) | ||
end | ||
end) | ||
|
||
it("should work with aliases and folgezettels with aliases", function() | ||
local line = | ||
"[[1232sd|this is an alias]] hi this is [[2547ad|HEy!!! ok]]# a link right there" | ||
assert.same({{1, 27, "1232sd", false}, {40, 60, "2547ad", true}}, | ||
u.get_all_link_indices(line)) | ||
end) | ||
|
||
end) |