Skip to content

Commit 71d7dc2

Browse files
author
Jiatong Li
committed
feat(amazonq): send relative file path for inline completion
1 parent 116ea07 commit 71d7dc2

File tree

3 files changed

+56
-10
lines changed

3 files changed

+56
-10
lines changed

server/aws-lsp-codewhisperer/src/language-server/inline-completion/codeWhispererServer.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ import {
4141
SINGLE_LINE_FILE_CUTOFF_INDEX,
4242
SOME_CLOSED_FILE,
4343
SOME_FILE,
44+
SOME_FILE_UNDER_WORKSPACE_FOLDER,
4445
SOME_FILE_WITH_ALT_CASED_LANGUAGE_ID,
4546
SOME_FILE_WITH_EXTENSION,
4647
SOME_SINGLE_LINE_FILE,
4748
SOME_UNSUPPORTED_FILE,
49+
SOME_WORKSPACE_FOLDER,
4850
SPECIAL_CHARACTER_HELLO_WORLD,
4951
stubCodeWhispererService,
5052
} from '../../shared/testUtils'
@@ -148,6 +150,7 @@ describe('CodeWhisperer Server', () => {
148150
.openDocument(SOME_UNSUPPORTED_FILE)
149151
.openDocument(SOME_FILE_WITH_EXTENSION)
150152
.openDocument(SOME_SINGLE_LINE_FILE)
153+
.openDocument(SOME_FILE_UNDER_WORKSPACE_FOLDER)
151154
})
152155

