diff --git a/.changes/unreleased/Changed-20241213-091505.yaml b/.changes/unreleased/Changed-20241213-091505.yaml new file mode 100644 index 000000000..0ce6136ae --- /dev/null +++ b/.changes/unreleased/Changed-20241213-091505.yaml @@ -0,0 +1,4 @@ +project: extensions/vscode +kind: Changed +body: Improved autocomplete in untitled files +time: 2024-12-13T09:15:05.552877-08:00 diff --git a/.changes/unreleased/Fixed-20241213-091454.yaml b/.changes/unreleased/Fixed-20241213-091454.yaml new file mode 100644 index 000000000..fb8d92584 --- /dev/null +++ b/.changes/unreleased/Fixed-20241213-091454.yaml @@ -0,0 +1,4 @@ +project: extensions/vscode +kind: Fixed +body: Display more mid-line completions +time: 2024-12-13T09:14:54.808683-08:00 diff --git a/.changes/unreleased/Fixed-20241213-091519.yaml b/.changes/unreleased/Fixed-20241213-091519.yaml new file mode 100644 index 000000000..84a193224 --- /dev/null +++ b/.changes/unreleased/Fixed-20241213-091519.yaml @@ -0,0 +1,4 @@ +project: extensions/vscode +kind: Fixed +body: Restored syntax highlighting +time: 2024-12-13T09:15:19.485189-08:00 diff --git a/core/autocomplete/filtering/streamTransforms/StreamTransformPipeline.ts b/core/autocomplete/filtering/streamTransforms/StreamTransformPipeline.ts index 180c339e6..1850729f1 100644 --- a/core/autocomplete/filtering/streamTransforms/StreamTransformPipeline.ts +++ b/core/autocomplete/filtering/streamTransforms/StreamTransformPipeline.ts @@ -16,6 +16,8 @@ import { streamWithNewLines, } from "./lineStream"; +const STOP_AT_PATTERNS = ["diff --git"]; + export class StreamTransformPipeline { async *transform( generator: AsyncGenerator, @@ -28,7 +30,10 @@ export class StreamTransformPipeline { ): AsyncGenerator { let charGenerator = generator; - charGenerator = stopAtStopTokens(generator, stopTokens); + charGenerator = stopAtStopTokens(generator, [ + ...stopTokens, + ...STOP_AT_PATTERNS, + ]); charGenerator = stopAtStartOf(charGenerator, suffix); for (const charFilter of helper.lang.charFilters ?? []) { charGenerator = charFilter({ diff --git a/core/autocomplete/filtering/test/util.ts b/core/autocomplete/filtering/test/util.ts index 7aa7f7c66..41a35db07 100644 --- a/core/autocomplete/filtering/test/util.ts +++ b/core/autocomplete/filtering/test/util.ts @@ -52,6 +52,7 @@ export async function testAutocompleteFiltering( const line = prefix.split("\n").length - 1; const character = prefix.split("\n")[line].length; const autocompleteInput: AutocompleteInput = { + isUntitledFile: false, completionId: "test-completion-id", filepath: fileUri, pos: { diff --git a/core/autocomplete/postprocessing/index.ts b/core/autocomplete/postprocessing/index.ts index ed7c41f05..c2cf5049b 100644 --- a/core/autocomplete/postprocessing/index.ts +++ b/core/autocomplete/postprocessing/index.ts @@ -90,6 +90,17 @@ export function postprocessCompletion({ completion = completion.slice(1); } } + + // When there is no suffix, Codestral tends to begin with a new line + // We do this to avoid double new lines + if ( + suffix.length === 0 && + prefix.endsWith("\n\n") && + completion.startsWith("\n") + ) { + // Remove a single leading \n from the completion + completion = completion.slice(1); + } } if (llm.model.includes("granite")) { diff --git a/core/autocomplete/prefiltering/index.ts b/core/autocomplete/prefiltering/index.ts index 7512b0ab1..ed1266ecc 100644 --- a/core/autocomplete/prefiltering/index.ts +++ b/core/autocomplete/prefiltering/index.ts @@ -58,6 +58,14 @@ export async function shouldPrefilter( return true; } + // Don't offer completions when we have no information (untitled file and no file contents) + if ( + helper.filepath.includes("Untitled") && + helper.fileContents.trim() === "" + ) { + return true; + } + // if ( // helper.options.transform && // (await shouldLanguageSpecificPrefilter(helper)) diff --git a/core/autocomplete/templating/index.ts b/core/autocomplete/templating/index.ts index d130a3c18..9722ee328 100644 --- a/core/autocomplete/templating/index.ts +++ b/core/autocomplete/templating/index.ts @@ -4,15 +4,15 @@ import { CompletionOptions } from "../.."; import { AutocompleteLanguageInfo } from "../constants/AutocompleteLanguageInfo"; import { HelperVars } from "../util/HelperVars"; +import { SnippetPayload } from "../snippets"; import { AutocompleteTemplate, getTemplateForModel, } from "./AutocompleteTemplate"; -import { getStopTokens } from "./getStopTokens"; -import { SnippetPayload } from "../snippets"; -import { formatSnippets } from "./formatting"; import { getSnippets } from "./filtering"; import { getUriPathBasename } from "../../util/uri"; +import { formatSnippets } from "./formatting"; +import { getStopTokens } from "./getStopTokens"; function getTemplate(helper: HelperVars): AutocompleteTemplate { if (helper.options.template) { @@ -62,6 +62,9 @@ export function renderPrompt({ // If prefix is manually passed let prefix = helper.input.manuallyPassPrefix || helper.prunedPrefix; let suffix = helper.input.manuallyPassPrefix ? "" : helper.prunedSuffix; + if (suffix === "") { + suffix = "\n"; + } const reponame = getUriPathBasename(workspaceDirs[0] ?? "myproject"); diff --git a/core/autocomplete/util/types.ts b/core/autocomplete/util/types.ts index d54d57bd2..c11d8c2d9 100644 --- a/core/autocomplete/util/types.ts +++ b/core/autocomplete/util/types.ts @@ -13,6 +13,7 @@ export type RecentlyEditedRange = RangeInFile & { }; export interface AutocompleteInput { + isUntitledFile: boolean; completionId: string; filepath: string; pos: Position; diff --git a/core/llm/index.ts b/core/llm/index.ts index b54c29ec3..dc55f8a78 100644 --- a/core/llm/index.ts +++ b/core/llm/index.ts @@ -476,7 +476,7 @@ export abstract class BaseLLM implements ILLM { ): AsyncGenerator { const { completionOptions, log } = this._parseCompletionOptions(options); - const madeUpFimPrompt = `${prefix}${suffix}`; + const madeUpFimPrompt = `<|fim_prefix|>${prefix}<|fim_suffix|>${suffix}<|fim_middle|>`; if (log) { if (this.writeLog) { await this.writeLog( diff --git a/core/protocol/passThrough.ts b/core/protocol/passThrough.ts index ddc911b1b..cfd260ab8 100644 --- a/core/protocol/passThrough.ts +++ b/core/protocol/passThrough.ts @@ -72,6 +72,7 @@ export const CORE_TO_WEBVIEW_PASS_THROUGH: (keyof ToWebviewFromCoreProtocol)[] = "didChangeAvailableProfiles", "setTTSActive", "getWebviewHistoryLength", + "getCurrentSessionId", "signInToControlPlane", "openDialogMessage", "docs/suggestions", diff --git a/core/protocol/webview.ts b/core/protocol/webview.ts index fb6a8837c..27fd2fc38 100644 --- a/core/protocol/webview.ts +++ b/core/protocol/webview.ts @@ -24,6 +24,7 @@ export type ToWebviewFromIdeOrCoreProtocol = { ]; setTTSActive: [boolean, void]; getWebviewHistoryLength: [undefined, number]; + getCurrentSessionId: [undefined, string]; signInToControlPlane: [undefined, void]; openDialogMessage: ["account", void]; "docs/suggestions": [PackageDocsResult[], void]; diff --git a/core/util/findLast.ts b/core/util/findLast.ts new file mode 100644 index 000000000..fb39829e7 --- /dev/null +++ b/core/util/findLast.ts @@ -0,0 +1,14 @@ +export function findLastIndex( + arr: any[], + criterion: (item: any) => boolean, +): number { + let lastIndex = -1; + + for (let i = arr.length - 1; i >= 0; i--) { + if (criterion(arr[i])) { + lastIndex = i; + break; + } + } + return lastIndex; +} diff --git a/extensions/vscode/CHANGELOG.md b/extensions/vscode/CHANGELOG.md index 0a9868f14..cb5b8b4be 100644 --- a/extensions/vscode/CHANGELOG.md +++ b/extensions/vscode/CHANGELOG.md @@ -5,6 +5,13 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html), and is generated by [Changie](https://github.com/miniscruff/changie). +Pre-release Changes +### Changed +* Improved autocomplete in untitled files +### Fixed +* Display more mid-line completions +* Restored syntax highlighting + ## 0.8.62 - 2024-12-10 ### Added * Tool use diff --git a/extensions/vscode/package-lock.json b/extensions/vscode/package-lock.json index 04349b1f1..3e213c999 100644 --- a/extensions/vscode/package-lock.json +++ b/extensions/vscode/package-lock.json @@ -1,12 +1,12 @@ { "name": "continue", - "version": "0.9.243", + "version": "0.9.244", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "continue", - "version": "0.9.243", + "version": "0.9.244", "license": "Apache-2.0", "dependencies": { "@continuedev/fetch": "^1.0.3", @@ -20,6 +20,7 @@ "core": "file:../../core", "cors": "^2.8.5", "dbinfoz": "^0.14.0", + "diff": "^7.0.0", "downshift": "^7.6.0", "esbuild": "^0.17.19", "express": "^4.18.2", @@ -4981,9 +4982,9 @@ "integrity": "sha512-gxtyfqMg7GKyhQmb056K7M3xszy/myH8w+B4RT+QXBQsvAOdc3XymqDDPHx1BgPgsdAA5SIifona89YtRATDzw==" }, "node_modules/diff": { - "version": "5.2.0", - "resolved": "https://registry.npmjs.org/diff/-/diff-5.2.0.tgz", - "integrity": "sha512-uIFDxqpRZGZ6ThOk84hEfqWoHx2devRFvpTZcTHur85vImfaxUbTW9Ryh4CpCuDnToOP1CEtXKIgytHBPVff5A==", + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-7.0.0.tgz", + "integrity": "sha512-PJWHUb1RFevKCwaFA9RlG5tCd+FO5iRh9A8HEtkmBH2Li03iJriB6m6JIN4rGz3K3JLawI7/veA1xzRKP6ISBw==", "engines": { "node": ">=0.3.1" } @@ -8858,6 +8859,15 @@ "wrap-ansi": "^7.0.0" } }, + "node_modules/mocha/node_modules/diff": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-5.2.0.tgz", + "integrity": "sha512-uIFDxqpRZGZ6ThOk84hEfqWoHx2devRFvpTZcTHur85vImfaxUbTW9Ryh4CpCuDnToOP1CEtXKIgytHBPVff5A==", + "dev": true, + "engines": { + "node": ">=0.3.1" + } + }, "node_modules/mocha/node_modules/minimatch": { "version": "5.1.6", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.1.6.tgz", @@ -13165,6 +13175,14 @@ "node": ">=8" } }, + "node_modules/uvu/node_modules/diff": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-5.2.0.tgz", + "integrity": "sha512-uIFDxqpRZGZ6ThOk84hEfqWoHx2devRFvpTZcTHur85vImfaxUbTW9Ryh4CpCuDnToOP1CEtXKIgytHBPVff5A==", + "engines": { + "node": ">=0.3.1" + } + }, "node_modules/v8-to-istanbul": { "version": "9.3.0", "resolved": "https://registry.npmjs.org/v8-to-istanbul/-/v8-to-istanbul-9.3.0.tgz", diff --git a/extensions/vscode/package.json b/extensions/vscode/package.json index 24b8ebf1f..627c0c974 100644 --- a/extensions/vscode/package.json +++ b/extensions/vscode/package.json @@ -2,7 +2,7 @@ "name": "continue", "icon": "media/icon.png", "author": "Continue Dev, Inc", - "version": "0.9.244", + "version": "0.9.245", "repository": { "type": "git", "url": "https://github.com/continuedev/continue" @@ -678,6 +678,7 @@ "core": "file:../../core", "cors": "^2.8.5", "dbinfoz": "^0.14.0", + "diff": "^7.0.0", "downshift": "^7.6.0", "esbuild": "^0.17.19", "express": "^4.18.2", diff --git a/extensions/vscode/src/VsCodeIde.ts b/extensions/vscode/src/VsCodeIde.ts index b8968f7b0..22814e3a3 100644 --- a/extensions/vscode/src/VsCodeIde.ts +++ b/extensions/vscode/src/VsCodeIde.ts @@ -449,7 +449,6 @@ class VsCodeIde implements IDE { const contents = new TextDecoder().decode(truncatedBytes); return contents; } catch (e) { - console.warn("Error reading file", e); return ""; } } diff --git a/extensions/vscode/src/autocomplete/completionProvider.ts b/extensions/vscode/src/autocomplete/completionProvider.ts index c5366572e..a3a8ff8c0 100644 --- a/extensions/vscode/src/autocomplete/completionProvider.ts +++ b/extensions/vscode/src/autocomplete/completionProvider.ts @@ -6,6 +6,7 @@ import { import { ConfigHandler } from "core/config/ConfigHandler"; import { v4 as uuidv4 } from "uuid"; import * as vscode from "vscode"; +const Diff = require("diff"); import { showFreeTrialLoginMessage } from "../util/messages"; import { VsCodeWebviewProtocol } from "../webviewProtocol"; @@ -23,6 +24,13 @@ import URI from "uri-js"; import type { IDE } from "core"; import type { TabAutocompleteModel } from "../util/loadAutocompleteModel"; +interface DiffType { + count: number; + added: boolean; + removed: boolean; + value: string; +} + interface VsCodeCompletionInput { document: vscode.TextDocument; position: vscode.Position; @@ -100,6 +108,12 @@ export class ContinueCompletionProvider return null; } + // Don't autocomplete with multi-cursor + const editor = vscode.window.activeTextEditor; + if (editor && editor.selections.length > 1) { + return null; + } + // If the text at the range isn't a prefix of the intellisense text, // no completion will be displayed, regardless of what we return if ( @@ -189,10 +203,17 @@ export class ContinueCompletionProvider } } } + + // Manually pass file contents for unsaved, untitled files + if (document.isUntitled) { + manuallyPassFileContents = document.getText(); + } + // Handle commit message input box let manuallyPassPrefix: string | undefined = undefined; const input: AutocompleteInput = { + isUntitledFile: document.isUntitled, completionId: uuidv4(), filepath: document.uri.toString(), pos, @@ -248,13 +269,67 @@ export class ContinueCompletionProvider // Construct the range/text to show const startPos = selectedCompletionInfo?.range.start ?? position; - const completionRange = new vscode.Range( - startPos, - startPos.translate(0, outcome.completion.length), - ); + let range = new vscode.Range(startPos, startPos); + let completionText = outcome.completion; + const isSingleLineCompletion = outcome.completion.split("\n").length <= 1; + + if (isSingleLineCompletion) { + const lastLineOfCompletionText = completionText.split("\n").pop(); + const currentText = document + .lineAt(startPos) + .text.substring(startPos.character); + const diffs: DiffType[] = Diff.diffWords( + currentText, + lastLineOfCompletionText, + ); + + if (diffPatternMatches(diffs, ["+"])) { + // Just insert, we're already at the end of the line + } else if ( + diffPatternMatches(diffs, ["+", "="]) || + diffPatternMatches(diffs, ["+", "=", "+"]) + ) { + // The model repeated the text after the cursor to the end of the line + range = new vscode.Range( + startPos, + document.lineAt(startPos).range.end, + ); + } else if ( + diffPatternMatches(diffs, ["+", "-"]) || + diffPatternMatches(diffs, ["-", "+"]) + ) { + // We are midline and the model just inserted without repeating to the end of the line + // We want to move the cursor to the end of the line + // range = new vscode.Range( + // startPos, + // document.lineAt(startPos).range.end, + // ); + // // Find the last removed part of the diff + // const lastRemovedIndex = findLastIndex( + // diffs, + // (diff) => diff.removed === true, + // ); + // const lastRemovedContent = diffs[lastRemovedIndex].value; + // completionText += lastRemovedContent; + } else { + // Diff is too complicated, just insert the first added part of the diff + // This is the safe way to ensure that it is displayed + if (diffs[0]?.added) { + completionText = diffs[0].value; + } else { + // If the first part of the diff isn't an insertion, then the model is + // probably rewriting other parts of the line + return undefined; + } + } + } else { + // Extend the range to the end of the line for multiline completions + range = new vscode.Range(startPos, document.lineAt(startPos).range.end); + } + const completionItem = new vscode.InlineCompletionItem( - outcome.completion, - completionRange, + completionText, + range, { title: "Log Autocomplete Outcome", command: "continue.logAutocompleteOutcome", @@ -293,3 +368,26 @@ export class ContinueCompletionProvider return true; } } + +type DiffPartType = "+" | "-" | "="; + +function diffPatternMatches( + diffs: DiffType[], + pattern: DiffPartType[], +): boolean { + if (diffs.length !== pattern.length) { + return false; + } + + for (let i = 0; i < diffs.length; i++) { + const diff = diffs[i]; + const diffPartType: DiffPartType = + !diff.added && !diff.removed ? "=" : diff.added ? "+" : "-"; + + if (diffPartType !== pattern[i]) { + return false; + } + } + + return true; +} diff --git a/extensions/vscode/src/commands.ts b/extensions/vscode/src/commands.ts index 4603cfd35..227022392 100644 --- a/extensions/vscode/src/commands.ts +++ b/extensions/vscode/src/commands.ts @@ -720,9 +720,10 @@ const getCommandsMap: ( "continue.applyCodeFromChat": () => { void sidebar.webviewProtocol.request("applyCodeFromChat", undefined); }, - "continue.toggleFullScreen": () => { + "continue.toggleFullScreen": async () => { focusGUI(); + const sessionId = await sidebar.webviewProtocol.request("getCurrentSessionId", undefined); // Check if full screen is already open by checking open tabs const fullScreenTab = getFullScreenTab(); @@ -743,6 +744,7 @@ const getCommandsMap: ( vscode.ViewColumn.One, { retainContextWhenHidden: true, + enableScripts: true, }, ); fullScreenPanel = panel; @@ -755,6 +757,13 @@ const getCommandsMap: ( undefined, true, ); + + panel.onDidChangeViewState(() => { + vscode.commands.executeCommand("continue.newSession"); + if(sessionId){ + vscode.commands.executeCommand("continue.focusContinueSessionId", sessionId); + } + }); // When panel closes, reset the webview and focus panel.onDidDispose( @@ -767,6 +776,7 @@ const getCommandsMap: ( ); vscode.commands.executeCommand("workbench.action.copyEditorToNewWindow"); + vscode.commands.executeCommand("workbench.action.closeAuxiliaryBar"); }, "continue.openConfig": () => { core.invoke("config/openProfile", { diff --git a/gui/src/components/History/index.tsx b/gui/src/components/History/index.tsx index 9c6bc054e..24ac376e3 100644 --- a/gui/src/components/History/index.tsx +++ b/gui/src/components/History/index.tsx @@ -23,8 +23,6 @@ export function History() { const [searchTerm, setSearchTerm] = useState(""); - const sessionTitle = useAppSelector((state) => state.session.title); - const minisearch = useRef( new MiniSearch({ fields: ["title"], diff --git a/gui/src/components/Layout.tsx b/gui/src/components/Layout.tsx index b183e24e4..78085b431 100644 --- a/gui/src/components/Layout.tsx +++ b/gui/src/components/Layout.tsx @@ -65,8 +65,6 @@ const Layout = () => { const showDialog = useAppSelector((state) => state.ui.showDialog); - const isStreaming = useAppSelector((state) => state.session.isStreaming); - useWebviewListener( "openDialogMessage", async (message) => { diff --git a/gui/src/hooks/useSetup.ts b/gui/src/hooks/useSetup.ts index 0e97965bb..2db1252ab 100644 --- a/gui/src/hooks/useSetup.ts +++ b/gui/src/hooks/useSetup.ts @@ -137,6 +137,14 @@ function useSetup() { [history], ); + useWebviewListener( + "getCurrentSessionId", + async () => { + return sessionId + }, + [sessionId], + ); + useWebviewListener("setInactive", async () => { dispatch(setInactive()); });