A JavaScript development environment for Emacs https://indium.readthedocs.io
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
4.6 KiB

  1. ;;; indium-debugger-frames.el --- List the stack frame -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2017 Nicolas Petton
  3. ;; Author: Nicolas Petton <nicolas@petton.fr>
  4. ;; This program is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ;;; Commentary:
  15. ;;
  16. ;;; Code:
  17. (require 'indium-render)
  18. (require 'indium-script)
  19. (require 'indium-structs)
  20. (declare-function indium-debugger-select-frame "indium-debugger.el")
  21. (defun indium-debugger-stack-frames ()
  22. "List the stack frames in a separate buffer and switch to it."
  23. (interactive)
  24. (let ((buf (indium-debugger-frames-get-buffer-create))
  25. (inhibit-read-only t))
  26. (with-current-buffer buf
  27. (indium-debugger-frames-list (indium-current-connection-frames)
  28. (indium-current-connection-current-frame)))
  29. (pop-to-buffer buf)))
  30. (defun indium-debugger-frames-maybe-refresh ()
  31. "When a buffer listing the stack frames is open, refresh it."
  32. (interactive)
  33. (let ((buf (indium-debugger-frames-get-buffer))
  34. (inhibit-read-only t))
  35. (when buf
  36. (with-current-buffer buf
  37. (indium-debugger-frames-list (indium-current-connection-frames)
  38. (indium-current-connection-current-frame))))))
  39. (defun indium-debugger-frames-list (frames &optional current-frame)
  40. "Render the list of stack frames FRAMES.
  41. CURRENT-FRAME is the current stack frame in the debugger."
  42. (save-excursion
  43. (erase-buffer)
  44. (indium-render-header "Debugger stack")
  45. (newline 2)
  46. (seq-doseq (frame frames)
  47. (indium-render-frame frame
  48. (indium-script-url (indium-frame-script frame))
  49. (eq current-frame frame))
  50. (newline))))
  51. (defun indium-debugger-frames-select-frame (frame)
  52. "Select FRAME and switch to the corresponding debugger buffer."
  53. (interactive)
  54. (indium-debugger-select-frame frame))
  55. (defun indium-debugger-frames-next-frame ()
  56. "Go to the next frame in the stack."
  57. (interactive)
  58. (indium-debugger-frames-goto-next 'next))
  59. (defun indium-debugger-frames-previous-frame ()
  60. "Go to the previos frame in the stack."
  61. (interactive)
  62. (indium-debugger-frames-goto-next 'previous))
  63. (defun indium-debugger-frames-goto-next (direction)
  64. "Go to the next frame in DIRECTION."
  65. (let ((next (eq direction 'next)))
  66. (forward-line (if next 1 -1))
  67. (back-to-indentation)
  68. (while (and (not (if next
  69. (eobp)
  70. (bobp)))
  71. (not (get-text-property (point) 'indium-action)))
  72. (forward-char (if next 1 -1)))))
  73. (defun indium-debugger-frames-get-buffer ()
  74. "Return the buffer listing frames for the current connection.
  75. If no buffer is found, return nil."
  76. (get-buffer (indium-debugger-frames-buffer-name)))
  77. (defun indium-debugger-frames-buffer-name ()
  78. "Return the name of the frames buffer for the current connection."
  79. "*JS Frames*")
  80. (defun indium-debugger-frames-get-buffer-create ()
  81. "Create a buffer for listing frames unless one exists, and return it."
  82. (let ((buf (indium-debugger-frames-get-buffer)))
  83. (unless buf
  84. (setq buf (generate-new-buffer (indium-debugger-frames-buffer-name)))
  85. (indium-debugger-frames-setup-buffer buf))
  86. buf))
  87. (defun indium-debugger-frames-setup-buffer (buffer)
  88. "Setup the frames BUFFER."
  89. (with-current-buffer buffer
  90. (indium-debugger-frames-mode)
  91. (setq-local truncate-lines nil)))
  92. (defvar indium-debugger-frames-mode-map
  93. (let ((map (make-sparse-keymap)))
  94. (define-key map [return] #'indium-follow-link)
  95. (define-key map (kbd "C-m") #'indium-follow-link)
  96. (define-key map (kbd "n") #'indium-debugger-frames-next-frame)
  97. (define-key map (kbd "p") #'indium-debugger-frames-previous-frame)
  98. (define-key map [tab] #'indium-debugger-frames-next-frame)
  99. (define-key map [backtab] #'indium-debugger-frames-previous-frame)
  100. map))
  101. (define-derived-mode indium-debugger-frames-mode special-mode "Frames"
  102. "Major mode visualizind and navigating the JS stack.
  103. \\{indium-debugger-frames--mode-map}"
  104. (setq buffer-read-only t)
  105. (font-lock-ensure)
  106. (read-only-mode))
  107. (provide 'indium-debugger-frames)
  108. ;;; indium-debugger-frames.el ends here