Skip to content

lsp: Language Server Protocol Support #142

Closed
Closed

Description

@cueckoo

Originally opened by @rudolph9 in cuelang/cue#142

Here is an example of a YAML file LSP https://github.com/redhat-developer/yaml-language-server

I've never created one so I can't say for certian how much work it would be but it would be great for integrations in to vim, emacs, VSCode, etc.

https://langserver.org/

Activity

  1. cueckoo commented on Jul 3, 2021

    @cueckoo
    CollaboratorAuthor

    Original reply by @rudolph9 in cuelang/cue#142 (comment)

    Somewhat related, looks like someone made a vim plugin https://github.com/jjo/vim-cue

  2. cueckoo commented on Jul 3, 2021

    @cueckoo
    CollaboratorAuthor

    Original reply by @jlongtine in cuelang/cue#142 (comment)

    Some other links as well:

    I was chatting with @mpvl about this a bit at KubeCon this week, and I think this could be highly valuable for CUE.

  3. cueckoo commented on Jul 3, 2021

    @cueckoo
    CollaboratorAuthor

    Original reply by @mpvl in cuelang/cue#142 (comment)

    To get a better idea of what this should look like: what kind of functionality and analysis would people want to see supported by a LSP implementation?

  4. cueckoo commented on Jul 3, 2021

    @cueckoo
    CollaboratorAuthor

    Original reply by @mxey in cuelang/cue#142 (comment)

    To get a better idea of what this should look like: what kind of functionality and analysis would people want to see supported by a LSP implementation?

    • Syntax checking/validation (basically, show errors without having to run cue eval first)
    • Symbols for completion, documentation and jumping to definition

    basically, I'd like the same editing experience for Kubernetes objects in CUE as you can get for YAML in VS Code

  5. cueckoo commented on Jul 3, 2021

    @cueckoo
    CollaboratorAuthor

    Original reply by @mpvl in cuelang/cue#142 (comment)

    To get a better idea of what this should look like: what kind of functionality and analysis would people want to see supported by a LSP implementation?

    • find all locations contributing to a current position
    • expand implied fields into current struct, to allow editing them as if it were a form
    • grey out or otherwise mark fields that could be removed with trim (highly important fields)
    • remove unchanged implied fields (implied fields could be marked with @Implied()
    • trim at cursor
    • hoist/relax (anti-unify) at cursor
  6. cueckoo commented on Jul 3, 2021

    @cueckoo
    CollaboratorAuthor

    Original reply by @rudolph9 in cuelang/cue#142 (comment)

    Long term goal, I'd like to build something like Barlin.

    This gets into some very meta concepts underpinning cue but theoretically a cue evaluator could be written in cue similar to the way Barlin utilizes a scheme interpreter implemented in MiniKanren which implemented scheme 🤔

  7. cueckoo commented on Jul 3, 2021

    @cueckoo
    CollaboratorAuthor

    Original reply by @zchee in cuelang/cue#142 (comment)

    I created packages that lsp for Go. (for my project, Neovim LSP plugin written in Go :D)
    It almost works IIRC. I'm using zchee/nvim-lsp (private).

    If we develop lsp for CUE written in Go, it might be helpful that package.

  8. cueckoo commented on Jul 3, 2021

    @cueckoo
    CollaboratorAuthor

    Original reply by @myitcv in cuelang/cue#142 (comment)

    We can likely reuse some of the work in golang.org/x/tools/internal/lsp/... to get us started.

    Probably worth starting with simple diagnostics. That will allow us to flesh out a basic skeleton for what the LSP server should look like (including how to handle modules, a la Go modules).

    AFAIK, syntax definitions fall outside the spec of LSP. I plan to implement a Vim plugin in Go using govim for AST driven syntax highlighting to try out the parser/AST.

  9. cueckoo commented on Jul 3, 2021

    @cueckoo
    CollaboratorAuthor

    Original reply by @metalmatze in cuelang/cue#142 (comment)

    I've talked to @mpvl at FOSDEM and it might be worth checking out the Prometheus Language Server @slrtbtfs created as part of his internship in our team. It reuses parts of gopls but in a more general purpose way. @slrtbtfs should have more details about why things needed to be changed, in case you have questions. That should already get this going a bit quicker :)

  10. cueckoo commented on Jul 3, 2021

    @cueckoo
    CollaboratorAuthor

    Original reply by @slrtbtfs in cuelang/cue#142 (comment)

    Hi,

    I've had a quick look at cue, and it should probably be possible to reuse parts of the PromQL language server for the cue language server.

    What is definitely reusable and turned out be very useful is the golang.org/x/tools/internal/lsp/proctocol package, which contains (almost) all the types defined in the LSP specification.

    What could be useful for you, too, is forking the github.com/prometheus-community/promql-langserver/langserver/cache package, removing all the PromQL stuff there and replacing the yaml parser with the cue parser. This package basically takes care of all the statekeeping in the language server.

    Then the only thing left to do is implementing the protocol.Server interface. You can start with leaving everything unimplemented and then add more and more language features over time.

    Feel free to reach out if you have any questions.

  11. cueckoo commented on Jul 3, 2021

    @cueckoo
    CollaboratorAuthor

    Original reply by @mikelnrd in cuelang/cue#142 (comment)

    Another more recent development related to language servers is The Language Server Index Format (LSIF).

    Basically the idea is to spit out the analysis to a JSON file. Sourcegraph (a code search tool) lets you upload the LSIF file and gives you language-server-like code navigation (but without the language server even running!).

    Perhaps cue could spit out the LSIF JSON data?

    I've just grep'ed for my notes on the topic and thought I'd share them here in case they are helpful. See below.

    Loving cue at the moment and the vscode-cue plugin. Can't wait for a language server!

    ..............................................................
    https://code.visualstudio.com/blogs/2019/02/19/lsif

    https://github.com/microsoft/lsif-node/blob/master/README.md
    https://github.com/Microsoft/language-server-protocol/blob/master/indexFormat/specification.md

    vscode extension (to serve LSP from the file)
    https://github.com/microsoft/lsif-node/blob/master/README.md#lsif-extension

    https://about.sourcegraph.com/blog/sourcegraph-3.9
    https://about.sourcegraph.com/blog/writing-an-lsif-indexer
    https://about.sourcegraph.com/blog/sourcegraph-3.8
    https://about.sourcegraph.com/blog/code-intelligence-with-lsif

    https://docs.sourcegraph.com/user/code_intelligence/lsif

    go get -u github.com/sourcegraph/lsif-go/cmd/lsif-go

    https://github.com/sourcegraph/lsif-go
    https://www.youtube.com/watch?v=fMIRKRj_A88
    https://youtu.be/fMIRKRj_A88?t=218

    https://github.com/microsoft/lsif-node/tree/master/tsc

    lsif-protocol: Protocol defined as TypeScript interfaces
    lsif-util: Utility tools for LSIF development
    lsif-tsc: LSIF indexer for TypeScript
    lsif-npm: Linker for NPM monikers

  12. cueckoo commented on Jul 3, 2021

    @cueckoo
    CollaboratorAuthor

    Original reply by @galli-leo in cuelang/cue#142 (comment)

    Hi,

    I semi-hacked together a working implementation (using promql as a base) over the past two days. You can see it running in action in vscode below :). Currently, I have the following working:

    • Diagnostics: parse errors, compile errors and even output of cue vet
    • Definition: navigate to definition (including multiple, see GIF)
    • Hovering: showing definition on hover + comments, works decentish, but could use some work
    • Completion: Extremely basic at the moment, would need a lot more work here

    (There might be one or two crashes though 😅 )

    Should I fork this project and add what I have up until know, then open a PR?
    Or should I just commit it to a personal repo, that's separate?

    Some thoughts I had during my implementation, most of these are probably my fault for not being familiar with the codebase though 😛 :

    • It is very annoying that compilation fails immediately, if there is any error (or maybe I haven't found the correct options yet?). At the moment I need to keep the last Instance of a document around, otherwise I only have the AST. Seems to be possible by using build.Instance instead. However, if a top level identifier is unknown, inst.Value() will still return nothing, so would need to access some internal stuff.
    • Navigating the AST and Instance is very annoying. I wrote my own Cursor for the AST, but nothing yet for Instance.
    • Matching between something in the AST and a corresponding Value is even more annoying, that code is very messy and should probably be reworked. Currently, I iterate through all Values in the Instance and check if val.Source() == node (node is an ast.Node).
    • There doesn't seem to be a way to get a Value corresponding to a field definition, which made some stuff a lot harder.
    • Mapping between source locations is also not trivial and seems to be the cause of one of the crashes.
    • There doesn't seem to be a distinction between an ast.Ident when used e.g. a: b (i.e. one is field definition label, the other field definition type?). Ideally, there was a way to know which one is which, since certain stuff (like auto complete or hover), have different info based on that (at least the way I implemented it at the moment).

    Lastly, I wanted to say, that I really love this project! I think it's really cool and we plan on using it in our new infrastructure for all config management (hence why I decided to spend some time implementing this :)).

    vscode

  13. 187 remaining items

  14. moved this from Backlog to Planned in CUE LSP Roadmapon Feb 14, 2024
  15. moved this from Planned to In progress in CUE LSP Roadmapon Feb 14, 2024
  16. changed the title [-]x/tools: Language Server Protocol Support[/-] [+]lsp: Language Server Protocol Support[/+] on Nov 14, 2024
  17. cuematthew commented on Sep 3, 2025

    @cuematthew
    Contributor

    For everyone waiting on this, there is now good progress on CUE's LSP server, and regular announcements will be added to #1776 going forwards.

  18. mvdan commented on Dec 6, 2025

    @mvdan
    Member

    With cue lsp shipping in v0.15.0 with many features, I believe we can close this as resolved now; we will continue to post updates and announcements relating to the LSP via #1776 and the #lsp channels on Discord and Slack :)

    Please send your thanks to @cuematthew, who has done the majority of the work in the LSP!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions