mirror of
https://github.com/microsoft/vscode-extension-samples.git
synced 2026-06-13 07:10:26 +08:00
Inline Completions Sample
This sample demonstrates usage of the inline completions API with two different variations:
- Pattern-Based Provider - Advanced features including proposed APIs
- Simple Context-Aware Provider - Basic inline completions for common code patterns
Two Variations
Variation 1: Pattern-Based Provider (Default)
This provider demonstrates advanced inline completion features:
- Custom syntax for triggering completions via special comments
- Support for snippets with the
sflag - Bracket pair completion with the
bflag - Proposed API features (
handleDidShowCompletionItem,handleDidPartiallyAcceptCompletionItem) - Commands attached to completion items
Usage: Add special comment lines in your code like:
// [0,*):console.log("Hello")
// Type here and the completion will appear
The syntax is: // [start,end)flags:completion_text
start: Starting column position (0-based)end: Ending column position, or*for end of lineflags: Optional flags (sfor snippet,bfor bracket pairs)completion_text: The text to insert
Variation 2: Simple Context-Aware Provider
This provider demonstrates basic inline completion features:
- Context-aware completions based on what you're typing
- Simple pattern matching for common code constructs
- No proposed API usage (more stable, production-ready)
Examples:
- Type
console.→ suggestslog() - Type
function→ suggestsmyFunction() { } - Type
if→ suggests(condition) { } - Type
for→ suggests(let i = 0; i < length; i++) { }
Switching Between Variations
You can switch between the two providers by running the command: "Inline Completion: Switch Provider (Pattern-Based ↔ Simple)" from the Command Palette (Ctrl+Shift+P / Cmd+Shift+P).
Running the Sample
- Run
npm installin terminal to install dependencies - A
postinstallscript would download latest version ofvscode.proposed.*.d.ts - Run the
Run Extensiontarget in the Debug View. This will:- Start a task
npm: watchto compile the code - Run the extension in a new VS Code window
- Start a task
- Try both variations:
- Pattern-Based: Use the playground.js file with special comment syntax
- Simple: Type common code patterns like
console.,if,for, etc. - Use the Command Palette to switch between providers and see the differences
