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

Implement display function to allow custom result rendering #47

Open
wants to merge 5 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
4 changes: 2 additions & 2 deletions fnl/snap/common/buffer.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
"Helper function for adding selected highlighting"
(vim.api.nvim_buf_add_highlight bufnr namespace :SnapMultiSelect (- row 1) 0 -1))

(defn add-positions-highlight [bufnr row positions]
(defn add-positions-highlight [bufnr row positions offset]
"Helper function for adding positions highlights"
(local line (- row 1))
(each [_ col (ipairs positions)]
(add-highlight bufnr :SnapPosition line (- col 1) col)))
(add-highlight bufnr :SnapPosition line (+ (- col 1) offset) (+ col offset))))

(defn create []
"Creates a scratch buffer"
Expand Down
23 changes: 20 additions & 3 deletions fnl/snap/init.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@
(assertstring field "field passed to snap.has_meta must be a string")
(and (= (getmetatable result) meta_tbl) (not= (. result field) nil)))

(defn display [result]
"Optionally runs a custom display function for a result"
;; TODO: This could cache the result of the display function on the result and return a
;; meta_result for re-use. Would increase performance but require some refactoring elsewhere.
(let [display_fn
(if (has_meta result :display)
(do
(assertfunction result.display "display meta must be a function")
result.display)
tostring)]
(display_fn result)))

;; Run docs:
;;
;; @config: {
Expand Down Expand Up @@ -325,7 +337,7 @@
partial-results []]
(each [_ result (ipairs results)
:until (= max (length partial-results))]
(table.insert partial-results (tostring result)))
(table.insert partial-results (display result)))
;; Set the lines, but make sure tables are converted to strings
(buffer.set-lines results-view.bufnr 0 -1 partial-results)
;; Make sure the cursor is always updated
Expand All @@ -342,7 +354,11 @@
(match (type result.positions)
:table result.positions
:function (result:positions)
_ (assert false "result positions must be a table or function"))))
_ (assert false "result positions must be a table or function"))
;; Include the offset for highlighting, if one exists
(if (has_meta result :highlight_offset)
result.highlight_offset
0)))
;; Add selected highlighting
(when
(. selected (tostring result))
Expand Down Expand Up @@ -433,7 +449,8 @@
;; Collects results progressively and renders early if possible
(fn config.on-value [value]
;; Check the type
(asserttable value "Main producer yielded a non-yieldable value")
(asserttable value (string.format "Main producer yielded a non-yieldable value: %s"
(vim.inspect value)))
;; Accumulate the results
(when (> (length value) 0)
(tbl.accumulate results value)
Expand Down
4 changes: 2 additions & 2 deletions lua/snap/common/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ do
local v_0_
do
local v_0_0
local function add_positions_highlight0(bufnr, row, positions)
local function add_positions_highlight0(bufnr, row, positions, offset)
local line = (row - 1)
for _, col in ipairs(positions) do
add_highlight(bufnr, "SnapPosition", line, (col - 1), col)
add_highlight(bufnr, "SnapPosition", line, ((col - 1) + offset), (col + offset))
end
return nil
end
Expand Down
45 changes: 38 additions & 7 deletions lua/snap/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,29 @@ do
t_0_["has_meta"] = v_0_
has_meta = v_0_
end
local display
do
local v_0_
do
local v_0_0
local function display0(result)
local display_fn
if has_meta(result, "display") then
assert((type(result.display) == "function"), "display meta must be a function")
display_fn = result.display
else
display_fn = tostring
end
return display_fn(result)
end
v_0_0 = display0
_0_["display"] = v_0_0
v_0_ = v_0_0
end
local t_0_ = (_0_)["aniseed/locals"]
t_0_["display"] = v_0_
display = v_0_
end
local run
do
local v_0_
Expand Down Expand Up @@ -549,25 +572,33 @@ do
local partial_results = {}
for _, result in ipairs(results0) do
if (max == #partial_results) then break end
table.insert(partial_results, tostring(result))
table.insert(partial_results, display(result))
end
buffer["set-lines"](results_view.bufnr, 0, -1, partial_results)
update_cursor()
for row in pairs(partial_results) do
local result = (results0)[row]
if has_meta(result, "positions") then
local function _16_()
local _16_
do
local _15_ = type(result.positions)
if (_15_ == "table") then
return result.positions
_16_ = result.positions
elseif (_15_ == "function") then
return result:positions()
_16_ = result:positions()
else
local _ = _15_
return assert(false, "result positions must be a table or function")
_16_ = assert(false, "result positions must be a table or function")
end
end
local function _17_()
if has_meta(result, "highlight_offset") then
return result.highlight_offset
else
return 0
end
end
buffer["add-positions-highlight"](results_view.bufnr, row, _16_())
buffer["add-positions-highlight"](results_view.bufnr, row, _16_, _17_())
end
if selected[tostring(result)] then
buffer["add-selected-highlight"](results_view.bufnr, row)
Expand Down Expand Up @@ -676,7 +707,7 @@ do
end
end
config2["on-value"] = function(value)
assert((type(value) == "table"), "Main producer yielded a non-yieldable value")
assert((type(value) == "table"), string.format("Main producer yielded a non-yieldable value: %s", vim.inspect(value)))
if (#value > 0) then
tbl.accumulate(results0, value)
if not has_meta(tbl.first(results0), "score") then
Expand Down
2 changes: 1 addition & 1 deletion lua/snap/producer/ripgrep/vimgrep.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ local snap = require("snap")
local tbl = snap.get("common.tbl")
local general = snap.get("producer.ripgrep.general")
local vimgrep = {}
local line_args = {"--line-buffered", "-M", 100, "--no-heading", "--column"}
local args = {"--line-buffered", "-M", 100, "--vimgrep"}
local line_args = {"--line-buffered", "-M", 100, "--no-heading", "--column"}
vimgrep.default = function(request)
local cwd = snap.sync(vim.fn.getcwd)
return general(request, {args = tbl.concat(args, {request.filter}), cwd = cwd})
Expand Down