emeraldwalk / vscode-runonsave

Visual Studio Code extension to run commands whenever a file is saved.
Apache License 2.0
182 stars 57 forks source link

Negotiation in match #86

Closed ripetti closed 1 month ago

ripetti commented 10 months ago

Hi,

I've got a file named parity.js I generate a parity number into this file before every save by runonsave extension.

I would like to config runonsave to run in case of saving every .js and .css and .html. The only exception that if I change parity.js file itself. (in some situation I have to write it manually) If parity.js has been changed then runonsave extension shouldn't run. But I cannot create a valid match string to achieve this. Changing of parity.js always triggers runonsave indeed.

My config is:

"emeraldwalk.runonsave": {
        "commands": [
            {
                "match": "^(?!parity\.js)(.+\.js|.+\.css|.+\.html)$",
                "isAsync": false, 
                "cmd": "echo export const parity = %RANDOM%; > parity.js"
            },
        ]
    },

What match string do I need to feel the wonderful fragrance of success? Thx.

bmingles commented 1 month ago

@ripetti An important thing to note is that you won't want to use the leading ^ since the match is run against the full path of the file. The only cases where you'd want this would be if your regex actually included an absolute path to match.

I think this regex should work for what you want for your scenario:

(\b(?!parity\b)\w+\.js)|.+\.(html|css)$

Here's what it looks like on regex101.com

image

And to use in the vscode extension, you'll need to escape the \ characters by doubling them:

"match": "(\\b(?!parity\\b)\\w+\\.js)|.+\\.(html|css)$",
bmingles commented 1 month ago

@ripetti actually a cleaner config would be to use notMatch which I just realized is undocumented.

{
    "match": "\\.(html|css|js)$",
    "notMatch": "parity\\.js$",
    "cmd": "echo export const parity = %RANDOM%; > parity.js"
}

I've created Document notMatch #99 to address documenting the notMatch.