Skip to content

Commit

Permalink
Move deletion marking into AST
Browse files Browse the repository at this point in the history
Resolves #13.
  • Loading branch information
Chris Barrett committed Apr 9, 2017
1 parent 142c24a commit d62539c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
1 change: 1 addition & 0 deletions contributing.org
Expand Up @@ -128,4 +128,5 @@ sugared
| ( nav-prop ( navtype SEXP+ ) ast* )
| ( copy-prop STRING ast* )
| ( key-value keywidth STRING STRING )
| ( mark-for-delete ast* )
#+END_EXAMPLE
41 changes: 33 additions & 8 deletions kubernetes.el
Expand Up @@ -946,6 +946,15 @@ Warning: This could blow the stack if the AST gets too deep."
,inner-ast)
indent-level))

(`(mark-for-delete . ,inner-ast)
(let ((pt (point)))
(kubernetes--eval-ast inner-ast indent-level)
(let ((end-line (line-number-at-pos)))
(save-excursion
(goto-char pt)
(while (< (line-number-at-pos) end-line)
(kubernetes--insert-delete-mark-for-line-at-pt (point))
(forward-line 1))))))

((and actions (pred listp))
(dolist (action actions)
Expand All @@ -955,6 +964,21 @@ Warning: This could blow the stack if the AST gets too deep."
(x
(error "Unknown AST form: %s" x)))))

(defun kubernetes--insert-delete-mark-for-line-at-pt (point)
(save-excursion
(goto-char point)
(goto-char (line-beginning-position))
(let* ((existing-props (text-properties-at (point)))
(props (append existing-props '(face kubernetes-delete-mark)))
(mark-str (concat (apply #'propertize "D" props)
(apply #'propertize " " existing-props))))
(cond
((looking-at-p (rx bol space space))
(delete-char 2)
(insert mark-str))
(t
(insert mark-str))))))


;; Context section rendering.

Expand Down Expand Up @@ -1074,7 +1098,7 @@ Warning: This could blow the stack if the AST gets too deep."
((member name kubernetes--pods-pending-deletion)
`(propertize (face kubernetes-pending-deletion) ,line))
((member name kubernetes--marked-pod-names)
`(propertize (face kubernetes-delete-mark) ,line))
`(mark-for-delete ,line))
(t
line))))))

Expand Down Expand Up @@ -1161,7 +1185,7 @@ Warning: This could blow the stack if the AST gets too deep."
((member name kubernetes--configmaps-pending-deletion)
`(propertize (face kubernetes-pending-deletion) ,line))
((member name kubernetes--marked-configmap-names)
`(propertize (face kubernetes-delete-mark) ,line))
`(mark-for-delete ,line))
(t
line))))))

Expand Down Expand Up @@ -1237,7 +1261,7 @@ Warning: This could blow the stack if the AST gets too deep."
((member name kubernetes--secrets-pending-deletion)
`(propertize (face kubernetes-pending-deletion) ,line))
((member name kubernetes--marked-secret-names)
`(propertize (face kubernetes-delete-mark) ,line))
`(mark-for-delete ,line))
(t
line))))))

Expand Down Expand Up @@ -1337,7 +1361,7 @@ Warning: This could blow the stack if the AST gets too deep."
((member name kubernetes--services-pending-deletion)
`(propertize (face kubernetes-pending-deletion) ,line))
((member name kubernetes--marked-service-names)
`(propertize (face kubernetes-delete-mark) ,line))
`(mark-for-delete ,line))
(t
line))))))

Expand Down Expand Up @@ -1677,9 +1701,10 @@ POD-NAME is the name of the pod to display."
(add-to-list 'kubernetes--marked-secret-names secret-name)))
(_
(user-error "Nothing here can be marked")))
(kubernetes--redraw-buffers)
(goto-char point)
(forward-line 1))

(let ((inhibit-read-only t))
(kubernetes--insert-delete-mark-for-line-at-pt point))
(magit-section-forward))

(defun kubernetes-unmark (point)
"Unmark the thing at POINT, then advance to the next line."
Expand All @@ -1693,7 +1718,7 @@ POD-NAME is the name of the pod to display."
(setq kubernetes--marked-configmap-names (delete configmap-name kubernetes--marked-configmap-names))))
(kubernetes--redraw-buffers)
(goto-char point)
(forward-line 1))
(magit-section-forward))

(defun kubernetes-unmark-all ()
"Unmark everything in the buffer."
Expand Down
30 changes: 30 additions & 0 deletions test/eval-ast-test.el
Expand Up @@ -178,4 +178,34 @@
(should-error (kubernetes--eval-ast ast)))))


;; mark-for-delete

(ert-deftest eval-ast--mark-for-delete-no-indentation ()
(let ((ast '(mark-for-delete (line "Test"))))
(with-temp-buffer
(save-excursion (kubernetes--eval-ast ast))
(should (equal "D Test\n" (buffer-string)))
(should (equal '(face kubernetes-delete-mark)
(text-properties-at 0 (buffer-string))))
(should (not (text-properties-at 1 (buffer-string)))))))

(ert-deftest eval-ast--mark-for-delete-with-indentation ()
(let ((ast '(mark-for-delete (line " Test"))))
(with-temp-buffer
(save-excursion (kubernetes--eval-ast ast))
(should (equal "D Test\n" (buffer-string)))
(should (equal '(face kubernetes-delete-mark)
(text-properties-at 0 (buffer-string))))
(should (not (text-properties-at 1 (buffer-string)))))))

(ert-deftest eval-ast--mark-for-delete-multiple-lines ()
(let ((ast '((mark-for-delete (line "foo")
(line "bar")
(line "baz"))
(line "frotz"))))
(with-temp-buffer
(save-excursion (kubernetes--eval-ast ast))
(should (equal "D foo\nD bar\nD baz\nfrotz\n" (buffer-string))))))


;;; eval-ast-test.el ends here

0 comments on commit d62539c

Please sign in to comment.