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

Autoindent isn't working on VS Code 1.3.0 + plugin 0.4.0

Open hcarty opened this issue 9 years ago • 11 comments

I updated to the latest plugin version today and autoindent stopped working (automatic indentation of new line when pressing enter). This is on OSX 10.11, OCaml 4.02.3. It was working on an earlier version of the plugin - I think 0.1.0 or 0.2.0.

hcarty avatar Jul 08 '16 14:07 hcarty

Thank you for your report. I've checked it works fine on my mac with latest version.

Did you check if "Format Code" command works with OCaml files? If it works, please check "editor.formatOnType" is set to true.

BTW: Current only *.ml and *.mli are supported. *.mll and *.mly are not supported.

hackwaly avatar Jul 08 '16 14:07 hackwaly

"editor.formatOnType" was set to false in the default configuration, so it's a bit better now as it formats after I press enter at the end of the newly created line. However I still get:

let () =<press enter>
<-- Cursor ends up here

when I would expect/hope for:

let () =<press enter>
  <-- Cursor ending up here

with the new line starting out at the proper indentation level.

hcarty avatar Jul 08 '16 15:07 hcarty

This is all for a foo.ml, to be clear.

Also, thank you for the plugin - it's a very nice addition to the community!

hcarty avatar Jul 08 '16 15:07 hcarty

Likely I add '\n' to trigger chars of auto format cause this. Should we only indent cursor line instead of whole document? So we can use "--indent-empty" option of ocp-indent to solve this problem.

hackwaly avatar Jul 08 '16 15:07 hackwaly

I'd prefer to only indent the cursor line, ideally having the case I showed working without enabling "editor.formatOnType".

hcarty avatar Jul 08 '16 15:07 hcarty

If "editor.formatOnType" on: we should format whole document or a range the text edit affects, like Typescript extension does. If "editor.formatOnType" off: we should only indent the cursor line as you say. Looks like with current VS Code API it's not possible. It only support regex based indent rule now.

I found a way: combine two indent request (with and without "--indent-empty") result to achieve that.

hackwaly avatar Jul 08 '16 15:07 hackwaly

Remark: Current implementation did not respect user's indent size setting. This should fix at the same time.

hackwaly avatar Jul 08 '16 15:07 hackwaly

I've tried a lot. Seems formatOnType can't work well with '\n'.

If we use formatOnType to impl auto indent, it will encounter weird bugs, likes indentation is inserted after cursor not before cursor. I see TypeScript extension also has these bugs. So we'd better to wait for VS Code provide better API to allow us to impl auto indent.

For now. We must remove '\n' from formatOnType's trigger chars. And impl auto indent via regex based auto-indent rules.

Try new version 0.4.18!

In this version: Enter after "type ... =", "let ... =", "do", "begin", "struct", "sig", "with" will auto indent. Type last char of "done" and "end" will do format.

hackwaly avatar Jul 09 '16 10:07 hackwaly

Thanks! I'll give it a try on Monday if not before then.

hcarty avatar Jul 09 '16 11:07 hcarty

Just tested and I can confirm that the initial issue is fixed. Unfortunately outdenting seems to have suffered:

let () =
  for i = 1 to 10 do
    ()
    done

This is with "editor.formatOnType" disabled. It does work as expected with "editor.formatOnType" enabled.

hcarty avatar Jul 09 '16 18:07 hcarty

Unfortunately outdenting seems to have suffered

Yes, due to this issue https://github.com/Microsoft/vscode/issues/2262, I didn't implement auto outdent through decreaseIndentPattern. I choose formatOnType right now until VS Code fix that issue.

hackwaly avatar Jul 10 '16 06:07 hackwaly