mirror of
https://github.com/pulsar-edit/pulsar.git
synced 2024-10-26 11:08:40 +03:00
[snippets] Fix incorrect range traversal when resolving variables
When we expand a snippet, we know where the variables are within the snippet; they're described using `Point`s, but the origin of the coordinate system is the beginning of the snippet. To translate them into actual buffer `Point`s during snippet expansion, we “add” each point to the `Point` that marks the insertion point of the snippet. When doing so, we need to remember that, when a snippet contains a newline and content after that newline, that second line of content will be indented by the same amount as the initial line; we know how much leading whitespace (if any) there was before the snippet trigger text and apply it at the head of each line of the expansion. For this reason, we were adding the two points incorrectly. Since the column position doesn't reset to `0` each time a row is advanced, we should've been using `Point#translate`, not `Point#traverse`. I'd have caught this earlier if I had thought to test the combination of variable expansion and leading whitespace. I see other usages of `Point#traverse` in different contexts in this file, but I'll leave them be until they can be proven to be the source of a bug.
This commit is contained in:
parent
4b1edf4d16
commit
4eec1c211e
@ -215,10 +215,12 @@ module.exports = class SnippetExpansion {
|
||||
// markers are crucial for ensuring we adapt to those changes.
|
||||
for (const variable of this.snippet.variables) {
|
||||
const {point} = variable
|
||||
const marker = this.getMarkerLayer(this.editor).markBufferRange([
|
||||
startPosition.traverse(point),
|
||||
startPosition.traverse(point)
|
||||
], {exclusive: false})
|
||||
let markerRange = new Range(
|
||||
startPosition.translate(point),
|
||||
startPosition.translate(point)
|
||||
)
|
||||
const marker = this.getMarkerLayer(this.editor).markBufferRange(
|
||||
markerRange, {exclusive: false})
|
||||
this.markersForVariables.set(variable, marker)
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,14 @@
|
||||
{
|
||||
"env": { "jasmine": true },
|
||||
"rules": {
|
||||
"node/no-unpublished-require": "off",
|
||||
"node/no-extraneous-require": "off",
|
||||
"no-unused-vars": "off",
|
||||
"no-empty": "off",
|
||||
"object-curly-spacing": ["error", "always"],
|
||||
"semi": ["error", "always"]
|
||||
},
|
||||
"globals": {
|
||||
"waitsForPromise": true
|
||||
}
|
||||
}
|
||||
|
@ -1055,6 +1055,17 @@ foo\
|
||||
editor.insertText('TEST');
|
||||
expect(editor.getText()).toBe("// TEST\n// ====");
|
||||
});
|
||||
|
||||
describe("and the editor is indented", () => {
|
||||
it("traverses newlines properly", () => {
|
||||
editor.setText(' bannerGeneric');
|
||||
editor.setCursorScreenPosition([0, 15]);
|
||||
simulateTabKeyEvent();
|
||||
expect(editor.getText()).toBe(" // \n // ");
|
||||
editor.insertText('TEST');
|
||||
expect(editor.getText()).toBe(" // TEST\n // ====");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("and the document is HTML", () => {
|
||||
|
Loading…
Reference in New Issue
Block a user