From 7316635abacae53727064d228299a9452a7cc6ca Mon Sep 17 00:00:00 2001 From: "Paul Gauthier (aider)" Date: Mon, 17 Mar 2025 21:10:12 -0700 Subject: [PATCH] feat: Remove padding from markdown code blocks using NoInsetMarkdown --- aider/mdstream.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/aider/mdstream.py b/aider/mdstream.py index e7e19df87..2d593d610 100755 --- a/aider/mdstream.py +++ b/aider/mdstream.py @@ -5,7 +5,8 @@ import time from rich.console import Console from rich.live import Live -from rich.markdown import Markdown +from rich.markdown import Markdown, CodeBlock +from rich.syntax import Syntax from rich.text import Text from aider.dump import dump # noqa: F401 @@ -46,6 +47,27 @@ The end. """ # noqa: E501 +class NoInsetCodeBlock(CodeBlock): + """A code block with syntax highlighting and no padding.""" + + def __rich_console__(self, console, options): + code = str(self.text).rstrip() + syntax = Syntax( + code, self.lexer_name, theme=self.theme, word_wrap=True, padding=0 + ) + yield syntax + + +class NoInsetMarkdown(Markdown): + """Markdown with code blocks that have no padding.""" + + @classmethod + def make_elements(cls): + elements = super().make_elements() + elements["code_block"] = NoInsetCodeBlock + return elements + + class MarkdownStream: """Streaming markdown renderer that progressively displays content with a live updating window. @@ -88,7 +110,7 @@ class MarkdownStream: # Render the markdown to a string buffer string_io = io.StringIO() console = Console(file=string_io, force_terminal=True) - markdown = Markdown(text, **self.mdargs) + markdown = NoInsetMarkdown(text, **self.mdargs) console.print(markdown) output = string_io.getvalue() @@ -186,6 +208,7 @@ if __name__ == "__main__": _text = _text * 10 pm = MarkdownStream() + print("Using NoInsetMarkdown for code blocks with padding=0") for i in range(6, len(_text), 5): pm.update(_text[:i]) time.sleep(0.01)