Skip to content

Commit

Permalink
Only show snippet markers for the current snippet on edit (#5478)
Browse files Browse the repository at this point in the history
* remove previous tabmanager if there is one

* show tabstop markers for only selected snippet

* fix if tab goes to -zsh of selected snippet

* adds test

* update test
  • Loading branch information
oykuyilmaz authored Feb 2, 2024
1 parent d9814b4 commit 7dcfa41
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/snippets.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,8 @@ class SnippetManager {
//@ts-expect-error TODO: potential wrong arguments
tabstopManager.addTabstops(processedSnippet.tabstops, range.start, end, selectionId);
}

insertSnippet(editor, snippetText, options={}) {
var self = this;

if (editor.inVirtualSelectionMode)
return self.insertSnippetForSelection(editor, snippetText, options);

Expand Down Expand Up @@ -894,8 +892,10 @@ class TabstopManager {
if (index == max)
index = 0;
this.selectTabstop(index);
if (index === 0)
this.updateTabstopMarkers();
if (index === 0) {
this.detach();
}
}
selectTabstop(index) {
this.$openTabstops = null;
Expand Down Expand Up @@ -943,9 +943,10 @@ class TabstopManager {
var i = this.index;
var arg = [i + 1, 0];
var ranges = this.ranges;
var snippetId = this.snippetId = (this.snippetId || 0) + 1;
tabstops.forEach(function(ts, index) {
var dest = this.$openTabstops[index] || ts;

dest.snippetId = snippetId;
for (var i = 0; i < ts.length; i++) {
var p = ts[i];
/**@type {Range & {original?: Range, tabstop?: any, linked?: boolean}}}*/
Expand Down Expand Up @@ -999,6 +1000,19 @@ class TabstopManager {
range.markerId = null;
});
}
updateTabstopMarkers() {
if (!this.selectedTabstop) return;
var currentSnippetId = this.selectedTabstop.snippetId;
// back to the parent snippet tabstops if $0
if ( this.selectedTabstop.index === 0) {
currentSnippetId--;
}
this.tabstops.forEach(function(ts) {
// show marker only for the tabstops of the currently active snippet
if (ts.snippetId === currentSnippetId) this.addTabstopMarkers(ts);
else this.removeTabstopMarkers(ts);
}, this);
}
removeRange(range) {
var i = range.tabstop.indexOf(range);
if (i != -1) range.tabstop.splice(i, 1);
Expand Down
17 changes: 17 additions & 0 deletions src/snippets_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,23 @@ module.exports = {
testTabstop(tabstops[4], "[5/3]> [5/3],[2/3]> [2/3]");
testTabstop(tabstops[5], "[6/2]> [6/5]");
},
"test: insert snippet inside snippet and check markers": function() {
var editor = this.editor;
editor.session.setValue("");
editor.insertSnippet("{$1}");

// check first snippet's marker
var snippetMarkers = Object.values(editor.session.$backMarkers).filter(function(i) {return i.clazz == "ace_snippet-marker";});
assert.jsonEquals(snippetMarkers[0].range.start, {row: 0, column: 2});
assert.jsonEquals(snippetMarkers[1].range.start, {row: 0, column: 1});

// check markers after insertion of the second snippet
editor.insertSnippet("\"examples\": [$1]");
snippetMarkers = Object.values(editor.session.$backMarkers).filter(function(i) {return i.clazz == "ace_snippet-marker";});
assert.equal(snippetMarkers.length, 2);
assert.jsonEquals(snippetMarkers[0].range.start, {row: 0, column: 15});
assert.jsonEquals(snippetMarkers[1].range.start, {row: 0, column: 14});
},
"test: linking": function() {
var editor = this.editor;
editor.setOption("enableMultiselect", false);
Expand Down

0 comments on commit 7dcfa41

Please sign in to comment.