Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to insert dynamically generated snippets through extensions #3210

Closed
shardulm94 opened this issue Feb 21, 2016 · 46 comments
Closed

Ability to insert dynamically generated snippets through extensions #3210

shardulm94 opened this issue Feb 21, 2016 · 46 comments
Assignees
Labels
api feature-request Request for new features or functionality snippets
Milestone

Comments

@shardulm94
Copy link

Currently, you can only insert a string from an extension using TextEditorEdit. I am in the process of creating a jsdoc comments extension, and it can insert TextMate-style snippets, but only as a string. It would be much better if one could iterate through all the placeholders rather than manually moving the cursor.
The effect would be similar to the one in the Function Documentation section here
Would it be possible to get a feature like this?

@egamma egamma added feature-request Request for new features or functionality api editor labels Feb 21, 2016
@jrieken
Copy link
Member

jrieken commented Apr 29, 2016

@shardulm94 Unsure if it works but you explore adding a completion item provider that returns completions of kind Snippet. With that you should get support for placeholders

@daviwil
Copy link
Contributor

daviwil commented May 4, 2016

Whoa, interesting. So if a completion is of type Snippet and has the necessary formatting for placeholders, it should work as expected? This could mean interesting things for the PowerShell extension...

@jrieken
Copy link
Member

jrieken commented May 6, 2016

go for it @daviwil

@alexdima alexdima removed their assignment May 6, 2016
@shardulm94
Copy link
Author

@jrieken I tried this today. But it inserts the tabstops and placeholders as text itself.

image

@jrieken
Copy link
Member

jrieken commented May 6, 2016

the syntax is like so: new vscode.CompletionItem('hello {{world}}. hello {{world}}') using double curly brackets. Unsure, if we plan to align that to the snippet syntax that seems to be ${thing}

@shardulm94
Copy link
Author

shardulm94 commented May 6, 2016

@jrieken Great, thanks! This fits my needs!
Is there any way to order tab stops with this syntax?

EDIT: It would be really helpful if you could point me to where this syntax is documented.

@jrieken
Copy link
Member

jrieken commented May 6, 2016

unsure. @alexandrudima will know

@alexdima
Copy link
Member

alexdima commented May 6, 2016

We didn't document it because we didn't want it to spread.

@jrieken maybe completions coming from the extension host should go through https://github.com/Microsoft/vscode/blob/master/src/vs/editor/contrib/snippet/common/snippet.ts#L226 such that we don't end up documenting and making API of our internal format.

@joelday
Copy link
Contributor

joelday commented Aug 19, 2016

This would be a huge step for https://github.com/joelday/vscode-docthis. (I still haven't figured out a way to change the selection position to inside the range of new edits.)

Hope to see this get formalized as an external API.

@jrieken jrieken self-assigned this Aug 19, 2016
@jrieken jrieken added this to the August 2016 milestone Aug 19, 2016
@jrieken
Copy link
Member

jrieken commented Aug 19, 2016

Stretch for August, likely Sept

@jchannon
Copy link

Looking forward to this

@jrieken
Copy link
Member

jrieken commented Oct 24, 2016

We have basically two options for this, an easy and a complicated option. Option A (and that is the easy one) is that we simply document our internal snippet format and build on it in the future. It has the advantage of not requiring new API to signal this completion string uses snippet syntax and should be handled special because already today it is like that. The obvious disadvantage is that we introduce another syntax in addition to the TM snippet syntax. Option B would be to build on the TM snippet syntax and it would mean that we need a flag/type to signal this string is a TM snippet such that we don't break existing completion providers. It would also require us to reach more parity with the actual TM snippet syntax and it would raise the question if the existing, implicit, behaviour should be turned off. Suggestions welcome!

@kumarharsh
Copy link
Contributor

kumarharsh commented Oct 25, 2016

I think, if it helps, just do away with the whole textmate syntax for snippets. VSCode anyways supports only a limited subset of the snippets, writing/converting the existing tmSnippets to JSON is painful, and there are bugs even in that, such as tabs not properly jumping to the next snippet, tabs jumping to first snippet after the last one, etc, which make the snippets part a very weak part of the software. If there is a better way, why not use that? This new optionA although sounds good, will create more fragmentation in the way snippets are written/maintained IMO.

@jrieken jrieken modified the milestones: October 2016, November 2016 Oct 26, 2016
@jchannon
Copy link

jchannon commented Nov 19, 2016

Just having a look at this on insiders build. Is there any sample code on this new API can work?

@DanTup
Copy link
Contributor

DanTup commented Nov 19, 2016

@jchannon I think this is the Typescript one being updated?

0abf274

@jchannon
Copy link

Cheers. I just downloaded the insiders build but I can't seem to be able
to resolve SnippetString.

That typescript example is not quite what I want I believe. I would like
to be able to write a snippet to a file/open editor/doc and for the user to
then be able to press tab where I have placeholders $1 etc

On 19 November 2016 at 15:29, Danny Tuppeny [email protected]
wrote:

@jchannon https://github.com/jchannon I think this is the Typescript
one being updated?

0abf274
0abf274


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#3210 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAGappY33xk1IiVmuj8tLyH_T9jYysDtks5q_xXmgaJpZM4He7xA
.

@DanTup
Copy link
Contributor

DanTup commented Nov 19, 2016

