Skip to content

Commit

Permalink
Merge pull request #17 from Tomatower/languageserver
Browse files Browse the repository at this point in the history
Added initial languageserver connection
  • Loading branch information
Antyos authored Feb 9, 2021
2 parents 8590af5 + a40d7af commit 8fcf6c1
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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
Expand Down Expand Up @@ -92,6 +93,8 @@ export function activate(context: vscode.ExtensionContext): void {
},
});
}

activateLanguageServer(context);
}

// Called when extension is deactivated
Expand Down
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');
});
}

0 comments on commit 8fcf6c1

Please sign in to comment.