Skip to content

Commit

Permalink
feat!(simulation): v2 rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
tris203 committed Dec 19, 2024
1 parent 33a018a commit ecaaa79
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 682 deletions.
303 changes: 0 additions & 303 deletions lua/precognition/horizontal_motions.lua

This file was deleted.

30 changes: 11 additions & 19 deletions lua/precognition/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -243,29 +243,18 @@ local function display_marks()
local line_len = vim.fn.strcharlen(cur_line)
---@type Precognition.ExtraPadding[]
local extra_padding = {}
-- local after_cursor = vim.fn.strcharpart(cur_line, cursorcol + 1)
-- local before_cursor = vim.fn.strcharpart(cur_line, 0, cursorcol - 1)
-- local before_cursor_rev = string.reverse(before_cursor)
-- local under_cursor = vim.fn.strcharpart(cur_line, cursorcol - 1, 1)

local hm = require("precognition.horizontal_motions")

-- FIXME: Lua patterns don't play nice with utf-8, we need a better way to
-- get char offsets for more complex motions.
--

---@type Precognition.VirtLine
local virtual_line_marks = {
Caret = hm.line_start_non_whitespace(cur_line, cursorcol, line_len),
w = hm.next_word_boundary(cur_line, cursorcol, line_len, false),
e = hm.end_of_word(cur_line, cursorcol, line_len, false),
b = hm.prev_word_boundary(cur_line, cursorcol, line_len, false),
W = hm.next_word_boundary(cur_line, cursorcol, line_len, true),
E = hm.end_of_word(cur_line, cursorcol, line_len, true),
B = hm.prev_word_boundary(cur_line, cursorcol, line_len, true),
MatchingPair = hm.matching_pair(cur_line, cursorcol, line_len)(cur_line, cursorcol, line_len),
Dollar = hm.line_end(cur_line, cursorcol, line_len),
Zero = 1,
}
local virtual_line_marks = require("precognition.sim").check(cur_line, cursorcol)
---return 0 for any hint that is not found in the simmed table
setmetatable(virtual_line_marks, {
__index = function()
return 0
end,
})

if compat.inlay_hints_enabled({ bufnr = 0 }) then
local inlays_hints = vim.lsp.inlay_hint.get({
Expand Down Expand Up @@ -490,6 +479,9 @@ local state = {
ns = function()
return ns
end,
default_hint_config = function()
return defaultHintConfig
end,
is_visible = function()
return visible
end,
Expand Down
37 changes: 37 additions & 0 deletions lua/precognition/sim.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
local mt = require("mini.test")

local M = {}

local function check_pos(string, col, default_config)
local result = {}
local locations = vim.tbl_keys(default_config)
local buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_lines(buf, 0, -1, false, { " ", string, " " })
vim.api.nvim_set_current_buf(buf)
for _, motion_name in ipairs(locations) do
local motion_key = default_config[motion_name].text
vim.fn.setcursorcharpos(2, col)
vim.api.nvim_feedkeys(motion_key, "x", true)
local cur_pos = vim.fn.getcursorcharpos(0)
if cur_pos[2] == 2 then
if motion_name == "MatchingPair" and cur_pos[3] ~= col then
result[motion_name] = cur_pos[3]
else
result[motion_name] = cur_pos[3]
end
end
end

return result
end

M.check = function(line, col)
local remote = mt.new_child_neovim()
remote.start()

local remote_data = remote.lua_func(check_pos, line, col, require("precognition").default_hint_config)
remote.stop()
return remote_data
end

return M
Loading

0 comments on commit ecaaa79

Please sign in to comment.