Skip to content

Commit 1c76e96

Browse files
Initial bits for integrating the LSP API.
1 parent f853458 commit 1c76e96

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,71 @@ The low-level API is as follows:
204204

205205
For examples how to use them, please refer to the README of the above mentioned solc-js releases.
206206

207+
#### Language Server Mode
208+
209+
Since version 0.8.11, the solidity compiler natively supports the
210+
language server protocol. With solc-js, you can now use it as follows:
211+
212+
```javascript
213+
var solc = require('solc');
214+
215+
// Callback to be invoked when additional files have to be opened during
216+
// source code analysis stage.
217+
//
218+
// This function behaves similar to the compilation file reader callback.
219+
function fileReadCallback(path)
220+
{
221+
if ('path' === 'file:///project/lib.sol') {
222+
return {
223+
contents: 'library L { function f() internal returns (uint) { return 7; } }';
224+
};
225+
}
226+
else
227+
return { error: 'File not found' };
228+
}
229+
230+
// Put solcjs into LSP mode.
231+
// Needs to be called only once before the actual LSP I/O calls.
232+
solc.lspStart(fileReadCallback);
233+
234+
// Send some LSP JSON-RPC message and optionally receive a reply.
235+
var lspInitializationMessage = {
236+
'jsonrpc': '2.0',
237+
'method': 'initialize',
238+
'params': {
239+
'rootUri': 'file:///project/',
240+
'capabilities': {
241+
'textDocument': {
242+
'publishDiagnostics': {'relatedInformation': true}
243+
},
244+
'workspace': {
245+
'applyEdit': true,
246+
'configuration': true,
247+
'didChangeConfiguration': {'dynamicRegistration': true},
248+
'workspaceEdit': {'documentChanges': true},
249+
'workspaceFolders': true
250+
}
251+
}
252+
}
253+
};
254+
solc.lspSendReceive(JSON.stringify(lspInitializationMessage)));
255+
solc.lspSendReceive(JSON.stringify({'jsonrpc': '2.0', 'method': 'initialized'}));
256+
257+
// Now, with the LSP server, being set up the following
258+
// can be called as often as needed.
259+
function lspRoundtrip(jsonRpcInputObject)
260+
{
261+
return JSON.parse(solc.lspSendReceive(JSON.stringify(jsonRpcInputObject)));
262+
}
263+
```
264+
265+
This is a low level API endpoint for use by language server clients,
266+
such as Visual Studio Code, or any other editor.
267+
In order to know what you can pass in and what can come out,
268+
it is highly recommended to have a look at:
269+
270+
https://microsoft.github.io/language-server-protocol/specification
271+
207272
### Using with Electron
208273

209274
**Note:**

wrapper.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,25 @@ function setupMethods (soljson) {
9292
};
9393
};
9494

95+
let lspStart = null;
96+
if ('_solidity_lsp_start' in soljson) {
97+
const wrappedLspStart = soljson.cwrap('solidity_lsp_start', 'int', []);
98+
lspStart = function (callbacks) {
99+
return runWithCallbacks(callbacks, wrappedLspStart, []);
100+
};
101+
}
102+
103+
let lspSendReceive = null;
104+
if ('_solidity_lsp_send_receive' in soljson) {
105+
const wrappedLspSendReceive = soljson.cwrap('solidity_lsp_send_receive', 'string', ['string']);
106+
lspSendReceive = function (input: String, callbacks) {
107+
// We are reusing the `runWithCallbacks` function that was supposed to
108+
// only receive _solidity_compile as second parameter.
109+
// I may be wrong, but that should work analogous.
110+
return runWithCallbacks(callbacks, wrappedLspSendReceive, [input]);
111+
};
112+
}
113+
95114
// This calls compile() with args || cb
96115
const runWithCallbacks = function (callbacks, compile, args) {
97116
if (callbacks) {
@@ -324,6 +343,8 @@ function setupMethods (soljson) {
324343
importCallback: compileJSONCallback !== null || compileStandard !== null,
325344
nativeStandardJSON: compileStandard !== null
326345
},
346+
lspStart: lspStart,
347+
lspSendReceive: lspSendReceive,
327348
compile: compileStandardWrapper,
328349
// Loads the compiler of the given version from the github repository
329350
// instead of from the local filesystem.

0 commit comments

Comments
 (0)