Skip to content

Commit ca14dd1

Browse files
committed
Fix nxml-get-inside (Bug#32003)
The change from 2016-01-16 "lisp/nxml: Use syntax-tables for comments" made nxml-get-inside return non-nil for any string or comment, including attribute strings. This caused incorrect and therefore indentation. * lisp/nxml/nxml-rap.el: Update commentary to reflect changes to nxml-mode parsing. (nxml-get-inside): Only return non-nil when inside comments and generic strings, not normal quote-delimited strings. * test/lisp/nxml/nxml-mode-tests.el: New tests.
1 parent e7ab351 commit ca14dd1

File tree

2 files changed

+82
-22
lines changed

2 files changed

+82
-22
lines changed

lisp/nxml/nxml-rap.el

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,35 +35,25 @@
3535
;;
3636
;; Our strategy is to keep track of just the problematic things.
3737
;; Specifically, we keep track of all comments, CDATA sections and
38-
;; processing instructions in the instance. We do this by marking all
39-
;; except the first character of these with a non-nil nxml-inside text
40-
;; property. The value of the nxml-inside property is comment,
41-
;; cdata-section or processing-instruction. The first character does
42-
;; not have the nxml-inside property so we can find the beginning of
43-
;; the construct by looking for a change in a text property value
44-
;; (Emacs provides primitives for this). We use text properties
45-
;; rather than overlays, since the implementation of overlays doesn't
46-
;; look like it scales to large numbers of overlays in a buffer.
47-
;;
48-
;; We don't in fact track all these constructs, but only track them in
49-
;; some initial part of the instance.
38+
;; processing instructions in the instance. We do this by marking
39+
;; the first character of these with the generic string syntax by setting
40+
;; a 'syntax-table' text property in `sgml-syntax-propertize'.
5041
;;
5142
;; Thus to parse some random point in the file we first ensure that we
52-
;; have scanned up to that point. Then we search backwards for a
53-
;; <. Then we check whether the < has an nxml-inside property. If it
54-
;; does we go backwards to first character that does not have an
55-
;; nxml-inside property (this character must be a <). Then we start
56-
;; parsing forward from the < we have found.
43+
;; have scanned up to that point. Then we search backwards for a <.
44+
;; Then we check whether the < has the generic string syntax. If it
45+
;; does we go backwards to first character of the generic string (this
46+
;; character must be a <). Then we start parsing forward from the <
47+
;; we have found.
5748
;;
5849
;; The prolog has to be parsed specially, so we also keep track of the
5950
;; end of the prolog in `nxml-prolog-end'. The prolog is reparsed on
6051
;; every change to the prolog. This won't work well if people try to
6152
;; edit huge internal subsets. Hopefully that will be rare.
6253
;;
63-
;; We keep track of the changes by adding to the buffer's
64-
;; after-change-functions hook. Scanning is also done as a
65-
;; prerequisite to fontification by adding to fontification-functions
66-
;; (in the same way as jit-lock). This means that scanning for these
54+
;; We rely on the `syntax-propertize-function' machinery to keep track
55+
;; of the changes in the buffer. Fontification also relies on correct
56+
;; `syntax-table' properties. This means that scanning for these
6757
;; constructs had better be quick. Fortunately it is. Firstly, the
6858
;; typical proportion of comments, CDATA sections and processing
6959
;; instructions is small relative to other things. Secondly, to scan
@@ -79,7 +69,15 @@
7969
"Integer giving position following end of the prolog.")
8070

8171
(defsubst nxml-get-inside (pos)
82-
(save-excursion (nth 8 (syntax-ppss pos))))
72+
"Return non-nil if inside comment, CDATA, or PI."
73+
(let ((ppss (save-excursion (syntax-ppss pos))))
74+
(or
75+
;; Inside comment.
76+
(nth 4 ppss)
77+
;; Inside "generic" string which is used for CDATA, and PI.
78+
;; "Normal" double and single quoted strings are used for
79+
;; attribute values.
80+
(eq t (nth 3 ppss)))))
8381

8482
(defun nxml-inside-end (pos)
8583
"Return the end of the inside region containing POS.

test/lisp/nxml/nxml-mode-tests.el

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
;;; nxml-mode-tests.el --- Test NXML Mode -*- lexical-binding: t; -*-
2+
3+
;; Copyright (C) 2019 Free Software Foundation, Inc.
4+
5+
;; GNU Emacs is free software: you can redistribute it and/or modify
6+
;; it under the terms of the GNU General Public License as published by
7+
;; the Free Software Foundation, either version 3 of the License, or
8+
;; (at your option) any later version.
9+
10+
;; GNU Emacs is distributed in the hope that it will be useful,
11+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
;; GNU General Public License for more details.
14+
15+
;; You should have received a copy of the GNU General Public License
16+
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
17+
18+
;;; Code:
19+
20+
(require 'ert)
21+
(require 'nxml-mode)
22+
23+
(defun nxml-mode-tests-correctly-indented-string (str)
24+
(with-temp-buffer
25+
(nxml-mode)
26+
(insert str)
27+
(indent-region (point-min) (point-max))
28+
(equal (buffer-string) str)))
29+
30+
(ert-deftest nxml-indent-line-after-attribute ()
31+
(should (nxml-mode-tests-correctly-indented-string "
32+
<settings
33+
xmlns=\"http://maven.apache.org/SETTINGS/1.0.0\"
34+
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
35+
xsi:schemaLocation=\"http://maven.apache.org/SETTINGS/1.0.0
36+
https://maven.apache.org/xsd/settings-1.0.0.xsd\">
37+
<mirrors>
38+
...
39+
</mirrors>
40+
</settings>
41+
"))
42+
(should (nxml-mode-tests-correctly-indented-string "\
43+
<x>
44+
<abc xx=\"x/x/x/x/x/x/x/
45+
y/y/y/y/y/y/
46+
\">
47+
<zzz/>
48+
</abc>
49+
<nl>&#10;</nl>
50+
</x>
51+
")))
52+
53+
(ert-deftest nxml-balanced-close-start-tag-inline ()
54+
(with-temp-buffer
55+
(nxml-mode)
56+
(insert "<a><b c=\"\"</a>")
57+
(search-backward "</a>")
58+
(nxml-balanced-close-start-tag-inline)
59+
(should (equal (buffer-string) "<a><b c=\"\"></b></a>"))))
60+
61+
(provide 'nxml-mode-tests)
62+
;;; nxml-mode-tests.el ends here

0 commit comments

Comments
 (0)