[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/swift-mode 09b50fa 042/496: [Fix #12] Merge mode code in a
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/swift-mode 09b50fa 042/496: [Fix #12] Merge mode code in a single file |
Date: |
Sun, 29 Aug 2021 11:33:04 -0400 (EDT) |
branch: elpa/swift-mode
commit 09b50fa88d87e8cdb4aee2030b43e6bae0577364
Author: Bozhidar Batsov <bozhidar@batsov.com>
Commit: Bozhidar Batsov <bozhidar@batsov.com>
[Fix #12] Merge mode code in a single file
---
swift-indent.el | 177 --------------------------------------------------------
swift-mode.el | 145 ++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 145 insertions(+), 177 deletions(-)
diff --git a/swift-indent.el b/swift-indent.el
deleted file mode 100644
index 0b97d6c..0000000
--- a/swift-indent.el
+++ /dev/null
@@ -1,177 +0,0 @@
-;;; swift-indent.el --- Indentation commands for Swift mode.
-
-;; Copyright (C) 2014 Chris Barrett
-
-;; Author: Chris Barrett <chris.d.barrett@me.com>
-;; Version: 0.1
-
-;; This file is not part of GNU Emacs.
-
-;; This program is free software: you can redistribute it and/or modify
-;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation, either version 3 of the License, or
-;; (at your option) any later version.
-
-;; This program is distributed in the hope that it will be useful,
-;; but WITHOUT ANY WARRANTY; without even the implied warranty of
-;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-;; GNU General Public License for more details.
-
-;; You should have received a copy of the GNU General Public License
-;; along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-;;; Commentary:
-
-;; Indentation commands for Swift mode. It's based on the code for `rust-mode',
-;; which is very clean.
-
-;;; Code:
-
-(defgroup swift-indent nil
- "Configuration for swift-mode indentation behaviour."
- :group 'languages
- :prefix "swift-indent-")
-
-(defcustom swift-indent-offset 4
- "Defines the indentation offset for Swift code."
- :group 'swift-indent
- :type 'integer)
-
-(defun swift-indent--paren-level ()
- "Return the paren level at point."
- (nth 0 (syntax-ppss)))
-
-(defun swift-indent--in-str-or-cmnt ()
- "Non-nil if point is in a string or comment."
- (nth 8 (syntax-ppss)))
-
-(defun swift-indent--back-to-start-of-level ()
- "Move backwards up to the start of the current indentation level."
- (let ((current-level (swift-indent--paren-level)))
- (back-to-indentation)
- (while (> (swift-indent--paren-level) current-level)
- (backward-up-list)
- (back-to-indentation))))
-
-(defun swift-indent--rewind-past-str-cmnt ()
- (goto-char (nth 8 (syntax-ppss))))
-
-(defun swift-indent--rewind-irrelevant ()
- (let ((starting (point)))
- (skip-chars-backward "[:space:]\n")
- (if (looking-back "\\*/") (backward-char))
- (if (swift-indent--in-str-or-cmnt)
- (swift-indent--rewind-past-str-cmnt))
- (if (/= starting (point))
- (swift-indent--rewind-irrelevant))))
-
-(defun swift-indent--align-to-expr-after-brace ()
- (save-excursion
- (forward-char)
- ;; We don't want to indent out to the open bracket if the
- ;; open bracket ends the line.
- (when (not (looking-at "[[:blank:]]*\\(?://.*\\)?$"))
- (when (looking-at "[[:space:]]")
- (forward-word 1)
- (backward-word 1))
- (current-column))))
-
-(defun swift-indent--rewind-to-beginning-of-current-level-expr ()
- (let ((current-level (swift-indent--paren-level)))
- (back-to-indentation)
- (while (> (swift-indent--paren-level) current-level)
- (backward-up-list)
- (back-to-indentation))))
-
-(defun swift-indent--calculate-indentation ()
- "Calculate the indentation column to use for `swift-indent-line'.
-Returns the column number as an integer."
- (save-excursion
- (back-to-indentation)
- ;; Point is now at beginning of line.
- (let* ((level (swift-indent--paren-level))
- ;; Our "baseline" is one level out from the indentation of the
- ;; expression containing the innermost enclosing opening bracket.
- ;; That way if we are within a block that has a different
indentation
- ;; than this mode would give it, we still indent the inside of it
- ;; correctly relative to the outside.
- (baseline
- (if (zerop level)
- 0
- (save-excursion
- (backward-up-list)
- (swift-indent--back-to-start-of-level)
- (+ (current-column) swift-indent-offset)))))
- (cond
- ;; A function return type is indented to the corresponding function
arguments
- ((looking-at "->")
- (save-excursion
- (backward-list)
- (or (swift-indent--align-to-expr-after-brace)
- (+ baseline swift-indent-offset))))
-
- ;; A closing brace is 1 level unindented
- ((looking-at "}") (- baseline swift-indent-offset))
-
- ;; Doc comments in /** style with leading * indent to line up the *s
- ((and (nth 4 (syntax-ppss)) (looking-at "*"))
- (+ 1 baseline))
-
- ;; If we're in any other token-tree / sexp, then:
- (t
- (or
- ;; If we are inside a pair of braces, with something after the
- ;; open brace on the same line and ending with a comma, treat
- ;; it as fields and align them.
- (when (> level 0)
- (save-excursion
- (swift-indent--rewind-irrelevant)
- (backward-up-list)
- ;; Point is now at the beginning of the containing set of braces
- (swift-indent--align-to-expr-after-brace)))
-
- (progn
- (back-to-indentation)
- ;; Point is now at the beginning of the current line
- (cond
- ;; If this line begins with "else" or "{", stay on the
- ;; baseline as well (we are continuing an expression,
- ;; but the "else" or "{" should align with the beginning
- ;; of the expression it's in.)
- ((looking-at (rx (or (and bow "else" eow) "{")))
- baseline)
- ;; Cases are indented to the same level as the enclosing switch
statement.
- ((looking-at (rx bow (or "case" "default") eow))
- (- baseline swift-indent-offset))
-
- ;; If we are at the first line, no indentation is needed, so stay
at baseline.
- ((save-excursion
- (swift-indent--rewind-irrelevant)
- ;; Point is now at the end of the previous line
- (= 1 (line-number-at-pos (point))))
- baseline)
-
- ((save-excursion
- (swift-indent--rewind-irrelevant)
- ;; Point is now at the end of the previous line
- ;; If the previous line ends with any of these:
- ;; { ? : ( , ; [ }
- ;; then we are at the beginning of an expression, so stay on
the baseline.
- (looking-back "[(,:;?[{}]\\|[^|]|"))
- baseline)
-
- (t
- baseline)))))))))
-
-(defun swift-indent-line ()
- "Indent the current line. Also see `swift-indent-offset'."
- (interactive "*")
- (let ((indent (swift-indent--calculate-indentation)))
- (if (<= (current-column) (current-indentation))
- (indent-line-to indent)
- (save-excursion
- (indent-line-to indent)))))
-
-(provide 'swift-indent)
-
-;;; swift-indent.el ends here
diff --git a/swift-mode.el b/swift-mode.el
index 5be6c72..0989932 100644
--- a/swift-mode.el
+++ b/swift-mode.el
@@ -37,6 +37,151 @@
"Set variable VAR to value VAL in current buffer."
(list 'set (list 'make-local-variable (list 'quote var)) val))))
+(defgroup swift nil
+ "Configuration for swift-mode."
+ :group 'languages
+ :prefix "swift-")
+
+(defcustom swift-indent-offset 4
+ "Defines the indentation offset for Swift code."
+ :group 'swift
+ :type 'integer)
+
+(defun swift-indent--paren-level ()
+ "Return the paren level at point."
+ (nth 0 (syntax-ppss)))
+
+(defun swift-indent--in-str-or-cmnt ()
+ "Non-nil if point is in a string or comment."
+ (nth 8 (syntax-ppss)))
+
+(defun swift-indent--back-to-start-of-level ()
+ "Move backwards up to the start of the current indentation level."
+ (let ((current-level (swift-indent--paren-level)))
+ (back-to-indentation)
+ (while (> (swift-indent--paren-level) current-level)
+ (backward-up-list)
+ (back-to-indentation))))
+
+(defun swift-indent--rewind-past-str-cmnt ()
+ (goto-char (nth 8 (syntax-ppss))))
+
+(defun swift-indent--rewind-irrelevant ()
+ (let ((starting (point)))
+ (skip-chars-backward "[:space:]\n")
+ (if (looking-back "\\*/") (backward-char))
+ (if (swift-indent--in-str-or-cmnt)
+ (swift-indent--rewind-past-str-cmnt))
+ (if (/= starting (point))
+ (swift-indent--rewind-irrelevant))))
+
+(defun swift-indent--align-to-expr-after-brace ()
+ (save-excursion
+ (forward-char)
+ ;; We don't want to indent out to the open bracket if the
+ ;; open bracket ends the line.
+ (when (not (looking-at "[[:blank:]]*\\(?://.*\\)?$"))
+ (when (looking-at "[[:space:]]")
+ (forward-word 1)
+ (backward-word 1))
+ (current-column))))
+
+(defun swift-indent--rewind-to-beginning-of-current-level-expr ()
+ (let ((current-level (swift-indent--paren-level)))
+ (back-to-indentation)
+ (while (> (swift-indent--paren-level) current-level)
+ (backward-up-list)
+ (back-to-indentation))))
+
+(defun swift-indent--calculate-indentation ()
+ "Calculate the indentation column to use for `swift-indent-line'.
+Returns the column number as an integer."
+ (save-excursion
+ (back-to-indentation)
+ ;; Point is now at beginning of line.
+ (let* ((level (swift-indent--paren-level))
+ ;; Our "baseline" is one level out from the indentation of the
+ ;; expression containing the innermost enclosing opening bracket.
+ ;; That way if we are within a block that has a different
indentation
+ ;; than this mode would give it, we still indent the inside of it
+ ;; correctly relative to the outside.
+ (baseline
+ (if (zerop level)
+ 0
+ (save-excursion
+ (backward-up-list)
+ (swift-indent--back-to-start-of-level)
+ (+ (current-column) swift-indent-offset)))))
+ (cond
+ ;; A function return type is indented to the corresponding function
arguments
+ ((looking-at "->")
+ (save-excursion
+ (backward-list)
+ (or (swift-indent--align-to-expr-after-brace)
+ (+ baseline swift-indent-offset))))
+
+ ;; A closing brace is 1 level unindented
+ ((looking-at "}") (- baseline swift-indent-offset))
+
+ ;; Doc comments in /** style with leading * indent to line up the *s
+ ((and (nth 4 (syntax-ppss)) (looking-at "*"))
+ (+ 1 baseline))
+
+ ;; If we're in any other token-tree / sexp, then:
+ (t
+ (or
+ ;; If we are inside a pair of braces, with something after the
+ ;; open brace on the same line and ending with a comma, treat
+ ;; it as fields and align them.
+ (when (> level 0)
+ (save-excursion
+ (swift-indent--rewind-irrelevant)
+ (backward-up-list)
+ ;; Point is now at the beginning of the containing set of braces
+ (swift-indent--align-to-expr-after-brace)))
+
+ (progn
+ (back-to-indentation)
+ ;; Point is now at the beginning of the current line
+ (cond
+ ;; If this line begins with "else" or "{", stay on the
+ ;; baseline as well (we are continuing an expression,
+ ;; but the "else" or "{" should align with the beginning
+ ;; of the expression it's in.)
+ ((looking-at (rx (or (and bow "else" eow) "{")))
+ baseline)
+ ;; Cases are indented to the same level as the enclosing switch
statement.
+ ((looking-at (rx bow (or "case" "default") eow))
+ (- baseline swift-indent-offset))
+
+ ;; If we are at the first line, no indentation is needed, so stay
at baseline.
+ ((save-excursion
+ (swift-indent--rewind-irrelevant)
+ ;; Point is now at the end of the previous line
+ (= 1 (line-number-at-pos (point))))
+ baseline)
+
+ ((save-excursion
+ (swift-indent--rewind-irrelevant)
+ ;; Point is now at the end of the previous line
+ ;; If the previous line ends with any of these:
+ ;; { ? : ( , ; [ }
+ ;; then we are at the beginning of an expression, so stay on
the baseline.
+ (looking-back "[(,:;?[{}]\\|[^|]|"))
+ baseline)
+
+ (t
+ baseline)))))))))
+
+(defun swift-indent-line ()
+ "Indent the current line. Also see `swift-indent-offset'."
+ (interactive "*")
+ (let ((indent (swift-indent--calculate-indentation)))
+ (if (<= (current-column) (current-indentation))
+ (indent-line-to indent)
+ (save-excursion
+ (indent-line-to indent)))))
+
;; Font lock.
(defvar swift-mode--type-decl-keywords
- [nongnu] elpa/swift-mode 592a33f 021/496: Copy syntax table from rust-mode., (continued)
- [nongnu] elpa/swift-mode 592a33f 021/496: Copy syntax table from rust-mode., ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 142c7db 023/496: Merge pull request #7 from syohex/fix-for-older-emacs, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 90802b5 041/496: Use lexical scoping, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 8641ebc 048/496: Mention old-school installation method, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 8e72839 004/496: Add basic syntax table, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 5274877 010/496: Add single-line comments to syntax table, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode d110515 011/496: Font lock tweaks, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 272473b 005/496: Remove currently unused mode map, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode cbb19f2 003/496: Add basic font-locking, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 7df8b6d 037/496: Remove dash.el dependency., ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 09b50fa 042/496: [Fix #12] Merge mode code in a single file,
ELPA Syncer <=
- [nongnu] elpa/swift-mode c882c70 053/496: Replace `if` exprs with `when` exprs, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode d136303 076/496: Adding support for Swift REPL via comint-mode, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 286c4b8 077/496: Merge pull request #21 from ap4y/feature_repl, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode e0fba71 083/496: repl -> REPL, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 48cbf55 088/496: Extend a bit installation instructions for package.el, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 7e77211 087/496: Remove redundant autoload, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode a092adc 091/496: Return default paragraph definitions., ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode c9c1271 089/496: Make flycheck work with multiple sources, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 4ac4ea3 098/496: Add font-lock for declaration expression with destructuring, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 6abb788 121/496: Add keywords header, ELPA Syncer, 2021/08/29