This repository was archived by the owner on Aug 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 477
fix(chat): document code command now properly indents generated comments especially python docstrings #8039
Open
ichim-david
wants to merge
11
commits into
main
Choose a base branch
from
ichimdav/document_insert_edit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
43348e1
fix(non-stop): Improve fixup insertion and docstring placement
ichim-david 48e5619
fix(tree-sitter): Adjust symbol capture condition in query wrappers
ichim-david 989d66b
Merge branch 'main' into ichimdav/document_insert_edit
ichim-david 4382b4e
fix(edit): Improve code indentation and docstring generation for fixups
ichim-david 2756d15
Update test snapshot for document code.
ichim-david 9f730cb
Merge remote-tracking branch 'origin/main' into ichimdav/document_ins…
ichim-david 989e34f
Fix: Handle null arguments in vscode.commands.executeCommand and add …
ichim-david 4468e7c
pnpm biome
ichim-david 0131617
Fix: Improve Python docstring insertion in FixupController
ichim-david ae8943b
biome fix
ichim-david 3c12d6a
Fix: Use editor tab size for Python docstring indentation in FixupCon…
ichim-david File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,15 +24,14 @@ import { | |
| type EditMode, | ||
| EditModeTelemetryMetadataMapping, | ||
| } from '../edit/types' | ||
| import { isStreamedIntent } from '../edit/utils/edit-intent' | ||
| import { getOverriddenModelForIntent } from '../edit/utils/edit-models' | ||
| import type { ExtensionClient } from '../extension-client' | ||
| import { isRunningInsideAgent } from '../jsonrpc/isRunningInsideAgent' | ||
| import { logDebug } from '../output-channel-logger' | ||
| import { charactersLogger } from '../services/CharactersLogger' | ||
| import { splitSafeMetadata } from '../services/telemetry-v2' | ||
| import { countCode } from '../services/utils/code-count' | ||
|
|
||
| import { isStreamedIntent } from '../edit/utils/edit-intent' | ||
| import { FixupDocumentEditObserver } from './FixupDocumentEditObserver' | ||
| import type { FixupFile } from './FixupFile' | ||
| import { FixupFileObserver } from './FixupFileObserver' | ||
|
|
@@ -952,26 +951,83 @@ export class FixupController | |
| return false | ||
| } | ||
|
|
||
| // Get the index of the first non-whitespace character on the line where the insertion point is. | ||
| const nonEmptyStartIndex = document.lineAt( | ||
| task.insertionPoint.line | ||
| ).firstNonWhitespaceCharacterIndex | ||
| // Split the text into lines and prepend each line with spaces to match the indentation level | ||
| // of the line where the insertion point is. | ||
| const textLines = text.split('\n').map(line => ' '.repeat(nonEmptyStartIndex) + line) | ||
| // Join the lines back into a single string with newline characters | ||
| // Remove any leading whitespace from the first line, as we are inserting at the insertionPoint | ||
| // Keep any trailing whitespace on the last line to preserve the original indentation. | ||
| const replacementText = textLines.join('\n').trimStart() | ||
| // Get the indentation context for the insertion point | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. General comment, it would be great to extract this logic into a util and add tests for it There's quite a few edge cases (python file, doc intent, spaces vs tabs). Would be good to have those tested to avoid any regressions Amp should help do most of the heavy lifting for the test file! |
||
| const insertionLine = document.lineAt(task.insertionPoint.line) | ||
| let targetIndentSize = insertionLine.firstNonWhitespaceCharacterIndex | ||
| const textIndentSize = text.search(/\S/) || 0 | ||
|
|
||
| const isPythonFile = task.document.languageId === 'python' | ||
| const isDocIntent = task.intent === 'doc' | ||
|
|
||
| // Special case for Python documentation | ||
| if (targetIndentSize === 0 && isPythonFile && isDocIntent) { | ||
| targetIndentSize = 4 | ||
| } | ||
ichim-david marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Insert the updated text at the specified insertionPoint. | ||
| // Calculate indentation adjustments | ||
| const needsIndentAdjustment = targetIndentSize > textIndentSize | ||
| const indentDifference = needsIndentAdjustment ? targetIndentSize - textIndentSize : 0 | ||
|
|
||
| // Process the text lines | ||
| const textLines = text.split('\n') | ||
| const processedLines = textLines.map((line, index) => { | ||
| // Don't add extra indentation to empty lines | ||
| if (line.trim() === '') { | ||
| return line | ||
| } | ||
|
|
||
| // For the first line, only add indentation if we're at the start of a line | ||
| if ( | ||
| index === 0 && | ||
| task.insertionPoint.character > 0 && | ||
| line.startsWith(' '.repeat(textIndentSize)) | ||
| ) { | ||
| return line.trimStart() | ||
| } | ||
|
|
||
| // Add the calculated indentation difference | ||
| return ' '.repeat(indentDifference) + line | ||
|
Comment on lines
+983
to
+989
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this work on files that use tabs, not spaces, for indentation? |
||
| }) | ||
|
|
||
| // Ensure proper line ending and handle insertion at line start | ||
| const proposedText = | ||
| processedLines.join('\n') + | ||
| (!isPythonFile | ||
| ? task.insertionPoint.character > 0 | ||
| ? ' '.repeat(targetIndentSize) | ||
| : '' | ||
| : '') | ||
|
|
||
| const replacementText = proposedText | ||
| const startLine = task.insertionPoint.line > 0 ? task.insertionPoint.line - 1 : 0 | ||
| const startLineText = document.lineAt(startLine).text | ||
|
|
||
| // Insert the updated text at the specified insertionPoint | ||
| if (edit instanceof vscode.WorkspaceEdit) { | ||
| edit.insert(document.uri, task.insertionPoint, replacementText) | ||
| return vscode.workspace.applyEdit(edit) | ||
| } | ||
|
|
||
| // If we have a doc intent with python file and no start line text, | ||
| // we want to insert the docstring after the insertion point. | ||
| // This is because we need the docstring to be on the next line | ||
| // after the function or class definition. | ||
| const insertionPoint = new vscode.Position( | ||
| isPythonFile && isDocIntent && !startLineText | ||
| ? task.insertionPoint.line + 1 | ||
| : task.insertionPoint.line, | ||
| task.insertionPoint.character | ||
| ) | ||
|
|
||
| return edit(editBuilder => { | ||
| editBuilder.insert(task.insertionPoint, replacementText) | ||
| // Replace the code block if the start line matches the start of the text | ||
| // This happens sometimes with python document code action where instead | ||
| // of adding only the docstring, the LLM returns the entire code block | ||
| if (startLine > 0 && startLineText && text.startsWith(startLineText)) { | ||
| editBuilder.replace(task.originalRange, text) | ||
| } else { | ||
| editBuilder.insert(insertionPoint, replacementText) | ||
| } | ||
| }, options) | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is nice validation that the fix is good!