Skip to content

Commit

Permalink
Fix: (fish-get-normal-indent) Alternating continued lines
Browse files Browse the repository at this point in the history
According to the test-syntax.fish file, this fixes the bug mentioned
by me here:
<#37 (comment)>.
It should be possible to further simplify this code, but since this
works and fixes this bug, I'm committing and pushing it now.

Fixes #37.
  • Loading branch information
alphapapa committed Feb 13, 2020
1 parent 4e3c298 commit 18a6a6f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 72 deletions.
143 changes: 71 additions & 72 deletions fish-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -501,80 +501,79 @@ POSITIVE-RE and NEGATIVE-RE are regular expressions."
(let ((cur-indent 0)
(not-indented t)
skipped-line-p)
(while (and not-indented
(not (bobp)))

;; move to previous line
(forward-line -1)

(cond
((or (looking-at-p (rx (1+ nonl) "\\" eol))
;; FIXME: This is confusing. :(
(save-excursion
(forward-line -1)
(looking-at-p (rx (1+ nonl) "\\" eol))))
;; After escaped newline.
(cond (skipped-line-p
;; Empty lines have been skipped, so ignore continued
;; lines. Loop again to find previous non-continued line.
nil)
((save-excursion
(forward-line -1)
(looking-at-p (rx (1+ nonl) "\\" eol)))
;; Consecutive continued lines.
(setq cur-indent (save-excursion
;; Return indentation of previous non-continued line.
(cl-loop do (forward-line -1)
while (looking-at-p (rx (1+ nonl) "\\" eol))
finally do (forward-line 1))
(+ (current-indentation) fish-indent-offset))
(cl-labels ((back-to-noncontinued
() (cl-loop do (forward-line -1)
while (line-continued-p)))
(line-continued-p
() (save-excursion
(forward-line -1)
(looking-at-p (rx (1+ nonl) "\\" eol))))
(line-continues-p
() (looking-at-p (rx (1+ nonl) "\\" eol))))
(while (and not-indented (not (bobp)))

(cond ((line-continued-p)
;; Return indentation of previous non-continued line.
(back-to-noncontinued)
(setq cur-indent (+ (current-indentation) fish-indent-offset)
not-indented nil))
(t
;; One continued line.
(setq cur-indent (+ (current-indentation) fish-indent-offset)
not-indented nil))))

;; found empty line, so just skip it
((fish/at-empty-line?)
(setf skipped-line-p t))

;; found comment line, so just skip it
((fish/at-comment-line?)
(setf skipped-line-p t))

;; found line that contains an open block
;; so increase indentation level
((fish/at-open-block?)
(setq cur-indent (+ (current-indentation)
fish-indent-offset)
not-indented nil))

;; found line that starts with 'else' or 'case'
;; so increase indentation level
((looking-at-p "[ \t]*\\(else\\|case\\)\\>")
(setq cur-indent (+ (current-indentation) fish-indent-offset)
not-indented nil))

;; found a line that starts with 'end'
;; so use this line indentation level
((looking-at-p "[ \t]*end\\>")
(setq cur-indent (current-indentation)
not-indented nil))

;; found a line that contains open 'end' term
;; and doesn't start with 'end' (the order matters!)
;; it means that this 'end' is indented to the right
;; so we need to decrease indentation level
((fish/at-open-end?)
(setq cur-indent (- (current-indentation)
fish-indent-offset)
not-indented nil))

;; default case
;; we just set current indentation level
(t
(setq cur-indent (current-indentation)
not-indented nil))))
;; Line not continued.

;; move to previous line
(forward-line -1)

(cond
((and (not skipped-line-p)
(line-continued-p))
;; Return indentation of previous non-continued line.
(back-to-noncontinued)
(setq cur-indent (current-indentation)
not-indented nil))

;; found empty line, so just skip it
((fish/at-empty-line?)
(back-to-noncontinued)
(setq cur-indent (current-indentation)
not-indented nil))

;; found comment line, so just skip it
((fish/at-comment-line?)
(setf skipped-line-p t))

;; found line that contains an open block
;; so increase indentation level
((fish/at-open-block?)
(setq cur-indent (+ (current-indentation)
fish-indent-offset)
not-indented nil))

;; found line that starts with 'else' or 'case'
;; so increase indentation level
((looking-at-p "[ \t]*\\(else\\|case\\)\\>")
(setq cur-indent (+ (current-indentation) fish-indent-offset)
not-indented nil))

;; found a line that starts with 'end'
;; so use this line indentation level
((looking-at-p "[ \t]*end\\>")
(setq cur-indent (current-indentation)
not-indented nil))

;; found a line that contains open 'end' term
;; and doesn't start with 'end' (the order matters!)
;; it means that this 'end' is indented to the right
;; so we need to decrease indentation level
((fish/at-open-end?)
(setq cur-indent (- (current-indentation)
fish-indent-offset)
not-indented nil))

;; default case
;; we just set current indentation level
(t
(setq cur-indent (current-indentation)
not-indented nil)))))))
cur-indent))

(defun fish-get-end-indent ()
Expand Down
7 changes: 7 additions & 0 deletions test-syntax.fish
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,10 @@ end
find . \
-type f \
-name '*.c'

# *** Alternating continued and non-continued lines

set -l var \
(math "1 + 1")
set -l another_var \
(math "2 + 2")

0 comments on commit 18a6a6f

Please sign in to comment.