Skip to content

Commit 60d46be

Browse files
committed
feat: add support for tab stops
1 parent 52ad15d commit 60d46be

File tree

5 files changed

+470
-7
lines changed

5 files changed

+470
-7
lines changed

src/editor/CodeHintManager.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,18 @@ define(function (require, exports, module) {
270270
// this is to check whether user has set any custom snippets as we need to show it at the first
271271
let customSnippetsDriver = null;
272272
let customSnippetsGlobal = null;
273+
let customSnippetsCursorManager = null;
273274

274275
// load custom snippets driver and global
275276
try {
276277
customSnippetsDriver = require("../extensionsIntegrated/CustomSnippets/src/driver");
277278
customSnippetsGlobal = require("../extensionsIntegrated/CustomSnippets/src/global");
279+
customSnippetsCursorManager = require("../extensionsIntegrated/CustomSnippets/src/snippetCursorManager");
278280
} catch (e) {
279281
// if unable to load we just set it to null to prevent other parts of the code from breaking
280282
customSnippetsDriver = null;
281283
customSnippetsGlobal = null;
284+
customSnippetsCursorManager = null;
282285
}
283286

284287
PreferencesManager.definePreference("showCodeHints", "boolean", true, {
@@ -579,11 +582,22 @@ define(function (require, exports, module) {
579582
(snippet) => snippet.abbreviation === abbreviation
580583
);
581584
if (matchedSnippet) {
582-
// replace the typed abbreviation with the template text
585+
// replace the typed abbreviation with the template text using cursor manager
583586
const wordInfo = customSnippetsDriver.getWordBeforeCursor();
584587
const start = { line: wordInfo.line, ch: wordInfo.ch + 1 };
585588
const end = sessionEditor.getCursorPos();
586-
sessionEditor.document.replaceRange(matchedSnippet.templateText, start, end);
589+
590+
if (customSnippetsCursorManager) {
591+
customSnippetsCursorManager.insertSnippetWithTabStops(
592+
sessionEditor,
593+
matchedSnippet.templateText,
594+
start,
595+
end
596+
);
597+
} else {
598+
// insert snippet just by replacing range if cursor manager is not available
599+
sessionEditor.document.replaceRange(matchedSnippet.templateText, start, end);
600+
}
587601
_endSession();
588602
return;
589603
}

src/extensionsIntegrated/CustomSnippets/htmlContent/snippets-panel.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@
9090

9191
<div id="template-text-box-wrapper">
9292
<label for="template-text-box"
93-
title="The actual code that will be inserted. Use ${cursor} to set cursor position after insertion."
93+
title="The actual code that will be inserted. Use ${1}, ${2}, ${3}, etc. for cursor positions. ${1} is the initial position, tab moves to ${2}, ${3}, etc. ${0} is the final position."
9494
data-placement="top">
9595
Template Text:
9696
</label>
97-
<textarea id="template-text-box" placeholder="console.log(${cursor});" autocomplete="off"></textarea>
97+
<textarea id="template-text-box" placeholder="console.log(${1});" autocomplete="off"></textarea>
9898
</div>
9999

100100
<div id="file-extn-box-wrapper">
@@ -135,11 +135,11 @@
135135

136136
<div id="edit-template-text-box-wrapper">
137137
<label for="edit-template-text-box"
138-
title="The actual code that will be inserted. Use ${cursor} to set cursor position after insertion."
138+
title="The actual code that will be inserted. Use ${1}, ${2}, ${3}, etc. for cursor positions. ${1} is the initial position, tab moves to ${2}, ${3}, etc. ${0} is the final position."
139139
data-placement="top">
140140
Template Text:
141141
</label>
142-
<textarea id="edit-template-text-box" placeholder="console.log(${cursor});" autocomplete="off"></textarea>
142+
<textarea id="edit-template-text-box" placeholder="console.log(${1});" autocomplete="off"></textarea>
143143
</div>
144144

145145
<div id="edit-file-extn-box-wrapper">

src/extensionsIntegrated/CustomSnippets/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ define(function (require, exports, module) {
1212
const Helper = require("./src/helper");
1313
const UIHelper = require("./src/UIHelper");
1414
const SnippetsState = require("./src/snippetsState");
15+
const SnippetCursorManager = require("./src/snippetCursorManager");
1516

1617
const snippetsPanelTpl = require("text!./htmlContent/snippets-panel.html");
1718
// the html content of the panel will be stored in this variable
@@ -197,5 +198,6 @@ define(function (require, exports, module) {
197198
_addToMenu();
198199
SnippetCodeHints.init();
199200
SnippetsState.loadSnippetsFromState();
201+
SnippetCursorManager.registerHandlers();
200202
});
201203
});

src/extensionsIntegrated/CustomSnippets/src/snippetCodeHints.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ define(function (require, exports, module) {
44
const Global = require("./global");
55
const Driver = require("./driver");
66
const Helper = require("./helper");
7+
const SnippetCursorManager = require("./snippetCursorManager");
78

89
/**
910
* Constructor
@@ -96,7 +97,9 @@ define(function (require, exports, module) {
9697
const end = cursor;
9798

9899
const matchedItem = Global.SnippetHintsList.find((snippet) => snippet.abbreviation === word.word);
99-
this.editor.document.replaceRange(matchedItem.templateText, start, end);
100+
101+
// Use the new cursor manager for snippet insertion with tab stops
102+
SnippetCursorManager.insertSnippetWithTabStops(this.editor, matchedItem.templateText, start, end);
100103

101104
return false;
102105
};

0 commit comments

Comments
 (0)