Updates inline completions demo

This commit is contained in:
Henning Dieterichs
2023-01-31 17:27:39 +01:00
parent 544e4b31b6
commit 0f78142d1f
9 changed files with 119 additions and 16895 deletions

View File

@ -10,7 +10,9 @@
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}"
"--extensionDevelopmentPath=${workspaceFolder}",
"${workspaceFolder}/playground.js",
"--disable-extensions"
],
"outFiles": [
"${workspaceFolder}/out/**/*.js"

View File

@ -1,30 +1,15 @@
# Inline Completions Sample
This sample demonstrates usage of the proposed inline completions API.
The `postinstall` script uses `vscode-dts dev && vscode-dts main` to download latest version of [`vscode.d.ts`](https://github.com/microsoft/vscode/blob/main/src/vs/vscode.d.ts) and [`vscode.proposed.d.ts`](https://github.com/microsoft/vscode/blob/main/src/vs/vscode.proposed.d.ts) from the master branch of [microsoft/vscode](https://github.com/microsoft/vscode).
You can read more about `vscode-dts` at: https://github.com/microsoft/vscode-dts.
- ⚠️ This sample can only be used for extension development in [Insider release](https://code.visualstudio.com/insiders/). You cannot publish extensions using Proposed API.
- You need `"enableProposedApi": true` in `package.json`.
This sample demonstrates usage of the inline completions API.
It also demonstrates some proposed API of the inline completions feature that is not yet finalized.
![Demo Video](./demo.gif)
## VS Code API
Not documented, as the used API is not finalized.
## Running the Sample
- Run `npm install` in terminal to install dependencies
- A `postinstall` script would download latest version of `vscode.proposed.d.ts`
- A `postinstall` script would download latest version of `vscode.proposed.*.d.ts`
- Run the `Run Extension` target in the Debug View. This will:
- Start a task `npm: watch` to compile the code
- Run the extension in a new VS Code window
Make sure that `showInlineCompletions` is enabled in your settings!
```
"editor.inlineSuggest.enabled": true
```

Binary file not shown.

Before

Width:  |  Height:  |  Size: 386 KiB

After

Width:  |  Height:  |  Size: 121 KiB

View File

@ -10,6 +10,7 @@
"hasInstallScript": true,
"devDependencies": {
"@types/node": "^16.11.7",
"@types/vscode": "^1.74.0",
"@typescript-eslint/eslint-plugin": "^5.42.0",
"@typescript-eslint/parser": "^5.42.0",
"eslint": "^8.26.0",
@ -129,6 +130,12 @@
"integrity": "sha512-21cFJr9z3g5dW8B0CVI9g2O9beqaThGQ6ZFBqHfwhzLDKUxaqTIy3vnfah/UPkfOiF2pLq+tGz+W8RyCskuslw==",
"dev": true
},
"node_modules/@types/vscode": {
"version": "1.74.0",
"resolved": "https://registry.npmjs.org/@types/vscode/-/vscode-1.74.0.tgz",
"integrity": "sha512-LyeCIU3jb9d38w0MXFwta9r0Jx23ugujkAxdwLTNCyspdZTKUc43t7ppPbCiPoQ/Ivd/pnDFZrb4hWd45wrsgA==",
"dev": true
},
"node_modules/@typescript-eslint/eslint-plugin": {
"version": "5.42.0",
"resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.42.0.tgz",
@ -1739,6 +1746,12 @@
"integrity": "sha512-21cFJr9z3g5dW8B0CVI9g2O9beqaThGQ6ZFBqHfwhzLDKUxaqTIy3vnfah/UPkfOiF2pLq+tGz+W8RyCskuslw==",
"dev": true
},
"@types/vscode": {
"version": "1.74.0",
"resolved": "https://registry.npmjs.org/@types/vscode/-/vscode-1.74.0.tgz",
"integrity": "sha512-LyeCIU3jb9d38w0MXFwta9r0Jx23ugujkAxdwLTNCyspdZTKUc43t7ppPbCiPoQ/Ivd/pnDFZrb4hWd45wrsgA==",
"dev": true
},
"@typescript-eslint/eslint-plugin": {
"version": "5.42.0",
"resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.42.0.tgz",

View File

@ -1,6 +1,6 @@
{
"enabledApiProposals": [
"inlineCompletions"
"inlineCompletionsAdditions"
],
"name": "inline-completion-sample",
"displayName": "Inline Completion Sample",
@ -22,7 +22,7 @@
"commands": [
{
"command": "extension.inline-completion-settings",
"title": "Inline Completion Settings"
"title": "My Inline Completion Demo Settings"
}
],
"menus": {
@ -39,7 +39,6 @@
"lint": "eslint \"src/**/*.ts\"",
"watch": "tsc -watch -p ./",
"download-api": "vscode-dts dev",
"postdownload-api": "vscode-dts main",
"postinstall": "npm run download-api"
},
"devDependencies": {
@ -48,6 +47,7 @@
"@typescript-eslint/parser": "^5.42.0",
"eslint": "^8.26.0",
"typescript": "^4.9.4",
"vscode-dts": "^0.3.3"
"vscode-dts": "^0.3.3",
"@types/vscode": "^1.74.0"
}
}

View File

@ -5,9 +5,10 @@
// [0,*):console.log(
// [0,*):console.log("foo"
// [0,*):console.log({ label: "("
// [0,*):console.log({ label: "("
// [0,*):console.log(`${(1+2}`)

View File

@ -1,52 +1,77 @@
import * as vscode from 'vscode';
// Try it out in `playground.js`
import { Range } from 'vscode';
export function activate(context: vscode.ExtensionContext) {
const disposable = vscode.commands.registerCommand(
'extension.inline-completion-settings',
() => {
vscode.window.showInformationMessage('Show settings');
}
);
context.subscriptions.push(disposable);
let someTrackingIdCounter = 0;
console.log('inline-completions demo started');
vscode.commands.registerCommand('demo-ext.command1', async (...args) => {
vscode.window.showInformationMessage('command1: ' + JSON.stringify(args));
});
const provider: vscode.InlineCompletionItemProvider = {
provideInlineCompletionItems: async (document, position, context, token) => {
async provideInlineCompletionItems(document, position, context, token) {
console.log('provideInlineCompletionItems triggered');
const regexp = /\/\/ \[(.+),(.+)\):(.*)/;
const regexp = /\/\/ \[(.+?),(.+?)\)(.*?):(.*)/;
if (position.line <= 0) {
return;
}
const lineBefore = document.lineAt(position.line - 1).text;
const matches = lineBefore.match(regexp);
if (matches) {
const result: vscode.InlineCompletionList = {
items: [],
commands: [],
};
let offset = 1;
while (offset > 0) {
const lineBefore = document.lineAt(position.line - offset).text;
const matches = lineBefore.match(regexp);
if (!matches || position.line - offset < 0) {
break;
}
offset++;
const start = matches[1];
const startInt = parseInt(start, 10);
const end = matches[2];
const endInt =
end === '*' ? document.lineAt(position.line).text.length : parseInt(end, 10);
const insertText = matches[3].replace(/\\n/g, '\n');
end === '*'
? document.lineAt(position.line).text.length
: parseInt(end, 10);
const flags = matches[3];
const completeBracketPairs = flags.includes('b');
const isSnippet = flags.includes('s');
const text = matches[4].replace(/\\n/g, '\n');
return [
{
insertText,
range: new vscode.Range(position.line, startInt, position.line, endInt),
someTrackingId: someTrackingIdCounter++,
},
] as MyInlineCompletionItem[];
result.items.push({
insertText: isSnippet ? new vscode.SnippetString(text) : text,
range: new Range(position.line, startInt, position.line, endInt),
completeBracketPairs,
});
}
if (result.items.length > 0) {
result.commands!.push({
command: 'demo-ext.command1',
title: 'My Inline Completion Demo Command',
arguments: [1, 2],
});
}
return result;
},
handleDidShowCompletionItem(completionItem: vscode.InlineCompletionItem): void {
console.log('handleDidShowCompletionItem');
},
/**
* Is called when an inline completion item was accepted partially.
* @param acceptedLength The length of the substring of the inline completion that was accepted already.
*/
handleDidPartiallyAcceptCompletionItem(
completionItem: vscode.InlineCompletionItem,
acceptedLength: number
): void {
console.log('handleDidPartiallyAcceptCompletionItem');
},
};
vscode.languages.registerInlineCompletionItemProvider({ pattern: '**' }, provider);
}
interface MyInlineCompletionItem extends vscode.InlineCompletionItem {
someTrackingId: number;
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,37 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
declare module 'vscode' {
// https://github.com/microsoft/vscode/issues/124024 @hediet @alexdima
export interface InlineCompletionItem {
/**
* If set to `true`, unopened closing brackets are removed and unclosed opening brackets are closed.
* Defaults to `false`.
*/
completeBracketPairs?: boolean;
}
export interface InlineCompletionItemProvider {
// eslint-disable-next-line local/vscode-dts-provider-naming
handleDidShowCompletionItem?(completionItem: InlineCompletionItem): void;
/**
* Is called when an inline completion item was accepted partially.
* @param acceptedLength The length of the substring of the inline completion that was accepted already.
*/
// eslint-disable-next-line local/vscode-dts-provider-naming
handleDidPartiallyAcceptCompletionItem?(completionItem: InlineCompletionItem, acceptedLength: number): void;
}
// When finalizing `commands`, make sure to add a corresponding constructor parameter.
export interface InlineCompletionList {
/**
* A list of commands associated with the inline completions of this list.
*/
commands?: Command[];
}
}