1
- --- @module " ninjection.util "
1
+ --- @module " ninjection.buffer "
2
2
--- @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.
7
5
---
8
6
local M = {}
9
7
--- @nodoc
10
8
--- @type Ninjection.Config
11
- local cfg = require (" ninjection.config" ).cfg
9
+ local cfg = require (" ninjection.config" ).values
12
10
local lspconfig = require (" lspconfig" )
13
11
14
12
-- We need to provide a way of recording and restoring whitespace from the parent
15
13
-- buffer to allow easily formatting the buffer without worrying about its
16
14
-- relative placement in the parent buffer.
17
15
18
- --- @tag ninjection.util .get_indents()
16
+ --- @tag ninjection.buffer .get_indents()
19
17
--- @brief
20
18
--- Finds whitespace indents (top, bottom, left) in the provided buffer.
21
19
---
@@ -45,9 +43,9 @@ M.get_indents = function(bufnr)
45
43
lines = raw_output
46
44
47
45
if # lines == 0 then
48
- if cfg .suppress_warnings == false then
46
+ if cfg .debug then
49
47
vim .notify (
50
- " ninjection.util .get_indents() warning: No lines returned "
48
+ " ninjection.buffer .get_indents() warning: No lines returned "
51
49
.. " from calling vim.api.nvim_buf_get_lines()" ,
52
50
vim .log .levels .WARN
53
51
)
@@ -100,7 +98,7 @@ M.get_indents = function(bufnr)
100
98
return indents , nil
101
99
end
102
100
103
- --- @tag ninjection.util .restore_indents()
101
+ --- @tag ninjection.buffer .restore_indents()
104
102
--- @brief
105
103
--- Restores the recorded whitespace indents (top, bottom, and left indent)
106
104
--- for the provided text.
@@ -130,9 +128,9 @@ M.restore_indents = function(text, indents)
130
128
--- @cast raw_output string[]
131
129
lines = raw_output
132
130
if # lines == 0 then
133
- if cfg .suppress_warnings == false then
131
+ if cfg .debug then
134
132
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()" ,
136
134
vim .log .levels .WARN
137
135
)
138
136
end
@@ -141,7 +139,7 @@ M.restore_indents = function(text, indents)
141
139
elseif type (text ) == " table" then
142
140
lines = text
143
141
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 )
145
143
end
146
144
--- @cast lines string[]
147
145
@@ -264,7 +262,7 @@ local function create_child_win(bufnr, style)
264
262
return 0
265
263
end
266
264
267
- --- @tag ninjection.util.create_child_buf ()
265
+ --- @tag ninjection.buffer.create_child ()
268
266
--- @brief
269
267
--- Creates a child buffer to edit injected language text.
270
268
---
280
278
-- Returns table containing handles for the child buffer and window, if
281
279
-- available, and parent indents.
282
280
--
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 )
284
282
--- @type boolean , unknown , string ?, integer ?
285
283
local ok , raw_output , err , c_bufnr
286
284
@@ -337,7 +335,7 @@ M.create_child_buf = function(p_bufnr, p_name, p_range, root_dir, text, lang)
337
335
if cfg .preserve_indents then
338
336
p_indents , err = M .get_indents (0 )
339
337
if not p_indents then
340
- if not cfg .suppress_warnings then
338
+ if cfg .debug then
341
339
vim .notify (
342
340
" ninjection.edit() warning: Unable to preserve indentation "
343
341
.. " with get_indents(): "
@@ -367,7 +365,7 @@ M.create_child_buf = function(p_bufnr, p_name, p_range, root_dir, text, lang)
367
365
return vim .cmd (" lua " .. cfg .format_cmd )
368
366
end )
369
367
if not ok then
370
- if not cfg .suppress_warnings then
368
+ if cfg .debug then
371
369
err = tostring (raw_output )
372
370
vim .notify (
373
371
' 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)
397
395
return { bufnr = c_bufnr , win = c_win , indents = p_indents }
398
396
end
399
397
400
- --- @tag ninjection.util .set_child_cur()
398
+ --- @tag ninjection.buffer .set_child_cur()
401
399
--- @brief
402
400
--- Sets the child cursor to the same relative position as in the parent window.
403
401
---
@@ -438,7 +436,7 @@ M.set_child_cur = function(c_win, p_cursor, s_row, indents)
438
436
return vim .api .nvim_win_set_cursor (c_win , offset_cur )
439
437
end )
440
438
if not ok then
441
- if not cfg .suppress_warnings then
439
+ if cfg .debug then
442
440
err = tostring (raw_output )
443
441
vim .notify (
444
442
" ninjection.edit() warning: Calling vim.api.nvim_win_set_cursor"
457
455
-- Autocommands don't trigger properly when creating and arbitrarily assigning
458
456
-- filetypes to buffers, so we need a function to start the appropriate LSP.
459
457
460
- --- @tag ninjection.util .start_lsp()
458
+ --- @tag ninjection.buffer .start_lsp()
461
459
--- @brief
462
460
--- Starts an appropriate LSP for the provided language.
463
461
---
@@ -475,7 +473,7 @@ M.start_lsp = function(lang, root_dir)
475
473
lang_lsp = cfg .lsp_map [lang ]
476
474
if not lang_lsp then
477
475
vim .notify (
478
- " ninjection.util .start_lsp() warning: No LSP mapped to "
476
+ " ninjection.buffer .start_lsp() warning: No LSP mapped to "
479
477
.. " language: "
480
478
.. lang
481
479
.. " check your configuration." ,
@@ -496,7 +494,7 @@ M.start_lsp = function(lang, root_dir)
496
494
local lsp_def = raw_output
497
495
if not lsp_def then
498
496
vim .notify (
499
- " ninjection.util .start_lsp() warning: Could not find "
497
+ " ninjection.buffer .start_lsp() warning: Could not find "
500
498
.. " default_config for "
501
499
.. lang_lsp
502
500
.. " . Ensure it is installed and "
@@ -513,7 +511,7 @@ M.start_lsp = function(lang, root_dir)
513
511
local lsp_cmd = lsp_def .cmd
514
512
if not lsp_cmd or # lsp_cmd == 0 then
515
513
vim .notify (
516
- " ninjection.util .start_lsp() warning: Command to execute "
514
+ " ninjection.buffer .start_lsp() warning: Command to execute "
517
515
.. lang_lsp
518
516
.. " does not exist. Ensure it is installed and configured." ,
519
517
vim .log .levels .WARN
@@ -532,7 +530,7 @@ M.start_lsp = function(lang, root_dir)
532
530
end
533
531
if raw_output ~= 1 then
534
532
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." ,
536
534
vim .log .levels .WARN
537
535
)
538
536
return { " no-exec" , - 1 }
@@ -541,7 +539,7 @@ M.start_lsp = function(lang, root_dir)
541
539
-- The LSP must support our injected language
542
540
if not vim .tbl_contains (lsp_def .filetypes , lang ) then
543
541
vim .notify (
544
- " ninjection.util .start_lsp() warning: The configured LSP: "
542
+ " ninjection.buffer .start_lsp() warning: The configured LSP: "
545
543
.. lang_lsp
546
544
.. " does not support "
547
545
.. lang
@@ -566,7 +564,7 @@ M.start_lsp = function(lang, root_dir)
566
564
local client_id = raw_output
567
565
if client_id == nil then
568
566
vim .notify (
569
- " ninjection.util .start_lsp() warning: The LSP: "
567
+ " ninjection.buffer .start_lsp() warning: The LSP: "
570
568
.. lang_lsp
571
569
.. " did not return a client_id, check your language client logs "
572
570
.. " (default ~/.local/state/nvim/lsp.log) for more information." ,
0 commit comments