Skip to content

Commit b3f080b

Browse files
pete3nGithub Actions
and
Github Actions
authored
Merge user setup function, module rename (#9)
* refactor: rename modules * refactor: util -> buffer * style: fix missing trail comma * [docgen] Update doc/ninjection.txt skip-checks: true * ci: typo * [docgen] Update doc/ninjection.txt skip-checks: true * refactor: update tags treesitter -> parse * [docgen] Update doc/ninjection.txt skip-checks: true * refactor: config.cfg -> config.values * feat: setup function for user config overrides * fix: provide default_config when no user overrides * style: function declarations * refactor: rename suppress_warnings -> debug * style: formatting fix * style: formatting fix * style: fix formatting --------- Co-authored-by: Github Actions <actions@github>
1 parent f26b218 commit b3f080b

File tree

9 files changed

+301
-194
lines changed

9 files changed

+301
-194
lines changed

ci/mini-doc.lua

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ require("mini.doc").setup({
2020

2121
local input_files = {
2222
"plugin/ninjection.lua",
23-
"lua/ninjection.lua",
23+
"lua/ninjection/init.lua",
2424
"lua/ninjection/config.lua",
2525
"lua/ninjection/types.lua",
2626
"lua/ninjection/health.lua",
27-
"lua/ninjection/treesitter.lua",
27+
"lua/ninjection/parse.lua",
28+
"lua/ninjection/buffer.lua",
2829
}
2930
require("mini.doc").generate(input_files, "doc/ninjection.txt")

doc/ninjection.txt

+85-12
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ Getting started with ninjection:
2222
`:Ninjection select` - should highlight the entire injected code block.
2323
`:Ninjection edit` - should open a floating window with the injected text
2424
and attach the appropriate LSP.
25-
`:Ninjection restore` - should apply any changes in the editing buffer to
25+
`:Ninjection replace` - should apply any changes in the editing buffer to
2626
the original buffer.
2727
You can also test functionality with:
28-
`:lua require("ninjection.ninjection").select()`
29-
`:lua require("ninjection.ninjection").edit()`
30-
`:lua require("ninjection.ninjection").replace()`
28+
`:lua require("ninjection").select()`
29+
`:lua require("ninjection").edit()`
30+
`:lua require("ninjection").replace()`
3131
3. Ninjection provides keymap plugs for you to map keybindings
3232
of your choosing:
3333
`<Plug>(NinjectionEdit)`
@@ -309,11 +309,11 @@ Return ~
309309

310310

311311
==============================================================================
312-
"ninjection.treesitter"
312+
"ninjection.parse"
313313

314-
The treesitter module contains all treesitter related functions for ninjection.
314+
The parse module contains all treesitter related functions for ninjection.
315315

316-
*ninjection.treesitter.qet_query()*
316+
*ninjection.parse.qet_query()*
317317

318318
Retrieves a parsed query from Treesitter given a language and pattern.
319319

@@ -325,7 +325,7 @@ Return ~
325325
`(vim.treesitter.Query)` `(optional)` parsed_query, string? err
326326
parsed Treesitter Query object
327327

328-
*ninjection.treesitter.get_root()*
328+
*ninjection.parse.get_root()*
329329

330330
Parses the root tree for a language in a buffer.
331331

@@ -337,7 +337,7 @@ Return ~
337337
`(TSNode)` `(optional)` root, string? err
338338
Root node of the TSTree for the language.
339339

340-
*ninjection.treesitter.get_node_table()*
340+
*ninjection.parse.get_node_table()*
341341

342342
Identifies the injected language node at the current cursor position
343343
with start and ending coordinates.
@@ -352,8 +352,8 @@ Returns a table containing:
352352
- node: `TSNode` - the Treesitter node element (see :h TSNode).
353353
- range: `NJRange` - row/col ranges for the node.
354354
NOTE: Coordinates may not match the actual text locations
355-
(see: `ninjection.treesitter.get_visual_range()` for this).
356-
*ninjection.treesitter.get_inj_lang()*
355+
(see: `ninjection.parse.get_visual_range()` for this).
356+
*ninjection.parse.get_inj_lang()*
357357

358358
Parse an injected content node for an associated language comment.
359359

@@ -366,7 +366,7 @@ injections in.
366366
Return ~
367367
`(string)` `(optional)` inj_lang , string? err - Injected language identified.
368368

369-
*ninjection.treesitter.get_visual_range()*
369+
*ninjection.parse.get_visual_range()*
370370

371371
Gets an adjusted "visual" range for a node by approximating the
372372
range of text that is actually seen (as returned by get_node_text).
@@ -388,4 +388,77 @@ Return ~
388388
`(NJRange)` `(optional)` vs_range, string? err - Range of text selected.
389389

390390

391+
==============================================================================
392+
"ninjection.buffer"
393+
394+
The buffer module contains helper functions utilized by the main ninjection
395+
module for creating and editing injected text in buffers.
396+
397+
*ninjection.buffer.get_indents()*
398+
399+
Finds whitespace indents (top, bottom, left) in the provided buffer.
400+
401+
Parameters ~
402+
{bufnr} `(integer)` - Buffer handle.
403+
404+
Return ~
405+
`(NJIndents)` `(optional)` indents, string? err
406+
Returns, on success, a table containing:
407+
- `t_indent`: number of blank lines at the top.
408+
- `b_indent`: number of blank lines at the bottom.
409+
- `l_indent`: minimum number of leading spaces on nonempty lines.
410+
411+
*ninjection.buffer.restore_indents()*
412+
413+
Restores the recorded whitespace indents (top, bottom, and left indent)
414+
for the provided text.
415+
416+
Parameters ~
417+
{text} `(string|table<integer,string>)` The text to restore indents to.
418+
Can be either a string (with newline separators) or a table of lines.
419+
{indents} `(NJIndents)` Table with indent values for t, b, l
420+
421+
Return ~
422+
`(string[])` `(optional)` restored_lines, string? err
423+
Lines with the indents restored.
424+
425+
*ninjection.buffer.create_child_buf()*
426+
427+
Creates a child buffer to edit injected language text.
428+
429+
Parameters ~
430+
{p_bufnr} `(integer)` - Buffer handle for parent buffer.
431+
{p_name} `(string)` - Name for parent buffer.
432+
{p_range} `(NJRange)` - Text range for the injected text.
433+
{root_dir} `(string)` - Root directory for project, or cwd.
434+
{text} `(string)` - Text to populate the child buffer with.
435+
{lang} `(string)` - Language to configure buffer for.
436+
437+
Return ~
438+
`({ bufnr: integer?, win: integer?, indents: NJIndents })` c_table, string? err
439+
*ninjection.buffer.set_child_cur()*
440+
441+
Sets the child cursor to the same relative position as in the parent window.
442+
443+
Parameters ~
444+
{c_win} `(integer)` Handle for child window to set the cursor in.
445+
{p_cursor} `(integer[])` Parent cursor pos.
446+
{s_row} `(integer)` Starting row from the parent to offset the child cursor by.
447+
{indents} `(NJIndents?)` Indents to calculate additional offsets with.
448+
449+
Return ~
450+
`(string)` `(optional)` err
451+
452+
*ninjection.buffer.start_lsp()*
453+
454+
Starts an appropriate LSP for the provided language.
455+
456+
Parameters ~
457+
{lang} `(string)` - The filetype of the injected language (e.g., "lua", "python").
458+
{root_dir} `(string)` - The root directory for the buffer.
459+
460+
Return ~
461+
`(NJLspStatus)` `(optional)` result, string? err - The LSP status.
462+
463+
391464
vim:tw=78:ts=8:noet:ft=help:norl:

doc/tags

+10-5
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@ Ninjection.Subcommand ninjection.txt /*Ninjection.Subcommand*
1111
config.reload() ninjection.txt /*config.reload()*
1212
default_config ninjection.txt /*default_config*
1313
lspconfig.Config ninjection.txt /*lspconfig.Config*
14+
ninjection.buffer.create_child_buf() ninjection.txt /*ninjection.buffer.create_child_buf()*
15+
ninjection.buffer.get_indents() ninjection.txt /*ninjection.buffer.get_indents()*
16+
ninjection.buffer.restore_indents() ninjection.txt /*ninjection.buffer.restore_indents()*
17+
ninjection.buffer.set_child_cur() ninjection.txt /*ninjection.buffer.set_child_cur()*
18+
ninjection.buffer.start_lsp() ninjection.txt /*ninjection.buffer.start_lsp()*
1419
ninjection.edit() ninjection.txt /*ninjection.edit()*
1520
ninjection.health.validate_config() ninjection.txt /*ninjection.health.validate_config()*
21+
ninjection.parse.get_inj_lang() ninjection.txt /*ninjection.parse.get_inj_lang()*
22+
ninjection.parse.get_node_table() ninjection.txt /*ninjection.parse.get_node_table()*
23+
ninjection.parse.get_root() ninjection.txt /*ninjection.parse.get_root()*
24+
ninjection.parse.get_visual_range() ninjection.txt /*ninjection.parse.get_visual_range()*
25+
ninjection.parse.qet_query() ninjection.txt /*ninjection.parse.qet_query()*
1626
ninjection.replace() ninjection.txt /*ninjection.replace()*
1727
ninjection.select() ninjection.txt /*ninjection.select()*
18-
ninjection.treesitter.get_inj_lang() ninjection.txt /*ninjection.treesitter.get_inj_lang()*
19-
ninjection.treesitter.get_node_table() ninjection.txt /*ninjection.treesitter.get_node_table()*
20-
ninjection.treesitter.get_root() ninjection.txt /*ninjection.treesitter.get_root()*
21-
ninjection.treesitter.get_visual_range() ninjection.txt /*ninjection.treesitter.get_visual_range()*
22-
ninjection.treesitter.qet_query() ninjection.txt /*ninjection.treesitter.qet_query()*

lua/ninjection/util.lua lua/ninjection/buffer.lua

+24-26
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
---@module "ninjection.util"
1+
---@module "ninjection.buffer"
22
---@brief
3-
--- The util module contains helper functions utilized by the main ninjection
4-
--- module for getting and recording indentation, creating new child buffers,
5-
--- creating new windows, setting cursor position, and starting and attaching
6-
--- the appropriate LSP to the child buffer.
3+
--- The buffer module contains helper functions utilized by the main ninjection
4+
--- module for creating and editing injected text in buffers.
75
---
86
local M = {}
97
---@nodoc
108
---@type Ninjection.Config
11-
local cfg = require("ninjection.config").cfg
9+
local cfg = require("ninjection.config").values
1210
local lspconfig = require("lspconfig")
1311

1412
-- We need to provide a way of recording and restoring whitespace from the parent
1513
-- buffer to allow easily formatting the buffer without worrying about its
1614
-- relative placement in the parent buffer.
1715

18-
---@tag ninjection.util.get_indents()
16+
---@tag ninjection.buffer.get_indents()
1917
---@brief
2018
--- Finds whitespace indents (top, bottom, left) in the provided buffer.
2119
---
@@ -45,9 +43,9 @@ M.get_indents = function(bufnr)
4543
lines = raw_output
4644

4745
if #lines == 0 then
48-
if cfg.suppress_warnings == false then
46+
if cfg.debug then
4947
vim.notify(
50-
"ninjection.util.get_indents() warning: No lines returned "
48+
"ninjection.buffer.get_indents() warning: No lines returned "
5149
.. "from calling vim.api.nvim_buf_get_lines()",
5250
vim.log.levels.WARN
5351
)
@@ -100,7 +98,7 @@ M.get_indents = function(bufnr)
10098
return indents, nil
10199
end
102100

103-
---@tag ninjection.util.restore_indents()
101+
---@tag ninjection.buffer.restore_indents()
104102
---@brief
105103
--- Restores the recorded whitespace indents (top, bottom, and left indent)
106104
--- for the provided text.
@@ -130,9 +128,9 @@ M.restore_indents = function(text, indents)
130128
---@cast raw_output string[]
131129
lines = raw_output
132130
if #lines == 0 then
133-
if cfg.suppress_warnings == false then
131+
if cfg.debug then
134132
vim.notify(
135-
"ninjection.util.restore_indents() warning: No lines " .. "returned from calling vim.split()",
133+
"ninjection.buffer.restore_indents() warning: No lines " .. "returned from calling vim.split()",
136134
vim.log.levels.WARN
137135
)
138136
end
@@ -141,7 +139,7 @@ M.restore_indents = function(text, indents)
141139
elseif type(text) == "table" then
142140
lines = text
143141
else
144-
error("ninjection.util.restore_indents() error: Text must be a string or " .. "a table of lines", 2)
142+
error("ninjection.buffer.restore_indents() error: Text must be a string or " .. "a table of lines", 2)
145143
end
146144
---@cast lines string[]
147145

@@ -264,7 +262,7 @@ local function create_child_win(bufnr, style)
264262
return 0
265263
end
266264

267-
---@tag ninjection.util.create_child_buf()
265+
---@tag ninjection.buffer.create_child()
268266
---@brief
269267
--- Creates a child buffer to edit injected language text.
270268
---
@@ -280,7 +278,7 @@ end
280278
-- Returns table containing handles for the child buffer and window, if
281279
-- available, and parent indents.
282280
--
283-
M.create_child_buf = function(p_bufnr, p_name, p_range, root_dir, text, lang)
281+
M.create_child = function(p_bufnr, p_name, p_range, root_dir, text, lang)
284282
---@type boolean, unknown, string?, integer?
285283
local ok, raw_output, err, c_bufnr
286284

@@ -337,7 +335,7 @@ M.create_child_buf = function(p_bufnr, p_name, p_range, root_dir, text, lang)
337335
if cfg.preserve_indents then
338336
p_indents, err = M.get_indents(0)
339337
if not p_indents then
340-
if not cfg.suppress_warnings then
338+
if cfg.debug then
341339
vim.notify(
342340
"ninjection.edit() warning: Unable to preserve indentation "
343341
.. "with get_indents(): "
@@ -367,7 +365,7 @@ M.create_child_buf = function(p_bufnr, p_name, p_range, root_dir, text, lang)
367365
return vim.cmd("lua " .. cfg.format_cmd)
368366
end)
369367
if not ok then
370-
if not cfg.suppress_warnings then
368+
if cfg.debug then
371369
err = tostring(raw_output)
372370
vim.notify(
373371
'ninjection.edit() warning: Calling vim.cmd("lua "' .. cfg.format_cmd .. ")\n" .. err,
@@ -397,7 +395,7 @@ M.create_child_buf = function(p_bufnr, p_name, p_range, root_dir, text, lang)
397395
return { bufnr = c_bufnr, win = c_win, indents = p_indents }
398396
end
399397

400-
---@tag ninjection.util.set_child_cur()
398+
---@tag ninjection.buffer.set_child_cur()
401399
---@brief
402400
--- Sets the child cursor to the same relative position as in the parent window.
403401
---
@@ -438,7 +436,7 @@ M.set_child_cur = function(c_win, p_cursor, s_row, indents)
438436
return vim.api.nvim_win_set_cursor(c_win, offset_cur)
439437
end)
440438
if not ok then
441-
if not cfg.suppress_warnings then
439+
if cfg.debug then
442440
err = tostring(raw_output)
443441
vim.notify(
444442
"ninjection.edit() warning: Calling vim.api.nvim_win_set_cursor"
@@ -457,7 +455,7 @@ end
457455
-- Autocommands don't trigger properly when creating and arbitrarily assigning
458456
-- filetypes to buffers, so we need a function to start the appropriate LSP.
459457

460-
---@tag ninjection.util.start_lsp()
458+
---@tag ninjection.buffer.start_lsp()
461459
---@brief
462460
--- Starts an appropriate LSP for the provided language.
463461
---
@@ -475,7 +473,7 @@ M.start_lsp = function(lang, root_dir)
475473
lang_lsp = cfg.lsp_map[lang]
476474
if not lang_lsp then
477475
vim.notify(
478-
"ninjection.util.start_lsp() warning: No LSP mapped to "
476+
"ninjection.buffer.start_lsp() warning: No LSP mapped to "
479477
.. "language: "
480478
.. lang
481479
.. " check your configuration.",
@@ -496,7 +494,7 @@ M.start_lsp = function(lang, root_dir)
496494
local lsp_def = raw_output
497495
if not lsp_def then
498496
vim.notify(
499-
"ninjection.util.start_lsp() warning: Could not find "
497+
"ninjection.buffer.start_lsp() warning: Could not find "
500498
.. "default_config for "
501499
.. lang_lsp
502500
.. ". Ensure it is installed and "
@@ -513,7 +511,7 @@ M.start_lsp = function(lang, root_dir)
513511
local lsp_cmd = lsp_def.cmd
514512
if not lsp_cmd or #lsp_cmd == 0 then
515513
vim.notify(
516-
"ninjection.util.start_lsp() warning: Command to execute "
514+
"ninjection.buffer.start_lsp() warning: Command to execute "
517515
.. lang_lsp
518516
.. " does not exist. Ensure it is installed and configured.",
519517
vim.log.levels.WARN
@@ -532,7 +530,7 @@ M.start_lsp = function(lang, root_dir)
532530
end
533531
if raw_output ~= 1 then
534532
vim.notify(
535-
"ninjection.util.start_lsp() warning: The LSP command: " .. lsp_cmd[1] .. " is not executable.",
533+
"ninjection.buffer.start_lsp() warning: The LSP command: " .. lsp_cmd[1] .. " is not executable.",
536534
vim.log.levels.WARN
537535
)
538536
return { "no-exec", -1 }
@@ -541,7 +539,7 @@ M.start_lsp = function(lang, root_dir)
541539
-- The LSP must support our injected language
542540
if not vim.tbl_contains(lsp_def.filetypes, lang) then
543541
vim.notify(
544-
"ninjection.util.start_lsp() warning: The configured LSP: "
542+
"ninjection.buffer.start_lsp() warning: The configured LSP: "
545543
.. lang_lsp
546544
.. " does not support "
547545
.. lang
@@ -566,7 +564,7 @@ M.start_lsp = function(lang, root_dir)
566564
local client_id = raw_output
567565
if client_id == nil then
568566
vim.notify(
569-
"ninjection.util.start_lsp() warning: The LSP: "
567+
"ninjection.buffer.start_lsp() warning: The LSP: "
570568
.. lang_lsp
571569
.. " did not return a client_id, check your language client logs "
572570
.. "(default ~/.local/state/nvim/lsp.log) for more information.",

0 commit comments

Comments
 (0)