Language server protocol

Do you consider turning feedforward into an LSP? This way people can make plugins for whatever editor they prefer (including vim, emacs, kak, etc), and benefit from the pattern-tracking logic which is pretty much done

4 Likes

Hi @ghales, I split this into a separate topic. I hadn't heard of LSP until now, looks super interesting:
https://langserver.org/

@lwlsn and I have been looking at getting better comms between tidal and atom with OSC, this looks like it could be a more general solution.. I wonder if it bakes in particular ways of working? If it's flexible enough to support that pattern tracking, that could be nice. Are you familiar with it?

1 Like

Hi! I have no experience at all coding LSPs, but I've used them in the past, and they work pretty well (though new and kind of experimental).
It works via JSON communication between editor and server: the editor sends text data and the server sends display information - it's great for stuff like linting, auto-completion, maybe intellisense. Maybe the server can schedule these messages, or something, to update the highlight according to patterns.
Notice that the editor is responsible for display, including theming - this means the pretty highlight blocks would only work if the user's theme includes them.

This is what I gather, might be wrong

ps. People don't have to make plugins (!) : there are already LSPs for all major editors, it seems

1 Like

One of my favourite features enabled by LSP in other languages is error checking as you type. I would certainly welcome checking for potential errors in a pattern before I evaluate it :slight_smile:

I'm not an expert (only an end-user leveraging LSP capabilities at my day job), but it seems that it should be possible to define custom extensions for the Tidal LSP server in case standard LSP protocol would not be sufficient. Such custom extensions would require some code on the client-side as well, which means writing some IDE plugin to be able to handle them. In case of sticking to the standard LSP messages, a lot of editors would support it out-of-the-box (as @ghales mentioned above).

Note: there seems to be some LSP server for Haskell, so perhaps we could leverage it for some use-cases: https://github.com/alanz/haskell-lsp

2 Likes

checking for potential errors in a pattern

Well. This requires a re-implementation (for the mini language) of several components of a (Haskell) compiler (parsing, name resolution, type checking). But some parts of this are already there?

For checking actual Haskell code (e.g., when working on Tidal source) I think the currently recommended package is GitHub - haskell/haskell-language-server: Official haskell ide support via language server (LSP). Successor of ghcide & haskell-ide-engine. . I have seen it work (in emacs and atom) for navigation (jump-to-definition, jump-to-error-location). That's already quite useful. Apparently it does not do refactoring (e.g., renaming) but that's next on the agenda.

1 Like

That's a great starting point!

Any further progress on this?

I would really like to use tidal cycles in helix, which does not support plugins but does support LSP.

The Haskell language sever supports plugins, which I think we could use to integrate support for error checking the custom pattern syntax, for starting the server, and for communicating with supercollider without to need write a plugin for each editor we wish to support.

My Haskell is really rusty but I’m willing to give this a shot!

1 Like

Hi @ludens, no progress as far as I know. I think the last time I looked, LSP/HLS doesn't support interpreting:

"The REPL model, based on the ephemeral evaluation of expressions that end up discarded and unrecorded, it's a massive waste of time." (from REPL Support · Issue #477 · haskell/haskell-language-server · GitHub)

They just don't see any point in wanting to use software while writing it. Pretty strange!

There does seem to be an eval plugin which is capable of running code, so it might be possible now? I don't know enough about the requirements of Tidal or what HLS currently supports to know for sure.

@ludens I switched to helix today and am also now trying to figure out if I can use it for tidal. I was thinking in lines of a / the LSP as well, but a simpler approach (albeit without the in-editor debugging) is perhaps via the fact that helix can send selections to a shell command. So, I'm now trying to figure out if there is a simple way of sending a block to a tidal REPL with a shell command. If so, it's a simple matter of making convenient keymaps.

Perhaps section 3.11. Running the interpreter in a separate process or 3.12. Running the interpreter on a different host in the GHCi docs can be of help?

1 Like

Great find @trespaul !! If that's possible, it's just a matter of spawning a process and sending commands to it. It's pretty much the same approach used everywhere (atom-tidal, vscode-tidal, nvim-tidal etc) so it's promising indeed

1 Like

Ok so I'm not familiar with tmux at all, but I gather from the vim-tidal readme that one can start a tmux session with tmux new-session -s tidal ghci -ghci-script Tidal.ghci but I have no clue how to send commands there... Should the editor also be started in the tmux session? I tried looking at the vim-tidal source code but I couldn't find anything useful in just a glance.

Update: So, one can run tmux send-keys -l (-l for literal characters, i.e., to not use named characters like "Enter") from anywhere, not just within the / a tmux session. But that command doesn't take pipe input, so you have to use xargs -0 tmux send-keys in helix (-0 because otherwise xargs thinks the arguments it gets are separated by whitespace). I haven't tried this with tidal yet but it seems like it should work.

Update 2: with the following in tidal_send.sh:

tmux send-keys ":{" "Enter"
xargs -0 tmux send-keys -l < /dev/stdin
tmux send-keys "Enter" ":}" "Enter"

you can pipe a selection to tidal with alt+| and entering ./tidal_send.sh as the command to pipe to. I'm sure there's a more compact way than the three lines above, but this works.

Update 3: In helix's config.toml:

[keys.normal]
"A-ret" = ":pipe-to ./tidal-send.sh"

(S-ret and C-ret are used by my terminal for other things, so I'm using alt...)

Update 4: To highlight .tidal files with Haskell syntax highlighting, add the following to .config/helix/languages.toml (I'm not sure all of these settings are necessary tbh, but this worked for me):

[[language]]
name = "tidal"
scope = "source.tidal"
roots = []
injection-regex = "tidal"
file-types = ["tidal"]
comment-token = "--"
indent = { tab-width = 2, unit = "  " }
grammar = "haskell"

and then link the Haskell highlight file into the user runtime directory:

ln -s /usr/lib/helix/runtime/queries/haskell/highlights.scm ~/.config/helix/runtime/queries/tidal

Update 5, a year later:

I use the kitty terminal, which is scriptable. So, as an alternative to tmux, I use the following keyboard shortcut:

:pipe-to kitten @ send-text --match-tab 'title:^tidal' --stdin ':{\n' && kitten @ send-text --match-tab 'title:^tidal' ':}\n'

to send the selected text to the kitty tab titled "tidal", preceded by ":{" and followed by ":}". This also means no helper script necessary.

kitty can also send to separate windows and all sorts of things. See Control kitty from scripts - kitty

LSP for tidal would be huge. I'm pretty stoked about the ZED editor (https://zed.dev/) which supports LSP. LSP LSP LSP!