Gruntfuggly / auto-snippet

A vscode extension to automatically insert a predefined snippet when a file is created, or an empty file is opened.
Other
14 stars 1 forks source link

php snippet issue #9

Closed dvaansiseelr closed 3 years ago

dvaansiseelr commented 3 years ago

Hello

After php snippet adding in .code-snippets.code-snippets, I created an empty php file. Unfortunately, this extension does not insert new php snippet automatically. I don't make sense. Help!

settings.json:
"autoSnippet.snippets": [
        {
            "pattern": "**/*.h",
            "snippet": "header-template"
        },
        {
            "pattern": "**/*.cpp",
            "snippet": "body-template"
        },
        {
            "language": "**/*.php",
            "snippet": "wp-default-snippet"
        }
    ]

.code-snippets.code-snippets:
{
    "wp-default-snippet": {
        //"scope": "php",
        "prefix": "wp",
        "body": [
            "<?php\n// Exit if accessed directly.\nif ( ! defined( 'ABSPATH' ) ) {\n\texit;\n}"
        ],
        "description": "WP Default snippet"
    }
}
dvaansiseelr commented 3 years ago

Opened empty file c:\index.php No snippet found

Gruntfuggly commented 3 years ago

Why are you using .code-snippets.code-snippets ? When I add snippets, they either go in global-snippets.code-snippets or cpp.json (for example).

Does the snippet work if you manually insert it?

Gruntfuggly commented 3 years ago

Have you got any further with this?

dvaansiseelr commented 3 years ago

Sorry for no replying! If I manually inserted the snippet, it works! But the extension does not automatically insert the snippet. I used php.json file.

Gruntfuggly commented 3 years ago

Could you try enabling the auto-snippet debug chanel by setting autoSnippet.debug to true. If you then open the output view and select "Auto Snippet" from the drop down, it should help show what is going on.

Please post whatever it shows.

dvaansiseelr commented 3 years ago
Opened empty file c:\Users\dvaansiseelr\Desktop\Experiments\index.php
 No snippet found
Gruntfuggly commented 3 years ago

OK - please attach your snippets file so I can try it here.

dvaansiseelr commented 3 years ago

settings.json

"autoSnippet.snippets": [
        // {
        //     "pattern": "**/*.h",
        //     "snippet": "header-template"
        // },
        // {
        //     "pattern": "**/*.cpp",
        //     "snippet": "body-template"
        // },
        {
            "language": "**/*.php",
            "snippet": "wp-default-snippet"
        },
        {
            "language": "**/*.css",
            "snippet": "insert_bg_color"
        }
    ],
    "autoSnippet.debug": true

php.json

{
    "wp-default-snippet": {
        //"scope": "php",
        "prefix": "wp",
        "body": [
            "<?php\n// Exit if accessed directly.\nif ( ! defined( 'ABSPATH' ) ) {\n\texit;\n}"
        ],
        "description": "WP Default snippet"
    }

}
Gruntfuggly commented 3 years ago

The configuration is slightly wrong. You need either:

{
    "pattern": "**/*.php",
    "snippet": "wp-default-snippet"
}

or

{
    "language": "php",
    "snippet": "wp-default-snippet"
}
Gruntfuggly commented 3 years ago

I'll see if I can add a check that the specified "language" is valid.

dvaansiseelr commented 3 years ago

Oh my bad! Now it works! Thank you!

JLDesignNetwork commented 3 years ago

I see that this is still open, and since I am experiencing the issue, I though I would see if you could help me.

I read through the responses here and I did some checking around in my vscode editior. I checked both the user and workspace settings. I checked and re-checked my snippets. This extension refuses to insert any snippet into a php file when it is created, regardless of what the output says.

I don't know if the issue has anything to do with the fact that I am on MacOS 11, but perhaps it does. Any assistance would be appreciated.

autoSnippet output (debug: true):

Opened empty file /Applications/XAMPP/xamppfiles/htdocs/projects/test.php
 Matched pattern **/*.php
 Inserting snippet new-file

settings.json:

"autoSnippet.debug": true,
    "autoSnippet.snippets": [
        { 
            "pattern": "**/*.php",
            "language": "PHP",
            "snippet": "new-file" 
        }
    ],

php.json:

