Giter Club home page Giter Club logo

lspserver's Introduction

LSPServer

LSPServer is a package that implements the Language Server Protocol for Wolfram Language and allows a Wolfram Language kernel to run as an LSP server.

Developing Wolfram Language Code in Other Editors and IDEs with LSP from WTC 2021: Watch Video (youtube)

LSPServer implements several LSP features:

  • Code diagnostics
  • Suggestions for fixes
  • Formatting files and selections
  • Semantic highlighting
  • Expand / shrink selection
  • Outline
  • Color swatches
  • Symbol references
  • Documentation on hover

This repo is for users who are interested in adding LSP support for Wolfram Language to LSP clients.

There are official Wolfram LSP clients for Sublime Text and Visual Studio Code.

Setup

LSPServer depends on CodeParser paclet, CodeInspector paclet, and CodeFormatter paclet.

LSPServer and its dependencies are included in Mathematica 13.0 and above.

Install LSPServer paclet and dependencies from the public paclet server:

PacletInstall["CodeParser"]
PacletInstall["CodeInspector"]
PacletInstall["CodeFormatter"]
PacletInstall["LSPServer"]

Build and install the LSPServer paclet locally

Using LSPServer

99% of users will not need to worry about using LSPServer directly. LSPServer is used internally when an LSP client launches a Wolfram kernel as an LSP server. This all happens in the background.

But it can be useful to run LSPServer when developing a new LSP client.

Create a file named server.wl:

Needs["LSPServer`"]

StartServer[]

And run from the command-line:

brenton@brenton2maclap % WolframKernel -noprompt -run Get\[\"server.wl\"\]
14:03:48.605 $CommandLine: {WolframKernel, -noprompt, -run, Get["server.wl"]}
14:03:48.607 


14:03:48.608 $commProcess: StdIO
14:03:48.608 


14:03:48.608 $ProcessID: 54603
14:03:48.609 


14:03:48.609 $ParentProcessID: 54582
14:03:48.609 


14:03:48.609 Starting server... (If this is the last line you see, then StartServer[] may have been called in an unexpected way and the server is hanging.)
14:03:48.610 

Notice the proper character escapes on the command-line.

The kernel process is blocked waiting on input to its stdin.

Properly formed LSP JSON-RPC can be sent to the kernel, and the kernel would send its response to stdout.

Troubleshooting

Make sure that the paclets can be found on your system:

Needs["LSPServer`"]

You may get LibraryFunction messages:

14:49:15.663 $CommandLine: {/Applications/Mathematica.app/Contents/MacOS/WolframKernel, -noinit, -noprompt, -nopaclet, -nostartuppaclets, -noicon, -run, Needs["LSPServer`"];LSPServer`StartServer["/Users/user/logs/"]}
14:49:15.664 


14:49:15.664 $commProcess: StdIO
14:49:15.664 


14:49:15.665 $ProcessID: 22400
14:49:15.665 


14:49:15.666 $ParentProcessID: 22394
14:49:15.666 


14:49:15.667 Directory[]: /private/var/folders/90/4fbnjdqx3f791xb65c02fm2m000bfy/T/Wolfram-LSPServer
14:49:15.667 


14:49:15.668 Starting server... (If this is the last line you see, then StartServer[] may have been called in an unexpected way and the server is hanging.)
14:49:15.668 



LibraryFunction::version: The version number 7 of the library is not consistent with the current or any previous WolframLibraryVersion.

LibraryFunction::initerr: A nonzero error code 7 was returned during the initialization of the library /Users/user/Library/Mathematica/Paclets/Repository/LSPServer-1.6/LibraryResources/MacOSX-x86-64/LSPServer.dylib.

LibraryFunction::libload: The function GetStartupError_LibraryLink was not loaded from the file /Users/user/Library/Mathematica/Paclets/Repository/LSPServer-1.6/LibraryResources/MacOSX-x86-64/LSPServer.dylib.
14:49:16.129 


14:49:16.129 Initialization failed: Failure["LibraryFunctionLoad", <|"Result" -> $Failed|>]
14:49:16.484 


14:49:16.485 Language Server kernel did not shutdown properly.
14:49:16.485 
14:49:16.487 This is the command that was used:
14:49:16.488 {/Applications/Mathematica.app/Contents/MacOS/WolframKernel, -noinit, -noprompt, -nopaclet, -nostartuppaclets, -noicon, -run, Needs["LSPServer`"];LSPServer`StartServer["/Users/user/logs/"]}
14:49:16.488 
14:49:16.489 To help diagnose the problem, run this in a notebook:
             Needs["LSPServer`"]
             LSPServer`RunServerDiagnostic[{"/Applications/Mathematica.app/Contents/MacOS/WolframKernel", "-noinit", "-noprompt", "-nopaclet", "-nostartuppaclets", "-noicon", "-run", "Needs[\"LSPServer`\"];LSPServer`StartServer[\"/Users/user/logs/\"]"}]
