SublimeLinter-xmllint icon indicating copy to clipboard operation
SublimeLinter-xmllint copied to clipboard

Error markers are shifted right on lines with non-ASCII characters (caret column is in bytes)

Open dpc00 opened this issue 2 months ago • 3 comments

The column comes from xmllint's caret line, and libxml2 positions that caret by bytes, not characters. So on a line with non-ASCII characters before the error, the marker in Sublime Text lands too far to the right (one extra column per extra UTF-8 byte).

Repro (ST 4215, SublimeLinter-xmllint 1.0.11, xmllint using libxml version 21504 / 2.15.4 on Windows). Two files that differ only in the four characters inside <a>:

ascii1.xml:

<r><a>eeee</a><b x="1" x="2"/></r>

utf1.xml (four U+00E9 é characters):

<r><a>éééé</a><b x="1" x="2"/></r>

xmllint --noout - < file output:

ascii1.xml:
-:1: parser error : Attribute x redefined
<r><a>eeee</a><b x="1" x="2"/></r>
                            ^          <- caret at column 28

utf1.xml:
-:1: parser error : Attribute x redefined
<r><a>éééé</a><b x="1" x="2"/></r>
                                ^      <- caret at column 32 (4 more, one per extra byte)

What SublimeLinter shows (persist.file_errors, start column and the text under the highlight):

file start col highlighted
ascii1.xml 28 / (the end of <b .../>, correct)
utf1.xml 32 r of </r> (4 characters too far right)

The col group in the regex is [^\^]* in front of the caret, and it is used as a character column, so it is off by the number of extra bytes for every multi-byte character earlier on that line. Any XML with accented text or non-Latin characters before the error position shows the same shift; on longer lines the marker can end up past the end of the line.

I searched the closed issues; #5 ("Encoding errors not displayed", 2014) is about something else.

dpc00 avatar Sep 29 '26 17:09 dpc00

Can we switch to an easier output format? That would be the or part of the preferred solution, actually.

kaste avatar Sep 29 '26 17:09 kaste

I checked: xmllint has no other diagnostic format. xmllint --help (libxml version 21504 / 2.15.4) lists only options that change the output document (--format, --pretty, --c14n, --noout, ...) or the validation mode. The diagnostics themselves are always the same three lines: file:line: type : message, the source line, and a caret line. There is no column number field and no JSON or GNU-style option.

So on the plugin side the caret position is the only column information, and libxml2 counts it in bytes of the UTF-8 source. What could work without changing the command: the regex already captures the source line that the caret points into, so the byte offset can be converted to a character offset with something like len(source_line.encode('utf-8')[:byte_col].decode('utf-8', 'ignore')). That would fix the shift for accented text and emoji in the common case. I have not checked how libxml2 shortens very long source lines (it prints a window around the error), which would need handling too.

I can prepare a PR with tests for that if you want it.

dpc00 avatar Sep 29 '26 17:09 dpc00

The claim is ack, let's see if you have a simple fix in mind.

kaste avatar Sep 29 '26 19:09 kaste