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

feat: add ignore_pattern option to filter language lines #217

Merged
merged 3 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 7 additions & 2 deletions lua/otter/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ local default_config = {
-- otter files are deleted on quit or main buffer close
write_to_disk = false,
--A table of preambles for each language. The key is the language and the value is a table of strings that will be written to the otter buffer starting on the first line.
preambles = {
}
preambles = {},
-- A table of patterns to ignore for each language. The key is the language and the value is a lua match pattern to ignore.
-- lua patterns: https://www.lua.org/pil/20.2.html
ignore_pattern = {
-- ipython cell magic (lines starting with %) and shell commands (lines starting with !)
python = "^(%s*[%%!].*)",
},
},
-- list of characters that should be stripped from the beginning and end of the code chunks
strip_wrapping_quote_characters = { "'", '"', "`" },
Expand Down
6 changes: 5 additions & 1 deletion lua/otter/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ M.export_otter_as = keeper.export_otter_as
---@param diagnostics boolean? Enable diagnostics for otter buffers. Default: true
---@param tsquery string? Explicitly provide a treesitter query. If nil, the injections query for the current filetyepe will be used. See :h treesitter-language-injections.
---@paramr preambles table? A table of preambles for each language. The key is the language and the value is a table of strings that will be written to the otter buffer starting on the first line.
M.activate = function(languages, completion, diagnostics, tsquery, preambles)
---@paramr ignore_pattern table? A table of patterns to ignore for each language. The key is the languang and the value is a regular expression string to match patterns to ignore.
M.activate = function(languages, completion, diagnostics, tsquery, preambles, ignore_pattern)
languages = languages or vim.tbl_keys(require("otter.tools.extensions"))
completion = completion ~= false
diagnostics = diagnostics ~= false
preambles = preambles or config.cfg.buffers.preambles
ignore_pattern = ignore_pattern or config.cfg.buffers.ignore_pattern
local main_nr = api.nvim_get_current_buf()
local main_path = api.nvim_buf_get_name(main_nr)
local main_lang = api.nvim_get_option_value("filetype", { buf = main_nr })
Expand Down Expand Up @@ -74,6 +76,7 @@ M.activate = function(languages, completion, diagnostics, tsquery, preambles)
buffers = {},
paths = {},
preambles = {},
ignore_pattern = {},
otter_nr_to_lang = {},
tsquery = tsquery,
query = query,
Expand Down Expand Up @@ -125,6 +128,7 @@ M.activate = function(languages, completion, diagnostics, tsquery, preambles)
keeper.rafts[main_nr].buffers[lang] = otter_nr
keeper.rafts[main_nr].paths[lang] = otter_path
keeper.rafts[main_nr].preambles[lang] = preambles[lang] or {}
keeper.rafts[main_nr].ignore_pattern[lang] = ignore_pattern[lang] or {}
keeper.rafts[main_nr].otter_nr_to_lang[otter_nr] = lang
table.insert(keeper.rafts[main_nr].languages, lang)

Expand Down
11 changes: 9 additions & 2 deletions lua/otter/keeper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ local cfg = require("otter.config").cfg
---@field buffers table<string, integer>
---@field paths table<string, string>
---@field preambles table<string, string[]>
---@field ignore_pattern table<string, string>
---@field otter_nr_to_lang table<integer, string>
---@field tsquery string?
---@field query vim.treesitter.Query
Expand Down Expand Up @@ -440,12 +441,18 @@ keeper.sync_raft = function(main_nr, language)

-- collect language lines
-- are allowed to overwrite the preamble
-- apply ignore_pattern filtering on read
local pattern = keeper.rafts[main_nr].ignore_pattern[lang]
for _, t in ipairs(code_chunks) do
local start_index = t.range["from"][1]
for i, l in ipairs(t.text) do
local index = start_index + i
table.remove(ls, index)
table.insert(ls, index, l)
if not string.match(l, pattern) then
table.remove(ls, index)
table.insert(ls, index, l)
else
table.remove(ls, index)
end
end
end

Expand Down