14:49:16.490 
14:49:16.490 Fix any problems then restart and try again.
14:49:16.491 


14:49:16.492 KERNEL IS EXITING HARD
14:49:16.492 

This means that LSPServer was built with a newer version of Wolfram System than your system supports.

To fix this, build LSPServer from source with the version of Wolfram System that you will use.

Server settings

Turn on debug logging from the kernel.

Give a string argument to StartServer[]. This is a directory that kernel logs will be written to.

Needs["LSPServer`"];LSPServer`StartServer["/path/to/log/directory/"]

lspserver's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

lspserver's Issues

Setup with Jupyter LSP

Hello,
I have tried to set this up to work in Jupyter Lab (using WolframLanguageForJupyter) with @krassowski/jupyterlab-lsp as follows:
Using macOS Big Sur 11.6.3, system python 3.8, and Jupyter core packages:
jupyter_client : 6.1.12
jupyter_core : 4.9.0
jupyter_server : 1.11.2
jupyterlab : 3.2.8
nbclient : 0.5.4
Installed Wolfram Engine 13 in /Applications
Cloned via 'git clone https://github.com/WolframResearch/WolframLanguageForJupyter.git' and
Built WolframLanguageForJupyter-0.9.2.paclet via ./configure-jupyter.wls build
Ran PacletInstall["WolframLanguageForJupyter-0.9.2.paclet"]
And ConfigureJupyter["Add", "JupyterInstallation" -> "/Users/ikasou/Library/Python/3.8/bin/jupyter"]
So far so good, notebooks in Jupyter work like a charm!

Screenshot 2022-02-01 at 11 22 48

For the LSP setup, I am using

  "LanguageServerManager": {
    "language_servers": {
      "wolfram-lspserver": {
        "version": 2,
        "argv": [
		      "/Applications/Wolfram Engine.app/Contents/MacOS/WolframKernel",
		      "-noinit",
          	      "-noprompt",
		      "-nopaclet",
		      "-noicon",
		      "-nostartuppaclets",
		      "-run",
		      "Needs[\"LSPServer`\"];LSPServer`StartServer[\"/Users/ikasou/Library/Logs/WolframLSP\"]"
	],
        "languages": ["vnd.wolfram.m", "Wolfram Language"],
        "mime_types": ["text/x-vnd.wolfram.m", "application/x-vnd.wolfram.m"],
        "display_name": "Wolfram Language"
      }
    }
  }
}

The system seems to think that the Server initialises properly:

Screenshot 2022-02-01 at 11 19 55

However looking at the logs I see that the server crashes immediately as soon as it starts:
kernelLog-2022-02-01_10-54-51.txt

The key lines seem to be:

10:54:52.182 Starting server... (If this is the last line you see, then StartServer[] may have been called in an unexpected way and the server is hanging.)
10:54:52.182 



KeyExistsQ::invrl: The argument Null is not a valid Association or a list of rules.

KeyExistsQ::invrl: The argument Null is not a valid Association or a list of rules.

KeyExistsQ::invrl: The argument Null is not a valid Association or a list of rules.

