vscode-bracket-select icon indicating copy to clipboard operation
vscode-bracket-select copied to clipboard

No brackets found if wrong brakets are hidden in comments <fix in comments>

Open ZeroTemplate opened this issue 2 years ago • 1 comments

Based on the following code (with comments) the bracket selection fails:

{
    //
    //  Some comment that is only here for testing ( oops I forgot to close this
    //  bracket ... now the selection search will not work
    //
}

Solution: What I've did locally (not the fastest or the best solution) is to remove all comments (replace them with spaces so that the indexes work) and then no more issues (as long as your code compiles )

String.prototype.replaceAt = function(index, replacement) {
    return this.substring(0, index) + replacement + this.substring(index + replacement.length);
}

function removeComments(string){
    //Takes a string of code, not an actual function.
    //Strip comments
    const commnt_list = string.match(/\/\*[\s\S]*?\*\/|\/\/.*/g);
    for (const key in commnt_list) {
        const arr = Array(commnt_list[key].length + 1);
        string = string.replaceAt(string.indexOf(commnt_list[key]), arr.join(' '));
    }
    return string;
}

And in the selectText method removed all the coments of the selection.

function selectText(includeBrack, selection) {
    const searchContext = getSearchContext(selection);
    let { text, backwardStarter, forwardStarter } = searchContext;
    if (backwardStarter < 0 || forwardStarter >= text.length) {
        return;
    }
    searchContext.text = removeComments(searchContext.text);

Performance wise this is quite slow (not a javascript dev.)

ZeroTemplate avatar Nov 15 '22 18:11 ZeroTemplate

can you try this extension ? https://github.com/FuPeiJiang/my-bracket-select yarn yarn c

in order to skip comments, I write different bracket parsers for different languages

also: works for string escape sequence "\"" works for regex literal /abc/ inside template literal: where | is your cursor: expands `${|}` to `|${}|` instead of `$|{}|`

FuPeiJiang avatar Nov 15 '22 20:11 FuPeiJiang