1
0
Fork 0
mirror of https://github.com/Reuh/anselme.git synced 2025-10-27 16:49:31 +00:00

[internal] Build indentation in Block:format instead of Node:format

This commit is contained in:
Étienne Fildadut 2024-01-15 16:37:08 +01:00
parent b32521cb60
commit 09b73301b4
3 changed files with 10 additions and 12 deletions

View file

@ -8,20 +8,21 @@ local Block = ast.abstract.Node {
expressions = {},
init = function(self)
self.expressions = {}
init = function(self, ...)
self.expressions = {...}
end,
add = function(self, expression) -- only for construction
table.insert(self.expressions, expression)
end,
_format = function(self, state, prio, ...)
_format = function(self, state, prio, indentation, ...)
local l = {}
local indent = ("\t"):rep(indentation)
for _, e in ipairs(self.expressions) do
if Flush:is(e) then
table.insert(l, (e:format(state, 0, ...):gsub("\n$", "")))
table.insert(l, indent..(e:format(state, 0, indentation, ...):gsub("\n$", "")))
else
table.insert(l, e:format(state, 0, ...))
table.insert(l, indent..e:format(state, 0, indentation, ...))
end
end
return table.concat(l, "\n")

View file

@ -27,11 +27,11 @@ PartialScope = ast.abstract.Node {
end,
_format = function(self, state, priority, indentation, ...)
if self.definitions[attached_block_symbol] then
if self.definitions[attached_block_symbol] and not Undefined:is(self.definitions[attached_block_symbol]) then
local block = self.definitions[attached_block_symbol]
local exp = self.expression:format(state, priority, indentation, ...)
if exp:sub(-2) == " _" then exp = exp:sub(1, -3) end
return exp.."\n\t"..block:format(state, priority, indentation + 1, ...)
return exp.."\n"..block:format(state, priority, indentation + 1, ...) -- block is a Block and will indent itself like a good boy
else
return self.expression:format(state, priority, indentation, ...)
end

View file

@ -14,7 +14,7 @@ local unpack = table.unpack or unpack
local uuid = require("anselme.common").uuid
local Call, Identifier, ArgumentTuple, Struct, Tuple, String, Pair
local Call, Identifier, Struct, Tuple, String, Pair
local resume_manager
local custom_call_identifier
@ -336,9 +336,6 @@ Node = class {
end
end
local indentation = ("\t"):rep(indentation_level)
s = s:gsub("\n", "\n"..indentation)
return s
end,
-- same as :format, but should be called only for nodes right of the current operator
@ -427,7 +424,7 @@ Node = class {
-- Thus, any require here that may require other Nodes shall be done here. This method is called in anselme.lua after everything else is required.
_i_hate_cycles = function(self)
local ast = require("anselme.ast")
Call, Identifier, ArgumentTuple, Struct, Tuple, String, Pair = ast.Call, ast.Identifier, ast.ArgumentTuple, ast.Struct, ast.Tuple, ast.String, ast.Pair
Call, Identifier, Struct, Tuple, String, Pair = ast.Call, ast.Identifier, ast.Struct, ast.Tuple, ast.String, ast.Pair
custom_call_identifier = Identifier:new("_!")
resume_manager = require("anselme.state.resume_manager")