0

I don't seem to be able to get a language server for JSON (not tried others) working in QtCreator.

I downloaded vscode-json-langageserver and set it up in QtCreator options... Language client:

*.json, 
startup behaviour: "Requires an Open File"
Executable: /usr/bin/node
Arguments: /home/netherda/node_modules/vscode-json-languageserver --node-ipc

I have set up MIME types, and tried various combinations. The capabilities field reports "Available after server was[sic] initialized" and I see no difference when I open a json file.

Any suggestions?

epsilon
  • 2,849
  • 16
  • 23

1 Answers1

0

I've never used that language server before but I've used the python one that Qt use in their example. I thought I'd take a look to see if it worked on my machine.

I'm using the following Qt Creator:

Qt Creator 4.10.0
Based on Qt 5.13.1 (GCC 5.3.1 20160406 (Red Hat 5.3.1-6), 64 bit)
Built on Sep 4 2019 04:47:37
From revision 9b7bab7d35

And I installed the plug in using these instructions:

npm install --save vscode-json-languageservice

The python service they use in the example communicates via stdio so I tried using that instead e.g.

*.json, 
startup behaviour: "Requires an Open File"
Executable: /usr/bin/node
Arguments: /home/netherda/node_modules/vscode-json-languageserver --stdio

When I tried this out locally it appeared to do something. Unfortunately the something was to produce an initialisation error:

Cannot read property 'provideFormatter' of undefined

I'm not sure whether the server or qt-creator is at fault (or a mix of both) but it appears to be caused by qt-creator not supplying any initialisation options and the server code assuming they'll be there.

I came up with a fix locally which is to check that initialisation options exist before trying to use them and that seems to work. I'm going to open up a pull request on vscode for a suggested fix.

In the mean time, if you see the same problem and are happy to use my idea, you can edit 2 lines of the connection.onInitialize((params) => callback inside jsonServerMain.(js/ts) (In the version my package manger supplied it's js but it appears to be changed to ts in the latest, the problem seems to exist whichever version you'd use though...)

e.g. on my machine it's: node_modules/vscode-json-languageserver/out/jsonServerMain.js

I changed:

dynamicFormatterRegistration = getClientCapability('textDocument.rangeFormatting.dynamicRegistration', false) && (typeof params.initializationOptions.provideFormatter !== 'boolean');

to be

dynamicFormatterRegistration = getClientCapability('textDocument.rangeFormatting.dynamicRegistration', false) && ((params.initializationOptions) && typeof params.initializationOptions.provideFormatter !== 'boolean');

and in the capabilities object def I changed:

documentRangeFormattingProvider: params.initializationOptions.provideFormatter === true

to be

documentRangeFormattingProvider: (params.initializationOptions) ? params.initializationOptions.provideFormatter === true : false

You can tell it's working if the capabilities field is populated. (Try changing the startup behaviour to "Always on" whilst testing):

Populated capabilities field

Chris B
  • 925
  • 4
  • 14
  • Thanks for taking the time to look at this. I followed your instruction, then opened a .json file. I get: `LanguageClient JSON: Error: Notification handler 'textDocument/didOpen' failed with message: Cannot read property 'create' of undefined LanguageClient JSON: Error: Notification handler 'textDocument/didChange' failed with message: Cannot read property 'update' of undefined LanguageClient JSON: Error: Notification handler 'textDocument/didChange' failed with message: Cannot read property 'update' of undefined` – David Netherwood Nov 14 '19 at 21:51
  • That's a new one for me. Did the capabilities field update in the menu? – Chris B Dec 02 '19 at 12:28