Code Folding Render
MediumSimulation ยท Jane Street
Simulate code folding on lines, where a line's indent is its number of leading spaces. A line is a header if the next line is indented more than it; its block is the run of lines right after it that are indented more than it. Nothing starts folded.
Each string in ops is "fold d", "unfold d" or "render", with d a 1-based position among the currently visible lines. fold and unfold fold or unfold that header and do nothing on a non-header; other headers keep their state. A folded header hides its block and shows with " ..." appended.
Return the visible lines for each "render", in order.
Example 1
- Input:
- lines = ["config:", " net:", " host", " port", " log:", " level", "end"], ops = ["fold 2", "fold 1", "render", "unfold 1", "render"]
- Output:
- [["config: ...", "end"], ["config:", " net: ...", " log:", " level", "end"]]
- Explanation
- After
fold 2,fold 1hitsconfig:, so only it andendshow. Unfoldingconfig:leavesnet:folded.
Example 2
- Input:
- lines = ["a", " b", " c", "d"], ops = ["fold 1", "render"]
- Output:
- [["a ...", "d"]]
- Explanation
- Both
bandcare indented more thana, so both are in its block.
Example 3
- Input:
- lines = ["x", " y"], ops = ["fold 2", "render", "unfold 1", "render"]
- Output:
- [["x", " y"], ["x", " y"]]
Constraints
1 <= lines.length <= 1000lines[i]has no trailing spaces, at least one non-space character, and only spaces for indentation1 <= ops.length <= 1000- Every
dis between1and the number of visible lines when the operation runs