Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and improved TypeScript types #18

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 71 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![npm version](https://badge.fury.io/js/codemirror-languageserver.svg)](https://www.npmjs.com/package/codemirror-languageserver)

This plugin enables code completion, hover tooltips, and linter functionality by connecting a CodeMirror 6 editor with a language server over WebSocket.
This plugin enables code completion, hover tooltips, and linter functionality by connecting a CodeMirror 6 editor with a language server over WebSocket or any compatible transport.

[How It Works](https://hjr265.me/blog/codemirror-lsp/)

Expand All @@ -12,35 +12,91 @@ This plugin enables code completion, hover tooltips, and linter functionality by
npm i codemirror-languageserver
```

### WebSockets transport
``` js
import { languageServer } from 'codemirror-languageserver';

const transport = new WebSocketTransport(serverUri)

var ls = languageServer({
// WebSocket server uri and other client options.
serverUri,
const lsPlugin = languageServer({
serverUri, // WebSocket server uri.
rootUri: 'file:///',

// Alternatively, to share the same client across multiple instances of this plugin.
client: new LanguageServerClient({
serverUri,
rootUri: 'file:///'
}),

documentUri: `file:///${filename}`,
languageId: 'cpp' // As defined at https://microsoft.github.io/language-server-protocol/specification#textDocumentItem.
});

var view = new EditorView({
const view = new EditorView({
state: EditorState.create({
extensions: [
// ...
lsPlugin,
// ...
]
})
});
```

### Re using the same client
``` js
import { languageServer } from 'codemirror-languageserver';

const client = new LanguageServerClient({
transport: new WebSocketTransport(serverUri),
rootUri: 'file:///'
})

const firstView = new EditorView({
state: EditorState.create({
extensions: [
// ...
languageServerWithClient({
client,
documentUri: `file:///${secondFileName}`,
languageId: 'cpp'
}),
// ...
]
})
});

const secondView = new EditorView({
state: EditorState.create({
extensions: [
// ...
languageServerWithClient({
client,
documentUri: `file:///${firstFileName}`,
languageId: 'cpp'
}),
// ...
]
})
});
```

### Custom transport
``` js
import { languageServer } from 'codemirror-languageserver';

const client = new LanguageServerClient({
transport: new AwesomeCustomTransport(),
rootUri: 'file:///'
})

const lsPlugin = languageServerWithClient({
client,
documentUri: `file:///${filename}`,
languageId: 'cpp'
})

const view = new EditorView({
state: EditorState.create({
extensions: [
// ...
ls,
lsPlugin,
// ...
]
})
});

```

## Contributing
Expand Down
39 changes: 21 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,40 +456,43 @@ class LanguageServerPlugin implements PluginValue {
}
}

interface LanguageServerBaseOptions {
rootUri: string | null;
workspaceFolders: LSP.WorkspaceFolder[] | null;
documentUri: string;
languageId: string;
type ServerUri = `ws://${string}` | `wss://${string}`;
interface LanguageServerOptions {
rootUri?: string;
workspaceFolders?: LSP.WorkspaceFolder[];
}

interface LanguageServerClientOptions extends LanguageServerBaseOptions {
transport: Transport,
interface LanguageServerClientOptions extends LanguageServerOptions {
transport?: Transport;
autoClose?: boolean;
}

interface LanguageServerOptions extends LanguageServerClientOptions {
client?: LanguageServerClient;
interface LanguageServerWithClientOptions {
documentUri: string;
client: LanguageServerClient;
languageId: string;
}

interface LanguageServerWebsocketOptions extends LanguageServerBaseOptions {
serverUri: `ws://${string}` | `wss://${string}`;
interface LanguageServerWebsocketOptions extends LanguageServerOptions {
documentUri: string;
serverUri: ServerUri;
languageId: string;
}

export function languageServer(options: LanguageServerWebsocketOptions){
const serverUri = options.serverUri;
delete options.serverUri;
return languageServerWithTransport({
return languageServerWithClient({
...options,
transport: new WebSocketTransport(serverUri)
client: new LanguageServerClient({
...options,
transport: new WebSocketTransport(serverUri)
})
})
}

export function languageServerWithTransport(options: LanguageServerOptions) {
export function languageServerWithClient(options: LanguageServerWithClientOptions) {
let plugin: LanguageServerPlugin | null = null;

return [
client.of(options.client || new LanguageServerClient({...options, autoClose: true})),
client.of(options.client),
documentUri.of(options.documentUri),
languageId.of(options.languageId),
ViewPlugin.define((view) => (plugin = new LanguageServerPlugin(view))),
Expand Down