(Note: I haven't updated to this yet, so I might be talking gibberish!)

but I can't seem to be able to resolve SnippetString

Di you need to update the vscode engine version in packages.json and run npm install to get the latest definitions downloaded?

I would like to be able to write a snippet to a file/open editor/doc and for the user to then be able to press tab where I have placeholders $1 etc

I'm not really sure what's different from what this does and what you want. There was undocumented functionality that allowed completion providers to have placeholders that the user could tab between (we use in Dart Code to pre-fill arguments) which is why this is a fairly small change for those already using that. If you weren't previously using it then there's probably additional work to get your snippets in via a completion provider.

@jrieken
Copy link
Member

jrieken commented Nov 22, 2016

We are done with this. Please try out things and report feedback

@jrieken jrieken closed this as completed Nov 22, 2016
@jrieken
Copy link
Member

jrieken commented Nov 22, 2016

@jchannon Re #3210 (comment) When programming against insiders you need the latest and greatest vscode.d.ts as described here. Were you looking for a way to insert a snippet straight into the editor without going through a completion provider? So, something like editor.insert(new SnippetString('foo$0bar'), somePos)?

@jchannon
Copy link

Yeah.

I added 1.8.0 for engine version and its seemed to download the d.ts but
then i didnt get any intellisense after that

On 22 November 2016 at 10:01, Johannes Rieken [email protected]
wrote:

@jchannon https://github.com/jchannon Re #3210 (comment)
#3210 (comment)
When programming against insiders you need the latest and greatest
vscode.d.ts as described here
#3210 (comment).
Were you looking for a way to insert a snippet straight into the editor
without going through a completion provider? So, something like editor.edit(snippet:
SnippetString, rangeOrPos: Range | Position)?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#3210 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAGapirzy7r0fdwJo2vGM83sppMB6RfLks5rAr2MgaJpZM4He7xA
.

@jrieken
Copy link
Member

jrieken commented Nov 22, 2016

Yeah - you might update your project/extension to run with TypeScript 2.0. See: http://code.visualstudio.com/updates/v1_6#_extension-authoring

@jchannon
Copy link

I have:

"engines": {
"vscode": "^1.8.0"
},

"devDependencies": {
"typescript": "^2.0.3",
"vscode": "^1.0.0",
"mocha": "^2.3.3",
"@types/node": "^6.0.40",
"@types/mocha": "^2.2.32"
},

But get no intellisense when typing vscode.

On 22 November 2016 at 12:44, Johannes Rieken [email protected]
wrote:

Yeah - you might update your project/extension to run with TypeScript 2.0.
See: http://code.visualstudio.com/updates/v1_6#_extension-authoring


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#3210 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAGapubSvzUDkcbJkXdDTFxmo3M7aAjIks5rAuOwgaJpZM4He7xA
.

@jchannon
Copy link

i have it working, had to compile ts files for some reason then vscode
behaved however the editor.edit command you mention, the intellisense
doesnt seem to match up with passing a snippetstring to the edit function

On 23 November 2016 at 09:18, Jonathan Channon [email protected]
wrote:

I have:

"engines": {
"vscode": "^1.8.0"
},

"devDependencies": {
"typescript": "^2.0.3",
"vscode": "^1.0.0",
"mocha": "^2.3.3",
"@types/node": "^6.0.40",
"@types/mocha": "^2.2.32"
},

But get no intellisense when typing vscode.

On 22 November 2016 at 12:44, Johannes Rieken [email protected]
wrote:

Yeah - you might update your project/extension to run with TypeScript
2.0. See: http://code.visualstudio.com/updates/v1_6#_extension-authoring


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#3210 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAGapubSvzUDkcbJkXdDTFxmo3M7aAjIks5rAuOwgaJpZM4He7xA
.

@jrieken
Copy link
Member

jrieken commented Nov 23, 2016

Yeah, TextEditor.insert(snippet: SnippetString, posOrRange) was just to confirm I didn't implement what you want ;-) I'll create a separate issue for this and I might be able to squeeze it in during Nov

@jrieken
Copy link
Member

jrieken commented Nov 23, 2016

@jchannon #15952 fyi

@jchannon
Copy link

👍 👍 👍

@ipatalas
Copy link

Hi guys,

great to have this feature allowing to use snippets from CompletionItems. However I can't make it work in my case.

I want the completion to insert something before the text being completed. I know I can do that using additionalTextEdits but I also want this part to be an editable snippet. The basic PoC code is as follows:

let range = new vsc.Range(position.translate(0, -1), position)

let item = new vsc.CompletionItem('var', vsc.CompletionItemKind.Snippet)
item.insertText = `var {{name}} = `
item.range = range

Unfortunately such completion item isn't even shown in the dropdown list. What I was able to figure is that it's related to the new range field.
If the range is set as new vsc.Range(position.translate(0, 0), position) then it works fine, completion is shown and text is inserted but at the end which is not what I'm trying to achieve.
When I move the start part of the range 1 character left it stops showing. Why is it like this?
I know this range must be on a single line and overlap the position where the completion was requested but this range fulfills these need.
Am I missing something?

VS Code version:
Version 1.10.2
Commit 8076a19
Date 2017-03-08T14:02:52.799Z
Shell 1.4.6
Renderer 53.0.2785.143
Node 6.5.0

@jrieken
Copy link
Member

jrieken commented Mar 31, 2017

@ipatalas A few things: (1) var {{name}} isn't the supported snippet syntax but an old undocumented deprecated variant, (2) use SnippetString to tell us that you want a snippet and not a string with curlies and dollar.

Then about range et al: Suggestion are filtered and score against the current text left of the cursor. What text that is defines the range of a completion item. However the rule is that the completion must match that text. Sample: The cursor sits right of the foo and suggestions are replacing foo must all start with foo, like foobar. If that isn't possible one should use the filterText property completion items have or make an append edit, e.g. not replace foo

@ipatalas
Copy link

@jrieken thanks for a very quick answer

Regarding the snippet syntax, I know. I've been just trying them all with no success and this one I ended up with so I took this example somehow by accident.

Thanks for the note about filterText. I deeply misunderstood what it's for previously. Just checked it with some examples and it works just fine. Thanks a lot for your help!

Cheers,
Irek

@vscodebot vscodebot bot locked and limited conversation to collaborators Nov 18, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api feature-request Request for new features or functionality snippets
Projects
None yet
Development

No branches or pull requests