From 891d5b2263f7e2830c2317ac965ad3d565fe014a Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Thu, 18 May 2023 07:00:50 -0700 Subject: [PATCH] aider: Added line numbers to the output of the `get_quoted_file` function. # Context: USER: git get_files_content prefix each line with a line number, starting at 1, padded out like ' 1' ASSISTANT: To add line numbers to the `get_files_content` method, we will modify the method in the `aider/coder.py` file. We will read the file content line by line and add the line number with padding to each line. Here's the change: ```python aider/coder.py <<<<<<< ORIGINAL def get_files_content(self, fnames=None): if not fnames: fnames = self.abs_fnames prompt = "" for fname in fnames: relative_fname = self.get_rel_fname(fname) prompt += utils.quoted_file(fname, relative_fname) return prompt ======= def get_files_content(self, fnames=None): ^C KeyboardInterrupt USER: sorry, in get_quoted_file prefix each line with a line number, starting at 1, padded out like ' 1' ASSISTANT: ^C KeyboardInterrupt USER: Please note that I shared content of these additional files: aider/utils.py ASSISTANT: To add line ^C KeyboardInterrupt USER: in get_quoted_file prefix each line with a line number, starting at 1, padded out like ' 1' ASSISTANT: To add line numbers to the `get_quoted_file` function in the `aider/utils.py` file, we will read the file content line by line and add the line number with padding to each line. Here's the change: ```python aider/utils.py <<<<<<< ORIGINAL def quoted_file(fname, display_fname): prompt = "\n" prompt += display_fname prompt += "\n```\n" prompt += Path(fname).read_text() prompt += "\n```\n" return prompt ======= def quoted_file(fname, display_fname): prompt = "\n" prompt += display_fname prompt += "\n```\n" file_content = Path(fname).read_text() lines = file_content.splitlines() for i, line in enumerate(lines, start=1): prompt += f"{i:3d} {line}\n" prompt += "```\n" return prompt >>>>>>> UPDATED ``` This change will add line numbers to the output of the `get_quoted_file` function, starting at 1 and padded out like ' 1'. --- aider/utils.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/aider/utils.py b/aider/utils.py index 1b9873b4b..9164c156b 100644 --- a/aider/utils.py +++ b/aider/utils.py @@ -118,8 +118,11 @@ def quoted_file(fname, display_fname): prompt = "\n" prompt += display_fname prompt += "\n```\n" - prompt += Path(fname).read_text() - prompt += "\n```\n" + file_content = Path(fname).read_text() + lines = file_content.splitlines() + for i, line in enumerate(lines, start=1): + prompt += f"{i:3d} {line}\n" + prompt += "```\n" return prompt