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

feat: Add Assembly support #87

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
* Add Erlang support (#83)
* Add basic Pascal support (#84)
* Add CMake support (#85)
* Add Assembly support (#86)

## 0.2.0
> Released Sep 01, 2023
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ If evil mode is loaded, then these commands are also added to the evil folding l

These languages are fairly complete:

- Assembly
- Bash / Beancount
- C / C++ / C# / Clojure / CMake / CSS
- Dart
Expand Down
8 changes: 8 additions & 0 deletions ts-fold-parsers.el
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
(declare-function ts-fold-range-block-comment "ts-fold.el")
(declare-function ts-fold-range-c-like-comment "ts-fold.el")

(declare-function ts-fold-range-asm-label "ts-fold.el")
(declare-function ts-fold-range-beancount-transaction "ts-fold.el")
(declare-function ts-fold-range-c-preproc-ifdef "ts-fold.el")
(declare-function ts-fold-range-c-preproc-if "ts-fold.el")
Expand Down Expand Up @@ -84,6 +85,13 @@
"Rule set for Agda."
'(()))

(defun ts-fold-parsers-asm ()
"Rule set for Assembly."
'((label . ts-fold-range-asm-label)
(line_comment
. (lambda (node offset)
(ts-fold-range-line-comment node offset ";;")))))

(defun ts-fold-parsers-bash ()
"Rule set for Bash."
'((compound_statement . ts-fold-range-seq)
Expand Down
3 changes: 3 additions & 0 deletions ts-fold-summary.el
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ type of content by checking the word boundary's existence."
;; TODO(everyone): keep this alist alphabetically sorted
(defcustom ts-fold-summary-parsers-alist
`((actionscript-mode . ts-fold-summary-javadoc)
(fasm-mode . ts-fold-summary-elisp)
(masm-mode . ts-fold-summary-elisp)
(nasm-mode . ts-fold-summary-elisp)
(bat-mode . ts-fold-summary-batch)
(beancount-mode . ts-fold-summary-elisp)
(c-mode . ts-fold-summary-c)
Expand Down
16 changes: 16 additions & 0 deletions ts-fold-util.el
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@

;;; Code:

;;
;; (@* "String" )
;;

(defun ts-fold-2str (obj)
"Convert OBJ to string."
(format "%s" obj))

;;
;; (@* "Cons" )
;;
Expand Down Expand Up @@ -104,6 +112,14 @@ Optional argument TRIM, see function `ts-fold--get-face'."
"Check to see if IN-VAL is between IN-MIN and IN-MAX."
(and (<= in-min in-val) (<= in-val in-max)))

;;
;; (@* "List" )
;;

(defun ts-fold-listify (obj)
"Ensure OBJ is a list."
(if (listp obj) obj (list obj)))

;;
;; (@* "TS node" )
;;
Expand Down
37 changes: 36 additions & 1 deletion ts-fold.el
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
;; alphabetically sorted
(defcustom ts-fold-range-alist
`((agda-mode . ,(ts-fold-parsers-agda))
(fasm-mode . ,(ts-fold-parsers-asm))
(masm-mode . ,(ts-fold-parsers-asm))
(nasm-mode . ,(ts-fold-parsers-asm))
(beancount-mode . ,(ts-fold-parsers-beancount))
(c-mode . ,(ts-fold-parsers-c))
(c++-mode . ,(ts-fold-parsers-c++))
Expand Down Expand Up @@ -424,6 +427,17 @@ If the current syntax node is not foldable, do nothing."
If NEXT is non-nil, return next sibling. Otherwirse, return previouse sibling."
(if next (tsc-get-next-sibling node) (tsc-get-prev-sibling node)))

(defun ts-fold--next-prev-node-skip-newline (node next)
"Like function `ts-fold--next-prev-node'.

For arguments NODE and NEXT, please see the function `ts-fold--next-prev-node'
for more information."
(let ((iter-node (ts-fold--next-prev-node node next)))
(while (and iter-node
(equal "\n" (tsc-node-text iter-node)))
(setq iter-node (ts-fold--next-prev-node iter-node next)))
iter-node))

(defun ts-fold--continuous-node-prefix (node prefix next)
"Iterate through node starting from NODE and compare node-text to PREFIX;
then return the last iterated node.
Expand All @@ -443,7 +457,7 @@ in backward direction."
(setq last-node iter-node last-line line
last-line-range (1+ (s-count-matches "\n" text)))
(setq break t))
(setq iter-node (ts-fold--next-prev-node iter-node next)))
(setq iter-node (ts-fold--next-prev-node-skip-newline iter-node next)))
last-node))

(defun ts-fold--one-liner-node (node)
Expand Down Expand Up @@ -516,6 +530,27 @@ more information."
;; (@* "Languages" )
;;

(defun ts-fold-range-asm--find-last-instruction (node)
"Find the last instruction node by starting NODE."
(let* ((iter-node (ts-fold--next-prev-node-skip-newline node t))
(last iter-node))
(while (and iter-node
(not (member (ts-fold-2str (tsc-node-type iter-node))
(ts-fold-listify "label"))))
(setq last iter-node
iter-node (ts-fold--next-prev-node-skip-newline iter-node t)))
last)) ; return last insturction node

(defun ts-fold-range-asm-label (node offset)
"Define fold range for `label' in Assembly.

For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(when-let* ((beg (tsc-node-end-position node))
(end (ts-fold-range-asm--find-last-instruction node))
(end (tsc-node-end-position end)))
(ts-fold--cons-add (cons beg end) offset)))

(defun ts-fold-range-beancount-transaction (node offset)
"Define fold range for `transaction' in Beancount.

Expand Down
Loading