Visual Studio Code Snippets for the Kirby Cheat Sheet

I converted my Atom snippets plugin to a Visual Studio Code plugin :confetti_ball:

If you find any bugs, please let me know.

preview

Want to use the generator for any other code editor? Change the site/templates/cheatsheet.php in this repository: https://github.com/medienbaecker/kirby-snippets/tree/generator

8 Likes

I got to try this out. Looks awesome. What happends if Kirby get new functions?

Then you have to learn instead of relying on a tool :stuck_out_tongue_winking_eye:

2 Likes

Itā€˜s generated fully automatically with a modified getkirby.com installation. I just need to know when something changed and I can regenerate it all, including the tab stops.

2 Likes

have been using it for atom ever since and it was great. then went to vscode and happy to see the port.

u switched to vscode @thguenther ?

Glad to hear that!

Atom has an even better autocomplete functionality in my opinion. For example thereā€™s no way I can add a ā€œread moreā€ link in Visual Studio Code. I also liked the K-icons.

But Atom is just super bulky and loved to torture my underpowered laptop. I first switched back to Espresso, my favorite code editor in terms of look and feel. But then I was missing more and more features and switched to Visual Studio Code a few months ago. Perfect compromise for me.

Now that I invested some time in a fully automatic Kirby snippets generator, I could easily switch to another editor though. Iā€™ll let you know. :slight_smile:

I would like to update this snippet collection to Kirby 3. While it would technically be possible to write a generator, we already talked about some automatic API in Slack.
When looking at the getkirby.com repository I see the api option is set to false.

What would be the best long-term approach for this?

1 Like

like i said in slack before and why i created the original auto-completion/intellisense idea issue:

in my humble opinion using a code editor snippets plugin is far less powerfull than real auto-completion/intellisense. such snippets should be used to create blocks of code not replace context/type-aware method chaining.

I think what could be done is a JSON schema for blueprints (there are plugins that apply those to yaml files).
So that we get autocompletion there. I thought those could be autogenerated from a Kirby plugin (the schema is read from an URL normally) this way it could also list custom fields or sections.

@bnomei For me personally it would be more than enough to have autosuggestions for stuff like the field/page methods and helpers. I type $pages->filt and get $pages->filterby($field, $args) with pre-filled tab stops. Easy.

In my opinion it would be great to automatically generate this snippet collection from the Kirby docs.

How far away are we from something like the Intellisense method you mentioned?

@thguenther do not get me wrongā€¦ i just see no reason for you snippets to exist at all. intellisense does all that and better. but i will not stop you if you want it anyways. :smiley:

But youā€™d have to insert the three lines in every file you work on?

/** @var Kirby\Cms\App $kirby */
/** @var Kirby\Cms\Site $site */
/** @var Kirby\Cms\Page $page */

Would it be possible to somehow automate the insertion of them? Maybe we could write a plugin?

1 Like

no it must be in same file. afaik every template and snippet.
for controllers, models etc you can declare the type as a function param.

<?php
return function (Kirby\Cms\Site $site, Kirby\Cms\Page $page, Kirby\Cms\App $kirby) {
    // ...
}

ah you mean an atom snippet to generate that ā€œintellisense headerā€? yes. why not.

1 Like

Yes, having to change my code for this is just a deal breaker for me. I thought of some kind of ā€œtoggle pluginā€ that tells Intellisense about the three variables.

@thguenther typehinting and documentation are no deal breaker for me but i can understand why you want it to be as seamless as possible.

I started writing a plugin for the getkirby.com docs to automatically generate the JSON snippets.

Hereā€™s what I have so far:

JSON output
"$field->escape()": {
  "prefix": "->escape()",
  "body": "escape(${1})",
  "description": "Escapes the field value to be safely used in HTML templates without the risk of XSS attacks",
  "scope": "php"
},
"$field->excerpt()": {
  "prefix": "->excerpt()",
  "body": "excerpt(${1})",
  "description": "Creates an excerpt of the field value without html or any other formatting.",
  "scope": "php"
},
...
Plugin code
<?php

Kirby::plugin('medienbaecker/snippet-generator', [
  'routes' => [
    [
      'pattern' => 'docs/snippets.json',
      'action'  => function () {
        if($reference = page("docs/reference")) {
          $json = array();
          if($fieldMethods = page("docs/reference/templates/field-methods")) {
            foreach($fieldMethods->children()->visible() as $fieldMethod) {
              $json[$fieldMethod->title()->value()] = array(
                "prefix" => '->' . $fieldMethod->content()->get('title') . '()',
                "body" => $fieldMethod->content()->get('title') . '(${1})',
                "description" => $fieldMethod->excerpt()->value(),
                "scope" => "php"
              );
            }
          }
          return $json;
        }
      }
    ]
  ]
]);

I must admit I donā€™t fully understand everything about the getkirby.com website yet, so right now I have no idea how to get the tab stops working. There seems to be a ->methodCall() function but it outputs a lot more than I need/want:

Output of $fieldMethod->methodCall():

$field->excerpt(int $chars = 0, bool $strip = true, string $rep = 'ā€¦'): 

Needed for Visual Studio Code:

excerpt(${1:\\$chars = 0}, ${2:\\$strip = true}, ${3:\\$rep = '...'})

I could probably convert it with some Regex voodoo, but as I donā€™t even know where Kirby gets these parameters there might be an easier way. Iā€™d appreciate any help!

Edit: I just found this code in site/plugins/site/src/Models/HelperPage.php. Canā€™t seem to find any way to use this ->parameters() thingy, though.

Edit 2: Nevermind. I found out how I can output the parameters:

$parameters = array_column($fieldMethod->parameters(), 'export');
$body = array();
$c = 0;
foreach($parameters as $parameter) {
  $c++;
  $body[] = '${' . $c . ':\\' . explode(" ", $parameter)[1] . '}';
}
2 Likes

I just published version 3.0: https://marketplace.visualstudio.com/items?itemName=medienbaecker.kirby-snippets
If you installed it via the extension manager you can update in the app.

Please tell me about any issues here: https://github.com/medienbaecker/vs-code-kirby-snippets/issues

3 Likes

I just published version 4.0 with a lot of changes:

After using Panicā€™s Nova editor for some time I now switched back to Visual Studio Code.

In the meantime some methods have been updated, the website/docs were relaunched and features like blocks have been added. So I updated my docs to json plugin, exported a new snippets.json and updated the extension.

8 Likes

I just published version 4.1.1 with the most recent version of Kirbyā€™s docs. It should update automatically in Visual Studio Code.

5 Likes

Hi, thanks for the slick extension and your time and work you have put in. You publish your plugin on vs code marketplace. I use vscodium, the foss version without telemetry ;o) and so one is not allowed to use the vs code marketplace. Instead I use open vsx registry. Would be fine to find your extension there too ;o) Nice work!