public static void AddTrivia(this IScriptTerminal node, ScriptTrivia trivia, bool before) { var trivias = node.Trivias; if (trivias == null) { node.Trivias = trivias = new ScriptTrivias(); } (before ? trivias.Before : trivias.After).Add(trivia); }
public override void Visit(ScriptNode node) { if (node == null) { return; } // Visit first to get nodes from left to right base.Visit(node); if (node is IScriptTerminal terminal) { var trivias = terminal.Trivias; if (trivias != null) { if (_leftTrivias == null) { _leftTrivias = trivias; bool previousHasSpaces = false; CompactSpaces(_leftTrivias.Before, ref previousHasSpaces); previousHasSpaces = false; CompactSpaces(_leftTrivias.After, ref previousHasSpaces); } else { if (_rightTrivias != null) { _leftTrivias = _rightTrivias; } _rightTrivias = trivias; bool previousHasSpaces = false; CompactSpaces(_leftTrivias.After, ref previousHasSpaces); CompactSpaces(_rightTrivias.Before, ref previousHasSpaces); previousHasSpaces = false; CompactSpaces(_rightTrivias.After, ref previousHasSpaces); } } else { // Reset if we have a terminal with no trivias _leftTrivias = null; _rightTrivias = null; } } }
public void CompressSpaces(ScriptNode node) { if (node == null) { return; } _leftTrivias = null; _rightTrivias = null; node.RemoveLeadingSpace(); Visit(node); if (_rightTrivias != null) { bool previousHasSpaces = false; CompactSpaces(_rightTrivias.After, ref previousHasSpaces); } node.RemoveTrailingSpace(); }