mirror of
https://github.com/microsoft/vscode-extension-samples.git
synced 2026-04-27 16:55:44 +08:00
Normalize code style for lsp-sample
This commit is contained in:
@ -5,41 +5,54 @@
|
||||
'use strict';
|
||||
|
||||
import * as path from 'path';
|
||||
|
||||
import { workspace, ExtensionContext } from 'vscode';
|
||||
|
||||
import {
|
||||
LanguageClient, LanguageClientOptions, ServerOptions, TransportKind
|
||||
LanguageClient,
|
||||
LanguageClientOptions,
|
||||
ServerOptions,
|
||||
TransportKind
|
||||
} from 'vscode-languageclient';
|
||||
|
||||
let client: LanguageClient;
|
||||
|
||||
export function activate(context: ExtensionContext) {
|
||||
|
||||
// The server is implemented in node
|
||||
let serverModule = context.asAbsolutePath(path.join('server', 'out', 'server.js'));
|
||||
let serverModule = context.asAbsolutePath(
|
||||
path.join('server', 'out', 'server.js')
|
||||
);
|
||||
// The debug options for the server
|
||||
let debugOptions = { execArgv: ["--nolazy", "--inspect=6009"] };
|
||||
// --inspect=6009: runs the server in Node's Inspector mode so VS Code can attach to the server for debugging
|
||||
let debugOptions = { execArgv: ['--nolazy', '--inspect=6009'] };
|
||||
|
||||
// If the extension is launched in debug mode then the debug server options are used
|
||||
// Otherwise the run options are used
|
||||
let serverOptions: ServerOptions = {
|
||||
run : { module: serverModule, transport: TransportKind.ipc },
|
||||
debug: { module: serverModule, transport: TransportKind.ipc, options: debugOptions }
|
||||
}
|
||||
run: { module: serverModule, transport: TransportKind.ipc },
|
||||
debug: {
|
||||
module: serverModule,
|
||||
transport: TransportKind.ipc,
|
||||
options: debugOptions
|
||||
}
|
||||
};
|
||||
|
||||
// Options to control the language client
|
||||
let clientOptions: LanguageClientOptions = {
|
||||
// Register the server for plain text documents
|
||||
documentSelector: [{scheme: 'file', language: 'plaintext'}],
|
||||
documentSelector: [{ scheme: 'file', language: 'plaintext' }],
|
||||
synchronize: {
|
||||
// Notify the server about file changes to '.clientrc files contained in the workspace
|
||||
fileEvents: workspace.createFileSystemWatcher('**/.clientrc'),
|
||||
fileEvents: workspace.createFileSystemWatcher('**/.clientrc')
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Create the language client and start the client.
|
||||
client = new LanguageClient('languageServerExample', 'Language Server Example', serverOptions, clientOptions);
|
||||
client = new LanguageClient(
|
||||
'languageServerExample',
|
||||
'Language Server Example',
|
||||
serverOptions,
|
||||
clientOptions
|
||||
);
|
||||
|
||||
// Start the client. This will also launch the server
|
||||
client.start();
|
||||
@ -50,4 +63,4 @@ export function deactivate(): Thenable<void> {
|
||||
return undefined;
|
||||
}
|
||||
return client.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,37 +4,41 @@
|
||||
* ------------------------------------------------------------------------------------------ */
|
||||
'use strict';
|
||||
|
||||
import * as vscode from 'vscode'
|
||||
import * as assert from 'assert'
|
||||
import { getDocUri, activate } from './helper'
|
||||
import * as vscode from 'vscode';
|
||||
import * as assert from 'assert';
|
||||
import { getDocUri, activate } from './helper';
|
||||
|
||||
describe('Should do completion', () => {
|
||||
const docUri = getDocUri('completion.txt')
|
||||
const docUri = getDocUri('completion.txt');
|
||||
|
||||
it('Completes JS/TS in txt file', async () => {
|
||||
await testCompletion(docUri, new vscode.Position(0, 0), {
|
||||
items: [
|
||||
{ label: 'JavaScript', kind: vscode.CompletionItemKind.Text },
|
||||
{ label: 'TypeScript', kind: vscode.CompletionItemKind.Text }
|
||||
]
|
||||
})
|
||||
})
|
||||
})
|
||||
it('Completes JS/TS in txt file', async () => {
|
||||
await testCompletion(docUri, new vscode.Position(0, 0), {
|
||||
items: [
|
||||
{ label: 'JavaScript', kind: vscode.CompletionItemKind.Text },
|
||||
{ label: 'TypeScript', kind: vscode.CompletionItemKind.Text }
|
||||
]
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
async function testCompletion(docUri: vscode.Uri, position: vscode.Position, expectedCompletionList: vscode.CompletionList) {
|
||||
await activate(docUri)
|
||||
async function testCompletion(
|
||||
docUri: vscode.Uri,
|
||||
position: vscode.Position,
|
||||
expectedCompletionList: vscode.CompletionList
|
||||
) {
|
||||
await activate(docUri);
|
||||
|
||||
// Executing the command `vscode.executeCompletionItemProvider` to simulate triggering completion
|
||||
const actualCompletionList = (await vscode.commands.executeCommand(
|
||||
'vscode.executeCompletionItemProvider',
|
||||
docUri,
|
||||
position
|
||||
)) as vscode.CompletionList
|
||||
// Executing the command `vscode.executeCompletionItemProvider` to simulate triggering completion
|
||||
const actualCompletionList = (await vscode.commands.executeCommand(
|
||||
'vscode.executeCompletionItemProvider',
|
||||
docUri,
|
||||
position
|
||||
)) as vscode.CompletionList;
|
||||
|
||||
assert.equal(actualCompletionList.items.length, expectedCompletionList.items.length);
|
||||
expectedCompletionList.items.forEach((expectedItem, i) => {
|
||||
const actualItem = actualCompletionList.items[i]
|
||||
assert.equal(actualItem.label, expectedItem.label)
|
||||
assert.equal(actualItem.kind, expectedItem.kind)
|
||||
})
|
||||
}
|
||||
assert.equal(actualCompletionList.items.length, expectedCompletionList.items.length);
|
||||
expectedCompletionList.items.forEach((expectedItem, i) => {
|
||||
const actualItem = actualCompletionList.items[i];
|
||||
assert.equal(actualItem.label, expectedItem.label);
|
||||
assert.equal(actualItem.kind, expectedItem.kind);
|
||||
});
|
||||
}
|
||||
|
||||
@ -4,42 +4,45 @@
|
||||
* ------------------------------------------------------------------------------------------ */
|
||||
'use strict';
|
||||
|
||||
import * as vscode from 'vscode'
|
||||
import * as path from 'path'
|
||||
import * as vscode from 'vscode';
|
||||
import * as path from 'path';
|
||||
|
||||
export let doc: vscode.TextDocument
|
||||
export let editor: vscode.TextEditor
|
||||
export let documentEol: string
|
||||
export let platformEol: string
|
||||
export let doc: vscode.TextDocument;
|
||||
export let editor: vscode.TextEditor;
|
||||
export let documentEol: string;
|
||||
export let platformEol: string;
|
||||
|
||||
/**
|
||||
* Activates the vscode.lsp-sample extension
|
||||
*/
|
||||
export async function activate(docUri: vscode.Uri) {
|
||||
// The extensionId is `publisher.name` from package.json
|
||||
const ext = vscode.extensions.getExtension('vscode.lsp-sample')
|
||||
await ext.activate();
|
||||
try {
|
||||
doc = await vscode.workspace.openTextDocument(docUri)
|
||||
editor = await vscode.window.showTextDocument(doc)
|
||||
await sleep(2000) // Wait for server activation
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
// The extensionId is `publisher.name` from package.json
|
||||
const ext = vscode.extensions.getExtension('vscode.lsp-sample');
|
||||
await ext.activate();
|
||||
try {
|
||||
doc = await vscode.workspace.openTextDocument(docUri);
|
||||
editor = await vscode.window.showTextDocument(doc);
|
||||
await sleep(2000); // Wait for server activation
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
async function sleep(ms: number) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
export const getDocPath = (p: string) => {
|
||||
return path.resolve(__dirname, '../../testFixture', p)
|
||||
}
|
||||
return path.resolve(__dirname, '../../testFixture', p);
|
||||
};
|
||||
export const getDocUri = (p: string) => {
|
||||
return vscode.Uri.file(getDocPath(p))
|
||||
}
|
||||
return vscode.Uri.file(getDocPath(p));
|
||||
};
|
||||
|
||||
export async function setTestContent(content: string): Promise<boolean> {
|
||||
const all = new vscode.Range(doc.positionAt(0), doc.positionAt(doc.getText().length))
|
||||
return editor.edit(eb => eb.replace(all, content))
|
||||
const all = new vscode.Range(
|
||||
doc.positionAt(0),
|
||||
doc.positionAt(doc.getText().length)
|
||||
);
|
||||
return editor.edit(eb => eb.replace(all, content));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user