Skip to content

Commit

Permalink
thread test
Browse files Browse the repository at this point in the history
  • Loading branch information
cshuaimin committed Jul 7, 2024
1 parent 9da83e7 commit c4c8585
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 57 deletions.
4 changes: 3 additions & 1 deletion lua/ssr/replace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ M.PinnedMatch = {}
function M.pin_matches(matches)
local res = {}
for _, row in ipairs(matches) do
local buf = row.file:load_buf()
-- local buf = row.file:load_buf()
local buf = vim.fn.bufadd(row.file.path)
vim.fn.bufload(buf)
for _, match in ipairs(row.matches) do
-- local pinned = { buf = buf, range = match.range:to_extmark(buf), captures = {} }
local pinned = { buf = buf, range = require("ssr.range").to_extmark(match.range, buf), captures = {} }
Expand Down
26 changes: 16 additions & 10 deletions lua/ssr/search.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- local ts = vim.treesitter
-- vim.func = require "vim.func"
vim.F = require "vim.F"
-- vim.F = require "vim.F"
local uv = vim.uv or vim.loop
local ts = require "vim.treesitter"
local Range = require "ssr.range"
Expand Down Expand Up @@ -133,13 +133,13 @@ end
function Searcher.new(lang, pattern)
-- $ can cause syntax errors in most languages
pattern = pattern:gsub("%$([_%a%d]+)", u.capture_prefix .. "%1")
-- local query = parse_pattern(lang, pattern)
-- if query == vim.NIL then return end
local query, captures = parse_pattern(lang, pattern)
if query == vim.NIL then return end
return setmetatable({
pattern = pattern,
rough_regex = u.build_rough_regex(pattern),
queries = {},
captures = nil,
queries = { [lang] = query },
captures = captures,
}, { __index = Searcher })
end

Expand Down Expand Up @@ -192,7 +192,9 @@ end
local cache = {}

---@class ssr.File
---@field path string
---@field text string
---@field lines string[]
---@field tree vim.treesitter.LanguageTree
---@field mtime { nsec: integer, sec: integer }

Expand All @@ -203,6 +205,10 @@ local cache = {}
---@param callback fun(ssr.Matches)
function Searcher:search(dir, callback)
-- Runs in a new Lua state.
---@param self ssr.Searcher
---@param path string
---@param file? ssr.File
---@return ssr.File?, ssr.Match[]?
local function work_func(self, path, file)
local uv = vim.uv or vim.loop
local ts = vim.treesitter
Expand All @@ -213,6 +219,7 @@ function Searcher:search(dir, callback)
local stat = uv.fs_fstat(fd) ---@cast stat -?
if not file or file.mtime.sec ~= stat.mtime.sec or file.mtime.nsec ~= stat.mtime.nsec then
local text = uv.fs_read(fd, stat.size, 0) ---@cast text -?
local lines = vim.split(text, "\n", { plain = true })
local lang = "lua"
local has_parser, tree = pcall(ts.get_string_parser, text, lang)
if not has_parser then
Expand All @@ -221,13 +228,11 @@ function Searcher:search(dir, callback)
return
end
tree:parse(true)
file = { text = text, tree = tree, mtime = stat.mtime }
file = { path = path, text = text, lines = lines, tree = tree, mtime = stat.mtime }
end
uv.fs_close(fd)

local s = require "ssr.search"
setmetatable(self, { __index = s })
return file, self:search_file(file)
local matches = self:search_file(file)
return file, matches
end

local works = 0
Expand All @@ -241,6 +246,7 @@ function Searcher:search(dir, callback)
if not file then return end
cache[path] = file
table.insert(matches, { file = file, matches = m })
vim.print(m)
done = done + 1
if rg_done and done == works then vim.schedule_wrap(callback)(matches) end
end)
Expand Down
13 changes: 7 additions & 6 deletions lua/ssr/ui/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,20 @@ function Ui:search()
if pattern == self.last_pattern then return end
self.last_pattern = pattern

local start = vim.loop.hrtime()
self.matches = {}
local found = 0
local matched_files = 0

