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

Twig without frameworks #52

Closed
Zekfad opened this issue Nov 11, 2024 · 8 comments
Closed

Twig without frameworks #52

Zekfad opened this issue Nov 11, 2024 · 8 comments

Comments

@Zekfad
Copy link
Contributor

Zekfad commented Nov 11, 2024

We're using twig 3 directly without any framework and custom extension to add filters, functions and tags. Is there any way to configure twiggy to point to custom twig initialization?

@moetelo
Copy link
Owner

moetelo commented Nov 11, 2024

Nope

upd:

The best idea I came up with is to implement something similar to the Craft CMS approach.

You'll have to create a file that returns the project's Twig environment

$twig = new \Twig\Environment($loader, [
    // ...
]);

return $twig;

and then, in Twiggy internals, something like this will be executed to get all of the information from the environment:

/** @var \Twig\Environment $twig */
$twig = require './your/twig.php';

...

@Zekfad
Copy link
Contributor Author

Zekfad commented Nov 11, 2024

Sound good enough for our use case, look forward see this implemented!

@moetelo
Copy link
Owner

moetelo commented Nov 16, 2024

"twiggy.framework": "twig",
"twiggy.vanillaTwigEnvironmentPath": "path/to/your/twig.php",

Ping me when you try the new feature out!

@Zekfad
Copy link
Contributor Author

Zekfad commented Nov 17, 2024

@moetelo thank you for this new feature!

I've loaded twig environment successfully! But errors during load are silent (you have to inspect output of twiggy language server to know if something went wrong).

It seems I can't use navigation: imports are not navigable not local nor global (via at-namespace).
image

Filters did pick up:
image

But doesn't seems to navigate back to definition, nor I see any way to add docs for function, so it could be picked up (maybe via reflection we can traverse back to original function and read php doc comment if any?).

Also it doesn't report invalid/non-existent filters, which would be much appreciated, since we can inspect twig and know them all, this would prevent misspelled filters.

On a same note when you open suggestions in apply block it doesn't suggest filters (which is the only thing you can use with apply).
image

But when you start to type something it searches for functions:
image

@moetelo
Copy link
Owner

moetelo commented Nov 17, 2024

imports are not navigable not local nor global (via at-namespace)

For me, this works:

bin/twig:

<?php

require __DIR__.'/../vendor/autoload.php';

$loader = new \Twig\Loader\FilesystemLoader();
$loader->addPath('./templates-new', '@global');

$twig = new \Twig\Environment($loader, []);

return $twig;
{{ include('@global/example.html.twig') }}
{% include '@global/example.html.twig' %}

Ctrl+click on both include paths works as expected

What's the output of $twig->getLoader()->getNamespaces() in your case?
Those are resolved using this piece of code.

it doesn't report invalid/non-existent filters

#58

@Zekfad
Copy link
Contributor Author

Zekfad commented Nov 17, 2024

It returns just an array:

D=0 0.0496 Array
(
    [0] => __main__
    [1] => shared
    [2] => global
)

To note: we use Twig3.

getNamespaces and getPaths are methods of FilesystemLoader and not LoaderInterface which has 3 more implementations.

I assume you should use $loader->getSourceContext($name)?->getPath() to retrieve target template, but I'm not sure how local resolution works.

I've never done vscode extensions development, but have some ts knowledge, if you point me how to setup development env, I can try to debug lsp it locally and see where things go south.

@moetelo
Copy link
Owner

moetelo commented Nov 17, 2024

That would be great!

  1. Install pnpm.
  2. pnpm install in the project dir.
  3. Press F5 in VS Code to start debugging.

You should find the Extension + Server compound task in the Run and Debug panel.

Also you might want to install connor4312.esbuild-problem-matchers so the debugger correctly attaches to the process.

The php scripts are debugged just using XDebug and php phpUtils/scripts.php ...args. There is also a task Listen for XDebug in launch.json for this.

@Zekfad
Copy link
Contributor Author

Zekfad commented Nov 17, 2024

I've tried to setup package on windows with many errors because I had no clue info about how to generate tree sitter bindings (generate doesn't regenerate bindings by the way, and build via pnpm is started before user script generate call).

Gyp patch:

diff --git a/packages/tree-sitter-twig/binding.gyp b/packages/tree-sitter-twig/binding.gyp
index ab18082..9af785e 100644
--- a/packages/tree-sitter-twig/binding.gyp
+++ b/packages/tree-sitter-twig/binding.gyp
@@ -3,7 +3,7 @@
     {
       "target_name": "tree_sitter_twig_binding",
       "include_dirs": [
-        "<!(node -e \"require('nan')\")",
+        "<!(node -p \"require('node-addon-api').include_dir\")",
         "src"
       ],
       "sources": [
@@ -13,6 +13,17 @@
       ],
       "cflags_c": [
         "-std=c99",
+      ],
+      'conditions': [
+        ['OS=="win"', {
+          "msvs_settings": {
+            "VCCLCompilerTool": {
+              "ExceptionHandling": 0,
+              "AdditionalOptions": ["/EHsc"]
+            }
+          },
+          "defines": [ "NAPI_DISABLE_CPP_EXCEPTIONS" ],
+        }]
       ]
     }
   ]

My succesefull attempt was so far:

# (optional if has global python) locally install python
echo 3.13.0 > .python-version
rye init
rye sync

# will fail
pnpm install

cd packages\tree-sitter-twig
pnpm install --ignore-scripts
pnpx tree-sitter-cli init --update
pnpx tree-sitter-cli generate

# (optional if has global python) can fail build but will install python shim
echo 3.13.0 > .python-version
rye sync

pnpm install node-addon-api
pnpm remove nan
pnpm install

Also to start builders I had to modify package to use new tree sitter cli and build via docker (emcc doesn't work on windows due to obscure process.spawn CVE with .bat fix?)

diff --git a/packages/tree-sitter-twig/package.json b/packages/tree-sitter-twig/package.json
index 32cb5b2..92c1a5e 100644
--- a/packages/tree-sitter-twig/package.json
+++ b/packages/tree-sitter-twig/package.json
@@ -4,13 +4,13 @@
   "description": "Twig grammar for tree-sitter",
   "main": "bindings/node",
   "scripts": {
-    "build": "tree-sitter generate && rm -f ./tree-sitter-twig.wasm",
-    "build-wasm": "pnpm build && tree-sitter build-wasm",
+    "build": "pnpx tree-sitter-cli generate && rm -f ./tree-sitter-twig.wasm",
+    "build-wasm": "pnpm build && pnpx tree-sitter-cli build -w",
     "test": "tree-sitter test"
   },
   "license": "Mozilla Public License 2.0",
   "dependencies": {
-    "nan": "^2.17.0"
+    "node-addon-api": "^8.2.2"
   },
   "devDependencies": {
     "tree-sitter-cli": "^0.20.8"
@@ -19,19 +19,5 @@
     "README.md",
     "LICENSE",
     "tree-sitter-twig.wasm"
-  ],
-  "tree-sitter": [
-    {
-      "scope": "twig",
-      "file-types": [
-        "twig",
-        "html.twig"
-      ],
-      "highlights": [
-        "queries/highlights.scm"
-      ],
-      "injections": "queries/injections.scm",
-      "injection-regex": "twig"
-    }
   ]
 }

Also there was some warning:

Warning: your package.json's `tree-sitter` field has been automatically migrated to the new `tree-sitter.json` config file
For more information, visit https://tree-sitter.github.io/tree-sitter/creating-parsers

I'll post more details when I'll debug some info out of lsp now that I've build it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants