mirror of
https://github.com/microsoft/vscode-extension-samples.git
synced 2026-04-27 16:55:44 +08:00
Add lsp code action ui sample
This commit is contained in:
6
lsp-user-input-sample/server/.eslintrc.json
Normal file
6
lsp-user-input-sample/server/.eslintrc.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"extends": "../.eslintrc.base.json",
|
||||
"rules": {
|
||||
"no-console": "error"
|
||||
}
|
||||
}
|
||||
45
lsp-user-input-sample/server/package-lock.json
generated
Normal file
45
lsp-user-input-sample/server/package-lock.json
generated
Normal file
@ -0,0 +1,45 @@
|
||||
{
|
||||
"name": "server",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
"vscode-jsonrpc": {
|
||||
"version": "5.0.0-next.4",
|
||||
"resolved": "https://registry.npmjs.org/vscode-jsonrpc/-/vscode-jsonrpc-5.0.0-next.4.tgz",
|
||||
"integrity": "sha512-Tos3tXP62ZTB9WowWwhvfVNdu1mEwQF/j7DqJuVL4QKhk311gH+mda0PZpG95LWyh5CCRpHMns4vNmMgZQrvXQ=="
|
||||
},
|
||||
"vscode-languageserver": {
|
||||
"version": "6.0.0-next.5",
|
||||
"resolved": "https://registry.npmjs.org/vscode-languageserver/-/vscode-languageserver-6.0.0-next.5.tgz",
|
||||
"integrity": "sha512-KMEyRR/cEhgPym7k3MOn7GUz70TjRYDcmvVIKJlmfknBEIrDU1GgI2DK0zasjfKdMGJWbLyU/rLfXJe46rynlw==",
|
||||
"requires": {
|
||||
"vscode-languageserver-protocol": "^3.15.0-next.11"
|
||||
}
|
||||
},
|
||||
"vscode-languageserver-protocol": {
|
||||
"version": "3.15.0-next.11",
|
||||
"resolved": "https://registry.npmjs.org/vscode-languageserver-protocol/-/vscode-languageserver-protocol-3.15.0-next.11.tgz",
|
||||
"integrity": "sha512-tpRnPtyS6q0EYH5RH12AtdMecgu3HVL2bBdBGzeQRN8Tf93I9LY4Fl5TXUNkIBjuxjMshkCM8ikhb+hlnWvB2w==",
|
||||
"requires": {
|
||||
"vscode-jsonrpc": "^5.0.0-next.3",
|
||||
"vscode-languageserver-types": "^3.15.0-next.7"
|
||||
}
|
||||
},
|
||||
"vscode-languageserver-textdocument": {
|
||||
"version": "1.0.0-next.5",
|
||||
"resolved": "https://registry.npmjs.org/vscode-languageserver-textdocument/-/vscode-languageserver-textdocument-1.0.0-next.5.tgz",
|
||||
"integrity": "sha512-1jp/zAidN/bF/sqPimhBX1orH5G4rzRw63k75TesukJDuxm8yW79ECStWbDSy41BHGOwSGN4M69QFvhancSr5A=="
|
||||
},
|
||||
"vscode-languageserver-types": {
|
||||
"version": "3.15.0-next.8",
|
||||
"resolved": "https://registry.npmjs.org/vscode-languageserver-types/-/vscode-languageserver-types-3.15.0-next.8.tgz",
|
||||
"integrity": "sha512-AEfWrSNyeamWMKPehh/kd3nBnKD9ZGCPhzfxMnW9YNqElSh28G2+Puk3knIQWyaWyV6Bzh28ok9BRJsPzXFCkQ=="
|
||||
},
|
||||
"vscode-uri": {
|
||||
"version": "2.0.3",
|
||||
"resolved": "https://registry.npmjs.org/vscode-uri/-/vscode-uri-2.0.3.tgz",
|
||||
"integrity": "sha512-4D3DI3F4uRy09WNtDGD93H9q034OHImxiIcSq664Hq1Y1AScehlP3qqZyTkX/RWxeu0MRMHGkrxYqm2qlDF/aw=="
|
||||
}
|
||||
}
|
||||
}
|
||||
22
lsp-user-input-sample/server/package.json
Normal file
22
lsp-user-input-sample/server/package.json
Normal file
@ -0,0 +1,22 @@
|
||||
{
|
||||
"name": "server",
|
||||
"version": "1.0.0",
|
||||
"private": true,
|
||||
"author": "Microsoft Corporation",
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/Microsoft/vscode-extension-samples.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/Microsoft/vscode-extension-samples/issues"
|
||||
},
|
||||
"engines": {
|
||||
"node": "*"
|
||||
},
|
||||
"dependencies": {
|
||||
"vscode-uri": "^2.0.3",
|
||||
"vscode-languageserver": "6.0.0-next.5",
|
||||
"vscode-languageserver-textdocument": "1.0.0-next.5"
|
||||
}
|
||||
}
|
||||
80
lsp-user-input-sample/server/src/sampleServer.ts
Normal file
80
lsp-user-input-sample/server/src/sampleServer.ts
Normal file
@ -0,0 +1,80 @@
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
* ------------------------------------------------------------------------------------------ */
|
||||
'use strict';
|
||||
|
||||
import {
|
||||
CodeAction, CodeActionKind, Command, createConnection, Diagnostic, DiagnosticSeverity, Position, Range, TextDocumentEdit,
|
||||
TextDocuments, TextDocumentSyncKind, TextEdit
|
||||
} from 'vscode-languageserver';
|
||||
import { TextDocument } from 'vscode-languageserver-textdocument';
|
||||
|
||||
const connection = createConnection();
|
||||
connection.console.info(`Sample server running in node ${process.version}`);
|
||||
|
||||
const documents: TextDocuments<TextDocument> = new TextDocuments(TextDocument);
|
||||
documents.listen(connection);
|
||||
|
||||
connection.onInitialize(() => {
|
||||
return {
|
||||
capabilities: {
|
||||
codeActionProvider: true,
|
||||
textDocumentSync: {
|
||||
openClose: true,
|
||||
change: TextDocumentSyncKind.Incremental
|
||||
},
|
||||
executeCommandProvider: {
|
||||
commands: ['sample.fixMe']
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
function validate(document: TextDocument): void {
|
||||
connection.sendDiagnostics({
|
||||
uri: document.uri,
|
||||
version: document.version,
|
||||
diagnostics: [
|
||||
Diagnostic.create(Range.create(0,0,0, 10), 'Something is wrong here', DiagnosticSeverity.Warning)
|
||||
]
|
||||
});
|
||||
}
|
||||
|
||||
documents.onDidOpen((event) => {
|
||||
validate(event.document);
|
||||
});
|
||||
|
||||
documents.onDidChangeContent((event) => {
|
||||
validate(event.document);
|
||||
});
|
||||
|
||||
connection.onCodeAction((params) => {
|
||||
const textDocument = documents.get(params.textDocument.uri);
|
||||
if (textDocument === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
const title = 'With User Input';
|
||||
return [CodeAction.create(title, Command.create(title, 'sample.fixMe', textDocument.uri), CodeActionKind.QuickFix)];
|
||||
});
|
||||
|
||||
connection.onExecuteCommand(async (params) => {
|
||||
if (params.command !== 'sample.fixMe' || params.arguments === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
const textDocument = documents.get(params.arguments[0]);
|
||||
if (textDocument === undefined) {
|
||||
return;
|
||||
}
|
||||
const newText = typeof params.arguments[1] === 'string' ? params.arguments[1] : 'Eclipse';
|
||||
connection.workspace.applyEdit({
|
||||
documentChanges: [
|
||||
TextDocumentEdit.create({ uri: textDocument.uri, version: textDocument.version }, [
|
||||
TextEdit.insert(Position.create(0, 0), newText)
|
||||
])
|
||||
]
|
||||
});
|
||||
});
|
||||
|
||||
connection.listen();
|
||||
18
lsp-user-input-sample/server/tsconfig.json
Normal file
18
lsp-user-input-sample/server/tsconfig.json
Normal file
@ -0,0 +1,18 @@
|
||||
{
|
||||
"extends": "../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"target": "es2017",
|
||||
"module": "commonjs",
|
||||
"moduleResolution": "node",
|
||||
"sourceMap": true,
|
||||
"outDir": "out",
|
||||
"rootDir": "src",
|
||||
"lib": [ "es2017" ]
|
||||
},
|
||||
"include": [
|
||||
"src"
|
||||
],
|
||||
"exclude": [
|
||||
"node_modules"
|
||||
]
|
||||
}
|
||||
22
lsp-user-input-sample/server/webpack.config.js
Normal file
22
lsp-user-input-sample/server/webpack.config.js
Normal file
@ -0,0 +1,22 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
//@ts-check
|
||||
|
||||
'use strict';
|
||||
|
||||
const withDefaults = require('../shared.webpack.config');
|
||||
const path = require('path');
|
||||
|
||||
module.exports = withDefaults({
|
||||
context: path.join(__dirname),
|
||||
entry: {
|
||||
extension: './src/eslintServer.ts',
|
||||
},
|
||||
output: {
|
||||
filename: 'eslintServer.js',
|
||||
path: path.join(__dirname, 'out')
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user