vscode-ocaml-platform icon indicating copy to clipboard operation
vscode-ocaml-platform copied to clipboard

More REPL features

Open d4hines opened this issue 4 years ago • 2 comments

Borrowing from https://github.com/BetterThanTomorrow/calva, it would be nice to have more REPL features. There are many great commands there, but the two most important for OCaml I believe are:

  • Evaluate current file
  • Evaluate current form
  • Evaluate current top-level-form

The first is fairly self explanatory and maps directly to "#use "path/to/current/file". The second two are slightly more tricky in OCaml than in Clojure. I think the thing to do for the for "Evaluate current form" is to "grow" the selection upwards until you reach the nearest let node in the AST and send all that to the REPL. For "Evaluate current top-level-form", I think it should grow until it reaches a "root" let node in that current module. Some examples, using ||| to indicate the position of the cursor:

Evaluate current form

let foo = ||| "bar";
(* sends: let foo = "bar"; *)

module Foo = struct
let foo x =
  let bar = |||"bam" in
  bar
(* sends: let bar = "bam" *)
end

Evaluate current top-level form

let foo = ||| "bar";
(* sends: let foo = "bar"; *)

module Foo = struct
let foo x =
  let bar = |||"bam" in
  bar
(* sends: 
let foo x =
  let bar = |||"bam" in
  bar
*)
end

Does ocamllsp make it easy to find parent nodes of the current node in the AST?

d4hines avatar Feb 26 '21 21:02 d4hines

This would be great to have, but would likely be a good amount of work to implement. I don't think Ocaml-LSP or Merlin makes it easy to traverse the AST in the way we need. Emacs' Tuareg-mode has a syntactic way of doing it, and it might be worth looking at. But just sending the file would be useful.

basus avatar Jun 18 '21 20:06 basus

Does ocamllsp make it easy to find parent nodes of the current node in the AST?

It depends what you mean by parent. But merlin already has a way to handle partial sources. I suggest we just use this mechanism to complete a valid ast from the partial one truncated at |||.

In reality, implementing this feature isn't very hard - at least if we settle on sending only complete toplevel bindings or even the whole file. We've certainly implemented much more complex features. It's just that I personally never use the repl myself, so it's hard to get me excited about this.

By the way, the main complexity in implementing this feature is setting up a module specific build environment for building this repl. This requires a certain amount of understanding of how dune's OCaml rules work to tackle.

rgrinberg avatar Jun 19 '21 01:06 rgrinberg