puppetlabs / puppet-vscode

Puppet Editing. Redefined.
https://puppet-vscode.github.io
Apache License 2.0
81 stars 40 forks source link

Encode and ship some basic Puppet code snippets #523

Closed jdub55 closed 5 years ago

jdub55 commented 5 years ago

What if the Puppet VS Code plugin were to ship a reference version of the Puppet style guide in the form of code snippets? So, for example, if a user typed in psfile, it would return a well formatted example of a file code block. Rinse and repeat for the rest of the common Puppet language primitives / commonly used commands. Thoughts?

jpogran commented 5 years ago

Do you mean types like file?

All built in types or resources (file, package, etc), plus any from modules in the module path (downloaded from the forge, etc), plus any from a control repo if that's the current workspace will show up in intellisense, hover, autocomplete, etc. The Puppet Extension will then provide a minimal snippet which can be tabbed through. After completing the tab order, you can invoke intellisense/autocomplete and then get a list of parameters and the accompanying documentation in a hover window.

See https://github.com/lingua-pupuli/puppet-vscode#auto-completion, https://github.com/lingua-pupuli/puppet-vscode#hover-support. These types and resources show up as symbols to VSCode, which lights up the ability for quick code navigation like https://github.com/lingua-pupuli/puppet-vscode#go-to-symbol.

Did I misunderstand your suggestion?

jpogran commented 5 years ago

TBH though, we should have the snippet thats auto generated from the Language Server be a little bit more filled out than name/ensure pairs.

glennsarti commented 5 years ago

@jpogran The Language Server only generates snippets for required parameters.

https://github.com/lingua-pupuli/puppet-editor-services/blob/master/lib/puppet-languageserver/manifest/completion_provider.rb#L246-L257

jdub55 commented 5 years ago

I was thinking more along the lines of a Puppet language reference where it would provide a "complete" snippet of all parameters for a given resource or Puppet code pattern / best practice.

jdub55 commented 5 years ago

Even better would be a place where people could share snippets but I suspect that's something Microsoft would have to provide within VS Code itself.

glennsarti commented 5 years ago

Even better would be a place where people could share snippets but I suspect that's something Microsoft would have to provide within VS Code itself.

No, you could dynamically fetch them. Interesting idea though...

glennsarti commented 5 years ago

"complete" snippet of all parameters for a given resource

This can be dangerous for resources like apache, but I understand the need

or Puppet code pattern / best practice.

This is one is really tricky as there can't really be a best practice for a resource as it's heavily dependent on the environment. Even a basic example like file differs greatly whether it's on a Windows or Linux or OSX node.

glennsarti commented 5 years ago

Here's another take on it....

What if users could have their own snippets file and the extension reads that in and offers it in a dynamic manner? So you can have your own set of commonly used snippets?

EDIT - Already supported natively in VSCode https://blog.logrocket.com/custom-polymorphic-code-snippets-in-vs-code-e76d8cad656b/

jpogran commented 5 years ago

Was going to point to https://code.visualstudio.com/docs/editor/userdefinedsnippets#_creating-your-own-snippets, but you beat me to it.

The https://code.visualstudio.com/docs/editor/userdefinedsnippets#_snippet-filenames and https://code.visualstudio.com/docs/editor/userdefinedsnippets#_global-snippets links imply we could ship community collaborated snippets in gists or pdk-templates, unsure of place to put it because I haven't tried yet

jpogran commented 5 years ago

FWIW, this isn't the first time we've discussed this https://github.com/lingua-pupuli/puppet-vscode/issues/265.

jdub55 commented 5 years ago

Having your own snippets to create and use is great. That does seem to ship out of the box in VSCode as you both pointed out. Thanks for the links.

I think any form of community collaborated snippets would be something very popular amongst Puppet VSCode users, including myself. :) I can see how the implementation might be tricky. The goal is certainly not to (re)-create the Forge of snippets, so the use case would need to focus on a specific niche. This is where I thought longer form examples of good Puppet code (e.g. style guide) could be useful. My $0.02. And now looking at the other issue, I do see there is a separate plugin for sharing(?) https://marketplace.visualstudio.com/items?itemName=bhurt.puppet-snippets

glennsarti commented 5 years ago

Yeah @RandomNoun7 created that. Mainly because it spanned languages. This extension is focusing on the Puppet Language (so .pp etc.) not Ruby.

The implementation isn't that bad to be honest, and isn't the problem. It's more the workflow, and trying to not end up with a billion-and-one snippets for the file resource.

I wonder if you could "subscribe" to blocks of snippets e.g.

I could subscribe to the Windows, IIS and ActiveDirectory snippets or I could subscribe to the CentOS and Apache snippets.

and so on.

wilrodriguez commented 5 years ago

Similarly, snippets for functions would be nice. I've started writing my own snippets for some of my more commonly used ones, but it would be really nice if the extension saw my functions and knew what they were.

jpogran commented 5 years ago

File > Preferences > User Snippets > New User Snippet for '<name_of_workspace>' creates a puppet.code-snippets file inside the .vscode directory inside a module folder. To create a 'shared' file, this file could then be added to the pdk-template repo, which is then sync'ed out to any module maintained by pdk update. With some care for both file and snippet naming, you could have a multiple files for each 'subject' that are automatically loaded when a folder is opened.

File > Preferences > User Snippets > New Global Snippets file... creates $env:APPDATA/Code/User/snippets/puppet.json, which is more of an organization/user scoped thing and not something controllable by us.

The extension ships some snippets in https://github.com/lingua-pupuli/puppet-vscode/tree/master/snippets, but there would have to be some agreement as to how 'authoritative' they are, to Glenn's point.

github-actions[bot] commented 5 years ago

This issue has been marked stale because it has been open for more than 30 days with no activity. If this issue is still important to you, please comment to keep this issue open. If not, this will be closed in 5 days