{
    "new-file": {
        "prefix": "php_new",
        "body": [
            "<?php",
            "$BLOCK_COMMENT_START*",
            " * Short Description: [SHORT DESCRIPTION]",
            " *",
            " * Long Description: [LONG DESCRIPTION]",
            " *",
            " * PHP version [PHP VERSION]",
            " *",
            " * LICENSE: [LICENSE TEXT]",
            " *",
            " * @category   [CATEGORY NAME]",
            " * @package    [PACKAGE NAME]",
            " * @author     Author Name <email@email.com>",
            " * @copyright  [COPYRIGHT]",
            " * @license    [LICENSE LINK][LICENSE TYPE](Types: GPL, GNU, LGPL, BSD, FreeBSD, MIT)",
            " * @version    SVN: $Id$",
            " * @link       http://pear.php.net/package/PackageName",
            " * @see        NetOther, Net_Sample::Net_Sample()",
            " * @since      File available since Release 1.2.0",
            " * @deprecated File deprecated in Release 2.0.0",
            " * @filename   $TM_FILENAME",
            " * @datetime   $CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE $CURRENT_HOUR:$CURRENT_MINUTE:$CURRENT_SECOND",
            " $BLOCK_COMMENT_END",
            "\n",
            "$LINE_COMMENT insert code here.",
            "\n",
            "?>"
        ],
        "description": "basic framework layout for php files."
    },
Gruntfuggly commented 3 years ago

You should use "pattern" or "language", but not both. Also, for language, does it work if you set it to "php" (lowercase)?

JLDesignNetwork commented 3 years ago

I have not tried php in lowecase for the language, however I have tried pattern and language, both separate and together. Every time, the extension has matched either the pattern or the language. As you can see from my post above, the extension says that it is inserting the snippet, but it never does.

Gruntfuggly commented 3 years ago

Is there anything in the developer tools console (Help->Toggle Developer Tools)?

If that doesn't show anything, I'll create a temporary debug version that you could try.

JLDesignNetwork commented 3 years ago

I tried putting PHP in all lowercase as suggested. Still nothing is inserted. Below is the autoSnippet output.

 Matched language php
 Matched pattern **/*.php
 Inserting snippet new-file
Opened empty file /Applications/XAMPP/xamppfiles/htdocs/projects/_classes/test.php
 Matched pattern **/*.php
 Inserting snippet new-file
Opened empty file /Applications/XAMPP/xamppfiles/htdocs/projects/_classes/test.php
 Matched language php
 Inserting snippet new-file

I also checked the developer tools. Nothing was in console.

Gruntfuggly commented 3 years ago

OK - stupid question - can you insert the snippet manually?

JLDesignNetwork commented 3 years ago

yes, using emmet shortcode

Gruntfuggly commented 3 years ago

There is something weird going on. If I create a test snippet for PHP in the php.json it doesn't show when I run F1: Insert Snippet.

It works fine if I put the snippet in the global-snippets.code-snippets file.

JLDesignNetwork commented 3 years ago

I have created two snippets files, one of which is a global snippet file (although I named it different from what you have). Inside both snippet files, I have the exact same snippets (copied the snippets from local file into global file after creation).

Gruntfuggly commented 3 years ago

If you do F1: Insert Snippet is the snippet listed?

JLDesignNetwork commented 3 years ago

I am on a Mac Air, F1 does nothing for me.

JLDesignNetwork commented 3 years ago

On macbook I use Command + Shift + P and typed Insert Snippet, which opens a list of snippets that can be inserted. The snippet that I want to be automatically inserted is listed in the list. In fact, since I have a user snippets files and global snippet files, some of my snippets show up twice in the list.

Screen Shot 2020-12-27 at 21 13 04
JLDesignNetwork commented 3 years ago

Any thoughts on this issue, or updates?

Gruntfuggly commented 3 years ago

I raised a vscode issue: https://github.com/microsoft/vscode/issues/113476

See today's comment - looks like there is some sort of behaviour with PHP files that auto-snippet won't be able to handle.

JLDesignNetwork commented 3 years ago

Thank you for the update.

Gruntfuggly commented 3 years ago

I did a quick test to see if using "language":"html" would work, but it didn't seem to make any difference. It might be possible to put some special handling in for PHP - maybe temporarily force the language to html or something. I'll try when a get some spare time.

JLDesignNetwork commented 3 years ago

I was thinking about that same thing. Let me know if you figure something out. Thanks again.

Gruntfuggly commented 3 years ago

Sounds like the only workaround at the moment is to define the snippet in global snippets.

JLDesignNetwork commented 3 years ago

Actually, it does not work for me from the global snippets file. I have both a local and global snippets file with exactly the same snippets in both. Neither works.

Gruntfuggly commented 3 years ago

OK - I think that's where we started. I'll carry on investigating tonight.

Gruntfuggly commented 3 years ago

Does your global snippet have a "scope" property? I can see it did have at one point. You could try changing the scope to "html,php".

JLDesignNetwork commented 3 years ago

I am sorry for the delay in response. I have had some issues here on my end.

Yes, the scope property is defined. I have not yet changed the scope, but I am going to do that now. I'll let you know how it goes.

JLDesignNetwork commented 3 years ago

Okay, that scope definition works. I checked it again the php extension and html extension. It works with the expected result of auto-inclusion of the snippet into files with the php and does not include the snippet into files with the html extension. Which is the correct behavior. Thank you for this hack.

Gruntfuggly commented 3 years ago

To be honest, I still think it's a bug in vscode - they're saying it's expected behaviour, but as a user, I would expect it to work if I follow the instructions that the app gives me!

JLDesignNetwork commented 3 years ago

Correct. If I create a php file, it should be recognized as such even if it is empty, instead of as an html file.

JLDesignNetwork commented 3 years ago

Just out of curiosity, how difficult would it be to port this into Atom as an extension? It seems as though VSCode was built on top of Atom, so it stands to reason that they share a similar framework and inner workings.

I am asking because I want to see if the same issue exists in Atom. If so, it discredits a comment on the VSCode issues tracking. But also, if works on Atom, I may just switch back to Atom. Especially since I just found out that VSCode is built on it.

Gruntfuggly commented 3 years ago

Although they share the same framework (Electron) they're actually quite different. I was an Atom user, but I switched to vscode because it felt much more responsive. When I found out how easy it was to write extensions I never went back.

From what I remember atom packages are similar to a degree, but most of this extension uses the VScode API, so you would need to find equivalent calls to do the same things.

VSCode is definitely not built on Atom though. If anything, I believe the way that VSCode handles this sort of thing is using Textmate grammar definitions, so it might be that the root of the issue is there.

Gruntfuggly commented 3 years ago

I'm going to close this as I don't think there is anything I can change in the extension. If vscode is updated, feel free to reopen.