moetelo / twiggy

Twig language support for VS Code and Neovim.
https://marketplace.visualstudio.com/items?itemName=moetelo.twiggy
33 stars 4 forks source link

CraftCMS support #10

Closed marcusgaius closed 6 months ago

marcusgaius commented 7 months ago

Currently, the extension only supports the first half of the twig debug command, which is to target the binary, however, the 2nd part of the command is hardcoded, and thus only useable in symfony (possibly laravel) applications.

It would be a great quality of life to allow users of other frameworks, namely CraftCMS, to submit their local twig environments. Craft doesn't come with such a command out of the box, but it's possible to collect all the information which symfony command retrieves, and send it through a console command.

I have tried fiddling with your source code locally, but I am not familiar with the VSCE API and I am too busy at the moment to learn it and to be able to contribute with a PR.

I have found that the easiest way to go about it would be to make this line return the entire command and remove the hard-coded part, and then no additional work would need to be implemented. That way everyone would have to provide the whole console command, but it would work the same way for everyone.

Thank you for your good work,

Best regards, Marko

moetelo commented 6 months ago

make this line return the entire command and remove the hard-coded part

Making that line return the entire command will make other developers reimplement the same logic of collecting all of the information. That is not desired.

Better: introduce the new collection logic for CraftCMS inside of the Twiggy language server.

We could detect Symfony and CraftCMS projects separately, and if it is the Craft project, then we will run some other logic instead.

The next question here is how to collect paths, globals, functions and filters in CraftCMS.

As far as I understand, Craft is built on top of Yii. Does Yii have such kind of command out of the box? If no, how could we collect such data manually?

The farthest I could get is this: https://github.com/craftcms/cms/blob/856f45f2766b1a1df5db437b5cd3fe5cce896b5b/src/web/View.php#L364-L406 I think we could provide some PHP code to resolve environments in Craft/Yii.

marcusgaius commented 6 months ago

To my knowledge, Yii2 doesn't implement Twig out of the box in any way, it's purely Craft.

The next question here is how to collect paths, globals, functions and filters in CraftCMS. The farthest I could get is this: craftcms/cms@856f45f/src/web/View.php#L364-L406

In addition to the setter you linked here, there is also the getter just above it. It retrieves the instance of the Twig\Environment that is centrally used in Craft to provide all globals, tests, functions and filters. Paths are provided directly from the Craft's View module.

I have managed to get all the relevant information for the project by using the snippet below, although, it should be parsed/"JSONified" for metadata, similarly (or, more likely, exactly) how it's done in Symfony, especially the globals, as they mix twig globals and craft global sets, which can branch out infinitely, and will always exhaust the memory.

/** @var \craft\web\View $view */
$view = Craft::$app->getView();
$twig = Craft::$app->getView()->getTwig();

$tests = $twig->getTests();
$functions = $twig->getFunctions();
$filters = $twig->getFilters();
$globals = $twig->getGlobals();
$paths = $view->getSiteTemplateRoots();

The caveat is that there is no OOB way to use this in Craft, and any implementation is (for now) strictly per-project, but having this feature in the extension might incentivize the creators of Craft to provide such a command of their own.

moetelo commented 6 months ago

11 is a draft, although is open for suggestions and testing. Kinda works on my machine, let's see if it does on yours.

Tested on DDEV starter with php-legacy arch package.

image

marcusgaius commented 6 months ago

I am not exactly sure how to add the PR of the extension into my VS code. Could you give me a pointer or two? :)

moetelo commented 6 months ago

@marcusgaius Try this https://github.com/moetelo/twiggy#development

  • Install pnpm.
  • pnpm install in the project dir.
  • Press F5 in VS Code to start debugging.
moetelo commented 6 months ago

Let's call it a Basic CraftCMS support, and for extended support create new issues.

15