This commit is contained in:
@@ -366,11 +366,36 @@ function blockEnd(lines, start, indent) {
|
||||
return end;
|
||||
}
|
||||
|
||||
/**
|
||||
* How wide a line's indentation is.
|
||||
*
|
||||
* The same measurement the parser makes — `yaml.js` reads a leading run of
|
||||
* whitespace and counts a tab as two — and it has to be, because a key the
|
||||
* parser can see and the
|
||||
* writer cannot is a key the writer will decide is missing and add a second
|
||||
* copy of.
|
||||
*
|
||||
* That is not hypothetical: a definition stored on this account indents with
|
||||
* U+00A0. JavaScript's `\s` matches it, so the parser read the file correctly
|
||||
* and every screen showed the right values; the writer compared against literal
|
||||
* spaces, found no `title:` inside `ui:`, and appended a whole second `ui:`
|
||||
* block on the first edit. The Go port agrees with the parser here too — see
|
||||
* `jsIsSpace` in `internal/definition/jsvalue.go`, which lists `0x00A0` — so the
|
||||
* writer was the only thing in the chain using a narrower idea of a space.
|
||||
*/
|
||||
const indentWidth = (line) => (line.match(/^\s*/)?.[0] || '').replace(/\t/g, ' ').length;
|
||||
|
||||
/** The index of `key` at `indent` within `[from, to)`, or -1. */
|
||||
function findKey(lines, key, indent, from, to) {
|
||||
const pattern = new RegExp(`^${' '.repeat(indent)}${key.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\s*:`);
|
||||
const pattern = new RegExp(`^${key.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\s*:`);
|
||||
for (let i = from; i < to; i += 1) {
|
||||
if (pattern.test(lines[i])) return i;
|
||||
const line = lines[i];
|
||||
if (line === undefined || indentWidth(line) !== indent) continue;
|
||||
/* Measured, then matched on what is left — so the comparison is about how
|
||||
deep the key sits, never about which characters were used to put it
|
||||
there. Identical for ASCII input, which is every definition this
|
||||
repository ships. */
|
||||
if (pattern.test(line.replace(/^\s*/, ''))) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user