Skip to content

Commit 9238343

Browse files
authored
fix(amazonq): Correct indent when insertAtCursor (#5447)
Problem: - When inserting code from Q chat, the indentation is wrong. Solution: - Pad recommendation with correct indentation Note: This solution cannot solve cases when code suggestion contains a new line raw string in the string variable but it should be an improvement over existing experience.
1 parent ee1d2be commit 9238343

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "Bug Fix",
3+
"description": "Correct indentation when insert Q chat code at cursor position"
4+
}

packages/core/src/amazonq/commons/controllers/contentController.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,42 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6+
import * as vscode from 'vscode'
67
import { Position, TextEditor, window } from 'vscode'
78
import { getLogger } from '../../../shared/logger'
89

910
export class EditorContentController {
11+
/* *
12+
* Insert the Amazon Q chat written code to the cursor position
13+
* Add current intentation to the next few lines of the recommendation
14+
* @param text the raw text from Amazon Q chat
15+
* @param trackCodeEdit callback to track user edits
16+
*/
1017
public insertTextAtCursorPosition(
1118
text: string,
1219
trackCodeEdit: (editor: TextEditor, cursorStart: Position) => void
1320
) {
1421
const editor = window.activeTextEditor
1522
if (editor) {
1623
const cursorStart = editor.selection.active
24+
const indentRange = new vscode.Range(new vscode.Position(cursorStart.line, 0), cursorStart)
25+
// use the user editor intent if the position to the left of cursor is just space or tab
26+
// otherwise indent with empty space equal to the intent at this position
27+
let indent = editor.document.getText(indentRange)
28+
if (indent.trim().length !== 0) {
29+
indent = ' '.repeat(indent.length - indent.trimStart().length)
30+
}
31+
let textWithIndent = ''
32+
text.split('\n').forEach((line, index) => {
33+
if (index === 0) {
34+
textWithIndent += line
35+
} else {
36+
textWithIndent += '\n' + indent + line
37+
}
38+
})
1739
editor
1840
.edit((editBuilder) => {
19-
editBuilder.insert(cursorStart, text)
41+
editBuilder.insert(cursorStart, textWithIndent)
2042
})
2143
.then(
2244
(appliedEdits) => {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
import * as vscode from 'vscode'
6+
import assert from 'assert'
7+
import { EditorContentController } from '../../../amazonq/commons/controllers/contentController'
8+
import { openATextEditorWithText } from '../../testUtil'
9+
10+
describe('contentController', () => {
11+
let controller: EditorContentController
12+
13+
beforeEach(async () => {
14+
controller = new EditorContentController()
15+
})
16+
17+
describe('insertTextAtCursorPosition', () => {
18+
it('insert code when left hand size has no non empty character', async () => {
19+
const editor = await openATextEditorWithText('def hello_world():\n ', 'test.py')
20+
if (editor) {
21+
const pos = new vscode.Position(1, 4)
22+
editor.selection = new vscode.Selection(pos, pos)
23+
controller.insertTextAtCursorPosition(
24+
'abc\n def',
25+
(editor: vscode.TextEditor, cursorStart: vscode.Position) => {
26+
assert.equal(editor.document.getText(), 'def hello_world():\n abc\n def')
27+
}
28+
)
29+
} else {
30+
assert.fail('Failed to open a text editor')
31+
}
32+
})
33+
34+
it('insert code when left hand size has non empty character', async () => {
35+
const editor = await openATextEditorWithText('def hello_world():\n ', 'test.py')
36+
if (editor) {
37+
const pos = new vscode.Position(0, 4)
38+
editor.selection = new vscode.Selection(pos, pos)
39+
controller.insertTextAtCursorPosition(
40+
'abc\n def',
41+
(editor: vscode.TextEditor, cursorStart: vscode.Position) => {
42+
assert.equal(editor.document.getText(), 'def abc\n defhello_world():\n')
43+
}
44+
)
45+
} else {
46+
assert.fail('Failed to open a text editor')
47+
}
48+
})
49+
})
50+
})

0 commit comments

Comments
 (0)