local start = vim.loop.hrtime()
local searcher = Searcher.new(self.lang, pattern)
if not searcher then return self:set_status "ERROR" end
searcher:search(vim.fn.getcwd(-1), function(matches)
local elapsed = (vim.loop.hrtime() - start) / 1E6
self.matches = matches
vim.print(#matches)
local found = 0
for _, match in ipairs(matches) do
found = found + #match.matches
end

self.main_win.result_list:set(self.matches)
self:set_status(string.format("%d found in %d files (%dms)", found, matched_files, elapsed))
self:set_status(string.format("%d found in %d files (%dms)", found, #matches, elapsed))
end)
end

Expand Down
64 changes: 24 additions & 40 deletions lua/ssr/ui/result_list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ local Fold = {}
function Fold.new(folded, file, matches)
local preview_lines = {}
for _, match in ipairs(matches) do
local line = file:get_line(match.range.start_row)
local line = file.lines[match.range.start_row + 1]
line = line:gsub("^%s*", "")
table.insert(preview_lines, "" .. line)
end
Expand All @@ -35,17 +35,13 @@ function Fold.new(folded, file, matches)
end

function Fold:len()
if self.folded then
return 1
end
if self.folded then return 1 end
return 1 + #self.preview_lines
end

---@private
function Fold:get_lines()
if self.folded then
return { string.format(" %s %s %d", self.filename, self.path, #self.preview_lines) }
end
if self.folded then return { string.format(" %s %s %d", self.filename, self.path, #self.preview_lines) } end
local lines = { string.format(" %s %s %d", self.filename, self.path, #self.preview_lines) }
vim.list_extend(lines, self.preview_lines)
return lines
Expand Down Expand Up @@ -77,20 +73,24 @@ function ResultList.new(buf, win, extmark)
items = {},
}, { __index = ResultList })

vim.keymap.set("n", config.opts.keymaps.next_match, function()
self:next_match()
end, { buffer = self.buf, nowait = true })
vim.keymap.set("n", config.opts.keymaps.prev_match, function()
self:prev_match()
end, { buffer = self.buf, nowait = true })
vim.keymap.set(
"n",
config.opts.keymaps.next_match,
function() self:next_match() end,
{ buffer = self.buf, nowait = true }
)
vim.keymap.set(
"n",
config.opts.keymaps.prev_match,
function() self:prev_match() end,
{ buffer = self.buf, nowait = true }
)

return self
end

---@private
function ResultList:get_start()
return api.nvim_buf_get_extmark_by_id(self.buf, u.namespace, self.extmark, {})[1] + 1
end
function ResultList:get_start() return api.nvim_buf_get_extmark_by_id(self.buf, u.namespace, self.extmark, {})[1] + 1 end

---@params matches ssr.Matches
function ResultList:set(matches)
Expand Down Expand Up @@ -123,9 +123,7 @@ function ResultList:set_folded(folded, cursor)
cursor = cursor or u.get_cursor(self.win) - result_start
local item = self.items[cursor + 1] -- +1 beacause `cursor` is 0-based
local fold = self.folds[item.fold_idx]
if fold.folded == folded then
return
end
if fold.folded == folded then return end

local start = cursor - item.match_idx -- like C macro `container_of`
local end_ = start + fold:len()
Expand All @@ -140,27 +138,19 @@ function ResultList:set_folded(folded, cursor)
end_ = result_start + end_
api.nvim_buf_set_lines(self.buf, start, end_, true, lines)
fold:highlight(self.buf, start)
if folded then
u.set_cursor(self.win, start, 0)
end
if folded then u.set_cursor(self.win, start, 0) end
end

function ResultList:next_match()
local cursor = u.get_cursor(self.win)
local result_start = self:get_start()
cursor = cursor > result_start and cursor - result_start or 0
local item = self.items[cursor + 1] -- +1: lua index
if not item then
return
end
if item.match_idx == 0 then
self:set_folded(false, cursor)
end
if not item then return end
if item.match_idx == 0 then self:set_folded(false, cursor) end
cursor = cursor + 1
item = self.items[cursor + 1]
if not item then
return
end
if not item then return end
if item.match_idx == 0 then
self:set_folded(false, cursor)
cursor = cursor + 1
Expand All @@ -172,24 +162,18 @@ function ResultList:prev_match()
local cursor = u.get_cursor(self.win)
local result_start = self:get_start()
if cursor <= result_start then
if #self.items == 0 then
return
end
if #self.items == 0 then return end
self:set_folded(false, #self.items - 1)
return u.set_cursor(self.win, result_start + #self.items - 1, 0)
end

cursor = cursor - result_start
local item = self.items[cursor + 1]
if not item then
return
end
if not item then return end
if item.match_idx <= 1 then
cursor = cursor - item.match_idx - 1
item = self.items[cursor + 1]
if not item then
return
end
if not item then return end
local fold = self.folds[item.fold_idx]
if fold.folded then
self:set_folded(false, cursor)
Expand Down

0 comments on commit c4c8585

Please sign in to comment.