apps/ui-kit/docs/language-server-protocol.md
Beekeeper Studio UI Kit supports the Language Server Protocol (LSP), which enables powerful features like code completion, diagnostics, hover information, and more.
The Language Server Protocol (LSP) defines a common protocol for communication between text editors and language servers, which provide language-specific features like intelligent code completion, error checking, and formatting. By supporting LSP, the Beekeeper Studio UI Kit allows you to connect your editors to any language server that implements the protocol.
To enable LSP support in the Text Editor component, you need to configure the lsConfig property:
textEditor.lsConfig = {
// Language ID (required)
languageId: "javascript",
// Workspace root URI (required)
rootUri: "/path/to/project",
// Document URI (required)
documentUri: "/path/to/project/file.js",
// WebSocket transport (required)
transport: {
wsUri: "ws://localhost:3000/lsp"
},
// Optional timeout in milliseconds
timeout: 10000,
};
The Text Editor component will automatically send requests to the language server when necessary. But you can also use the LSP helpers to send requests directly:
textEditor.addEventListener("bks-lsp-ready", async () => {
// Get the language server helpers
const helpers = textEditor.ls();
// Request a document formatting and apply it
await helpers.formatDocument({ tabSize: 2, insertSpaces: true });
// Get the language server client
const client = helpers.getClient();
// Request a custom command to the language server
await client.request({
method: "workspace/executeCommand",
params: { command: "fixAllFixableProblems" },
});
})
Make sure that you wait for the bks-lsp-ready event before interacting with the language server.
For more information about the helpers, see the Language Server Helpers API.
The LSP integration supports the following features:
Here's an example of setting up the Text Editor with a JavaScript language server:
<bks-text-editor id="js-editor"></bks-text-editor>
<script>
const jsEditor = document.getElementById("js-editor");
// Set content
jsEditor.value = `function hello(name) {
return "Hello, " + name;
}`;
// Configure language server
jsEditor.lsConfig = {
languageId: "javascript",
rootUri: "/path/to/project",
documentUri: "/path/to/project/script.js",
transport: {
wsUri: "ws://localhost:3000/javascript-language-server"
},
};
// Listen for LSP ready event
jsEditor.addEventListener("bks-lsp-ready", (event) => {
console.log("Language server ready with capabilities:", event.detail.capabilities);
});
</script>
To use LSP features, you need to run a language server that the Text Editor can connect to. There are many language servers available for different programming languages.
For example, to set up a JavaScript/TypeScript language server:
Install the language server:
npm install -g typescript-language-server typescript
Run the language server with WebSocket support (you may need additional tooling to expose the language server over WebSockets).
For more control over the WebSocket connection, you can use the WebSocketTransport class instead of a plain object with wsUri:
import { WebSocketTransport } from '@open-rpc/client-js';
const transport = new WebSocketTransport("ws://localhost:3000/server");
textEditor.lsConfig = {
languageId: "javascript",
rootUri: "/path/to/project",
documentUri: "/path/to/project/file.js",
transport,
};
See the API reference below for more details.