question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I recreate the vscode's snippet functionality?

See original GitHub issue

Hello 👋

I would like to have the exact same functionality as vscode does for snippets when they are inserted into the editor.

To clarify; I would like to insert a piece of snippet text into the editor e.g.

const snippetTextToInsert = [
  "for (const ${2:defaultElement} of ${1:defaultArray}) {",
  "\tconsole.log(${2});",
  "}",
];

Then have that render in the editor as it does in vscode e.g.

for (const defaultElement of defaultArray) {
  console.log(defaultElement);
}

Once the code is inserted in the editor, I would like to be able to press the TAB key and have it focus on the variable elements in the order provided i.e. ${1:defaultArray} then ${2:defaultElement} and so on.

Thank you!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
rcjsuencommented, Oct 13, 2018

@jackrobertscott There is no good way to do this. See #342.

let editor = monaco.editor.create(document.getElementById("container"), {
    value: "",
    language: "javascript"
});

let contribution = editor.getContribution("snippetController2");
contribution.insert('for (const ${2:defaultElement} of ${1:defaultArray}) {\n\tconsole.log(${2});\n}');
2reactions
rcjsuencommented, Oct 13, 2018

Duplicate of #734.

function createDependencyProposals() {
    return [
        {
            label: 'forloop',
            insertText: {
                value: 'for (const ${2:defaultElement} of ${1:defaultArray}) {\n\tconsole.log(${2});\n}'
            }
        }
    ];
}


monaco.languages.registerCompletionItemProvider('javascript', {
    provideCompletionItems: function(model, position) {
        return createDependencyProposals();
    }
});

monaco.editor.create(document.getElementById("container"), {
    value: "for",
    language: "javascript"
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Snippets in Visual Studio Code
To create or edit your own snippets, select User Snippets under File > Preferences (Code > Preferences on macOS), and then select the...
Read more >
How To Add Custom Code Snippets in VS Code
First of all, open the Snippet Manager in the Command Palette. To do this, type CMD + Shift + P ( CTRL on...
Read more >
Visual Studio Code Snippets – the Definitive VS Code Snippet ...
To create the snippets file, run the 'Preferences: Configure User Snippets' command, which opens a quickpick dialog as below. Your selection ...
Read more >
How to Create Snippets for VS Code | by christoffer noring
This means we are about to create snippets using language specific constructs like types and other things. Let's recreate our class snippet but...
Read more >
Introduction To Snippets In VS code - Cloud Analogy
You can have global snippet files (JSON with .code-snippets extension) in your project scope. For project scoped snippets, go to File > ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found