153156
afterEach(() => {
@@ -245,6 +248,35 @@ describe('CodeWhisperer Server', () => {
245248
)
246249
})
247250

251+
it('should correctly get filename', async () => {
252+
features.workspace.getWorkspaceFolder
253+
.withArgs(SOME_FILE_UNDER_WORKSPACE_FOLDER.uri)
254+
.returns(SOME_WORKSPACE_FOLDER)
255+
const result = await features.doInlineCompletionWithReferences(
256+
{
257+
textDocument: { uri: SOME_FILE_UNDER_WORKSPACE_FOLDER.uri },
258+
position: { line: 0, character: 0 },
259+
context: { triggerKind: InlineCompletionTriggerKind.Invoked },
260+
},
261+
CancellationToken.None
262+
)
263+
264+
// Check the completion result
265+
assert.deepEqual(result, EXPECTED_RESULT)
266+
267+
const expectedGenerateSuggestionsRequest = {
268+
fileContext: {
269+
fileUri: SOME_FILE_UNDER_WORKSPACE_FOLDER.uri,
270+
filename: 'relativePath/test.cs',
271+
programmingLanguage: { languageName: 'csharp' },
272+
leftFileContent: '',
273+
rightFileContent: HELLO_WORLD_IN_CSHARP,
274+
},
275+
maxResults: 5,
276+
}
277+
sinon.assert.calledOnceWithExactly(service.generateSuggestions, expectedGenerateSuggestionsRequest)
278+
})
279+
248280
it('should return recommendations when using a different languageId casing', async () => {
249281
const result = await features.doInlineCompletionWithReferences(
250282
{

server/aws-lsp-codewhisperer/src/language-server/inline-completion/codeWhispererServer.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
TextDocument,
1414
ResponseError,
1515
LSPErrorCodes,
16+
WorkspaceFolder,
1617
} from '@aws/language-server-runtimes/server-interface'
1718
import { AWSError } from 'aws-sdk'
1819
import { autoTrigger, triggerType } from './auto-trigger/autoTrigger'
@@ -56,13 +57,15 @@ import { UserWrittenCodeTracker } from '../../shared/userWrittenCodeTracker'
5657

5758
const EMPTY_RESULT = { sessionId: '', items: [] }
5859
export const FILE_URI_CHARS_LIMIT = 1024
60+
export const FILENAME_CHARS_LIMIT = 1024
5961
export const CONTEXT_CHARACTERS_LIMIT = 10240
6062

6163
// Both clients (token, sigv4) define their own types, this return value needs to match both of them.
6264
const getFileContext = (params: {
6365
textDocument: TextDocument
6466
position: Position
6567
inferredLanguageId: CodewhispererLanguage
68+
workspaceFolder: WorkspaceFolder | null | undefined
6669
}): {
6770
fileUri: string
6871
filename: string
@@ -81,18 +84,13 @@ const getFileContext = (params: {
8184
end: params.textDocument.positionAt(params.textDocument.getText().length),
8285
})
8386

84-
let relativeFileName = params.textDocument.uri
85-
relativeFileName = path.basename(params.textDocument.uri)
86-
// const workspaceFolder = WorkspaceFolderManager.getInstance()?.getWorkspaceFolder(params.textDocument.uri)
87-
// if (workspaceFolder) {
88-
// relativeFileName = getRelativePath(workspaceFolder, params.textDocument.uri)
89-
// } else {
90-
// relativeFileName = path.basename(params.textDocument.uri)
91-
// }
87+
const relativeFilePath = params.workspaceFolder
88+
? getRelativePath(params.workspaceFolder, params.textDocument.uri)
89+
: path.basename(params.textDocument.uri)
9290

9391
return {
9492
fileUri: params.textDocument.uri.substring(0, FILE_URI_CHARS_LIMIT),
95-
filename: relativeFileName,
93+
filename: relativeFilePath.substring(0, FILENAME_CHARS_LIMIT),
9694
programmingLanguage: {
9795
languageName: getRuntimeLanguage(params.inferredLanguageId),
9896
},
@@ -346,7 +344,12 @@ export const CodewhispererServerFactory =
346344
params.context.triggerKind == InlineCompletionTriggerKind.Automatic
347345
const maxResults = isAutomaticLspTriggerKind ? 1 : 5
348346
const selectionRange = params.context.selectedCompletionInfo?.range
349-
const fileContext = getFileContext({ textDocument, inferredLanguageId, position: params.position })
347+
const fileContext = getFileContext({
348+
textDocument,
349+
inferredLanguageId,
350+
position: params.position,
351+
workspaceFolder: workspace.getWorkspaceFolder(textDocument.uri),
352+
})
350353
const workspaceState = WorkspaceFolderManager.getInstance()?.getWorkspaceState()
351354
const workspaceId = workspaceState?.webSocketClient?.isConnected()
352355
? workspaceState.workspaceId

server/aws-lsp-codewhisperer/src/shared/testUtils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { SsoConnectionType } from './utils'
55
import { stubInterface } from 'ts-sinon'
66
import { StreamingClientServiceBase } from './streamingClientService'
77
import { SessionData } from '../language-server/inline-completion/session/sessionManager'
8+
import { WorkspaceFolder } from '@aws/language-server-runtimes/protocol'
89

910
export const HELLO_WORLD_IN_CSHARP = `class HelloWorld
1011
{
@@ -34,6 +35,16 @@ export const SOME_UNSUPPORTED_FILE = TextDocument.create(
3435
'INPUT HELLO ; OUTPUT WORLD'
3536
)
3637
export const SOME_FILE_WITH_EXTENSION = TextDocument.create('file:///missing.hpp', '', 1, HELLO_WORLD_IN_CSHARP)
38+
export const SOME_WORKSPACE_FOLDER: WorkspaceFolder = {
39+
uri: 'file:///tmp/workspaceFolderTest',
40+
name: 'workspaceFolder',
41+
}
42+
export const SOME_FILE_UNDER_WORKSPACE_FOLDER = TextDocument.create(
43+
`${SOME_WORKSPACE_FOLDER.uri}/relativePath/test.cs`,
44+
'csharp',
45+
1,
46+
HELLO_WORLD_IN_CSHARP
47+
)
3748

3849
export const SAMPLE_FILE_OF_60_LINES_IN_JAVA = `import java.util.List;
3950
// we need this comment on purpose because chunk will be trimed right, adding this to avoid trimRight and make assertion easier

0 commit comments

Comments
 (0)