mirror of
https://github.com/microsoft/vscode-extension-samples.git
synced 2026-04-27 16:55:44 +08:00
Add URI handler sample (#405)
* add uri handler sample * comment feedback
This commit is contained in:
committed by
GitHub
parent
3de53a210d
commit
b45728df10
19
uri-handler-sample/.eslintrc.json
Normal file
19
uri-handler-sample/.eslintrc.json
Normal file
@ -0,0 +1,19 @@
|
||||
{
|
||||
"root": true,
|
||||
"parser": "@typescript-eslint/parser",
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 6,
|
||||
"sourceType": "module"
|
||||
},
|
||||
"plugins": [
|
||||
"@typescript-eslint"
|
||||
],
|
||||
"rules": {
|
||||
"@typescript-eslint/naming-convention": "warn",
|
||||
"@typescript-eslint/semi": "warn",
|
||||
"curly": "warn",
|
||||
"eqeqeq": "warn",
|
||||
"no-throw-literal": "warn",
|
||||
"semi": "off"
|
||||
}
|
||||
}
|
||||
7
uri-handler-sample/.vscode/extensions.json
vendored
Normal file
7
uri-handler-sample/.vscode/extensions.json
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
// See http://go.microsoft.com/fwlink/?LinkId=827846
|
||||
// for the documentation about the extensions.json format
|
||||
"recommendations": [
|
||||
"dbaeumer.vscode-eslint"
|
||||
]
|
||||
}
|
||||
34
uri-handler-sample/.vscode/launch.json
vendored
Normal file
34
uri-handler-sample/.vscode/launch.json
vendored
Normal file
@ -0,0 +1,34 @@
|
||||
// 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",
|
||||
"args": [
|
||||
"--extensionDevelopmentPath=${workspaceFolder}"
|
||||
],
|
||||
"outFiles": [
|
||||
"${workspaceFolder}/out/**/*.js"
|
||||
],
|
||||
"preLaunchTask": "${defaultBuildTask}"
|
||||
},
|
||||
{
|
||||
"name": "Extension Tests",
|
||||
"type": "extensionHost",
|
||||
"request": "launch",
|
||||
"args": [
|
||||
"--extensionDevelopmentPath=${workspaceFolder}",
|
||||
"--extensionTestsPath=${workspaceFolder}/out/test/suite/index"
|
||||
],
|
||||
"outFiles": [
|
||||
"${workspaceFolder}/out/test/**/*.js"
|
||||
],
|
||||
"preLaunchTask": "${defaultBuildTask}"
|
||||
}
|
||||
]
|
||||
}
|
||||
11
uri-handler-sample/.vscode/settings.json
vendored
Normal file
11
uri-handler-sample/.vscode/settings.json
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
// Place your settings in this file to overwrite default and user settings.
|
||||
{
|
||||
"files.exclude": {
|
||||
"out": false // set this to true to hide the "out" folder with the compiled JS files
|
||||
},
|
||||
"search.exclude": {
|
||||
"out": true // set this to false to include "out" folder in search results
|
||||
},
|
||||
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
|
||||
"typescript.tsc.autoDetect": "off"
|
||||
}
|
||||
20
uri-handler-sample/.vscode/tasks.json
vendored
Normal file
20
uri-handler-sample/.vscode/tasks.json
vendored
Normal 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
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
42
uri-handler-sample/README.md
Normal file
42
uri-handler-sample/README.md
Normal file
@ -0,0 +1,42 @@
|
||||
# Uri Handlers
|
||||
|
||||
This sample demonstrates how to implement a Uri handler in VS Code.
|
||||
A Uri handler is run when a browser redirects to VS Code with a specific extension id as the authority.
|
||||
|
||||
Examples:
|
||||
|
||||
* `vscode://vscode-samples.uri-handler-sample`
|
||||
* `vscode-insiders://vscode-samples.uri-handler-sample`
|
||||
|
||||
If you paste these Uris into your browser, they will open VS Code and VS Code insiders, respectively.
|
||||
|
||||
This sample provides a simple Uri handler that shows an information message when a Uri is handled.
|
||||
Additionally, if a query string was included in the Uri, it will include that in the message.
|
||||
|
||||
Run the sample and try opening the following Uris in your browser:
|
||||
|
||||
* `vscode://vscode-samples.uri-handler-sample`
|
||||
* `vscode://vscode-samples.uri-handler-sample?q=hello`
|
||||
|
||||
> Note: use `vscode-insiders://` if you ran the sample in insiders.
|
||||
|
||||
## VS Code API
|
||||
|
||||
This sample uses following APIs
|
||||
|
||||
### APIs
|
||||
|
||||
- `window.registerUriHandler`
|
||||
- `env.uriScheme`
|
||||
- `env.asExternalUri`
|
||||
- `UriHandler`
|
||||
|
||||
> Be sure to look at the [API Docs](https://code.visualstudio.com/api/references/vscode-api) for usage.
|
||||
|
||||
## Running the Sample
|
||||
|
||||
- Open this example in VS Code Insiders
|
||||
- `npm install`
|
||||
- `npm run watch`
|
||||
- `F5` to start debugging
|
||||
- Run the `Start handling Uris` command
|
||||
4504
uri-handler-sample/package-lock.json
generated
Normal file
4504
uri-handler-sample/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
45
uri-handler-sample/package.json
Normal file
45
uri-handler-sample/package.json
Normal file
@ -0,0 +1,45 @@
|
||||
{
|
||||
"name": "uri-handler-sample",
|
||||
"publisher": "vscode-samples",
|
||||
"description": "Uri Handler Sample",
|
||||
"version": "0.0.1",
|
||||
"engines": {
|
||||
"vscode": "^1.46.0"
|
||||
},
|
||||
"categories": [
|
||||
"Other"
|
||||
],
|
||||
"activationEvents": [
|
||||
"onCommand:uri-handler-sample.start"
|
||||
],
|
||||
"main": "./out/extension.js",
|
||||
"contributes": {
|
||||
"commands": [
|
||||
{
|
||||
"command": "uri-handler-sample.start",
|
||||
"title": "Start handling Uris"
|
||||
}
|
||||
]
|
||||
},
|
||||
"scripts": {
|
||||
"vscode:prepublish": "npm run compile",
|
||||
"compile": "tsc -p ./",
|
||||
"watch": "tsc -watch -p ./",
|
||||
"pretest": "npm run compile && npm run lint",
|
||||
"lint": "eslint src --ext ts",
|
||||
"test": "node ./out/test/runTest.js"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/glob": "^7.1.3",
|
||||
"@types/mocha": "^8.0.4",
|
||||
"@types/node": "^12.11.7",
|
||||
"@types/vscode": "^1.46.0",
|
||||
"@typescript-eslint/eslint-plugin": "^4.9.0",
|
||||
"@typescript-eslint/parser": "^4.9.0",
|
||||
"eslint": "^7.15.0",
|
||||
"glob": "^7.1.6",
|
||||
"mocha": "^8.1.3",
|
||||
"typescript": "^4.1.2",
|
||||
"vscode-test": "^1.4.1"
|
||||
}
|
||||
}
|
||||
35
uri-handler-sample/src/extension.ts
Normal file
35
uri-handler-sample/src/extension.ts
Normal file
@ -0,0 +1,35 @@
|
||||
import * as vscode from 'vscode';
|
||||
|
||||
// Our implementation of a UriHandler.
|
||||
class MyUriHandler implements vscode.UriHandler {
|
||||
// This function will get run when something redirects to VS Code
|
||||
// with your extension id as the authority.
|
||||
handleUri(uri: vscode.Uri): vscode.ProviderResult<void> {
|
||||
let message = "Handled a Uri!";
|
||||
if (uri.query) {
|
||||
message += ` It came with this query: ${uri.query}`;
|
||||
}
|
||||
vscode.window.showInformationMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
export function activate(context: vscode.ExtensionContext) {
|
||||
|
||||
let disposable = vscode.commands.registerCommand('uri-handler-sample.start', async () => {
|
||||
// Create our new UriHandler
|
||||
const uriHandler = new MyUriHandler();
|
||||
|
||||
// And register it with VS Code. You can only register a single UriHandler for your extension.
|
||||
context.subscriptions.push(vscode.window.registerUriHandler(uriHandler));
|
||||
|
||||
// You don't have to get the Uri from the `vscode.env.asExternalUri` API but it will add a query
|
||||
// parameter (ex: "windowId%3D14") that will help VS Code decide which window to redirect to.
|
||||
// If this query parameter isn't specified, VS Code will pick the last windows that was focused.
|
||||
const uri = await vscode.env.asExternalUri(vscode.Uri.parse(`${vscode.env.uriScheme}://vscode-samples.uri-handler-sample`));
|
||||
vscode.window.showInformationMessage(`Starting to handle Uris. Open ${uri} in your browser to test.`);
|
||||
});
|
||||
|
||||
context.subscriptions.push(disposable);
|
||||
}
|
||||
|
||||
export function deactivate() {}
|
||||
21
uri-handler-sample/tsconfig.json
Normal file
21
uri-handler-sample/tsconfig.json
Normal file
@ -0,0 +1,21 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"target": "es6",
|
||||
"outDir": "out",
|
||||
"lib": [
|
||||
"es6"
|
||||
],
|
||||
"sourceMap": true,
|
||||
"rootDir": "src",
|
||||
"strict": true /* enable all strict type-checking options */
|
||||
/* Additional Checks */
|
||||
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
|
||||
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
|
||||
// "noUnusedParameters": true, /* Report errors on unused parameters. */
|
||||
},
|
||||
"exclude": [
|
||||
"node_modules",
|
||||
".vscode-test"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user