StyLua icon indicating copy to clipboard operation
StyLua copied to clipboard

Range formatting dedents statements inside function expressions (table constructor values)

Open riptscripts opened this issue 3 months ago • 0 comments

Summary

--range-start/--range-end re-indents statements one level too shallow when the statement is nested inside a function expression (e.g. a function assigned as a table-constructor value). Statements inside plain block-level functions (local function f()) are unaffected. Reproduced on stylua 2.5.2 with stock settings (no stylua.toml).

Reproduction

repro.lua (tabs, already stylua-canonical — whole-file stylua repro.lua is a no-op):

local M = {}

M.catalog = {
	build = function()
		local payload = {}
		return payload
	end,
}

return M

Format just the local payload = {} line (bytes 48–68):

stylua --range-start 48 --range-end 68 repro.lua

Result:

 	build = function()
-		local payload = {}
+	local payload = {}
 		return payload

The statement is re-emitted at one indent level less than its actual nesting — as if the table-constructor (or function-expression) level isn't counted when computing the additional indent for a range-formatted nested statement.

Notes

  • Control: the same single-line range invocation on a statement inside a block-level local function f() ... end keeps its indent — the trigger is specifically expression-nested function bodies.
  • A range covering the entire enclosing top-level statement (all of M.catalog = { ... }) formats correctly, so the indent computation is only wrong when the range starts inside the expression.
  • Deeper nesting in real code shows the same behavior: every statement fully inside the range dedents one level, partially-contained statements are (correctly, per the documented caveat) skipped — which produces mixed indentation in the output file.
  • Historical context: #239 was a different range bug in the same area (nested-block statements not formatted at all); this one formats them but at the wrong level.

Found via an editor-integration-style pipeline that range-formats only changed lines after each edit; happy to provide more cases if useful.

riptscripts avatar Aug 11 '26 01:08 riptscripts