Skip to content

Commit 9e4af31

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

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,51 @@ 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 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+
solc.lspStart(fileReadCallback);
232+
233+
// Send some LSP JSON-RPC message and optionally receive a reply.
234+
var lspMessage = {
235+
'rootUri': 'file:///project/',
236+
'capabilities': {
237+
'textDocument': {
238+
'publishDiagnostics': {'relatedInformation': true}
239+
},
240+
'workspace': {
241+
'applyEdit': true,
242+
'configuration': true,
243+
'didChangeConfiguration': {'dynamicRegistration': true},
244+
'workspaceEdit': {'documentChanges': true},
245+
'workspaceFolders': true
246+
}
247+
}
248+
};
249+
var lspReply = JSON.parse(solc.lspSendReceive(JSON.stringify(lspMessage)));
250+
```
251+
207252
### Using with Electron
208253

209254
**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+
let 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+
let 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)