Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiline variable declarations. #56

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions typescript-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -2120,6 +2120,26 @@ moved on success."
(when location
(goto-char location))))

(defun typescript--multiline-declaration-indentation ()
"Returns the proper indentation for lines which are continuations of
multiline var/let/const declarations. If the point is not on such a line,
returns nil."
(save-excursion
(beginning-of-line)
(let ((decl-re "^ *\\(var\\|const\\|let\\)\\_> *")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see another regex searching for var, const, let elsewhere in typescript--font-lock-keywords-3. Can we perhaps combine the two regexes into a single defconst and refer to the new constant, so as to avoid duplication?

(is-looking-at-decl-keyword nil))
(save-match-data
(while (and (looking-back ",[[:space:]\n]+" nil)
(= (forward-line -1) 0)
(not (setq is-looking-at-decl-keyword (looking-at decl-re)))))
(when is-looking-at-decl-keyword
;; Found the var/let/const declaration of which the initial line was a
;; continuation of. The correct indentation is therefore the relative
;; offset of the word following the var keyword.
(let ((bol-pos (point)))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bol to me means "beginning of line". Sometimes, this point is the beginning of line, but not always, right? If that's the case, I'd give it a different name. keyword-start maybe??

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point.

(re-search-forward decl-re)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If is-looking-at-decl-keyword is true, then match-end should already contain the point you are looking for. Is there something I'm missing? If I'm right then the whole when block could be reduced to:

(when is-looking-at-decl-keyword
  (- (match-end) (point)))

or something of that order.

(- (point) bol-pos)))))))

(defun typescript--proper-indentation (parse-status)
"Return the proper indentation for the current line."
(save-excursion
Expand All @@ -2130,6 +2150,7 @@ moved on success."
((typescript--ctrl-statement-indentation))
((eq (char-after) ?#) 0)
((save-excursion (typescript--beginning-of-macro)) 4)
((typescript--multiline-declaration-indentation))
((nth 1 parse-status)
(let ((same-indent-p (looking-at
"[]})]\\|\\_<case\\_>\\|\\_<default\\_>"))
Expand Down