Skip to content

Commit

Permalink
feat: more way to get code chunks
Browse files Browse the repository at this point in the history
- within a range and from the cursor to the end
  • Loading branch information
jmbuhr committed May 6, 2023
1 parent ae3b91b commit 74e569c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
44 changes: 39 additions & 5 deletions lua/otter/keeper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,9 @@ local function get_code_chunks_with_eval_true(main_nr, lang, row_from, row_to)
lang_capture = text
end
if name == 'code' and lang_capture == lang then
local row1, col1, row2, col2 = node:range()
if row_from and row_to then
if row1 < row_from or row2 > row_to then
local row_start, col1, row_end, col2 = node:range()
if row_from ~= nil and row_to ~= nil then
if (row_start >= row_to and row_to > 0) or row_end < row_from then
goto continue
end
end
Expand All @@ -312,10 +312,27 @@ M.get_language_lines_to_cursor = function(include_eval_false)
end
local otter_nr = M._otters_attached[main_nr].buffers[lang]
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
row = row - 1
if include_eval_false then
return vim.api.nvim_buf_get_lines(otter_nr, 0, row + 1, false)
return vim.api.nvim_buf_get_lines(otter_nr, 0, row + 2, false)
end
return get_code_chunks_with_eval_true(main_nr, lang, 0, row + 1)
return get_code_chunks_with_eval_true(main_nr, lang, 0, row + 2)
end

M.get_language_lines_from_cursor = function(include_eval_false)
local main_nr = vim.api.nvim_get_current_buf()
M.sync_raft(main_nr)
local lang = get_current_language_context()
if lang == nil then
return
end
local otter_nr = M._otters_attached[main_nr].buffers[lang]
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
row = row - 1
if include_eval_false then
return vim.api.nvim_buf_get_lines(otter_nr, row, -1, false)
end
return get_code_chunks_with_eval_true(main_nr, lang, row, -1)
end


Expand All @@ -333,5 +350,22 @@ M.get_language_lines = function(include_eval_false)
return get_code_chunks_with_eval_true(main_nr, lang)
end

M.get_language_lines_in_visual_selection = function(include_eval_false)
local main_nr = vim.api.nvim_get_current_buf()
M.sync_raft(main_nr)
local lang = get_current_language_context()
if lang == nil then
return
end
local otter_nr = M._otters_attached[main_nr].buffers[lang]
local row_start, _ = unpack(api.nvim_buf_get_mark(main_nr, '<'))
local row_end, _ = unpack(api.nvim_buf_get_mark(main_nr, '>'))
row_start = row_start - 1
row_end = row_end - 1
if include_eval_false then
return vim.api.nvim_buf_get_lines(otter_nr, row_start, row_end + 2, false)
end
return get_code_chunks_with_eval_true(main_nr, lang, row_start, row_end + 2)
end

return M
19 changes: 10 additions & 9 deletions lua/otter/tools/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ end
--- @param path string a path
--- @return string
M.otterpath_to_path = function(path)
local s,_ = path:gsub('-tmp%..+', '')
local s, _ = path:gsub('-tmp%..+', '')
return s
end

--- @param path string a path
--- @return string
M.otterpath_to_plain_path = function(path)
local s,_ = path:gsub('%..+', '')
local s, _ = path:gsub('%..+', '')
return s
end

Expand Down Expand Up @@ -98,7 +98,7 @@ M.is_otter_context = function(main_nr, tsquery)
end

M.is_otter_language_context = function(lang)
vim.b['quarto_is_'..lang..'_chunk'] = false
vim.b['quarto_is_' .. lang .. '_chunk'] = false
local ft = vim.api.nvim_buf_get_option(0, 'filetype')
local parsername = vim.treesitter.language.get_lang(ft)
local language_tree = ts.get_parser(0, parsername)
Expand Down Expand Up @@ -126,14 +126,14 @@ M.is_otter_language_context = function(lang)
end
-- the corresponding code is in the current range
if found and name == 'code' and ts.is_in_node_range(node, row, col) then
vim.b['quarto_is_'..lang..'_chunk'] = true
vim.b['quarto_is_' .. lang .. '_chunk'] = true
end
end
end
end


M.get_current_language_context = function ()
M.get_current_language_context = function()
local ft = vim.api.nvim_buf_get_option(0, 'filetype')
local parsername = vim.treesitter.language.get_lang(ft)
local language_tree = ts.get_parser(0, parsername)
Expand All @@ -144,8 +144,6 @@ M.get_current_language_context = function ()
local query = vim.treesitter.query.parse(parsername, require 'otter.tools.queries'[parsername])

local row, col = unpack(vim.api.nvim_win_get_cursor(0))
row = row - 1
col = col

-- get text ranges
for pattern, match, metadata in query:iter_matches(root, 0) do
Expand All @@ -162,8 +160,11 @@ M.get_current_language_context = function ()
lang = text
end
-- the corresponding code is in the current range
if found and name == 'code' and ts.is_in_node_range(node, row, col) then
return lang
if found and name == 'code' then
local row_start, col_start, row_end, col_end = node:range()
if row_start <= row and row_end >= row - 1 then
return lang
end
end
end
end
Expand Down

0 comments on commit 74e569c

Please sign in to comment.