@@ -139,14 +139,31 @@ private meta partial def updateSyntaxTrailing (trailing : Substring.Raw) : Synta
139139 | .atom info val => .atom (updateInfoTrailing trailing info) val
140140 | .ident info rawVal val pre => .ident (updateInfoTrailing trailing info) rawVal val pre
141141 | n@(.node info k args) =>
142- if h : args.size = 0 then n
143- else
144- let i := args.size - 1
142+ if let some i := findIdxRev? (not ∘ empty) args then
145143 let last := updateSyntaxTrailing trailing args[i]
146144 let args := args.set i last;
147145 Syntax.node info k args
146+ else n
148147 | s => s
149148where
149+ findIdxRev? {α} (p : α → Bool) (xs : Array α) : Option (Fin xs.size) := do
150+ if h : xs.size = 0 then failure
151+ else
152+ let mut n : Fin xs.size := ⟨xs.size - 1 , by grind⟩
153+ repeat
154+ if p xs[n] then return n
155+ if n.val = 0 then
156+ break
157+ else
158+ n := ⟨n.val - 1 , by grind⟩
159+ failure
160+
161+ empty : Syntax → Bool
162+ | .atom .. | .ident .. | .missing => false
163+ | .node .none _ args | .node (.synthetic ..) _ args => args.all empty
164+ | .node (.original leading _ trailing _) _ args =>
165+ leading.startPos == leading.stopPos && trailing.startPos == trailing.stopPos && args.all empty
166+
150167 updateInfoTrailing (trailing : Substring.Raw) : SourceInfo → SourceInfo
151168 | .original leading pos _ endPos => .original leading pos trailing endPos
152169 | .none =>
@@ -199,8 +216,9 @@ where
199216 let top := s.stxStack.back
200217 if let some ⟨_, tr⟩ := getTailContext? c.fileMap.source top then
201218 let tr := { tr with stopPos := s.pos }
202- s.popSyntax.pushSyntax (updateSyntaxTrailing tr top)
203- else s
219+ s.popSyntax.pushSyntax <| updateSyntaxTrailing tr top
220+ else
221+ s
204222
205223/--
206224As we elaborate a `#doc` command top-level-block by top-level-block, the Lean environment will
0 commit comments