public registerInlineCompletion()

in packages/amazonq/src/app/inline/completion.ts [64:165]


    public registerInlineCompletion() {
        const onInlineAcceptance = async (
            sessionId: string,
            item: InlineCompletionItemWithReferences,
            editor: TextEditor,
            requestStartTime: number,
            startLine: number,
            firstCompletionDisplayLatency?: number
        ) => {
            // TODO: also log the seen state for other suggestions in session
            const params: LogInlineCompletionSessionResultsParams = {
                sessionId: sessionId,
                completionSessionResult: {
                    [item.itemId]: {
                        seen: true,
                        accepted: true,
                        discarded: false,
                    },
                },
                totalSessionDisplayTime: Date.now() - requestStartTime,
                firstCompletionDisplayLatency: firstCompletionDisplayLatency,
            }
            this.languageClient.sendNotification(this.logSessionResultMessageName, params)
            this.disposable.dispose()
            this.disposable = languages.registerInlineCompletionItemProvider(
                CodeWhispererConstants.platformLanguageIds,
                this.inlineCompletionProvider
            )
            if (item.references && item.references.length) {
                const referenceLog = ReferenceLogViewProvider.getReferenceLog(
                    item.insertText as string,
                    item.references,
                    editor
                )
                ReferenceLogViewProvider.instance.addReferenceLog(referenceLog)
                ReferenceHoverProvider.instance.addCodeReferences(item.insertText as string, item.references)
            }
            if (item.mostRelevantMissingImports?.length) {
                await ImportAdderProvider.instance.onAcceptRecommendation(editor, item, startLine)
            }
        }
        commands.registerCommand('aws.amazonq.acceptInline', onInlineAcceptance)

        const onInlineRejection = async () => {
            await commands.executeCommand('editor.action.inlineSuggest.hide')
            // TODO: also log the seen state for other suggestions in session
            this.disposable.dispose()
            this.disposable = languages.registerInlineCompletionItemProvider(
                CodeWhispererConstants.platformLanguageIds,
                this.inlineCompletionProvider
            )
            const sessionId = this.sessionManager.getActiveSession()?.sessionId
            const itemId = this.sessionManager.getActiveRecommendation()[0]?.itemId
            if (!sessionId || !itemId) {
                return
            }
            const params: LogInlineCompletionSessionResultsParams = {
                sessionId: sessionId,
                completionSessionResult: {
                    [itemId]: {
                        seen: true,
                        accepted: false,
                        discarded: false,
                    },
                },
            }
            this.languageClient.sendNotification(this.logSessionResultMessageName, params)
        }
        commands.registerCommand('aws.amazonq.rejectCodeSuggestion', onInlineRejection)

        /*
            We have to overwrite the prev. and next. commands because the inlineCompletionProvider only contained the current item
            To show prev. and next. recommendation we need to re-register a new provider with the previous or next item
        */

        const swapProviderAndShow = async () => {
            await commands.executeCommand('editor.action.inlineSuggest.hide')
            this.disposable.dispose()
            this.disposable = languages.registerInlineCompletionItemProvider(
                CodeWhispererConstants.platformLanguageIds,
                new AmazonQInlineCompletionItemProvider(
                    this.languageClient,
                    this.recommendationService,
                    this.sessionManager,
                    false
                )
            )
            await commands.executeCommand('editor.action.inlineSuggest.trigger')
        }

        const prevCommandHandler = async () => {
            this.sessionManager.decrementActiveIndex()
            await swapProviderAndShow()
        }
        commands.registerCommand('editor.action.inlineSuggest.showPrevious', prevCommandHandler)

        const nextCommandHandler = async () => {
            this.sessionManager.incrementActiveIndex()
            await swapProviderAndShow()
        }
        commands.registerCommand('editor.action.inlineSuggest.showNext', nextCommandHandler)
    }