Skip to content

Commit

Permalink
Cleanup some regexes.
Browse files Browse the repository at this point in the history
Extract them once instead of compiling them for every visual lines.
  • Loading branch information
hadronized committed Oct 9, 2022
1 parent c8cc9c7 commit ef37a76
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion doc/hop.txt
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ doing that for you.
of all the words in the window and will make the cursor jump to the one
fully reduced.

`hop.jump_target.regex_by_line_start()` *hop.jump_target.regex_by_line_start*
`hop.jump_target.by_line_start()` *hop.jump_target.by_line_start*
Highlight the beginning of each line in the current window.

`hop.jump_target.regex_by_line_start_skip_whitespace()` *hop.jump_target.regex_by_line_start_skip_whitespace*
Expand Down
2 changes: 1 addition & 1 deletion lua/hop/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ function M.hint_lines(opts)
end

M.hint_with(
generator(jump_target.regex_by_line_start()),
generator(jump_target.by_line_start()),
opts
)
end
Expand Down
20 changes: 13 additions & 7 deletions lua/hop/jump_target.lua
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,13 @@ function M.regex_by_searching(pat, plain_search)
if plain_search then
pat = vim.fn.escape(pat, '\\/.$^~[]')
end

local regex = vim.regex(pat)

return {
oneshot = false,
match = function(s)
return vim.regex(pat):match_str(s)
return regex:match_str(s)
end
}
end
Expand All @@ -370,10 +373,12 @@ function M.regex_by_case_searching(pat, plain_search, opts)
pat = '\\c' .. pat
end

local regex = vim.regex(pat)

return {
oneshot = false,
match = function(s)
return vim.regex(pat):match_str(s)
return regex:match_str(s)
end
}
end
Expand All @@ -384,7 +389,7 @@ function M.regex_by_word_start()
end

-- Line regex.
function M.regex_by_line_start()
function M.by_line_start()
local c = vim.fn.winsaveview().leftcol

return {
Expand All @@ -403,22 +408,23 @@ end
-- Line regex at cursor position.
function M.regex_by_vertical()
local position = vim.api.nvim_win_get_cursor(0)[2]
local pattern = vim.regex(string.format("^.\\{0,%d\\}\\(.\\|$\\)", position))
local regex = vim.regex(string.format("^.\\{0,%d\\}\\(.\\|$\\)", position))
return {
oneshot = true,
match = function(s)
return pattern:match_str(s)
return regex:match_str(s)
end
}
end

-- Line regex skipping finding the first non-whitespace character on each line.
function M.regex_by_line_start_skip_whitespace()
local pat = vim.regex("\\S")
local regex = vim.regex("\\S")

return {
oneshot = true,
match = function(s)
return pat:match_str(s)
return regex:match_str(s)
end
}
end
Expand Down

0 comments on commit ef37a76

Please sign in to comment.