Skip to content

Commit b621218

Browse files
Allow getting table node at specific cursor position
1 parent 1564999 commit b621218

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

lua/orgmode/treesitter/table.lua

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
local ts_utils = require('nvim-treesitter.ts_utils')
1+
local ts_utils = require('orgmode.utils.treesitter')
22
local Table = require('orgmode.parser.table')
33
local utils = require('orgmode.utils')
44
local config = require('orgmode.config')
@@ -10,13 +10,16 @@ local query = vim.treesitter.query
1010
---@field tbl Table
1111
local TsTable = {}
1212

13-
function TsTable.from_current_node()
13+
function TsTable.from_current_node(linenr)
1414
vim.treesitter.get_parser(0, 'org', {}):parse()
15-
local view = vim.fn.winsaveview()
16-
-- Go to first non blank char
17-
vim.cmd([[norm! _]])
18-
local node = ts_utils.get_node_at_cursor()
19-
vim.fn.winrestview(view)
15+
local cursor = vim.api.nvim_win_get_cursor(0)
16+
if linenr then
17+
local line = vim.fn.getline(linenr)
18+
local spaces = line:match('^%s*')
19+
local col = spaces and spaces:len() or 0
20+
cursor = { linenr, col }
21+
end
22+
local node = ts_utils.get_node_at_cursor(cursor)
2023
if not node then
2124
return false
2225
end
@@ -94,8 +97,8 @@ function TsTable:add_row()
9497
return TsTable.from_current_node():reformat()
9598
end
9699

97-
local function format()
98-
local tbl = TsTable.from_current_node()
100+
local function format(linenr)
101+
local tbl = TsTable.from_current_node(linenr)
99102

100103
if not tbl then
101104
return false

0 commit comments

Comments
 (0)