Xtextで作成したLSPサーバとVS Codeクライアント(LSPクライアント)を接続する

Xtextで作成したLSPサーバとVS Codeクライアント(LSPクライアント)を接続する

前提

プロジェクト作成

yo codeを実行します。「New Language Support」ではなく「New Extension (TypeScript)」にしてみます。

– ? What type of extension do you want to create✔️ **New Extension (TypeScript)**
– ? What’s the name of your extension?✔️ **New Extension (TypeScript)**
– ? What’s the identifier of your extension? ✔️ **mydsl-ext**
– ? What’s the description of your extension?  ✔️ **mydsl-ext**
– ? Initialize a git repository?  ✔️ **n**
– ? Which bundler to use?  ✔️ **esbuild**
– ? Which package manager to use?  ✔️ **npm**

package.json修正

追記します。

  "activationEvents": ["onLanguage:mydsl"],
  "contributes": {
    "languages": [
      {
        "id": "mydsl",
        "aliases": [
          "MyDSL"
        ],
        "extensions": [
          ".mydsl"
        ]
      }
    ]
  },

vscode-languageclientモジュールインストール

vscode-languageclientをインストールします。

npm i vscode-languageclient

server

serverフォルダ作成し、fat.jar(org.xtext.example.mydsl.ide-1.0.0-SNAPSHOT-ls.jar)を配置します。

src/extension.ts修正

extension.tsを修正します。

import * as vscode from 'vscode';
import { LanguageClient, LanguageClientOptions, ServerOptions } from 'vscode-languageclient/node';
import * as path from 'path';

let client: LanguageClient;

export async function activate(context: vscode.ExtensionContext) {

	console.log('Congratulations, your extension "mydsl-vscode" is now active!');
	const jarPath = context.asAbsolutePath(
        path.join('server', 'org.xtext.example.mydsl.ide-1.0.0-SNAPSHOT-ls.jar')
    );
    const serverOptions: ServerOptions = {
        command: 'java',
        args: ['-jar', jarPath]
    };

    const clientOptions: LanguageClientOptions = {
        documentSelector: [{ scheme: 'file', language: 'mydsl' }],
        synchronize: {
            fileEvents: vscode.workspace.createFileSystemWatcher('**/*.mydsl')
        }
    };

    client = new LanguageClient(
        'mydslLanguageServer',
        'MyDSL Language Server',
        serverOptions,
        clientOptions
    );
	await client.start();
	context.subscriptions.push({
		dispose: () => {
			if (!client) {
				return;
			}
			client.stop();
		}
	});
}

export function deactivate(): Thenable | undefined {
	if (!client) {
		return undefined;
	}

    return client.stop();
}

extension.tsを修正したら、npm run compileします。

デバッグ

F5でVS Code(拡張機能開発ホスト)を起動して確認します。

株式会社CONFRAGE ITソリューション事業部をもっと見る

今すぐ購読し、続きを読んで、すべてのアーカイブにアクセスしましょう。

続きを読む

タイトルとURLをコピーしました