user0
user0

Reputation: 195

Indenting OCaml code in visual studio code

There are several questions here on indenting code in visual studio code (How do you format code in Visual Studio Code (VSCode)?), and on indenting OCaml code (How to indent existing OCaml code), but none of the answers work for indenting OCaml code in visual studio code. I have installed the OCaml Platform visual studio code extension, vscode-ocaml-format (following https://dev.realworldocaml.org/install.html), ocaml-lsp-server (following https://ocaml.org/learn/tutorials/up_and_running.html) using opam, updated and upgraded everything, used eval $(opam env), but this still does not work. I'm a bit at a loss...

Upvotes: 3

Views: 3118

Answers (1)

ivg
ivg

Reputation: 35210

It is very hard to answer such questions as we need more debugging input from your side. On the other hand, it is so hard to get it from vscode so it is better just to walk you through the whole process from the very beginning to see where things might go wrong. I would suggest you follow the process for ease of debugging. After you have everything working you can adapt it to your particular setup.

  1. Create a fresh new folder and put some OCaml file into it, let's name it test.ml and let's put into it the following code,
let test = [
  "hello"
]
  1. Now create a fresh local opam switch, by issuing in the same folder as the test.ml file the following command, (note the dot at the end of the command, it is required, it will create a local switch for you)
opam switch create .
  1. Next install the required dependencies.
opam install ocamlformat ocamlformat-rpc ocaml-lsp-server
  1. Create the .ocamlformat file in the same folder as the test.ml file. It will tell ocamlformat that you want to be ocamlformatted and you can use this file to setup your preferences.

  2. Make sure that you have installed ocamlplatform for vscode

  3. Now we are ready for the test. Start vscode and open test.ml. It will ask you to select the sandbox. Select the sandbox that corresponds to the folder where you put test.ml (it should be marked as local and have the same name as the folder name, and there will be the full path to it, so it will be easy for you to find it). The code syntax should be highlighted and there should be no error messages from vscode. Finally, hit Ctrl-Shift-I to re-indent your file, it should transform your code to,

let test = [ "hello" ]

Upvotes: 6

Related Questions