Improvements to OCaml code editing: the basics of a refactor engine

43 nukifw 11 8/20/2025, 1:37:15 PM tarides.com ↗

Comments (11)

mhitza · 1h ago
Extract expression is such a common refactoring, happy to see support for it. I'm even more curious to see now if there are going to be more advanced refactoring possible. A List.map, to for loop, and back refactoring would be such a great thing to have in how I program.

Only thing that would go on my OCD nerve, is the lack of an empty newline when the show_markup function is extracted. Kind of "common sense" when writing top level bindings to leave some breathing room between them.

nukifw · 55m ago
For the first point, you are right. We start by a common (but very useful) feature. Since OCaml allows an infinite level of nesting (and different kind of structure item) it was still a bit of a challenge, mostly for finding the right UX.

For the second point we delay the aeration convention to the formatter (ocamlformat). It can be configure in a different way :)

Thanks for your feedback!

panglesd · 2h ago
Very cool!

Does it replace identical expressions in the same scope? Like:

    let tau = 3.14 +. 3.14
becomes

    let pi = 3.14
    let tau = pi +. pi
?

EDIT: Or even crazier with function:

    let _ = (x + 1) + (y + 1)
becomes

    let plus_one a = a + 1
    let _ = (plus_one x) + (plus_one y)

(I ask this just out of curiosity. Even the "simpler" version is very impressive!)
nukifw · 2h ago
Nop, for the moment, we try to not "infer user usages"! But if you extract an expression with variable, there will be, obviously, not be repeated:

    let tau =
      let pi = 3.14 in 
      pi + pi
if we extract `pi + pi` it will lead (if you do not give any concrete name) to the following code:

    let fun_name1 pi = 
       pi + pi

    let tau = 
       let pi = 3.14 in 
       fun_name1 pi
lemonwaterlime · 1h ago
I’ve been working on similar refactoring and grepping utility functions for vim, though mine are meant to work in any language.

The most recent one I’ve made runs 'git grep' on the word under cursor or on the visual selection and puts everything into a quick fix list.

Since it is generic, it works on any phrases as well, and helps me find prose snippets and phrases in docs or other writings.

amelius · 31m ago
Anyone tried large codebase refactorings using AI?
shortrounddev2 · 54m ago
I feel that Ocaml could really use better VSCode integration more than literally anything else, but the community seems completely oriented around emacs
nukifw · 47m ago
We try to invest equal time between the OCaml platform (on VSCode) and OCaml-eglot (for Emacs). In fact, I find the VSCode extension to be rich (and generally iso-features with Emacs mode).

However, I (as maintainer of OCaml-eglot) find Emacs easier to extend (the VSCode extension is surprisingly complex when you stray from the beaten path), which seemed perfect for incubating an experimental feature. Furthermore, as mentioned at the end of the article, the feature can also be invoked from a code action, which makes it de facto callable from VSCode once the various PRs have been merged :)

shortrounddev2 · 43m ago
The LSP features, in my experience, work very well in VScode, no shade there. But debugging features like breakpoints still seem to be experimental? Whenever I ask others about it online they usually try to tell me that print debugging is superior anyways
Syzygies · 54m ago
Tarides has an OCaml focus. Elsewhere on their site, with an interesting omission:

"Examples of functional programming languages include OCaml, Erlang, Clojure, Haskell, Scala, and Common Lisp."

F# and OCaml are close cousins, and for me F# is even cleaner syntax and twice as fast (M4 Mac Studio).

It was a struggle for me to overcome my partisan preference for OCaml.