summaryrefslogtreecommitdiff
path: root/yasnippet-debug.el
blob: b12bcd4ba2610db8add1a9f25cb5073a76f03874 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
;;; yasnippet-debug.el --- debug functions for yasnippet

;; Copyright (C) 2010, 2013, 2014  Free Software Foundation, Inc.

;; Author: Jo�o T�vora
;; Keywords: emulations, convenience

;; 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:

;; Just some debug functions

;;; Code:

(require 'yasnippet)
(require 'cl)

(defun yas-debug-snippet-vars ()
  "Debug snippets, fields, mirrors and the `buffer-undo-list'."
  (interactive)
  (with-output-to-temp-buffer "*YASnippet trace*"
    (princ "Interesting YASnippet vars: \n\n")

    (princ (format "\nPost command hook: %s\n" post-command-hook))
    (princ (format "\nPre  command hook: %s\n" pre-command-hook))

    (princ (format "%s live snippets in total\n" (length (yas--snippets-at-point (quote all-snippets)))))
    (princ (format "%s overlays in buffer:\n\n" (length (overlays-in (point-min) (point-max)))))
    (princ (format "%s live snippets at point:\n\n" (length (yas--snippets-at-point))))


    (dolist (snippet (yas--snippets-at-point))
      (princ (format "\tsid: %d control overlay from %d to %d\n"
                     (yas--snippet-id snippet)
                     (overlay-start (yas--snippet-control-overlay snippet))
                     (overlay-end (yas--snippet-control-overlay snippet))))
      (princ (format "\tactive field: %s from %s to %s covering \"%s\"\n"
                     (yas--field-number (yas--snippet-active-field snippet))
                     (marker-position (yas--field-start (yas--snippet-active-field snippet)))
                     (marker-position (yas--field-end (yas--snippet-active-field snippet)))
                     (buffer-substring-no-properties (yas--field-start (yas--snippet-active-field snippet)) (yas--field-end (yas--snippet-active-field snippet)))))
      (when (yas--snippet-exit snippet)
        (princ (format "\tsnippet-exit: at %s next: %s\n"
                       (yas--exit-marker (yas--snippet-exit snippet))
                       (yas--exit-next (yas--snippet-exit snippet)))))
      (dolist (field (yas--snippet-fields snippet))
        (princ (format "\tfield: %s from %s to %s covering \"%s\" next: %s%s\n"
                       (yas--field-number field)
                       (marker-position (yas--field-start field))
                       (marker-position (yas--field-end field))
                       (buffer-substring-no-properties (yas--field-start field) (yas--field-end field))
                       (yas--debug-format-fom-concise (yas--field-next field))
                       (if (yas--field-parent-field field) "(has a parent)" "")))
        (dolist (mirror (yas--field-mirrors field))
          (princ (format "\t\tmirror: from %s to %s covering \"%s\" next: %s\n"
                         (marker-position (yas--mirror-start mirror))
                         (marker-position (yas--mirror-end mirror))
                         (buffer-substring-no-properties (yas--mirror-start mirror) (yas--mirror-end mirror))
                         (yas--debug-format-fom-concise (yas--mirror-next mirror)))))))

    (princ (format "\nUndo is %s and point-max is %s.\n"
                   (if (eq buffer-undo-list t)
                       "DISABLED"
                     "ENABLED")
                   (point-max)))
    (unless (eq buffer-undo-list t)
      (princ (format "Undpolist has %s elements. First 10 elements follow:\n" (length buffer-undo-list)))
      (let ((first-ten (subseq buffer-undo-list 0 (min 19
                                                       (length buffer-undo-list)))))
        (dolist (undo-elem first-ten)
          (princ (format "%2s:  %s\n" (position undo-elem first-ten) (truncate-string-to-width (format "%s" undo-elem) 70))))))))

(defun yas--debug-format-fom-concise (fom)
  (when fom
    (cond ((yas--field-p fom)
           (format "field %s from %d to %d"
                   (yas--field-number fom)
                   (marker-position (yas--field-start fom))
                   (marker-position (yas--field-end fom))))
          ((yas--mirror-p fom)
           (format "mirror from %d to %d"
                   (marker-position (yas--mirror-start fom))
                   (marker-position (yas--mirror-end fom))))
          (t
           (format "snippet exit at %d"
                   (marker-position (yas--fom-start fom)))))))


(defun yas-exterminate-package ()
  (interactive)
  (yas-global-mode -1)
  (yas-minor-mode -1)
  (mapatoms #'(lambda (atom)
                (when (string-match "yas[-/]" (symbol-name atom))
                  (unintern atom obarray)))))

(defun yas-debug-test (&optional quiet)
  (interactive "P")
  (yas-load-directory (or (and (listp yas-snippet-dirs)
                               (first yas-snippet-dirs))
                          yas-snippet-dirs
                          "~/Source/yasnippet/snippets/"))
  (set-buffer (switch-to-buffer "*YAS TEST*"))
  (mapc #'yas--commit-snippet (yas--snippets-at-point 'all-snippets))
  (erase-buffer)
  (setq buffer-undo-list nil)
  (setq undo-in-progress nil)
  (snippet-mode)
  (yas-minor-mode 1)
  (let ((abbrev))
    (setq abbrev "$f")
    (insert abbrev))
  (unless quiet
    (add-hook 'post-command-hook 'yas-debug-snippet-vars 't 'local)))

(provide 'yasnippet-debug)
;; Local Variables:
;; indent-tabs-mode: nil
;; byte-compile-warnings: (not cl-functions)
;; End:
;;; yasnippet-debug.el ends here