Browse Source

Update breakpoints when a script is parsed

workspaces
Nicolas Petton 4 years ago
parent
commit
c6678b7a2d
4 changed files with 77 additions and 45 deletions
  1. +56
    -35
      indium-breakpoint.el
  2. +0
    -1
      indium-v8.el
  3. +9
    -9
      test/unit/indium-breakpoint-test.el
  4. +12
    -0
      test/unit/indium-v8-test.el

+ 56
- 35
indium-breakpoint.el View File

@ -42,7 +42,7 @@ CONDITION is true."
(let* ((brk (make-indium-breakpoint :file (indium-location-file location)
:line (indium-location-line location)
:condition (or condition ""))))
(indium-breakpoint-add-overlay brk)
(indium-breakpoint--add-overlay brk)
(when-indium-connected
(indium-backend-add-breakpoint (indium-current-connection-backend)
brk))))
@ -64,13 +64,23 @@ CONDITION is true."
(when-indium-connected
(indium-backend-remove-breakpoint (indium-current-connection-backend)
(indium-breakpoint-id brk)))
(indium-breakpoint-remove-overlay)))
(indium-breakpoint--remove-overlay)))
(defun indium-breakpoint-remove-all ()
"Remove all breakpoints from the current buffer's file."
(indium-breakpoint-remove-all-overlays)
(indium-breakpoint-remove-breakpoints-from-backend))
(defun indium-breakpoint-at-point ()
"Return the breakpoint on the current line.
If there is no breakpoint set on the line, return nil."
(when-let ((ov (indium-breakpoint--overlay-on-current-line)))
(overlay-get ov 'indium-breakpoint)))
(defun indium-breakpoint-on-current-line-p ()
"Return non-nil if there is a breakpoint on the current line."
(not (null (indium-breakpoint--overlay-on-current-line))))
(defun indium-breakpoint-add-breakpoints-to-buffer ()
"Add all breakpoints markers to the current buffer.
This function does not add breakpoints."
@ -78,7 +88,7 @@ This function does not add breakpoints."
(save-excursion
(goto-char (point-min))
(forward-line (indium-location-line (indium-breakpoint-location brk)))
(indium-breakpoint-add-overlay brk)))
(indium-breakpoint--add-overlay brk)))
(indium-current-connection-get-breakpoints-in-file buffer-file-name)))
(defun indium-breakpoint-remove-all-overlays ()
@ -89,12 +99,6 @@ This function does no unset breakpoints."
'indium-breakpoint-ov
t))
(defun indium-breakpoint-update-breakpoints ()
"Update all breakpoints for the current buffer in the backend."
(when-indium-connected
(indium-breakpoint-remove-breakpoints-from-backend)
(indium-breakpoint-restore-breakpoints)))
(defun indium-breakpoint-remove-breakpoints-from-backend ()
"Remove all breakpoints from the current buffer.
This function does not remove any breakpoint overlay."
@ -103,25 +107,21 @@ This function does not remove any breakpoint overlay."
(indium-breakpoint-id brk)))
(indium-current-connection-get-breakpoints-in-file buffer-file-name)))
(defun indium-breakpoint-restore-breakpoints ()
"Restore all breakpoints set in the current buffer.
This function is used when reconnecting to a new connection."
(save-excursion
(let ((overlays (overlays-in (point-min) (point-max))))
(seq-doseq (ov overlays)
(when-let ((brk (overlay-get ov 'indium-breakpoint))
(start (overlay-start ov)))
(goto-char start)
(indium-breakpoint-add (indium-script-generated-location-at-point)
(indium-breakpoint-condition brk)))))))
(defun indium-breakpoint-restore-breakpoints-in-all-buffers ()
"Restore breakpoints in all buffers."
(seq-doseq (buf (buffer-list))
(with-current-buffer buf
(indium-breakpoint-restore-breakpoints))))
(indium-breakpoint--restore-breakpoints-in-current-buffer))))
(defun indium-breakpoint-update-breakpoints-in-all-buffers ()
"Update all breakpoints in all buffers."
(seq-doseq (buf (buffer-list))
(with-current-buffer buf
(indium-breakpoint--update-breakpoints-in-current-buffer))))
(defun indium-breakpoint-add-overlay (breakpoint)
(defun indium-breakpoint--add-overlay (breakpoint)
"Add an overlay for BREAKPOINT on the current line.
An icon is added to the left fringe."
(let ((ov (indium-breakpoint--ensure-overlay)))
@ -133,20 +133,38 @@ An icon is added to the left fringe."
breakpoint)
ov))
(defun indium-breakpoint-remove-overlay ()
(defun indium-breakpoint--remove-overlay ()
"Remove the breakpoint overlay from the current line."
(let ((ov (indium-breakpoint-overlay-on-current-line)))
(let ((ov (indium-breakpoint--overlay-on-current-line)))
(remove-overlays (overlay-start ov)
(overlay-end ov)
'indium-breakpoint-ov
t)))
(defun indium-breakpoint--update-breakpoints-in-current-buffer ()
"Update the breakpoints for the current buffer in the backend."
(when-indium-connected
(indium-breakpoint-remove-breakpoints-from-backend)
(indium-breakpoint--restore-breakpoints-in-current-buffer)))
(defun indium-breakpoint--restore-breakpoints-in-current-buffer ()
"Restore all breakpoints set in the current buffer.
This function is used when reconnecting to a new connection."
(save-excursion
(let ((overlays (overlays-in (point-min) (point-max))))
(seq-doseq (ov overlays)
(when-let ((brk (overlay-get ov 'indium-breakpoint))
(start (overlay-start ov)))
(goto-char start)
(indium-breakpoint-add (indium-script-generated-location-at-point)
(indium-breakpoint-condition brk)))))))
(defun indium-breakpoint--fringe-icon ()
"Return the fringe icon used for breakpoints."
(propertize "b" 'display
(list 'left-fringe 'indium-breakpoint 'indium-breakpoint-face)))
(defun indium-breakpoint-overlay-on-current-line ()
(defun indium-breakpoint--overlay-on-current-line ()
"Return the breakpoint overlay on the current-line.
If no overlay is present, return nil."
(seq-find (lambda (ov)
@ -156,20 +174,23 @@ If no overlay is present, return nil."
(defun indium-breakpoint--ensure-overlay ()
"Return the breakpoint overlay on the current line.
If there is no overlay, make one."
(or (indium-breakpoint-overlay-on-current-line)
(or (indium-breakpoint--overlay-on-current-line)
(let ((ov (make-overlay (point-at-bol) (point-at-eol) nil t)))
(overlay-put ov 'indium-breakpoint-ov t)
ov)))
(defun indium-breakpoint-at-point ()
"Return the breakpoint on the current line.
If there is no breakpoint set on the line, return nil."
(when-let ((ov (indium-breakpoint-overlay-on-current-line)))
(overlay-get ov 'indium-breakpoint)))
(defun indium-breakpoint--update-after-script-parsed (&rest _)
"Update all breakpoints each time a script has been parsed by the runtime."
(indium-breakpoint-update-breakpoints-in-all-buffers))
(defun indium-breakpoint-on-current-line-p ()
"Return non-nil if there is a breakpoint on the current line."
(not (null (indium-breakpoint-overlay-on-current-line))))
(defun indium-breakpoint--update-after-script-source-set (&rest _)
"Update the breakpoints in the current buffer each time its source is set."
(indium-breakpoint--update-breakpoints-in-current-buffer))
;; Update/Restore breakpoints
(add-hook 'indium-update-script-source-hook #'indium-breakpoint--update-after-script-source-set)
(add-hook 'indium-script-parsed-hook #'indium-breakpoint--update-after-script-parsed)
(add-hook 'indium-connection-open-hook #'indium-breakpoint-restore-breakpoints-in-all-buffers)
(and (display-images-p)
(define-fringe-bitmap 'indium-breakpoint


+ 0
- 1
indium-v8.el View File

@ -328,7 +328,6 @@ If WORKSPACE is non-nil, make it the workspace used for the connection."
(indium-v8--enable-tools)
(switch-to-buffer (indium-repl-buffer-create))
(when workspace (cd workspace))
(indium-breakpoint-restore-breakpoints-in-all-buffers)
(run-hooks 'indium-connection-open-hook))
(defun indium-v8--handle-ws-message (_ws frame)


+ 9
- 9
test/unit/indium-breakpoint-test.el View File

@ -46,7 +46,7 @@
(goto-char (point-min))
(open-line 1)
(forward-line)
(indium-breakpoint-remove-overlay)
(indium-breakpoint--remove-overlay)
(let ((overlays (seq-find (lambda (ov)
(overlay-get ov 'indium-breakpoint-ov))
(overlays-in (point-min) (point-max)))))
@ -62,7 +62,7 @@
(goto-char 5)
(newline)
(forward-line -1)
(indium-breakpoint-remove-overlay)
(indium-breakpoint--remove-overlay)
(let ((overlays (seq-find (lambda (ov)
(overlay-get ov 'indium-breakpoint-ov))
(overlays-in (point-min) (point-max)))))
@ -78,15 +78,15 @@
(it "should be able to access breakpoints on empty lines"
(with-js2-buffer ""
(let ((ov (indium-breakpoint--ensure-overlay)))
(expect (indium-breakpoint-overlay-on-current-line)
(expect (indium-breakpoint--overlay-on-current-line)
:to-be ov))))
(it "should be able to remove overlays on empty lines"
(with-js2-buffer ""
(indium-breakpoint--ensure-overlay)
(expect (indium-breakpoint-overlay-on-current-line) :not :to-be nil)
(indium-breakpoint-remove-overlay)
(expect (indium-breakpoint-overlay-on-current-line) :to-be nil)))
(expect (indium-breakpoint--overlay-on-current-line) :not :to-be nil)
(indium-breakpoint--remove-overlay)
(expect (indium-breakpoint--overlay-on-current-line) :to-be nil)))
(it "can get a breakpoint overlays on the current line when it changed"
(with-js2-buffer "let a = 1;"
@ -94,14 +94,14 @@
(goto-char (point-min))
(insert "\n")
(forward-line 1)
(expect (indium-breakpoint-overlay-on-current-line) :to-be ov)))))
(expect (indium-breakpoint--overlay-on-current-line) :to-be ov)))))
(describe "Accessing breakpoints"
(it "can get a breakpoint overlays on the current line"
(with-js2-buffer "let a = 1;"
(let ((ov (indium-breakpoint--ensure-overlay)))
(overlay-put ov 'indium-breakpoint t)
(expect (indium-breakpoint-overlay-on-current-line) :to-be ov))))
(expect (indium-breakpoint--overlay-on-current-line) :to-be ov))))
(it "can put a breakpoint on the current line"
(with-js2-buffer "let a = 1;"
@ -144,7 +144,7 @@
;; simulate getting the breakpoint ID from a backend
(setf (indium-breakpoint-id (indium-breakpoint-at-point)) 'id)
(with-fake-indium-connection
(indium-breakpoint-restore-breakpoints)
(indium-breakpoint--restore-breakpoints-in-current-buffer)
(expect #'indium-backend-add-breakpoint :to-have-been-called)
(expect #'indium-breakpoint-add :to-have-been-called-with loc condition))))))


+ 12
- 0
test/unit/indium-v8-test.el View File

@ -91,6 +91,18 @@
(indium-v8--send-request '((message . "message")) 'callback)
(expect (map-elt (indium-current-connection-callbacks) 'id) :to-equal 'callback))))
(describe "Receiving responses"
(it "should evaluate `indium-script-parsed-hook' when a script gets parsed"
(spy-on 'indium-script-add-script-parsed :and-return-value 'script)
;; Don't actually update breakpoints
(spy-on 'indium-breakpoint--update-after-script-parsed)
(spy-on 'test-hook-run)
(add-hook 'indium-script-parsed-hook 'test-hook-run)
(with-indium-connection (make-indium-connection :backend 'v8)
(indium-v8--handle-script-parsed '(params))
(remove-hook 'indium-script-parsed-hook 'test-hook-run)
(expect #'test-hook-run :to-have-been-called-with 'script))))
(describe "Making completion expressions"
(it "should return \"this\" if there is no property to complete"
(expect (indium-v8--completion-expression "foo")


Loading…
Cancel
Save