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

added initial languageserver connection #17

Merged
merged 1 commit into from
Feb 9, 2021
Merged
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -384,6 +384,7 @@
"dependencies": {
"command-exists": "^1.2.9",
"escape-string-regexp": "^4.0.0",
"ste-signals": "^1.6.11"
"ste-signals": "^1.6.11",
"vscode-languageclient": "^7.0.0"
}
}
3 changes: 3 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
import * as vscode from 'vscode';
import { Cheatsheet } from './cheatsheet';
import { PreviewManager } from './previewManager';
import { activateLanguageServer } from './languageclient';
import { DEBUG } from './config';

// New launch object
@@ -92,6 +93,8 @@ export function activate(context: vscode.ExtensionContext): void {
},
});
}

activateLanguageServer(context);
}

// Called when extension is deactivated
81 changes: 81 additions & 0 deletions src/languageclient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import * as vscode from 'vscode';
import * as languageclient from 'vscode-languageclient/node';
import * as net from 'net';

let langclient: languageclient.LanguageClient;

export function activateLanguageServer(context: vscode.ExtensionContext): void {
const outputChannel: vscode.OutputChannel = vscode.window.createOutputChannel(
'OpenSCAD'
);
// The server is implemented in node
const connectionInfo = {
port: 23725, // 0x5cad
host: 'localhost',
};

const serverOptions = () => {
// Connect to language server via socket
const socket = net.connect(connectionInfo);
const result: languageclient.StreamInfo = {
writer: socket,
reader: socket,
};

outputChannel.appendLine(
'[client] Connecting to openscad on port ' + connectionInfo.port
);
console.log(
'Opening connection to ',
connectionInfo.host + ':' + connectionInfo.port
);

return Promise.resolve(result);
};

// Options to control the language client
const clientOptions: languageclient.LanguageClientOptions = {
documentSelector: [{ scheme: 'file', language: 'scad' }],
synchronize: {},
outputChannel,
outputChannelName: 'OpenSCAD',
revealOutputChannelOn: languageclient.RevealOutputChannelOn.Info,
};

// Create the language client and start the client.
langclient = new languageclient.LanguageClient(
'openscad-lsp',
'OpenSCAD Language Server',
serverOptions,
clientOptions
);
langclient.registerProposedFeatures();

// enable tracing (.Off, .Messages, Verbose)
langclient.trace = languageclient.Trace.Verbose;

// Start the client. This will also launch the server
const disposable = langclient.start();

// Push the disposable to the context's subscriptions so that the
// client can be deactivated on extension deactivation
context.subscriptions.push(disposable);

langclient.onReady().then(() => {
outputChannel.appendLine('[client] Connection has been established');

// Only register the commands when the connection has been established.
context.subscriptions.push(
vscode.commands.registerCommand('openscad.lsp.preview', () => {
const editor = vscode.window.activeTextEditor;
if (editor) {
langclient.sendRequest('$openscad/preview', {
uri: editor.document.uri.toString(),
});
}
})
);

vscode.commands.executeCommand('openscad.lsp.preview');
});
}