Roo-Code/src/core/tools
KJ7LNW 270fd88cc8
refactor: improve readFileTool XML output format (#2340)
* fix: addLineNumbers handling of empty content

Empty files should not have line numbers, but non-empty files with empty content at a specific line offset should.
- If content is empty, return empty string for empty files
- If content is empty but startLine > 1, return line number for empty content at that offset
This ensures that the model does not think the file contains a single empty line.

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>

* refactor: improve readFileTool XML output format

- Remove unnecessary XML indentation that could confuse the model
- Separate file content from notices and errors using dedicated tags
- Add line range information to content tags
- Handle empty files properly with self-closing tags
- Add comprehensive test coverage

Fixes #2278

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>

* fix: always show line numbers in read_file XML output

- Always display line numbers in non-range reads
- Improve XML formatting with consistent newlines for better readability

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>

* test: update tests to match new XML format with line numbers

- Update test expectations to match the new XML format with newlines
- Update tests to expect line numbers attribute in content tags
- Modify test assertions to check for the correct line range values

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>

* fix: consistent blank line handling in addLineNumbers

- Add newline to all output
- Handle trailing newlines and empty lines consistently
- Add test cases for blank lines:
  - Multiple blank lines within content
  - Multiple trailing blank lines
  - Only blank lines with offset
  - Trailing newlines

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>

* test: use actual addLineNumbers in read-file-xml tests

- Modified extract-text mock to preserve actual addLineNumbers implementation
- Removed mock implementation of addLineNumbers
- Updated test data to account for trailing newline
- Removed unnecessary mock verification

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>

* test: ensure actual addLineNumbers function is called in tests

- Replace direct mocking of addLineNumbers with spy on actual implementation
- Add verification to ensure the real function is called when appropriate
- Add skipAddLineNumbersCheck option for cases where function should not be called
- Update test cases to use appropriate verification options
- Fix numberedFileContent to include trailing newline for consistency

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>

* fix: modify readLines to process data directly instead of line by line

- Direct data processing provides more accurate results by preserving exact content with carriage returns
- Improved performance through minimal buffering and efficient string operations
- Use string indexes to find newlines while maintaining their original format
- Handle all edge cases correctly with preserved line endings
- Add tests for various edge cases including empty files, single lines, and different line endings

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>

* test: remove unused mockInputContent variable

Remove unused variable declaration to appease ellipsis-dev linter requirements.

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>

---------

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>
Co-authored-by: Eric Wheeler <roo-code@z.ewheeler.org>
2025-04-08 23:04:44 -04:00
..
__tests__ Fix Gemini command escaping (#2120) 2025-03-30 14:07:00 -04:00
accessMcpResourceTool.ts Move use_mcp_tool and access_mcp_resource to tool files (#2101) 2025-03-30 01:47:19 -04:00
applyDiffTool.ts Move apply_diff to a tool file (#2094) 2025-03-30 01:04:38 -04:00
askFollowupQuestionTool.ts Move new_task, switch_mode, attempt_completion, and ask_followup_question to tool files (#2102) 2025-03-30 01:58:35 -04:00
attemptCompletionTool.ts Move new_task, switch_mode, attempt_completion, and ask_followup_question to tool files (#2102) 2025-03-30 01:58:35 -04:00
browserActionTool.ts feat(browserTool): Implement hover action (#2368) 2025-04-07 09:35:14 -04:00
executeCommandTool.ts Fix Gemini command escaping (#2120) 2025-03-30 14:07:00 -04:00
fetchInstructionsTool.ts Remove switch from tools (#2091) 2025-03-30 00:31:56 -04:00
insertContentTool.ts Move insert_content to a tool file (#2095) 2025-03-30 01:10:26 -04:00
listCodeDefinitionNamesTool.ts Move list_code_definition_names to a tool file (#2097) 2025-03-30 01:21:01 -04:00
listFilesTool.ts Fix a type in the listFiles tool (#2092) 2025-03-30 00:37:13 -04:00
newTaskTool.ts Move new_task, switch_mode, attempt_completion, and ask_followup_question to tool files (#2102) 2025-03-30 01:58:35 -04:00
readFileTool.ts refactor: improve readFileTool XML output format (#2340) 2025-04-08 23:04:44 -04:00
searchAndReplaceTool.ts Move search_and_replace to a tool file (#2096) 2025-03-30 01:15:43 -04:00
searchFilesTool.ts Move search_files to a tool file (#2098) 2025-03-30 01:28:58 -04:00
switchModeTool.ts Move new_task, switch_mode, attempt_completion, and ask_followup_question to tool files (#2102) 2025-03-30 01:58:35 -04:00
types.ts Move new_task, switch_mode, attempt_completion, and ask_followup_question to tool files (#2102) 2025-03-30 01:58:35 -04:00
useMcpToolTool.ts Move use_mcp_tool and access_mcp_resource to tool files (#2101) 2025-03-30 01:47:19 -04:00
writeToFileTool.ts Move write_to_file to a tool file (#2093) 2025-03-30 00:49:08 -04:00