Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: independent search #30

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
## Features

- Diagnostics
- Search (requires [nvim-hlslens](https://github.com/kevinhwang91/nvim-hlslens))
- Search (requires [nvim-hlslens](https://github.com/kevinhwang91/nvim-hlslens), or [plenary.nvim](https://github.com/nvim-lua/plenary.nvim) and [ripgrep](https://github.com/BurntSushi/ripgrep))

## Requirements

- Neovim >= 0.5.1
- [nvim-hlslens](https://github.com/kevinhwang91/nvim-hlslens) (optional)
- [plenary.nvim](https://github.com/nvim-lua/plenary.nvim) (optional)
- [ripgrep](https://github.com/BurntSushi/ripgrep) (optional)

## Installation

Expand All @@ -44,7 +46,7 @@ require("scrollbar").setup()

![search](./assets/search.gif)

Run after loading hlslens
Run after loading hlslens if you do not want to use builtin search.

```lua
require("scrollbar.handlers.search").setup()
Expand Down Expand Up @@ -117,6 +119,7 @@ require("scrollbar").setup({
## Acknowledgements

- [kevinhwang91/nvim-hlslens](https://github.com/kevinhwang91/nvim-hlslens) for implementation on how to hide search results
- [folke/todo-comments.nvim](https://github.com/folke/todo-comments.nvim) for implementation of builtin search

## License

Expand Down
11 changes: 10 additions & 1 deletion lua/scrollbar/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,16 @@ local config = {
},
handlers = {
diagnostic = true,
search = false, -- Requires hlslens to be loaded
search = true, -- Requires hlslens to be loaded
},
search = {
use_builtin = true,
command = "rg",
args = {
"--color=never",
"--no-heading",
"--line-number",
},
},
}

Expand Down
89 changes: 83 additions & 6 deletions lua/scrollbar/handlers/search.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,67 @@ local render = require("scrollbar").render

local M = {}

function M.process(lines)
local results = {}
for _, line in pairs(lines) do
local row = line:match("^(%d+):.*$")
table.insert(results, { row })
end
return results
end

function M.search(cb)
local config = require("scrollbar.config").get()
local bufnr = vim.api.nvim_get_current_buf()

local command = config.search.command

if vim.fn.executable(command) ~= 1 then
vim.notify(command .. " was not found on your path, trying to fall back to grep", vim.log.levels.ERROR)
command = "grep"
end


if vim.fn.executable(command) ~= 1 then
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably isn't needed anymore

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably worth just keeping this just in case

Copy link
Contributor Author

@tbung tbung Jan 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this basically cleanly saveguards against not having grep installed. I know grep is on every Linux system, I don't know about macOS, and I'm sure Windows doesn't have it.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this check on line 27 and on line 21 are the same?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it's to check for rg then grep

vim.notify(command .. " was not found on your path", vim.log.levels.ERROR)
return
end

local ok, Job = pcall(require, "plenary.job")
if not ok then
vim.notify("search requires https://github.com/nvim-lua/plenary.nvim", vim.log.levels.ERROR)
return
end

local ok, Path = pcall(require, "plenary.path")
if not ok then
vim.notify("search requires https://github.com/nvim-lua/plenary.nvim", vim.log.levels.ERROR)
return
end

local search_regex = vim.fn.getreginfo("/").regcontents[1]
local search_path = vim.fn.expand("%:p")
if not Path:new(search_path):exists() or search_path == "" then
return
end

local args = vim.tbl_flatten({ config.search.args, search_regex, search_path })
Job
:new({
command = command,
args = args,
on_exit = vim.schedule_wrap(function(j, code)
if code == 2 then
local error = table.concat(j:stderr_result(), "\n")
vim.notify(command .. " failed with code " .. code .. "\n" .. error, vim.log.levels.ERROR)
end
local lines = j:result()
cb(M.process(lines))
end),
})
:start()
end

M.handler = {
show = function(plist)
local config = require("scrollbar.config").get()
Expand Down Expand Up @@ -49,13 +110,29 @@ M.setup = function(overrides)
local config = require("scrollbar.config").get()
config.handlers.search = true

local hlslens_config = vim.tbl_deep_extend("force", {
build_position_cb = function(plist, _, _, _)
M.handler.show(plist.start_pos)
end,
}, overrides or {})
if not config.search.use_builtin then
local hlslens_config = vim.tbl_deep_extend("force", {
build_position_cb = function(plist, _, _, _)
M.handler.show(plist.start_pos)
end,
}, overrides or {})

require("hlslens").setup(hlslens_config)
require("hlslens").setup(hlslens_config)
else
if config.autocmd and config.autocmd.render and #config.autocmd.render > 0 then
vim.cmd(string.format(
[[
augroup scrollbar_search_show
autocmd!
autocmd %s * lua require('scrollbar.handlers.search').search(require('scrollbar.handlers.search').handler.show)
augroup END
]],
table.concat(config.autocmd.render, ",")
))
end
vim.cmd([[
]])
end

vim.cmd([[
augroup scrollbar_search_hide
Expand Down