From d7af8707026968887ea4ee3a4e6fa51fd4372033 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Thu, 24 Sep 2026 13:30:07 +0100 Subject: fix(lua): enable nil-safety diagnostics in runtime code Enable EmmyLua's need-check-nil diagnostic and resolve nullable values through more precise types, explicit invariants, and guards for missing runtime state. Keep analyzer-specific workarounds local and update the affected API documentation. Exclude the bundled re.lua from analysis so its nil-check warnings do not fail CI. AI-assisted --- runtime/lua/vim/treesitter/query.lua | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'runtime/lua/vim/treesitter/query.lua') diff --git a/runtime/lua/vim/treesitter/query.lua b/runtime/lua/vim/treesitter/query.lua index 38621be625..13be18b9e0 100644 --- a/runtime/lua/vim/treesitter/query.lua +++ b/runtime/lua/vim/treesitter/query.lua @@ -723,34 +723,38 @@ local directive_handlers = { local end_idx = #node_text local start_idx = 1 + local end_line = node_text[end_idx] if trim_end_lines then - while end_idx > 0 and node_text[end_idx]:find('^%s*$') do + while end_line and end_line:find('^%s*$') do end_idx = end_idx - 1 + end_line = node_text[end_idx] end_row = end_row - 1 -- set the end position to the last column of the next line, or 0 if we just trimmed the -- last line - end_col = end_idx > 0 and #node_text[end_idx] or 0 + end_col = end_line and #end_line or 0 end end if trim_end_cols then - if end_idx == 0 then + if not end_line then end_row = start_row end_col = start_col else - local whitespace_start = node_text[end_idx]:find('(%s*)$') + local whitespace_start = assert(end_line:find('(%s*)$')) end_col = (whitespace_start - 1) + (end_idx == 1 and start_col or 0) end end + local start_line = node_text[start_idx] if trim_start_lines then - while start_idx <= end_idx and node_text[start_idx]:find('^%s*$') do + while start_idx <= end_idx and assert(start_line):find('^%s*$') do start_idx = start_idx + 1 + start_line = node_text[start_idx] start_row = start_row + 1 start_col = 0 end end - if trim_start_cols and node_text[start_idx] then - local _, whitespace_end = node_text[start_idx]:find('^(%s*)') + if trim_start_cols and start_line then + local _, whitespace_end = start_line:find('^(%s*)') whitespace_end = whitespace_end or 0 start_col = (start_idx == 1 and start_col or 0) + whitespace_end end -- cgit