Merge pull request #108 from fcrespo82/codelens-sample

Add a CodeLens API sample
This commit is contained in:
Pine
2019-10-25 09:47:57 -07:00
committed by GitHub
11 changed files with 2622 additions and 0 deletions

View File

@ -177,6 +177,13 @@ const samples = [
guide: '/api/extension-guides/custom-data-extension',
apis: [],
contributions: []
},
{
description: 'CodeLens Provider Sample',
path: 'codelens-sample',
guide: null,
apis: [`languages.registerCodeLensProvider`, `CodeLensProvider`, `CodeLens`],
contributions: []
}
]

35
codelens-sample/.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,35 @@
// A launch configuration that compiles the extension and then opens it inside a new window
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
{
"version": "0.2.0",
"configurations": [{
"name": "Run Extension",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}"
],
"outFiles": [
"${workspaceFolder}/out/**/*.js"
],
"preLaunchTask": "npm: watch"
},
{
"name": "Run Extension Tests",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/out/test"
],
"outFiles": [
"${workspaceFolder}/out/test/**/*.js"
],
"preLaunchTask": "npm: watch"
}
]
}

3
codelens-sample/.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"editor.insertSpaces": false
}

20
codelens-sample/.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,20 @@
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
{
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "watch",
"problemMatcher": "$tsc-watch",
"isBackground": true,
"presentation": {
"reveal": "never"
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

33
codelens-sample/README.md Normal file
View File

@ -0,0 +1,33 @@
# CodeLens Sample
This is a sample extension that shows the usage of the CodeLens API.
It is not intended as a production quality extension.
- Create a new file
- Write anything
- Click on the CodeLens for action example
- Can be enabled or disabled by command palete
## Demo
![demo](demo.gif)
## VS Code API
### `languages` module
- [`languages.registerCodeLensProvider`](https://code.visualstudio.com/api/references/vscode-api#languages.registerCodeLensProvider)
### CodeLens Provider
- [`CodeLensProvider`](https://code.visualstudio.com/api/references/vscode-api#CodeLensProvider)
- [`CodeLensProvider.provideCodeLenses`](https://code.visualstudio.com/api/references/vscode-api#CodeLensProvider.provideCodeLenses)
- [`CodeLensProvider.resolveCodeLens`](https://code.visualstudio.com/api/references/vscode-api#CodeLensProvider.resolveCodeLens)
## Running the Sample
- Run `npm install` in terminal to install dependencies
- 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

BIN
codelens-sample/demo.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 523 KiB

2363
codelens-sample/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,52 @@
{
"name": "codelens-sample",
"displayName": "CodeLens Sample",
"description": "Samples for VS Code's CodeLens API",
"version": "0.0.1",
"publisher": "ms-vscode",
"engines": {
"vscode": "^1.26.0"
},
"categories": [
"Other"
],
"activationEvents": [
"*"
],
"main": "./out/extension",
"contributes": {
"commands": [
{
"title": "Enable CodeLens",
"command": "codelens-sample.enableCodeLens",
"category": "CodeLens Sample"
},
{
"title": "Disable Codelens",
"command": "codelens-sample.disableCodeLens",
"category": "CodeLens Sample"
}
],
"configuration": {
"properties": {
"codelens-sample.enableCodeLens": {
"type": "boolean",
"default": true
}
}
}
},
"scripts": {
"vscode:prepublish": "npm run compile",
"compile": "tsc -p ./",
"lint": "tslint -p ./",
"watch": "tsc -watch -p ./"
},
"devDependencies": {
"@types/node": "10.3.6",
"@types/vscode": "^1.26.0",
"tslint": "5.10.0",
"typescript": "2.9.2",
"vscode": "^1.1.21"
}
}

View File

@ -0,0 +1,55 @@
import * as vscode from 'vscode';
/**
* CodelensProvider
*/
export class CodelensProvider implements vscode.CodeLensProvider {
private codeLenses: vscode.CodeLens[] = [];
private regex: RegExp;
private _onDidChangeCodeLenses: vscode.EventEmitter<void> = new vscode.EventEmitter<void>();
public readonly onDidChangeCodeLenses: vscode.Event<void> = this._onDidChangeCodeLenses.event;
constructor() {
this.regex = /(.+)/g;
vscode.workspace.onDidChangeConfiguration((_) => {
this._onDidChangeCodeLenses.fire();
});
}
public provideCodeLenses(document: vscode.TextDocument, token: vscode.CancellationToken): vscode.CodeLens[] | Thenable<vscode.CodeLens[]> {
if (vscode.workspace.getConfiguration("codelens-sample").get("enableCodeLens", true)) {
this.codeLenses = [];
const regex = new RegExp(this.regex);
const text = document.getText();
let matches;
while ((matches = regex.exec(text)) !== null) {
let line = document.lineAt(document.positionAt(matches.index).line);
let indexOf = line.text.indexOf(matches[0]);
let position = new vscode.Position(line.lineNumber, indexOf);
let range = document.getWordRangeAtPosition(position, new RegExp(this.regex));
if (range) {
this.codeLenses.push(new vscode.CodeLens(range));
}
}
return this.codeLenses;
}
return [];
}
public resolveCodeLens(codeLens: vscode.CodeLens, token: vscode.CancellationToken) {
if (vscode.workspace.getConfiguration("codelens-sample").get("enableCodeLens", true)) {
codeLens.command = {
title: "Codelens provided by sample extension",
tooltip: "Tooltip provided by sample extension",
command: "codelens-sample.codelensAction",
arguments: ["Argument 1", false]
};
return codeLens;
}
return null;
}
}

View File

@ -0,0 +1,35 @@
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import { ExtensionContext, languages, commands, Disposable, workspace, window, ConfigurationTarget } from 'vscode';
import { CodelensProvider } from './CodelensProvider';
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
var disposables: Disposable[] = [];
export function activate(context: ExtensionContext) {
let codelensProvider = new CodelensProvider();
languages.registerCodeLensProvider("*", codelensProvider);
commands.registerCommand("codelens-sample.enableCodeLens", () => {
workspace.getConfiguration("codelens-sample").update("enableCodeLens", true, true);
});
commands.registerCommand("codelens-sample.disableCodeLens", () => {
workspace.getConfiguration("codelens-sample").update("enableCodeLens", false, true);
});
commands.registerCommand("codelens-sample.codelensAction", (args) => {
window.showInformationMessage(`CodeLens action clicked with args=${args}`);
});
}
// this method is called when your extension is deactivated
export function deactivate() {
if (disposables) {
disposables.forEach(item => item.dispose());
}
disposables = [];
}

View File

@ -0,0 +1,19 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"outDir": "out",
"lib": [
"es6"
],
"sourceMap": true,
"rootDir": "src",
/* Strict Type-Checking Option */
"strict": true, /* enable all strict type-checking options */
/* Additional Checks */
"noUnusedLocals": true /* Report errors on unused locals. */
},
"exclude": [
"node_modules"
]
}