Skip to content

Commit cc34919

Browse files
committed
Never fail to indent
A proper indentation engine should never look past the previous line. This is because a user frequently wants custom indentation, in all programming languages. A PureScript example: ```haskell f1 x = case x of true -> true _ -> false f2 x = case x of true -> true _ -> false ``` So the proper behavior is: "take previous-line indentation, then if previous line has a modifier increase/decrease it". This is both simple, robust, typically meets the user expectations, and is AFAIK what all built-in major modes do. Unfortunately we're long past that point, the purescript-mode indentation engine parses a lot of stuff it shouldn't. And frequently it gives a "parsing error". In my experience all those "errors" are bugs because the PS code is perfectly valid, but disregarding if it's a bug or an actual unfinished code in the buffer, engine should never leave a user with no indentation whatsoever. So what this commit does is it detects such "errors", and takes previous indentation level. Even if it's not the correct indentation, in practice it's only off by `purescript-indentation-left-offset`, so a user often has much less to type compared to "no indentation" at all. ------- Now, it turns out the mode has another bug. As explained in the function being added, `purescript-newline-and-indent` calls indentation calculations on the wrong line. This isn't some special code that only `purescript-newline-and-indent` uses, it is a code that's being called by the <kbd>TAB</kbd> indentation as well. As there are huge amounts of poorly documented code that shouldn't even be there in the first place, and are sometimes using local variables from other functions, I decided instead of trying to fix the code it would be more productive to just detect whether we're being called from `purescript-newline-and-indent` or not.
1 parent d187b3d commit cc34919

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

purescript-indentation.el

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -463,21 +463,39 @@ autofill-mode."
463463
(defun purescript-indentation-first-indentation ()
464464
(if (eq purescript-literate 'bird) '(2) '(0)))
465465

466+
(defun purescript-get-previous-indentation-hack ()
467+
"Return previous indentation level as a list element.
468+
469+
This function is a temporary workaround for
470+
`purescript-newline-and-indent' asking for indent before going to the
471+
new line, which makes it indistinguishable from just attempting to
472+
indent the current line. This has to be fixed elsewhere."
473+
(list
474+
(if (string= this-command "purescript-newline-and-indent")
475+
(current-indentation) ;; current line is actually previous one
476+
(save-excursion
477+
(forward-line -1)
478+
(current-indentation)))))
479+
466480
(defun purescript-indentation-find-indentations ()
467-
(let ((ppss (syntax-ppss)))
468-
(cond
469-
((nth 3 ppss)
470-
(purescript-indentation-first-indentation))
471-
((nth 4 ppss)
472-
(if (save-excursion
473-
(and (skip-syntax-forward "-")
474-
(eolp)
475-
(not (> (forward-line 1) 0))
476-
(not (nth 4 (syntax-ppss)))))
477-
(purescript-indentation-parse-to-indentations)
478-
(purescript-indentation-first-indentation)))
479-
(t
480-
(purescript-indentation-parse-to-indentations)))))
481+
(condition-case nil
482+
(let ((ppss (syntax-ppss)))
483+
(cond
484+
((nth 3 ppss)
485+
(purescript-indentation-first-indentation))
486+
((nth 4 ppss)
487+
(if (save-excursion
488+
(and (skip-syntax-forward "-")
489+
(eolp)
490+
(not (> (forward-line 1) 0))
491+
(not (nth 4 (syntax-ppss)))))
492+
(purescript-indentation-parse-to-indentations)
493+
(purescript-indentation-first-indentation)))
494+
(t
495+
(purescript-indentation-parse-to-indentations))))
496+
;; Ideally it should not return parse error but if it does just use the previous
497+
;; indentation.
498+
(parse-error (purescript-get-previous-indentation-hack))))
481499

482500
(defconst purescript-indentation-unicode-tokens
483501
'(("" . "->") ;; #x2192 RIGHTWARDS ARROW

0 commit comments

Comments
 (0)