microsoft / qsharp

Azure Quantum Development Kit, including the Q# programming language, resource estimator, and Quantum Katas
https://microsoft.github.io/qsharp/
MIT License
365 stars 72 forks source link

Language service #396

Open minestarks opened 1 year ago

minestarks commented 1 year ago

This issue tracks the implementation of the language service and editor features for Q#. Editor features are things like autocomplete, go-to-definition and hover. The language service is the component that uses the compiler internals to keep track of the current state of the program, update the program in response to document updates, and answer questions about it in response to requests from the editor.

Components involved

  1. Language service: this is a Rust crate that contains the actual "smarts" of the language service, including the implementation of each individual feature such as completion, hover, etc.
  2. JS bindings: this is a trivial WASM wrapper over the language service to expose it as a JavaScript class. It follows a similar pattern to the existing WASM wrapper for the interpreter (the run() method etc)
  3. npm package: This is the same package used by the playground that incorporates the Q# compiler. A new interface will be added to provide access to the language service methods, with the option to host the language service in a separate web worker (just like the interpreter today).
  4. VS Code extension: Implementation of the VS Code extensibility APIs. These APIs generally map directly to language service methods.
  5. Playground: Similar to VS Code, the language service methods just need to be wired up to the Monaco editor extension points in the playground.
  6. Python bindings: Very similar to the WASM/JS bindings mentioned above - a trivial wrapper over the language server crate to expose it as a Python class. Also similar to the Python interpreter wrapper that currently exists today in the pip package.
  7. Python LSP server: (for JupyterLab support) A standalone LSP (Language Server Protocol) server implemented in Python. While Python may not be the most obvious choice for an LSP server implementation (Node.js libraries maintained by VS Code would have been a more natural fit), it was chosen to make the installation simple (user can just pip install the extension instead of having to separately install Node.js) for a JupyterLab user.
  8. Jupyterlab extension: (for JupyterLab support) Includes the LSP server module and registers it with JupyterLab.
%%{init: { "flowchart": { "nodeSpacing": 3 } } }%%
flowchart TD

subgraph JS
  playground[Playground]
  vscode[VS Code extension]
  website[Website]
  npm[npm package]
end

subgraph Python
  jupyterlab[JupyterLab extension]
  lsp[LSP server]
end

subgraph Rust
  wasm["JS bindings (WASM)"]
  pip[Python bindings]
  interpreter[Interpreter]
  ls[Language service]
  compiler[Compiler]
end

playground
vscode
website
jupyterlab

playground-->npm
vscode-->npm
website-->npm
jupyterlab-->lsp

npm-->wasm
lsp-->pip
wasm-->interpreter
wasm-->ls
pip-->ls

interpreter-->compiler
ls-->compiler

Implementation plan

This list shows the proposed order of implementation, based on complexity (simpler features like hover are prioritized above more complicated ones like completions) and feature priority (VS Code extension being the highest priority deliverable).

The links are to LSP documentation. Even if we don't implement a true LSP server, we'll follow the LSP spec while designing the interface as it closely resembles the extensibility model used by many editors (VS Code, Monaco, JupyterLab, etc).

minestarks commented 1 year ago

Draft PR wiring up basic implementation of P1 features in VS Code and the playground: #364

sezna commented 5 months ago

@minestarks -- is #1038 going to unblock LSP support, or are there other changes required?

Also, perhaps we could file some separate issues for some of these features and use this issue to track them?

minestarks commented 5 months ago

@minestarks -- is #1038 going to unblock LSP support, or are there other changes required?

@sezna well, we'd have to implement an LSP server... but if you're asking if anything in the core language service crate needs to change, I don't think so. I had a prototype for this a few months ago using the https://github.com/python-lsp/python-lsp-jsonrpc package which was working in JupyterLab. I think it's just a matter of wiring pieces together.

Also, perhaps we could file some separate issues for some of these features and use this issue to track them?

Yes this issue is quite big, I don't mind breaking off a few items. I didn't want to create issues for things that aren't even on our radar, but for near-term stuff (e.g. formatting) and for stuff we want a dedicated discussion thread for, it makes sense.