Skip to content

Commit

Permalink
Resolved nested by checking naming.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed May 19, 2020
1 parent 3ed29db commit 8f97da8
Showing 1 changed file with 54 additions and 28 deletions.
82 changes: 54 additions & 28 deletions auto-rename-tag.el
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
;; Description: Automatically rename paired HTML/XML tag.
;; Keyword: auto-complete html rename tag xml
;; Version: 0.2.1
;; Package-Requires: ((emacs "24.4"))
;; Package-Requires: ((emacs "24.4") (cl-lib "0.6"))
;; URL: https://github.com/jcs090218/auto-rename-tag

;; This file is NOT part of GNU Emacs.
Expand All @@ -32,6 +32,8 @@

;;; Code:

(require 'cl-lib)

(defgroup auto-rename-tag nil
"Automatically rename paired HTML/XML tag."
:prefix "auto-rename-tag-"
Expand Down Expand Up @@ -179,6 +181,9 @@ DNC : duplicate nested count."
(unless current-word (setq current-word ""))
(unless name (setq name ""))

(unless (string= current-word name)
(setq is-end-tag t))

;; If closing tag.
(when is-end-tag
(when (string= current-word name)
Expand Down Expand Up @@ -215,6 +220,9 @@ DNC : duplicate nested count."
(unless current-word (setq current-word ""))
(unless name (setq name ""))

(unless (string= current-word name)
(setq is-end-tag nil))

;; If closing tag.
(when is-end-tag
(unless (= dup-nested-count 0)
Expand Down Expand Up @@ -296,6 +304,47 @@ DNC : duplicate nested count."
(setq tag-name (buffer-substring-no-properties tag-start tag-end))))
tag-name))

(defun auto-rename-tag--resolve-nested (direct)
"Resolved nested level by DIRECT.
DIRECT can be either only 'backward and 'forward."
(let ((nested-count 0) (is-closing-tag nil))
;; Get nested count.
(setq nested-count
(cl-case direct
('backward (auto-rename-tag--backward-count-nested-close-tag auto-rename-tag--record-prev-word))
('forward (auto-rename-tag--forward-count-nested-open-tag auto-rename-tag--record-prev-word))))

(message "nested-count: %s" nested-count)

;; Resolve nested.
(while (not (= nested-count 0))
(setq nested-count (- nested-count 1))
(cl-case direct
('backward
(auto-rename-tag--goto-backward-tag-name auto-rename-tag--record-prev-word)
(auto-rename-tag--goto-backward-tag-name auto-rename-tag--record-prev-word))
('forward
(auto-rename-tag--goto-forward-tag-name auto-rename-tag--record-prev-word)
(auto-rename-tag--goto-forward-tag-name auto-rename-tag--record-prev-word))))

;; Goto the target pair.
(cl-case direct
('backward
(auto-rename-tag--goto-backward-tag-name auto-rename-tag--record-prev-word))
('forward
(auto-rename-tag--goto-forward-tag-name auto-rename-tag--record-prev-word)
(ignore-errors (forward-char 1))))

(setq is-closing-tag (auto-rename-tag--is-closing-tag-p))

(message "%s : %s" (point) nested-count)

(cl-case direct
('backward
(when is-closing-tag (auto-rename-tag--resolve-nested direct)))
('forward
(unless is-closing-tag (auto-rename-tag--resolve-nested direct))))))


(defun auto-rename-tag--before-change-functions (_begin _end)
"Do stuff before buffer is changed.
Expand Down Expand Up @@ -331,8 +380,7 @@ LENGTH : deletion length."
(when auto-rename-tag--pre-command-actived
(save-excursion
(let ((is-end-tag nil)
(current-word "") (pair-tag-word "")
(nested-count 0))
(current-word "") (pair-tag-word ""))
;; Goto the first character inside the tag.
(auto-rename-tag--goto-the-start-of-tag-name)

Expand All @@ -343,18 +391,7 @@ LENGTH : deletion length."
(unless (string= current-word auto-rename-tag--record-prev-word)
;; NOTE: Is closing tag.
(when is-end-tag
;; Get nested count.
(setq nested-count
(auto-rename-tag--backward-count-nested-close-tag auto-rename-tag--record-prev-word))

;; Resolve nested.
(while (not (= nested-count 0))
(setq nested-count (- nested-count 1))
(auto-rename-tag--goto-backward-tag-name auto-rename-tag--record-prev-word)
(auto-rename-tag--goto-backward-tag-name auto-rename-tag--record-prev-word))

;; Goto the target pair.
(auto-rename-tag--goto-backward-tag-name auto-rename-tag--record-prev-word)
(auto-rename-tag--resolve-nested 'backward)

;; Get the tag name and ready to be compare.
(setq pair-tag-word (auto-rename-tag--get-tag-name-at-point))
Expand All @@ -371,20 +408,9 @@ LENGTH : deletion length."

;; NOTE: Is opening tag.
(unless is-end-tag
;; Get nested count.
(setq nested-count
(auto-rename-tag--forward-count-nested-open-tag auto-rename-tag--record-prev-word))

;; Resolve nested.
(while (not (= nested-count 0))
(setq nested-count (- nested-count 1))
(auto-rename-tag--goto-forward-tag-name auto-rename-tag--record-prev-word)
(auto-rename-tag--goto-forward-tag-name auto-rename-tag--record-prev-word))

;; Goto the target pair.
(auto-rename-tag--goto-forward-tag-name auto-rename-tag--record-prev-word)
(auto-rename-tag--resolve-nested 'forward)

(ignore-errors (forward-char 1))
(setq is-end-tag (auto-rename-tag--is-closing-tag-p))

;; Get the tag name and ready to be compare.
(setq pair-tag-word (auto-rename-tag--get-tag-name-at-point))
Expand Down

0 comments on commit 8f97da8

Please sign in to comment.