Skip to content
Browse files

Merge branch 'master' into refactor_indentation

  • Loading branch information...
2 parents 7f1a23b + 32b5639 commit e95797d6405171679d9805fe385609c3366dc62f @immerrr committed Apr 2, 2013
Showing with 76 additions and 52 deletions.
  1. +76 −52 lua-mode.el
View
128 lua-mode.el
@@ -580,9 +580,12 @@ Groups 6-9 can be used in any of argument regexps."
"Default expressions to highlight in Lua mode.")
(defvar lua-imenu-generic-expression
- ;; Very rough expression, but probably it's for the best, since it's used for
- ;; navigation. --immerrr
- '((nil "^[ \t]*\\(?:local[ \t]+\\)?function[ \t]+[[:alnum:]_:.]" 1))
+ ;; This regexp matches expressions which look like function
+ ;; definitions, but are not necessarily allowed by Lua syntax. This
+ ;; is done on purpose to avoid frustration when making a small error
+ ;; might cause a function get hidden from imenu index. --immerrr
+ '((nil "^[ \t]*\\(?:local[ \t]+\\)?function[ \t]+\\([[:alnum:]_:.]+\\)" 1)
+ (nil "^[ \t]*\\(?:local[ \t]+\\)?\\(\\_<[[:alnum:]_:.]+\\_>\\)[ \t]*=\[ \t]*\\_<function\\_>" 1))
"Imenu generic expression for lua-mode. See `imenu-generic-expression'.")
(defvar lua-sexp-alist '(("then" . "end")
@@ -1237,10 +1240,7 @@ use standalone."
(let ((line (line-number-at-pos)))
(lua-goto-matching-block-token nil found-pos 'backward)
(if (/= line (line-number-at-pos))
- (cons 'absolute
- (+ (current-indentation)
- (lua-calculate-indentation-block-modifier
- nil (point))))
+ (lua-calculate-indentation-info (point))
(cons 'remove-matching 0)))))
;; Everything else. This is from the original code: If opening a block
@@ -1268,6 +1268,8 @@ and the cdr of the replace-matching info is added in its place. This is used
when a middle-of the block (the only case is 'else') is seen on the same line
the block is opened."
(cond
+ ( (listp (cdr-safe pair))
+ (nconc pair info))
( (eq 'remove-matching (car pair))
; Remove head of list
(cdr info))
@@ -1278,37 +1280,64 @@ the block is opened."
; Just add the pair
(cons pair info))))
-(defun lua-calculate-indentation-info (&optional parse-start parse-end)
+(defun lua-calculate-indentation-info-1 (indentation-info bound)
+ "Helper function for `lua-calculate-indentation-info'.
+
+Return list of indentation modifiers from point to BOUND."
+ (while (lua-find-regexp 'forward lua-indentation-modifier-regexp
+ bound)
+ (let ((found-token (match-string 0))
+ (found-pos (match-beginning 0))
+ (found-end (match-end 0))
+ (data (match-data)))
+ (setq indentation-info
+ (lua-add-indentation-info-pair
+ (lua-make-indentation-info-pair found-token found-pos)
+ indentation-info))))
+ indentation-info)
+
+
+(defun lua-calculate-indentation-info (&optional parse-end)
"For each block token on the line, computes how it affects the indentation.
The effect of each token can be either a shift relative to the current
indentation level, or indentation to some absolute column. This information
is collected in a list of indentation info pairs, which denote absolute
and relative each, and the shift/column to indent to."
(let ((combined-line-end (line-end-position))
- (start-indentation (current-indentation)))
- (save-excursion
- (while (lua-last-token-continues-p)
- (lua-forward-line-skip-blanks)
- (setq combined-line-end (line-end-position))))
- (let ((search-stop (if parse-end
- (min parse-end combined-line-end)
- combined-line-end))
- (indentation-info nil))
- (if parse-start (goto-char parse-start))
- (save-excursion
- (beginning-of-line)
- (while (lua-find-regexp 'forward lua-indentation-modifier-regexp
- search-stop)
- (let ((found-token (match-string 0))
- (found-pos (match-beginning 0))
- (found-end (match-end 0))
- (data (match-data)))
- (setq indentation-info
- (lua-add-indentation-info-pair
- (lua-make-indentation-info-pair found-token found-pos)
- indentation-info))))
- (or indentation-info
- (list (cons 'absolute start-indentation)))))))
+ indentation-info)
+
+ (while (lua-is-continuing-statement-p)
+ (lua-forward-line-skip-blanks 'back))
+
+ ;; calculate indentation modifiers for the line itself
+ (setq indentation-info (list (cons 'absolute (current-indentation))))
+
+ (back-to-indentation)
+ (setq indentation-info
+ (lua-calculate-indentation-info-1
+ indentation-info (min parse-end (line-end-position))))
+
+ ;; and do the following for each continuation line before PARSE-END
+ (while (and (eql (forward-line 1) 0)
+ (<= (point) parse-end))
+
+ ;; handle continuation lines:
+ (if (lua-is-continuing-statement-p)
+ ;; if it's the first continuation line, add one level
+ (unless (eq (car (car indentation-info)) 'continued-line)
+ (push (cons 'continued-line lua-indent-level) indentation-info))
+
+ ;; if it's the first non-continued line, subtract one level
+ (when (eq (car (car indentation-info)) 'continued-line)
+ (pop indentation-info)))
+
+ ;; add modifiers found in this continuation line
+ (setq indentation-info
+ (lua-calculate-indentation-info-1
+ indentation-info (min parse-end (line-end-position)))))
+
+ indentation-info))
+
(defun lua-accumulate-indentation-info (info)
"Accumulates the indentation information previously calculated by
@@ -1325,21 +1354,21 @@ shift, or the absolute column to indent to."
info-list)
(cons type accu)))
-(defun lua-calculate-indentation-block-modifier (&optional parse-start
- parse-end)
+(defun lua-calculate-indentation-block-modifier (&optional parse-end)
"Return amount by which this line modifies the indentation.
Beginnings of blocks add lua-indent-level once each, and endings
of blocks subtract lua-indent-level once each. This function is used
to determine how the indentation of the following line relates to this
one."
- (if parse-start (goto-char parse-start))
- ;; First go back to the line that starts it all
- ;; lua-calculate-indentation-info will scan through the whole thing
- (while (lua-is-continuing-statement-p)
- (lua-forward-line-skip-blanks 'back))
- (let ((case-fold-search nil)
- (indentation-info (lua-accumulate-indentation-info
- (lua-calculate-indentation-info nil parse-end))))
+ (let (indentation-info)
+ (save-excursion
+ ;; First go back to the line that starts it all
+ ;; lua-calculate-indentation-info will scan through the whole thing
+ (let ((case-fold-search nil))
+ (setq indentation-info
+ (lua-accumulate-indentation-info
+ (lua-calculate-indentation-info parse-end)))))
+
(if (eq (car indentation-info) 'absolute)
(- (cdr indentation-info) (current-indentation))
(cdr indentation-info))))
@@ -1451,25 +1480,20 @@ to the left by the amount specified in lua-indent-level."
(defun lua-calculate-indentation (&optional parse-start)
"Return appropriate indentation for current line as Lua code."
(save-excursion
- (let ((continuing-p (lua-is-continuing-statement-p)))
+ (let ((continuing-p (lua-is-continuing-statement-p))
+ (cur-line-begin-pos (line-beginning-position)))
(or
;; when calculating indentation, do the following:
;; 1. check, if the line starts with indentation-modifier (open/close brace)
;; and if it should be indented/unindented in special way
(lua-calculate-indentation-override)
- ;; 2. otherwise, use indentation modifiers from previous line + it's own indentation
- ;; 3. if previous line doesn't contain indentation modifiers, additionally check
- ;; if current line is a continuation line and add lua-indent-level if it is
(when (lua-forward-line-skip-blanks 'back)
;; the order of function calls here is important. block modifier
;; call may change the point to another line
- (let ((modifier
- (lua-calculate-indentation-block-modifier nil (line-end-position))))
- (+ (if (and continuing-p (= 0 modifier))
- lua-indent-level
- modifier)
- (current-indentation))))
+ (let* ((modifier
+ (lua-calculate-indentation-block-modifier cur-line-begin-pos)))
+ (+ (current-indentation) modifier)))
;; 4. if there's no previous line, indentation is 0
0))))

0 comments on commit e95797d

Please sign in to comment.
Something went wrong with that request. Please try again.