[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] scratch/expand-region 6ed37a7c58 08/23: feat: add `er/mark-yaml-o
From: |
Stefan Monnier |
Subject: |
[elpa] scratch/expand-region 6ed37a7c58 08/23: feat: add `er/mark-yaml-outer-block` & `er/mark-yaml-inner-block` |
Date: |
Sun, 15 Oct 2023 20:53:32 -0400 (EDT) |
branch: scratch/expand-region
commit 6ed37a7c58549da761f5d9b6864192ff5e535bec
Author: Aaron Gonzales <aaronzinho@ucla.edu>
Commit: Magnar Sveen <magnars@gmail.com>
feat: add `er/mark-yaml-outer-block` & `er/mark-yaml-inner-block`
This also makes some fixes to marking for list items
---
yaml-mode-expansions.el | 74 ++++++++++++++++++++++++++++++++++++-------------
1 file changed, 54 insertions(+), 20 deletions(-)
diff --git a/yaml-mode-expansions.el b/yaml-mode-expansions.el
index 72c17bd62b..902285f178 100644
--- a/yaml-mode-expansions.el
+++ b/yaml-mode-expansions.el
@@ -24,6 +24,9 @@
;; - er/mark-yaml-key-value
;; - er/mark-yaml-list-item
;; - er/mark-yaml-block
+;; - er/mark-yaml-outer-block
+;; - er/mark-yaml-inner-block
+
;;; Code:
@@ -45,8 +48,7 @@
(defvar er--yaml-list-item-regex
(rx (seq "- "
(one-or-more
- (any "0-9A-Za-z" " '_-"))
- "\n")))
+ (any "0-9A-Za-z" "\"':=_-")))))
(defvar er--yaml-block-regex
(rx (seq (zero-or-more
@@ -69,8 +71,39 @@
(when (looking-at regex)
(set-mark (line-end-position))))
+(defun er/mark-yaml-block-static-base (regex)
+ "Mark yaml block based on REGEX passed. NEXT-INDENT-LEVEL can be used to
search outer blocks when necessary."
+ ;; go bac to indentation so always can get regexp
+ (back-to-indentation)
+ ;; make sure the cursor is set inside the block
+ ;; mark point at this higher code block
+ (set-mark (point))
+ ;; save level of this blocks indentation
+ (let ((block-indentation (current-indentation)))
+ (forward-line 1)
+ (while (and
+ ;; No need to go beyond the end of the buffer. Can't use
+ ;; eobp as the loop places the point at the beginning of
+ ;; line, but eob might be at the end of the line.
+ (not (= (point-max) (point-at-eol)))
+ ;; Proceed if: indentation is too deep
+ (or (> (current-indentation) block-indentation)
+ ;; Looking at an empty line
+ (looking-at (rx line-start (* whitespace) line-end))
+ ;; We're not looking at the start of a YAML block
+ ;; and the indent is deeper than the block's indent
+ (and (not (looking-at regex))
+ (> (current-indentation) block-indentation))))
+ (forward-line 1)
+ (back-to-indentation))
+ ;; Find the end of the block by skipping comments backwards
+ (python-util-forward-comment -1)
+ (exchange-point-and-mark))
+ (back-to-indentation))
+
(defun er/mark-yaml-block-base (regex &optional next-indent-level)
"Mark yaml block based on REGEX passed. NEXT-INDENT-LEVEL can be used to
search outer blocks when necessary."
+ ;; go bac to indentation so always can get regexp
(back-to-indentation)
;; make sure the cursor is set inside the block
(let ((next-indent-level
@@ -78,10 +111,10 @@
;; Use the given level
next-indent-level
;; used to mark current block
- ;; if true then at start of block and wanna mark itself
- ;; else were are inside the block already and will mark it)))
- ;; move up the code unti a parent code block is reached
(er--get-regex-indentation-level regex))))
+ ;; if true then at start of block and wanna mark itself
+ ;; else were are inside the block already and will mark it)))
+ ;; move up the code unti a parent code block is reached
(while (and (>= (current-indentation) next-indent-level)
(not (eq (current-indentation) 0)))
(re-search-backward regex (point-min) t)
@@ -100,7 +133,7 @@
(or (> (current-indentation) block-indentation)
;; Looking at an empty line
(looking-at (rx line-start (* whitespace) line-end))
- ;; We're not looking at the start of a Python block
+ ;; We're not looking at the start of a YAML block
;; and the indent is deeper than the block's indent
(and (not (looking-at regex))
(> (current-indentation) block-indentation))))
@@ -121,33 +154,34 @@
(interactive)
(er/mark-yaml-line-base er--yaml-list-item-regex))
-(defun er/mark-yaml-block ()
- "Mark the yaml block that surrounds the block around point.
-
-Command that wraps `er/mark-yaml-block-base'."
+(defun er/mark-yaml-inner-block ()
+ "Mark the yaml contents of the block at point. Command that wraps
`er/mark-yaml-block-base'."
(interactive)
- (er/mark-yaml-block-base er--yaml-block-regex))
+ (er/mark-yaml-block-base er--yaml-block-regex (current-indentation))
+ (forward-line)
+ (back-to-indentation))
-(defun er/mark-outer-yaml-block ()
- "Mark the outer yaml block that surrounds the block around point.
+(defun er/mark-yaml-block ()
+ "Mark the yaml block that point is currently at the top of. Command that
wraps `er/mark-yaml-block-base'."
+ (interactive)
+ (er/mark-yaml-block-static-base er--yaml-block-regex))
-Command that wraps `er/mark-yaml-block-base'."
+(defun er/mark-yaml-outer-block ()
+ "Mark the outer yaml block that surrounds the block around point. Command
that wraps `er/mark-yaml-block-base'."
(interactive)
(er/mark-yaml-block-base er--yaml-block-regex (current-indentation)))
(defun er/add-yaml-mode-expansions ()
"Add yaml-mode-specific expansions for buffers in yaml-mode."
- (let ((try-expand-list-additions '(
- er/mark-symbol
+ (let ((try-expand-list-additions '(er/mark-symbol
er/mark-outside-quotes
er/mark-yaml-list-item
er/mark-yaml-key-value
er/mark-yaml-block
- er/mark-outer-yaml-block
- mark-page)))
+ er/mark-yaml-outer-block
+ er/mark-yaml-inner-block)))
(set (make-local-variable 'expand-region-skip-whitespace) nil)
- (set (make-local-variable 'er/try-expand-list)
- (append try-expand-list-additions er/try-expand-list))))
+ (set (make-local-variable 'er/try-expand-list) try-expand-list-additions)))
(er/enable-mode-expansions 'yaml-mode 'er/add-yaml-mode-expansions)
- [elpa] branch scratch/expand-region created (now d919353455), Stefan Monnier, 2023/10/15
- [elpa] scratch/expand-region 09b44427b2 03/23: Add badges to README., Stefan Monnier, 2023/10/15
- [elpa] scratch/expand-region ea6b4cbb99 02/23: Make usage message optional, Stefan Monnier, 2023/10/15
- [elpa] scratch/expand-region 4b8322774d 04/23: Add installation instruction for use-package, Stefan Monnier, 2023/10/15
- [elpa] scratch/expand-region 6ed37a7c58 08/23: feat: add `er/mark-yaml-outer-block` & `er/mark-yaml-inner-block`,
Stefan Monnier <=
- [elpa] scratch/expand-region 519e92eabc 09/23: Merge pull request #265 from jcs-PR/badge, Stefan Monnier, 2023/10/15
- [elpa] scratch/expand-region dc161a3395 12/23: Revert "refact: replace `if` with `unless` or `when` for more idiomatic code", Stefan Monnier, 2023/10/15
- [elpa] scratch/expand-region 09ebc2df5c 13/23: Revert "feat: add `yaml-mode` expansions", Stefan Monnier, 2023/10/15
- [elpa] scratch/expand-region 51e0740e1f 14/23: Revert "Revert "feat: add `yaml-mode` expansions"", Stefan Monnier, 2023/10/15
- [elpa] scratch/expand-region 0481966ae4 16/23: Revert "Revert "fix: misplaced parenthesis"", Stefan Monnier, 2023/10/15
- [elpa] scratch/expand-region 530a593f5e 17/23: Revert "Revert "feat: add `er/mark-yaml-outer-block` & `er/mark-yaml-inner-block`"", Stefan Monnier, 2023/10/15
- [elpa] scratch/expand-region 7e5bbe2763 20/23: Add GPLv3 license file (#272), Stefan Monnier, 2023/10/15
- [elpa] scratch/expand-region 40049c1243 01/23: Replace deprecated 'cl package by 'cl-lib, Stefan Monnier, 2023/10/15
- [elpa] scratch/expand-region 4168da1b9d 05/23: feat: add `yaml-mode` expansions, Stefan Monnier, 2023/10/15
- [elpa] scratch/expand-region 8f1e82537d 06/23: refact: replace `if` with `unless` or `when` for more idiomatic code, Stefan Monnier, 2023/10/15