[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/lua-mode 88b0ec3 209/468: Fix indentation for blocks start
From: |
Philip Kaludercic |
Subject: |
[nongnu] elpa/lua-mode 88b0ec3 209/468: Fix indentation for blocks starting on continued lines |
Date: |
Thu, 5 Aug 2021 04:58:38 -0400 (EDT) |
branch: elpa/lua-mode
commit 88b0ec34374f8224daf7c21e0168b883642d529f
Author: immerrr <immerrr+lua@gmail.com>
Commit: immerrr <immerrr+lua@gmail.com>
Fix indentation for blocks starting on continued lines
local foo =
{
bar,
baz
} ^
1. these lines should be indented properly now
^
2. the following lines should be unindented properly now
Two principal changes here:
- continued lines are included in indentation-info
Previously, continued lines were handled in `lua-calculate-indentation'
function after block indentation offset was already calculated. This
prevented proper indentation for pt.1 from above since in that case
continued-line offset should be applied before block-open offset.
- lua-make-indentation-info-pair no longer folds indentation info
Previously, lua-make-indentation-info-pair for block-close tokens invoked
lua-calculate-indentation-block-modifier which returned single pair of
indentation-info, e.g. (absolute . POS). This was enough for block-close
token indentation because those're aligned with corresponding block-open
lines, but in order to indent the following lines properly we need to know
whether block-open was on continued line or not, so for pt.2 to happen
lua-make-indentation-info-pair was fixed.
The rest are minor fixes in indentation machinery to accomodate the above.
This should fix issue #33.
---
lua-mode.el | 90 ++++++++++++++++++++++++++++++++++---------------------------
1 file changed, 51 insertions(+), 39 deletions(-)
diff --git a/lua-mode.el b/lua-mode.el
index 62dc98c..c97a7f7 100644
--- a/lua-mode.el
+++ b/lua-mode.el
@@ -1225,10 +1225,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
@@ -1256,6 +1253,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))
@@ -1290,22 +1289,40 @@ 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))
+ 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))
- (save-excursion
- (beginning-of-line)
- (setq indentation-info (lua-calculate-indentation-info-1
- indentation-info search-stop))
- (or indentation-info
- (list (cons 'absolute start-indentation)))))))
(defun lua-accumulate-indentation-info (info)
"Accumulates the indentation information previously calculated by
@@ -1322,21 +1339,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 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))))
@@ -1445,25 +1462,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))))
- [nongnu] elpa/lua-mode e3a8282 099/468: Make URL/Version fields in lua-mode.el prettier, update parse expression in Makefile, (continued)
- [nongnu] elpa/lua-mode e3a8282 099/468: Make URL/Version fields in lua-mode.el prettier, update parse expression in Makefile, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 1c37d0d 114/468: Bump version, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode e17a48c 126/468: Give credit to people participated recently, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode dcf5fc1 138/468: Indentation fixes and tidy up., Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 3c8cfe6 160/468: Add credit, reformat new code to fit into 77 cols, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 6b48a94 165/468: Second part of the fix for #34, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 7a3a5f6 169/468: Add script to facilitate indentation testing, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 7f8e519 178/468: Add backward compatibility aliases for cl-assert & cl-labels functions, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode e6a46ee 182/468: Update README.md, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 4b407cc 203/468: Print 'Prefix key set to ...' only when setting via interactive function, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 88b0ec3 209/468: Fix indentation for blocks starting on continued lines,
Philip Kaludercic <=
- [nongnu] elpa/lua-mode 32b5639 210/468: Fix wrong imenu generic expression, add one more regexp to match 'x = function(...)' definitions, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 57d3e46 212/468: lua-mode.el: bump "Version:" field, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode c7c8d0c 179/468: Fix bug with lua-mode-syntax-table initialization, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 0b4ae97 184/468: Merge branch 'fix-36' of git://github.com/vhallac/lua-mode, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 562540e 073/468: Fix lua-calculate-indentation: indent to 0 when inside string literal, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 0f6048d 082/468: Implement lua-mark-all-multiline-literals function, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 96eba74 085/468: lua-(comment/string)-p: add optional pos parameter, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode aef55a6 091/468: Update contact info in README; bump version, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 8c28ed5 095/468: Add utility funcs lua-inside-multiline-p and lua-get-multiline-start, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 79ff5d0 110/468: lua-postprocess-output-buffer: replace save-excursion/set-buffer with with-current-buffer, Philip Kaludercic, 2021/08/05