Skip to content

Commit

Permalink
Fix text before cursor cache filter logic
Browse files Browse the repository at this point in the history
  • Loading branch information
arshad-yaseen committed Jul 21, 2024
1 parent dabe93f commit 173a7f4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 25 deletions.
3 changes: 1 addition & 2 deletions src/copilot/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
getCompletionCache,
} from '../utils/completion';

const DEBOUNCE_DELAY = 200;
const DEBOUNCE_DELAY = 250;

const debouncedFetchCompletionItem = debounce(
fetchCompletionItem,
Expand Down Expand Up @@ -83,7 +83,6 @@ const handleInlineCompletions = async ({
text,
model,
position,
token,
});

if (completion) {
Expand Down
9 changes: 0 additions & 9 deletions src/helpers/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,7 @@ export const fetchCompletionItem = async ({
externalContext,
model,
position,
token,
}: FetchCompletionItemParams): Promise<string | null> => {
const controller = new AbortController();

if (token.isCancellationRequested) {
controller.abort();
return null;
}

try {
const {completion} = await HTTP.POST<CompletionResponse, CompletionRequest>(
endpoint,
Expand All @@ -50,7 +42,6 @@ export const fetchCompletionItem = async ({
{
headers: {'Content-Type': CONTENT_TYPE_JSON},
error: 'Error while fetching completion item',
signal: controller.signal,
},
);

Expand Down
8 changes: 1 addition & 7 deletions src/types/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ import type {
} from 'groq-sdk/resources/chat/completions';

import {Endpoint, ExternalContext, Filename, Technologies} from './copilot';
import {
EditorCancellationToken,
EditorModel,
EditorPosition,
EditorRange,
} from './editor';
import {EditorModel, EditorPosition, EditorRange} from './editor';

export type CompletionModel = 'llama';

Expand Down Expand Up @@ -53,7 +48,6 @@ export interface FetchCompletionItemParams {
externalContext?: ExternalContext;
model: EditorModel;
position: EditorPosition;
token: EditorCancellationToken;
}

export type CompletionCache = {
Expand Down
18 changes: 11 additions & 7 deletions src/utils/completion/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@ export const getCompletionCache = (
);

return (
(currentTextBeforeCursorInLine === cache.textBeforeCursorInLine &&
cache.range.startLineNumber === position.lineNumber &&
// Check if the current text before cursor in line starts with the cached text before cursor
currentTextBeforeCursorInLine.startsWith(cache.textBeforeCursorInLine) &&
// Ensure crsor is at the start of the cached range
((cache.range.startLineNumber === position.lineNumber &&
position.column === cache.range.startColumn) ||
(cache.completion.startsWith(currentValueInRange) &&
cache.range.startLineNumber === position.lineNumber &&
position.column >=
cache.range.startColumn - currentValueInRange.length &&
position.column <= cache.range.endColumn)
// Ensure cached completion starts with the current value in cached range
(cache.completion.startsWith(currentValueInRange) &&
// Ensure the cursor is within the cached range
cache.range.startLineNumber === position.lineNumber &&
position.column >=
cache.range.startColumn - currentValueInRange.length &&
position.column <= cache.range.endColumn))
);
});

Expand Down

0 comments on commit 173a7f4

Please sign in to comment.