LSP-client-example
47 строк · 1.5 Кб
1/* --------------------------------------------------------------------------------------------
2* Copyright (c) Microsoft Corporation. All rights reserved.
3* Licensed under the MIT License. See License.txt in the project root for license information.
4* ------------------------------------------------------------------------------------------ */
5
6import * as vscode from 'vscode';
7import * as path from 'path';
8
9export let doc: vscode.TextDocument;
10export let editor: vscode.TextEditor;
11export let documentEol: string;
12export let platformEol: string;
13
14/**
15* Activates the vscode.lsp-sample extension
16*/
17export async function activate(docUri: vscode.Uri) {
18// The extensionId is `publisher.name` from package.json
19const ext = vscode.extensions.getExtension('vscode-samples.lsp-sample')!;
20await ext.activate();
21try {
22doc = await vscode.workspace.openTextDocument(docUri);
23editor = await vscode.window.showTextDocument(doc);
24await sleep(2000); // Wait for server activation
25} catch (e) {
26console.error(e);
27}
28}
29
30async function sleep(ms: number) {
31return new Promise(resolve => setTimeout(resolve, ms));
32}
33
34export const getDocPath = (p: string) => {
35return path.resolve(__dirname, '../../testFixture', p);
36};
37export const getDocUri = (p: string) => {
38return vscode.Uri.file(getDocPath(p));
39};
40
41export async function setTestContent(content: string): Promise<boolean> {
42const all = new vscode.Range(
43doc.positionAt(0),
44doc.positionAt(doc.getText().length)
45);
46return editor.edit(eb => eb.replace(all, content));
47}
48