KeyExistsQ::invrl: The argument Null is not a valid Association or a list of rules.

KeyExistsQ::invrl: The argument Missing[KeyAbsent, codeAction] is not a valid Association or a list of rules.
10:54:53.169 


10:54:53.169 invalid contents result (should match {_?AssociationQ ...}): handleContent[<|jsonrpc -> 2.0, method -> workspace/didChangeConfiguration, params -> <|settings -> <||>|>|>]
10:54:53.169 

10:54:53.170 Language Server kernel did not shutdown properly.

So it appears it is not getting the format it is expecting from jupyter-lsp somehow. Looking at the Jupyter logs I see (I have removed irrelevant lines):

[I 2022-02-01 10:54:32.883 ServerApp] jupyter_lsp | extension was successfully linked.
[I 2022-02-01 10:54:32.891 ServerApp] jupyter_server_mathjax | extension was successfully linked.
[I 2022-02-01 10:54:32.905 ServerApp] jupyterlab | extension was successfully linked.
[I 2022-02-01 10:54:32.905 ServerApp] jupyterlab_git | extension was successfully linked.
[I 2022-02-01 10:54:33.135 ServerApp] nbclassic | extension was successfully linked.
[I 2022-02-01 10:54:33.135 ServerApp] nbdime | extension was successfully linked.
[I 2022-02-01 10:54:33.202 ServerApp] nbclassic | extension was successfully loaded.
[I 2022-02-01 10:54:33.452 ServerApp] Skipped non-installed server(s): bash-language-server, dockerfile-language-server-nodejs, javascript-typescript-langserver, julia-language-server, pyright, sql-language-server, texlab, typescript-language-server, unified-language-server, vscode-css-languageserver-bin, vscode-html-languageserver-bin, vscode-json-languageserver-bin, yaml-language-server
[I 2022-02-01 10:54:33.616 ServerApp] jupyter_lsp | extension was successfully loaded.
[I 2022-02-01 10:54:33.617 ServerApp] jupyter_server_mathjax | extension was successfully loaded.
[I 2022-02-01 10:54:33.619 LabApp] JupyterLab extension loaded from /Users/ikasou/Library/Python/3.8/lib/python/site-packages/jupyterlab
[I 2022-02-01 10:54:33.619 LabApp] JupyterLab application directory is /Users/ikasou/Library/Python/3.8/share/jupyter/lab
[I 2022-02-01 10:54:33.622 ServerApp] jupyterlab | extension was successfully loaded.
[I 2022-02-01 10:54:33.627 ServerApp] jupyterlab_git | extension was successfully loaded.
[I 2022-02-01 10:54:33.705 ServerApp] nbdime | extension was successfully loaded.
[I 2022-02-01 10:54:33.707 ServerApp] Serving notebooks from local directory: /Users/ikasou/Documents
[I 2022-02-01 10:54:33.707 ServerApp] Jupyter Server 1.11.2 is running at:
[I 2022-02-01 10:54:38.830 LabApp] Build is up to date
[W 2022-02-01 10:54:40.579 ServerApp] Notebook Untitled.ipynb is not trusted
[I 2022-02-01 10:54:41.185 ServerApp] Kernel started: a397aea0-fdec-4e24-9142-4acc13b5c592
[I 2022-02-01 10:54:41.215 ServerApp] Kernel started: bf112415-60cd-4aab-aeda-7442fe93f56f
[I 2022-02-01 10:54:48.141 ServerApp] Adapting from protocol version 5.3.0 (kernel bf112415-60cd-4aab-aeda-7442fe93f56f) to 5.3 (client).
10:54:52.178 $CommandLine: {/Applications/Wolfram Engine.app/Contents/MacOS/WolframKernel, -noinit, -noprompt, -nopaclet, -noicon, -nostartuppaclets, -run, Needs["LSPServer`"];LSPServer`StartServer["/Users/ikasou/Library/Logs/WolframLSP"]}
10:54:52.179 


10:54:52.179 $commProcess: StdIO
10:54:52.180 


10:54:52.180 $ProcessID: 3724
10:54:52.180 


10:54:52.181 $ParentProcessID: 3687
10:54:52.181 


10:54:52.182 Starting server... (If this is the last line you see, then StartServer[] may have been called in an unexpected way and the server is hanging.)
10:54:52.182 



KeyExistsQ::invrl: The argument Null is not a valid Association or a list of rules.

KeyExistsQ::invrl: The argument Null is not a valid Association or a list of rules.

KeyExistsQ::invrl: The argument Null is not a valid Association or a list of rules.

KeyExistsQ::invrl: The argument Null is not a valid Association or a list of rules.

KeyExistsQ::invrl: The argument Missing[KeyAbsent, codeAction] is not a valid Association or a list of rules.
10:54:53.169 


10:54:53.169 invalid contents result (should match {_?AssociationQ ...}): handleContent[<|jsonrpc -> 2.0, method -> workspace/didChangeConfiguration, params -> <|settings -> <||>|>|>]
10:54:53.169 


10:54:53.170 Language Server kernel did not shutdown properly.
10:54:53.171 
10:54:53.172 This is the command that was used:
10:54:53.172 {/Applications/Wolfram Engine.app/Contents/MacOS/WolframKernel, -noinit, -noprompt, -nopaclet, -noicon, -nostartuppaclets, -run, Needs["LSPServer`"];LSPServer`StartServer["/Users/ikasou/Library/Logs/WolframLSP"]}
10:54:53.173 
10:54:53.173 To help diagnose the problem, run this in a notebook:
             Needs["LSPServer`"]
             LSPServer`RunServerDiagnostic[{"/Applications/Wolfram Engine.app/Contents/MacOS/WolframKernel", "-noinit", "-noprompt", "-nopaclet", "-noicon", "-nostartuppaclets", "-run", "Needs[\"LSPServer`\"];LSPServer`StartServer[\"/Users/ikasou/Library/Logs/WolframLSP\"]"}]
