Message Boards Message Boards

Check out this great VS Code extension for Wolfram language

Hey guys, I've developed a great extension on VS Code to write Wolfram under Microsoft's Language Server Protocol (LSP). It includes diagnostics, auto-completion, hovering and usage hints, etc. More features are on the way. It is NOT for syntax highlight. It could be easily extended to other editors too. For details, please check out our GitHub.


Wolfram Language Server

License: MIT

WolframLanguageServerLogo

by kenkangxgwe and hxianglong

Wolfram Language Server (WLServer) is an implementation of the Microsoft's Language Server Protocol (LSP) for Wolfram Language. This server is implemented in Wolfram Language itself.

Our current goal is to provide the experience as good as the Mathematica FrontEnd with addition power from the editor.

We have provided the client-side code for VS Code in this repo, which is based on some slight modifications of [Microsoft's LSP example][4]. If you are using other tools supporting LSP, some slight modifications to the client would certainly work too.

Installation

  1. Wolfram Mathematica (11.2 or higher (1)).

  2. Download the server from its repository.

    git clone https://github.com/kenkangxgwe/lsp-wl.git
  1. Install the client. Currently, we provide the VS Code extension on [Visual Studio Marketplace: Wolfram Language Server][5]

Run the Server

Clients can start the server by running the init.wls file from Wolfram Mathematica executables

wolfram -script /path/to/lsp-wl/init.wls [args]
# or
wolframscript -f /path/to/lsp-wl/init.wls [args]

The posible arguments for the server are

  • --help, -h to print help information.
  • --socket=port to assign the port that the server connect to. (Default: 6536)
    Socket is the only channel that we currently support.
  • --log=level, -l level to specify the logging level of the server. (Levels: error, warn, info, debug. Default: info)
  • --test, -t to run the unit test for the server.

If you want to run the server from Mathematica you can use the following code.

initfile = "/path/to/lsp-wl/init.wls";
args = {};
Block[{$ScriptCommandLine = Prepend[args, initfile], Quit = Function[{}, Throw[Null]]},
    Catch[<< (initfile)]
];

This is a good way to see the results from the unit tests.

You may also specify the following initializationOptions.

  • "theme": "dark" | "light" For better typesetting of documentation, SVG images are used in the popup such as hovering. Since the background is transparent, this setting is to ensure the text in the images are actually visible. If you use dark themes, then choose dark (2). (Default: dark)

Features

  • Hover: Provide definitions for Wolfram functions and system variables, such as String and $Path.

  • Completion: The completion is triggered by the client automatically. Currently, Wolfram functions and system variables would be displayed.

  • Completion Resolve: Further information would be provided for the items in the list.

  • Diagnostics: Syntax error would be underlined. However, the specific syntax error is not supported at the moment.

This is an early release, so more features are on the way. Syntax highlight is NOT supported according to the design of LSP, but there are already some good enough extensions like [Wolfram Language][6].

Here is a full list of LSP features.

Contribute

Design Principles

  1. The files are located according to its context name. The init.wls is the entry script that parses the commandline arguments, loads packages and starts the server.

  2. We implemented an stateless server in WolframLanguageServer`Server` that will parse and handle the messages.

  3. WolframLanguageServer`DataType` is a simple type system that supports pattern test on every field of a class. The operations on the objects are designed to be immutable.

  4. WolframLanguageServer`Test`* stores the unit tests for some of the packages.

Todo list

It will be nice if you want to make a contribution to the following topic.

  • Our server-client communication only supports Socket with TCP protocol. We tried to use ZMQ_Stream protocol and SocketListen[] to enable concurrency, but it fails to send responses back to the client.

  • It will be helpful to implement a stdio channel, so that the Mathematica earlier than 11.2 will also be supported.

  • Since we embed SVG image in the markupContent[] and VS Code client cannot resize the popup window according to its size, it is hard to tweak the image width and the font size on every machine. We are looking for a better solution.

  • More editor clients are needed. You can feel free to open a repository and create a pull request to add the clients in README.md once your client is released.

  • A scanner/parser might be needed to extract tokens for future usage. We have investigated serveral implementations in other languages, but we are still prefer a wolfram language scanner/parser written in wolfram language and can be easily integrated into this project.

If you want to help us with this project, feel free to fork and create a pull request. Do not forget to add unit tests if possible.

Donations

If you really like this project, please donate to us! $5 (or equivalently?35). A cup of coffee would certainly brighten our day! Your donation would be the motivation for us to move forward, thanks in advance :-) .

Footnotes

(1) SocketListen[] is used for server-client communication, which is introduced since 11.2. We plan to support stdio for better compatibility.

(2) This reminds me of a joke making fun of Project Managers. A programmer, who cannot put up with endless unreasonable requests from his project manager, complained about one stupid task from him online, which is to automatically change the theme of the app with the conion, 'Add an option for users to choose when launching the app, what is the color of your cell phone protector. Then change the them accordingly.'

POSTED BY: Xianglong Hu
3 Replies

This is a quite useful extension if you want to use VS Code as an editor/simple IDE for Wolfram Language. The 0.1.0 version is so far usable from my Windows 10 machine.

  • The docked summary window is in .svg graphcis file format
  • Web Reference -> <Symbol> is clickale and redirects properly to online Wolfram Docu pages
  • The feature is only available for system symbols at this moment

test

  • Auto-completion and hints are displayed during type

autochint

The packages launches a ZeroMQ based socket channel to the wolfram kernel, according to the document, after you launch wolframscript runtime through cmd. The service use loopback call on port 6235. My machine properly displayed Mathematica/Wolfram daemon at:

  • Windows system tray
  • tasklist /SVC

ent

Once you close the VS code application, wolfram.exe and mathematica.exe daemons terminate automatically.

The server log has some error spew related to non-UTF-8 encoded Chinese charaters because of my OS language is zh-CHS. English OS users need no worry in general.

VS code users on this forum can try this extension and make some suggestions for improvements to the developers.

POSTED BY: Shenghui Yang
Posted 4 years ago

Very cool! Just started using VS Code and will definitely try this out.

POSTED BY: Jesse Dohmann
Posted 1 year ago

Does the plugin support Language Server Protocol's "semantic tokens" yet for better code highlighting?

Visual Studio Code support for semantic token providers is described here.

POSTED BY: Mma Usr
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract