Skip to content

Commit

Permalink
feat(autoedit): inline-completion-provider integration
Browse files Browse the repository at this point in the history
  • Loading branch information
valerybugakov committed Nov 19, 2024
1 parent 0fe29c4 commit a934743
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 32 deletions.
68 changes: 37 additions & 31 deletions vscode/src/autoedits/autoedits-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,6 @@ export class AutoeditsProvider implements vscode.InlineCompletionItemProvider, v
this.disposables.push(
this.contextMixer,
this.rendererManager,
// Command is used to manually debug the autoedits provider
vscode.commands.registerCommand('cody.experimental.suggest', () => {
const editor = vscode.window.activeTextEditor
if (!editor) {
return
}
this.provideInlineCompletionItems(editor.document, editor.selection.active, {
triggerKind: vscode.InlineCompletionTriggerKind.Invoke,
selectedCompletionInfo: undefined,
})
}),
vscode.window.onDidChangeTextEditorSelection(this.onSelectionChangeDebounced),
vscode.workspace.onDidChangeTextDocument(event => {
this.onDidChangeTextDocument(event)
Expand Down Expand Up @@ -135,20 +124,10 @@ export class AutoeditsProvider implements vscode.InlineCompletionItemProvider, v
if (!lastSelection?.isEmpty || document.uri.scheme !== 'file') {
return
}
if (this.rendererManager.hasActiveEdit()) {
return
}
// Don't show suggestion on cursor movement if the text has not changed for a certain amount of time
if (
this.lastTextChangeTimeStamp &&
Date.now() - this.lastTextChangeTimeStamp <
RESET_SUGGESTION_ON_CURSOR_CHANGE_AFTER_INTERVAL_MS
) {
this.provideInlineCompletionItems(document, lastSelection.active, {
triggerKind: vscode.InlineCompletionTriggerKind.Automatic,
selectedCompletionInfo: undefined,
})
}
await vscode.commands.executeCommand('cody.supersuggest.dismiss')
await vscode.commands.executeCommand('editor.action.inlineSuggest.hide')
await vscode.commands.executeCommand('editor.action.inlineSuggest.trigger')
}

public async provideInlineCompletionItems(
Expand Down Expand Up @@ -183,7 +162,12 @@ export class AutoeditsProvider implements vscode.InlineCompletionItemProvider, v
return null
}
const { prediction, codeToReplaceData } = autoeditResponse
const inlineCompletionItems = this.tryMakeInlineCompletionResponse(prediction, codeToReplaceData)
const inlineCompletionItems = this.tryMakeInlineCompletionResponse(
prediction,
codeToReplaceData,
document,
position
)
if (inlineCompletionItems) {
return inlineCompletionItems
}
Expand All @@ -193,24 +177,46 @@ export class AutoeditsProvider implements vscode.InlineCompletionItemProvider, v

private tryMakeInlineCompletionResponse(
originalPrediction: string,
codeToReplace: CodeToReplaceData
codeToReplace: CodeToReplaceData,
document: vscode.TextDocument,
position: vscode.Position
): vscode.InlineCompletionItem[] | null {
const prediction = adjustPredictionIfInlineCompletionPossible(
originalPrediction,
codeToReplace.codeToRewritePrefix,
codeToReplace.codeToRewriteSuffix
)
const codeToRewriteAfterCurrentLine = codeToReplace.codeToRewriteFromCurrentLine
.split('\n')
.slice(1)
.join('\n')

const isPrefixMatch = prediction.startsWith(codeToReplace.codeToRewritePrefix)
const isSuffixMatch = prediction.endsWith(codeToReplace.codeToRewriteSuffix)
const isSuffixMatch = prediction.endsWith(codeToRewriteAfterCurrentLine)
console.log({ isPrefixMatch, isSuffixMatch })
if (isPrefixMatch && isSuffixMatch) {
const autocompleteResponse = extractInlineCompletionFromRewrittenCode(
let autocompleteResponse = extractInlineCompletionFromRewrittenCode(
prediction,
codeToReplace.codeToRewritePrefix,
codeToReplace.codeToRewriteSuffix
)
autoeditsLogger.logDebug('Autocomplete Inline Respone: ', autocompleteResponse)
const inlineCompletionItems = new vscode.InlineCompletionItem(autocompleteResponse)
return [inlineCompletionItems]
const currentLine = codeToReplace.codeToRewritePrefix.split('\n').pop()!
const currentLinePrefix = currentLine.slice(0, position.character)
autocompleteResponse = currentLinePrefix + autocompleteResponse

const inlineCompletionItem = new vscode.InlineCompletionItem(
autocompleteResponse,
new vscode.Range(
document.lineAt(position).range.start,
document.lineAt(position).range.end
)
)
autoeditsLogger.logDebug('Autocomplete Inline Response: ', autocompleteResponse)
autoeditsLogger.logDebug(
'Autocomplete Inline Range: ',
JSON.stringify(inlineCompletionItem.range)
)
return [inlineCompletionItem]
}
return null
}
Expand Down
13 changes: 12 additions & 1 deletion vscode/src/autoedits/prompt-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export interface CodeToReplaceData {
suffixInArea: string
codeToRewritePrefix: string
codeToRewriteSuffix: string
codeToRewriteFromCurrentLine: string
range: vscode.Range
}

Expand All @@ -70,6 +71,7 @@ interface CurrentFileContext {
codeToRewrite: PromptString
codeToRewritePrefix: PromptString
codeToRewriteSuffix: PromptString
codeToRewriteFromCurrentLine: PromptString
prefixInArea: PromptString
suffixInArea: PromptString
prefixBeforeArea: PromptString
Expand Down Expand Up @@ -167,13 +169,14 @@ export function getCurrentFilePromptComponents(
const codeToReplace = {
codeToRewrite: currentFileContext.codeToRewrite.toString(),
range: currentFileContext.range,
codeToRewriteFromCurrentLine: currentFileContext.codeToRewriteFromCurrentLine.toString(),
codeToRewritePrefix: currentFileContext.codeToRewritePrefix.toString(),
codeToRewriteSuffix: currentFileContext.codeToRewriteSuffix.toString(),
prefixBeforeArea: currentFileContext.prefixBeforeArea.toString(),
suffixAfterArea: currentFileContext.suffixAfterArea.toString(),
prefixInArea: currentFileContext.prefixInArea.toString(),
suffixInArea: currentFileContext.suffixInArea.toString(),
}
} satisfies CodeToReplaceData

const fileWithMarker = ps`${currentFileContext.prefixBeforeArea}
${AREA_FOR_CODE_MARKER}
Expand Down Expand Up @@ -240,6 +243,10 @@ export function getCurrentFileContext(options: CurrentFilePromptOptions): Curren
new vscode.Position(position.line, position.character),
positionAtLineEnd(codeToRewriteEnd)
),
codeToRewriteFromCurrentLine: new vscode.Range(
document.lineAt(position.line).range.start,
positionAtLineEnd(codeToRewriteEnd)
),
prefixInArea: new vscode.Range(
positionAtLineStart(areaStart),
positionAtLineStart(codeToRewriteStart)
Expand All @@ -253,6 +260,10 @@ export function getCurrentFileContext(options: CurrentFilePromptOptions): Curren
codeToRewrite: PromptString.fromDocumentText(document, ranges.codeToRewrite),
codeToRewritePrefix: PromptString.fromDocumentText(document, ranges.codeToRewritePrefix),
codeToRewriteSuffix: PromptString.fromDocumentText(document, ranges.codeToRewriteSuffix),
codeToRewriteFromCurrentLine: PromptString.fromDocumentText(
document,
ranges.codeToRewriteFromCurrentLine
),
prefixInArea: PromptString.fromDocumentText(document, ranges.prefixInArea),
suffixInArea: PromptString.fromDocumentText(document, ranges.suffixInArea),
prefixBeforeArea: PromptString.fromDocumentText(document, ranges.prefixBeforeArea),
Expand Down
4 changes: 4 additions & 0 deletions vscode/src/services/open-telemetry/debug-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { type Attributes, type Span, trace } from '@opentelemetry/api'

function bubbleSort(arr) {

Check failure on line 3 in vscode/src/services/open-telemetry/debug-utils.ts

View workflow job for this annotation

GitHub Actions / test-unit (ubuntu, 20)

'bubbleSort' is declared but its value is never read.

Check failure on line 3 in vscode/src/services/open-telemetry/debug-utils.ts

View workflow job for this annotation

GitHub Actions / test-unit (ubuntu, 20)

Parameter 'arr' implicitly has an 'any' type.

Check failure on line 3 in vscode/src/services/open-telemetry/debug-utils.ts

View workflow job for this annotation

GitHub Actions / test-unit (ubuntu, 18)

'bubbleSort' is declared but its value is never read.

Check failure on line 3 in vscode/src/services/open-telemetry/debug-utils.ts

View workflow job for this annotation

GitHub Actions / test-unit (ubuntu, 18)

Parameter 'arr' implicitly has an 'any' type.

Check failure on line 3 in vscode/src/services/open-telemetry/debug-utils.ts

View workflow job for this annotation

GitHub Actions / test-unit (windows, 20)

'bubbleSort' is declared but its value is never read.

Check failure on line 3 in vscode/src/services/open-telemetry/debug-utils.ts

View workflow job for this annotation

GitHub Actions / test-unit (windows, 20)

Parameter 'arr' implicitly has an 'any' type.

}

/**
* Adds OpenTelemetry event to the current active span in the development environment.
* Does nothing in production environments.
Expand Down

0 comments on commit a934743

Please sign in to comment.