Skip to content

Commit 84d866c

Browse files
committed
feat: add ignore_pattern option to filter language lines
Ignored pattern is matched as language line collection
1 parent 13bc3f3 commit 84d866c

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

lua/otter/config.lua

+7-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@ local default_config = {
2929
-- otter files are deleted on quit or main buffer close
3030
write_to_disk = false,
3131
--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.
32-
preambles = {
33-
}
32+
preambles = {},
33+
-- A table of patterns to ignore for each language. The key is the language and the value is a lua match pattern to ignore.
34+
-- lua patterns: https://www.lua.org/pil/20.2.html
35+
ignore_pattern = {
36+
-- ipython cell magic (lines starting with %) and shell commands (lines starting with !)
37+
python = "^(%s*[%%!].*)",
38+
},
3439
},
3540
-- list of characters that should be stripped from the beginning and end of the code chunks
3641
strip_wrapping_quote_characters = { "'", '"', "`" },

lua/otter/init.lua

+5-1
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ M.export_otter_as = keeper.export_otter_as
3737
---@param diagnostics boolean? Enable diagnostics for otter buffers. Default: true
3838
---@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.
3939
---@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.
40-
M.activate = function(languages, completion, diagnostics, tsquery, preambles)
40+
---@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.
41+
M.activate = function(languages, completion, diagnostics, tsquery, preambles, ignore_pattern)
4142
languages = languages or vim.tbl_keys(require("otter.tools.extensions"))
4243
completion = completion ~= false
4344
diagnostics = diagnostics ~= false
4445
preambles = preambles or config.cfg.buffers.preambles
46+
ignore_pattern = ignore_pattern or config.cfg.buffers.ignore_pattern
4547
local main_nr = api.nvim_get_current_buf()
4648
local main_path = api.nvim_buf_get_name(main_nr)
4749
local main_lang = api.nvim_get_option_value("filetype", { buf = main_nr })
@@ -74,6 +76,7 @@ M.activate = function(languages, completion, diagnostics, tsquery, preambles)
7476
buffers = {},
7577
paths = {},
7678
preambles = {},
79+
ignore_pattern = {},
7780
otter_nr_to_lang = {},
7881
tsquery = tsquery,
7982
query = query,
@@ -125,6 +128,7 @@ M.activate = function(languages, completion, diagnostics, tsquery, preambles)
125128
keeper.rafts[main_nr].buffers[lang] = otter_nr
126129
keeper.rafts[main_nr].paths[lang] = otter_path
127130
keeper.rafts[main_nr].preambles[lang] = preambles[lang] or {}
131+
keeper.rafts[main_nr].ignore_pattern[lang] = ignore_pattern[lang] or {}
128132
keeper.rafts[main_nr].otter_nr_to_lang[otter_nr] = lang
129133
table.insert(keeper.rafts[main_nr].languages, lang)
130134

lua/otter/keeper.lua

+9-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ local cfg = require("otter.config").cfg
1616
---@field buffers table<string, integer>
1717
---@field paths table<string, string>
1818
---@field preambles table<string, string[]>
19+
---@field ignore_pattern table<string, string>
1920
---@field otter_nr_to_lang table<integer, string>
2021
---@field tsquery string?
2122
---@field query vim.treesitter.Query
@@ -440,12 +441,18 @@ keeper.sync_raft = function(main_nr, language)
440441

441442
-- collect language lines
442443
-- are allowed to overwrite the preamble
444+
-- apply ignore_pattern filtering on read
445+
local pattern = keeper.rafts[main_nr].ignore_pattern[lang]
443446
for _, t in ipairs(code_chunks) do
444447
local start_index = t.range["from"][1]
445448
for i, l in ipairs(t.text) do
446449
local index = start_index + i
447-
table.remove(ls, index)
448-
table.insert(ls, index, l)
450+
if not string.match(l, pattern) then
451+
table.remove(ls, index)
452+
table.insert(ls, index, l)
453+
else
454+
table.remove(ls, index)
455+
end
449456
end
450457
end
451458

0 commit comments

Comments
 (0)