Skip to content

Commit 400ec52

Browse files
authored
Merge pull request #381 from jrwats/strip_trailing
Support trailing commas (["a", ], {"a": "b",}) in launch.json files.
2 parents 5b00946 + 464dfb4 commit 400ec52

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

dap-launch.el

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
(require 'f)
2929
(require 'rx)
3030

31-
(defun dap-launch-remove-comments ()
32-
"Remove all C-style comments in the current buffer.
31+
(defun dap-launch-sanitize-json ()
32+
"Remove all C-style comments and trailing commas in the current buffer.
3333
Comments in strings are ignored. The buffer is modified in place.
3434
Replacement starts at point, and strings before it are ignored,
3535
so you may want to move point to `point-min' with `goto-char'
@@ -40,12 +40,13 @@ supported."
4040
(or (group
4141
(or (: "//" (* nonl) eol)
4242
(: "/*" (* (or (not (any ?*))
43-
(: (+ ?*) (not (any ?/))))) (+ ?*) ?/)))
43+
(: (+ ?*) (not (any ?/))))) (+ ?*) ?/)
44+
(: "," (group (* (any blank ?\C-j)) (any ?\} ?\])))))
4445
(: "\"" (* (or (not (any ?\\ ?\")) (: ?\\ nonl))) "\"")))
4546
nil t)
4647
;; we matched a comment
4748
(when (match-beginning 1)
48-
(replace-match ""))))
49+
(replace-match (or (match-string 2) "")))))
4950

5051
(defun dap-launch-find-launch-json ()
5152
"Return the location of the launch.json file in the current project."
@@ -66,7 +67,7 @@ supported."
6667
(with-temp-buffer
6768
;; NOTE: insert-file-contents does not move point
6869
(insert-file-contents launch-json)
69-
(dap-launch-remove-comments)
70+
(dap-launch-sanitize-json)
7071
;; dap-launch-remove-comments does move point
7172
(goto-char (point-min))
7273

test/dap-test.el

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,13 @@
123123
(should (dap-python--equal actual expected))
124124
(should (dap-python--equal (dap-python--nearest-test expected) "::TestClass::test_foo"))))
125125

126-
(defun dap-launch-test--delete-string-comments (s)
126+
(defun dap-launch-test--sanitize-json (s)
127127
"Delete all comments in S."
128128
(with-temp-buffer
129129
(insert s)
130130
(goto-char (point-min))
131131

132-
(dap-launch-remove-comments)
132+
(dap-launch-sanitize-json)
133133
(buffer-string)))
134134

135135
(ert-deftest dap-launch-test--delete-comments ()
@@ -139,21 +139,34 @@
139139
/* \" // non-string */
140140
\"// string\"
141141
\"/*string*/\"")
142-
(post-exp (dap-launch-test--delete-string-comments orig)))
142+
(post-exp (dap-launch-test--sanitize-json orig)))
143143
(should (string= post-exp "
144144
145145
146146
147147
\"// string\"
148148
\"/*string*/\""))))
149149

150+
(ert-deftest dap-launch-test--delete-commas ()
151+
(let* ((orig "{
152+
\"abc\": 123,
153+
}")
154+
(post-exp (dap-launch-test--sanitize-json orig)))
155+
(should (string= post-exp "{
156+
\"abc\": 123
157+
}"))))
158+
150159
(ert-deftest dap-launch-test--comment-in-string ()
151160
(let ((orig "\"// orig\""))
152-
(should (string= orig (dap-launch-test--delete-string-comments orig)))))
161+
(should (string= orig (dap-launch-test--sanitize-json orig)))))
153162

154163
(ert-deftest dap-launch-test--comment-star ()
155164
(let ((orig "/* * **/"))
156-
(should (string-empty-p (dap-launch-test--delete-string-comments orig)))))
165+
(should (string-empty-p (dap-launch-test--sanitize-json orig)))))
166+
167+
(ert-deftest dap-launch-test--comma-in-string ()
168+
(let ((orig "\"abc, ]\""))
169+
(should (string= orig (dap-launch-test--sanitize-json orig)))))
157170

158171

159172
(defmacro dap-variables--define-compare-test (name conf expanded

0 commit comments

Comments
 (0)