10:54:53.174 
10:54:53.174 Fix any problems then restart and try again.
10:54:53.175 


10:54:53.175 KERNEL IS EXITING HARD
10:54:53.176 


[E 2022-02-01 10:56:40.808 ServerApp] <LspStdIoWriter(parent=<LanguageServerSession(language_server=wolfram-lspserver, argv=['/Applications/Wolfram Engine.app/Contents/MacOS/WolframKernel', '-noinit', '-noprompt', '-nopaclet', '-noicon', '-nostartuppaclets', '-run', 'Needs["LSPServer`"];LSPServer`StartServer["/Users/ikasou/Library/Logs/WolframLSP"]'])>)> couldn't write message: Content-Length: 227
    
    {"jsonrpc":"2.0","method":"textDocument/didSave","params":{"textDocument":{"uri":"file:///Users/ikasou/Documents/.virtual_documents/Python/BerkeleyDRL_fall2021/hw1/cs285/Untitled1.ipynb","version":2},"text":"Range[3]\n\n\n\n"}}
    Traceback (most recent call last):
      File "/Users/ikasou/Library/Python/3.8/lib/python/site-packages/jupyter_lsp/stdio.py", line 193, in write
        await convert_yielded(self._write_one(response.encode("utf-8")))
      File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/concurrent/futures/thread.py", line 57, in run
        result = self.fn(*self.args, **self.kwargs)
      File "/Users/ikasou/Library/Python/3.8/lib/python/site-packages/jupyter_lsp/stdio.py", line 201, in _write_one
        self.stream.write(message)
    BrokenPipeError: [Errno 32] Broken pipe
[I 2022-02-01 11:21:50.158 ServerApp] Starting buffering for a397aea0-fdec-4e24-9142-4acc13b5c592:76e701b9-6579-457d-a533-e0a39947d0f2
[E 2022-02-01 11:21:59.101 ServerApp] <LspStdIoWriter(parent=<LanguageServerSession(language_server=wolfram-lspserver, argv=['/Applications/Wolfram Engine.app/Contents/MacOS/WolframKernel', '-noinit', '-noprompt', '-nopaclet', '-noicon', '-nostartuppaclets', '-run', 'Needs["LSPServer`"];LSPServer`StartServer["/Users/ikasou/Library/Logs/WolframLSP"]'])>)> couldn't write message: Content-Length: 230
    
    {"jsonrpc":"2.0","id":1,"method":"textDocument/hover","params":{"textDocument":{"uri":"file:///Users/ikasou/Documents/.virtual_documents/Python/BerkeleyDRL_fall2021/hw1/cs285/Untitled1.ipynb"},"position":{"line":0,"character":4}}}
    Traceback (most recent call last):
      File "/Users/ikasou/Library/Python/3.8/lib/python/site-packages/jupyter_lsp/stdio.py", line 193, in write
        await convert_yielded(self._write_one(response.encode("utf-8")))
      File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/concurrent/futures/thread.py", line 57, in run
        result = self.fn(*self.args, **self.kwargs)
      File "/Users/ikasou/Library/Python/3.8/lib/python/site-packages/jupyter_lsp/stdio.py", line 201, in _write_one
        self.stream.write(message)
    BrokenPipeError: [Errno 32] Broken pipe

I can even run the diagnostics suggestion from the error message in the notebook:

Needs["LSPServer`"]
LSPServer`RunServerDiagnostic[{"/Applications/Wolfram Engine.app/Contents/MacOS/WolframKernel", "-noinit", "-noprompt", "-nopaclet", "-noicon", "-nostartuppaclets", "-run", "Needs[\"LSPServer`\"];LSPServer`StartServer[\"/Users/ikasou/Library/Logs/WolframLSP\"]"}]

But it doesn't shed any new light, besides some versioning info:

Running Language Server diagnostic...

Kernel that is running RunServerDiagnostic[] ($CommandLine[[1]]): 
 
>   /Applications/Wolfram Engine.app/Contents/Resources/Wolfram\
 
>    Player.app/Contents/MacOS/WolframKernel
Kernel that RunServerDiagnostic[] will start (RunServerDiagnostic[{kernel,\
 
>    ...}]): /Applications/Wolfram Engine.app/Contents/MacOS/WolframKernel
WARNING: RunServerDiagnostic[] should be run with same kernel that\
 
>   RunServerDiagnostic[] will start.

Starting Language Server kernel with command: 
 
>   {/Applications/Wolfram Engine.app/Contents/MacOS/WolframKernel, -noinit, 
 
>    -noprompt, -nopaclet, -noicon, -nostartuppaclets, -run, 
 
>    Needs["LSPServer`"];LSPServer`StartServer["/Users/ikasou/Library/Logs/Wo\
 
>     lframLSP"]}

If any messages are printed below, they must be fixed.

Writing initialize...
initialize was successful.
Writing diagnostics...
INFORMATION: Kernel version: 13.
INFORMATION: LSPServer version: 1.4
INFORMATION: CodeParser version: 1.4
INFORMATION: CodeInspector version: 1.4
INFORMATION: CodeFormatter version: 1.4
INFORMATION: LSPServer build date: Thu 09 Dec 2021 04:58:36
INFORMATION: CodeParser build date: Wed 08 Dec 2021 21:05:06
INFORMATION: CodeInspector build date: Wed 08 Dec 2021 18:22:29
INFORMATION: CodeFormatter build date: Wed 08 Dec 2021 21:19:45
diagnostics was successful.
Writing shutdown...
shutdown was successful.
Writing exit...
exit was successful.
INFO: Time to initialize server: 4.87181 seconds
No problems found.

Screenshot 2022-02-01 at 11 36 11

I am more than happy to send any more logs that may be necessary, e.g. --debug level logs from Jupyter so we can see all the <LspStdioWriter()> message exchanges; however from memory they don't seem to shed much light as all json-rpc info is mostly opaque I think.

I appreciate this is work in progress but just thought I might give this a try anyway.

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.