andywang425 / vscode-scriptmonkey

Userscript language support extension for vscode.
https://marketplace.visualstudio.com/items?itemName=andywang.vscode-scriptmonkey
MIT License
23 stars 3 forks source link

Activate only when filename ends with `.user.js` or document contains `// ==UserScript==` #11

Closed Araxeus closed 1 year ago

Araxeus commented 1 year ago

I think by default, each provider should only activate if the current document filename ends with .user.js then there should be an extension option to activate if the current document text contains // ==UserScript==

Maybe each provider could call a function like this?

function checkIfShouldRun(document: vscode.TextDocument, shouldSearchDocument: boolean): boolean {
    if (document.fileName.endsWith(".user.js")) {
        return true;
    }

    if (shouldSearchDocument || document.isUntitled) {
        const text = document.getText();
        return text.startsWith("// ==UserScript==") || text.includes(`${document.eol}// ==UserScript==`);
    }

    return false;
}

What do you think of this idea?

you-hengh commented 1 year ago

Thank you very much for the author's development. I also have the same requirement. I expect the plug-in to take effect only in the file with the suffix '.user.js'

andywang425 commented 1 year ago

That's a good idea. Maybe i can add an extension option like: Extension activate only in the .user.js file. Or let users to choose the the file suffixes that they expect the extension to take effect. Checking if the current document text contains //==UserScript== may cause a little bit performance loss, i think checking the file suffix is enough.

Araxeus commented 1 year ago

Checking if the current document text contains //==UserScript== may cause a little bit performance loss

My solution above only checks if one of the lines start with it, so it shouldn't matter much. And also it should be optional.

But most important is just some kind of way to control when it activates, since currently it pollutes any open js file

andywang425 commented 1 year ago

v0.0.7 has been published, check it out :) However, syntax highlight still can't be controlled by extension settings due to the limitations of vscode, editing the package.json or syntaxes/userjs.tmLanguage.json file manually seems to be the only solution.

Araxeus commented 1 year ago

This is great! I can't even find the syntax highlighting you are talking of, so LGTM!

Araxeus commented 1 year ago

@andywang425 would still be cool to have an option so that the extension checks if the first written line starts with // ==UserScript==

https://github.com/andywang425/vscode-scriptmonkey/blob/d57794ab1b629d47e48a203bef7835e4c6d091c5/src/other/fileSuffixChecker.ts#L10-L18

if (document.getText().trimStart().startsWith("// ==UserScript==")) {
    return true;
}
andywang425 commented 1 year ago

would still be cool to have an option so that the extension checks if the first written line starts with // ==UserScript==

Just did some work in this commit. I planned to implement this feature in the next release.