Visual Studio Code Snippets for the Kirby Cheat Sheet


#1

I converted my Atom snippets plugin to a Visual Studio Code plugin :confetti_ball:
https://marketplace.visualstudio.com/items?itemName=medienbaecker.kirby-snippets

If you find any bugs, please let me know.

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


#2

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


#3

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


#4

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.


#5

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 ?


#6

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:


#7

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?


#8

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.


#9

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.


#10

@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?


#11

@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:


#13

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?


#14

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.


#15

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.


#16

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


#17

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] . '}';
}

#18

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