OCamlPro / ocaml-top

A simple cross-platform OCaml code editor built for top-level evaluation.
http://typerex.org/ocaml-top.html
GNU General Public License v3.0
64 stars 8 forks source link

Exhausting mandatory ";;" and other indentation troubles (usability) #70

Closed RogerTarani closed 4 years ago

RogerTarani commented 5 years ago

Debian 9 ocaml-top 1.1.5 Ocaml 4.06.1

How is it possible to simply get rid of this ;; and keep code correctly indented? At least a Ctrl-J to add OR delete a ;; would be appreciated. How is it possible to simply fine tune ocp-indent or the ocaml-top code with a basic or normal knowledge of OCaml?

;; is required in order just to get a coherent code indentation within ocaml-top.

1/ If there a forgotten ;; in the code of a module (module <module-name> = struct ... end), the Toplevel doesn't give hand back (interruption is required with Esc). There is no reason to require ;; and don't parse it away.

2/ Sometimes, with just adding or deleting a " or ( all the text is rearranged in a totally incoherent way, and Ctrl Z is the only solution to get back.

3/ If some values are written along a function declaration for its evaluation, w/o some CR, it crawls all over the screen and becomes unreadable. Here is a simple example:

let a = 1

          a = 1
                b = 1
                      c = 1
                            d = 1
                                  e = 1 ........................

Other example: f 1 is flying in the middle of the screen. Adding a ;; "solves" the problem.

let f x = 
  let y = x * 100000 in let z = 100000 * y in z 

                                                 f 1
let f x = 
  let y = x * 100000 in let z = 100000 * y in z 
;;
f 1

The rule should be something like: "if there is a space between the end of let declaration an some expression then left align this expression else do the usual two spaces indentation"

4/ The position of a comment has not a reproducible behaviour regarding deleting/adding a ;; (non blocking). Consider a ;; positioned between one letwith a comment at its end and an new let declaration. If it is deleted, the comment becomes left-aligned. When a new ;; is added at the same place the comment keeps left-aligned. It seems depending on whether the CR of comment line is deleted or not

let a = 
  1    
  (* hmm *)
;;
let b = 
  2
let a = 
  1    
(* hmm *)

let b = 
  2

then

let a = 
  1    
(* hmm *)
;;
let b = 
  2

5/ When the compiler emits an error message, a red ! is displayed. If a new evaluation is successful, the red ! keeps on being displayed. Any character input makes it disappear. Even a << (terminate Toplevel...) doesn't fix that.

AltGr commented 5 years ago

I know it has been a while, but thanks a lot for your detailed report. I am just rolling an update of the project and upgrading to GTK3. You mention many different issues, so let me address them separately:

Annoying ;;

It seems to me that there is a little bit of misunderstanding of what exactly ;; is and does. Ocaml-top actually requires much less of them than the normal toplevel does, because it has its own parsing to delimit definitions.

In the standard toplevel, ;; are the points where evaluation is triggered, on either a single expression or any number of top-level values. To improve usability, especially when working with "normal" source files, ocaml-top separates the top-level expressions automatically to send them one by one to the toplevel (and stop at the first that fails, in case of error). So you should only need to add ;; when entering raw expressions in the buffer.

Thus, ;; should never be required within a module. It's true however that the normal toplevel will accept them, while ocaml-top will choke on them, which is indeed an issue that should be fixed. If sending an unterminated input to the ocaml toplevel, it will keep waiting for more input — this is the issue you describe in 1/, and this happens as well if you send an unclosed string or module directly. We should be able to detect that and refuse to send (for unclosed elements), and detect the ;; within a struct block as non-closing, as is done by the ocaml top-level: evaluating a partial module is impossible anyway.

Inconsistent indentation

2/ Sometimes, with just adding or deleting a " or ( all the text is rearranged in a totally incoherent way, and Ctrl Z is the only solution to get back.

Just closing the string or paren should work though ? Otherwise, it would mean that we broke Lablgtk/gtksourceview display engine state in some way, which would be a bug.

3/ If some values are written along a function declaration for its evaluation, w/o some CR, it crawls all over the screen and becomes unreadable. Here is a simple example:

This is totally intended, as it is exactly how OCaml will interpret it. Taking one of your examples:

let f x = 
  let y = x * 100000 in let z = 100000 * y in z 

                                                 f 1

this is understood as "apply function z to arguments f and 1, and the display is accurate. You probably wanted the ;; here, or to put a let foo = in front of f 1. White space and empty lines are not meaningful in ocaml, so we don't want to interpret them any different in the editor.

Moving comments

4/ The position of a comment has not a reproducible behaviour regarding deleting/adding a ;; (non blocking).

I can see how this can seem disturbing, but don't think it's worth fixing. What happens is that

Persistent error marks

5/ When the compiler emits an error message, a red ! is displayed. If a new evaluation is successful, the red ! keeps on being displayed. Any character input makes it disappear. Even a << (terminate Toplevel...) doesn't fix that.

A bug with lablgtk/gtksourceview. The upgrade to GTK3 thankfully seems to have fixed it \o/

--

Thanks for the experience report and detailed comments! Hope this explanation helps, and I will keep track of the "sending unclosed blocks to the ocaml toplevefl" issue.

RogerTarani commented 4 years ago

Thanks for your detailed explanation.

2/

Just closing the string or paren should work though ?

Yes, but it's not easy because all the text of the program has been chaned and has the same color! (try it). The best way is to add a ) or a " at the end of the expression (texte stays readable) then at the beginning.

BTW Would it be easy to add some basic search/replace text features and a basic program files explorer? (I didn't read the code of ocaml-top.). ocaml-top is nice when one starts with OCaml and has no time to setup and understand all the Emacs/Vim stuff (on Linux/MacOS and especially on Windows).

Thanks for the 1.1.5 available via Opam.

PS: https://www.typerex.org/ocaml-top.html still invites to download 1.1.2 while 1.1.5 is available https://github.com/OCamlPro/ocaml-top/tree/1.1.5