Skip to content

Commit 9592c26

Browse files
authored
feat(codewhisperer): do not format code on acceptance #3253
Problem Customer feedback on not wanting CodeWhisperer to format their documents. Solution Don't automatically format user's code on acceptance.
1 parent 683b793 commit 9592c26

File tree

3 files changed

+5
-83
lines changed

3 files changed

+5
-83
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "Feature",
3+
"description": "CodeWhisperer will no longer automatically format code after accepting code suggestions"
4+
}

src/codewhisperer/commands/onInlineAcceptance.ts

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import { vsCodeState, OnRecommendationAcceptanceEntry } from '../models/model'
99
import { runtimeLanguageContext } from '../util/runtimeLanguageContext'
1010
import { CodeWhispererTracker } from '../tracker/codewhispererTracker'
1111
import { CodeWhispererCodeCoverageTracker } from '../tracker/codewhispererCodeCoverageTracker'
12-
import { TextEdit, WorkspaceEdit, workspace } from 'vscode'
13-
import { getTabSizeSetting } from '../../shared/utilities/editorUtilities'
1412
import { getLogger } from '../../shared/logger/logger'
1513
import { RecommendationHandler } from '../service/recommendationHandler'
1614
import { InlineCompletionService } from '../service/inlineCompletionService'
@@ -83,13 +81,10 @@ export async function onInlineAcceptance(
8381
const languageContext = runtimeLanguageContext.getLanguageContext(acceptanceEntry.editor.document.languageId)
8482
const start = acceptanceEntry.range.start
8583
const end = acceptanceEntry.editor.selection.active
86-
const languageId = acceptanceEntry.editor.document.languageId
8784
RecommendationHandler.instance.reportUserDecisionOfRecommendation(
8885
acceptanceEntry.editor,
8986
acceptanceEntry.acceptIndex
9087
)
91-
// codewhisperer will be doing editing while formatting.
92-
// formatting should not trigger consoals auto trigger
9388
vsCodeState.isCodeWhispererEditing = true
9489
/**
9590
* Mitigation to right context handling mainly for auto closing bracket use case
@@ -105,29 +100,7 @@ export async function onInlineAcceptance(
105100
} catch (error) {
106101
getLogger().error(`${error} in handling right contexts`)
107102
}
108-
try {
109-
if (languageId === CodeWhispererConstants.python) {
110-
await vscode.commands.executeCommand('editor.action.format')
111-
} else {
112-
const range = new vscode.Range(start, end)
113-
const edits: TextEdit[] | undefined = await vscode.commands.executeCommand(
114-
'vscode.executeFormatRangeProvider',
115-
acceptanceEntry.editor.document.uri,
116-
range,
117-
{
118-
tabSize: getTabSizeSetting(),
119-
insertSpaces: true,
120-
}
121-
)
122-
if (edits && acceptanceEntry.editor) {
123-
const wEdit = new WorkspaceEdit()
124-
wEdit.set(acceptanceEntry.editor.document.uri, edits)
125-
await workspace.applyEdit(wEdit)
126-
}
127-
}
128-
} finally {
129-
vsCodeState.isCodeWhispererEditing = false
130-
}
103+
vsCodeState.isCodeWhispererEditing = false
131104

132105
CodeWhispererTracker.getTracker().enqueue({
133106
time: new Date(),

src/test/codewhisperer/commands/onInlineAcceptance.test.ts

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -46,61 +46,6 @@ describe('onInlineAcceptance', function () {
4646
assert.ok(spy.calledWith())
4747
})
4848

49-
it('Should format python code with command editor.action.format when current active document is python', async function () {
50-
const mockEditor = createMockTextEditor()
51-
const commandSpy = sinon.spy(vscode.commands, 'executeCommand')
52-
const globalState = new FakeMemento()
53-
await onInlineAcceptance(
54-
{
55-
editor: mockEditor,
56-
range: new vscode.Range(new vscode.Position(1, 0), new vscode.Position(1, 21)),
57-
acceptIndex: 0,
58-
recommendation: "print('Hello World!')",
59-
requestId: '',
60-
sessionId: '',
61-
triggerType: 'OnDemand',
62-
completionType: 'Line',
63-
language: 'python',
64-
references: undefined,
65-
},
66-
globalState
67-
)
68-
assert.ok(commandSpy.calledWith('editor.action.format'))
69-
})
70-
71-
it('Should format code selection with command vscode.executeFormatRangeProvider when current active document is not python', async function () {
72-
const mockEditor = createMockTextEditor("console.log('Hello')", 'test.js', 'javascript', 1, 0)
73-
const commandStub = sinon.stub(vscode.commands, 'executeCommand')
74-
const globalState = new FakeMemento()
75-
const fakeReferences = [
76-
{
77-
message: '',
78-
licenseName: 'MIT',
79-
repository: 'http://github.com/fake',
80-
recommendationContentSpan: {
81-
start: 0,
82-
end: 10,
83-
},
84-
},
85-
]
86-
await onInlineAcceptance(
87-
{
88-
editor: mockEditor,
89-
range: new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 27)),
90-
acceptIndex: 0,
91-
recommendation: "console.log('Hello World!')",
92-
requestId: '',
93-
sessionId: '',
94-
triggerType: 'OnDemand',
95-
completionType: 'Line',
96-
language: 'javascript',
97-
references: fakeReferences,
98-
},
99-
globalState
100-
)
101-
assert.ok(commandStub.calledWith('vscode.executeFormatRangeProvider'))
102-
})
103-
10449
it('Should report telemetry that records this user decision event', async function () {
10550
const mockEditor = createMockTextEditor()
10651
RecommendationHandler.instance.requestId = 'test'

0 commit comments

Comments
 (0)