Skip to content

Commit 3954302

Browse files
authored
fix(amazonq): send proper cursor position to lsp (#7113)
## Problem If you currently ask Q any questions about cursor position or highlighted text, it lacks the context necessary to answer. This is because in the payload sent to the LSP we send the cursor position as the wrong type: https://github.com/aws/aws-toolkit-vscode/blob/2e8d4c7eb0f0bcbc416d8f6e06ff7e00d57dc6b2/packages/amazonq/src/lsp/chat/messages.ts#L159 whereas [Flare](https://github.com/aws/language-server-runtimes/blob/d9960b5dd6df52c52309f0a7edae64a824da9003/types/chat.ts#L82) is expecting: `cursorState`. ## Solution - Convert the vscode selection to a Flare cursorState. - Add stronger typing by casting the params to `ChatParams` type before modifying to avoid these types of bugs. ## Future Work - This fixes flare being unable to identify cursor position, and mostly fixes highlighted text. However, I was still able to run into situations where the cursor position is correct, but the content is incorrect. ~~This could be fixed by sending the highlighted content to Flare, but~~ still trying to root cause the issue. <img width="1064" alt="image" src="https://github.com/user-attachments/assets/35e6fbc9-5c66-4f9f-8c6e-bf9ec4ab4ef2" /> --- - Treat all work as PUBLIC. Private `feature/x` branches will not be squash-merged at release time. - Your code changes must meet the guidelines in [CONTRIBUTING.md](https://github.com/aws/aws-toolkit-vscode/blob/master/CONTRIBUTING.md#guidelines). - License: I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 2e8d4c7 commit 3954302

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

packages/amazonq/src/lsp/chat/messages.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@ export function registerLanguageServerEventListener(languageClient: LanguageClie
7272
})
7373
}
7474

75+
function getCursorState(selection: readonly vscode.Selection[]) {
76+
return selection.map((s) => ({
77+
range: {
78+
start: {
79+
line: s.start.line,
80+
character: s.start.character,
81+
},
82+
end: {
83+
line: s.end.line,
84+
character: s.end.character,
85+
},
86+
},
87+
}))
88+
}
89+
7590
export function registerMessageListeners(
7691
languageClient: LanguageClient,
7792
provider: AmazonQChatViewProvider,
@@ -147,20 +162,21 @@ export function registerMessageListeners(
147162
break
148163
}
149164
case chatRequestType.method: {
165+
const chatParams = { ...message.params } as ChatParams
150166
const partialResultToken = uuidv4()
151167
const chatDisposable = languageClient.onProgress(chatRequestType, partialResultToken, (partialResult) =>
152-
handlePartialResult<ChatResult>(partialResult, encryptionKey, provider, message.params.tabId)
168+
handlePartialResult<ChatResult>(partialResult, encryptionKey, provider, chatParams.tabId)
153169
)
154170

155171
const editor =
156172
vscode.window.activeTextEditor ||
157173
vscode.window.visibleTextEditors.find((editor) => editor.document.languageId !== 'Log')
158174
if (editor) {
159-
message.params.cursorPosition = [editor.selection.active]
160-
message.params.textDocument = { uri: editor.document.uri.toString() }
175+
chatParams.cursorState = getCursorState(editor.selections)
176+
chatParams.textDocument = { uri: editor.document.uri.toString() }
161177
}
162178

163-
const chatRequest = await encryptRequest<ChatParams>(message.params, encryptionKey)
179+
const chatRequest = await encryptRequest<ChatParams>(chatParams, encryptionKey)
164180
const chatResult = (await languageClient.sendRequest(chatRequestType.method, {
165181
...chatRequest,
166182
partialResultToken,
@@ -169,7 +185,7 @@ export function registerMessageListeners(
169185
chatResult,
170186
encryptionKey,
171187
provider,
172-
message.params.tabId,
188+
chatParams.tabId,
173189
chatDisposable
174190
)
175191
break

0 commit comments

Comments
 (0)