better-comments icon indicating copy to clipboard operation
better-comments copied to clipboard

Multiline Comments not working - Workaround Found

Open Indeedornot opened this issue 2 years ago • 8 comments
trafficstars

image Hi! This extension is awesome, although there appears to be some kind of problem with html comments I do have the "better-comments.multilineComments": true,

Indeedornot avatar Nov 20 '22 21:11 Indeedornot

The problem seems to be in parser.js file, the FindBlockComments uses regex on each line, where as user expected behavior is for it to match regex once until it maches a delimiter tag again

  FindBlockComments(activeEditor) {
    // If highlight multiline is off in package.json or doesn't apply to his language, return
    if (!this.highlightMultilineComments) return;
    let text = activeEditor.document.getText();
    // Build up regex matcher for custom delimiter tags
    let characters = [];
    for (let commentTag of this.tags) {
      characters.push(commentTag.escapedTag);
    }
    // Combine custom delimiters and the rest of the comment block matcher
    let commentMatchString = "(^)+([ \\t]*[ \\t]*)(";
    commentMatchString += characters.join("|");
    commentMatchString += ")([ ]*|[:])+([^*/][^\\r\\n]*)";
    // Use start and end delimiters to find block comments
    let regexString = "(^|[ \\t])(";
    regexString += this.blockCommentStart;
    regexString += "[\\s])+([\\s\\S]*?)(";
    regexString += this.blockCommentEnd;
    regexString += ")";
    let regEx = new RegExp(regexString, "gm");
    let commentRegEx = new RegExp(commentMatchString, "igm");
    // Find the multiline comment block
    let match;
    while ((match = regEx.exec(text))) {
      let commentBlock = match[0];
      // Find the line
      let line;
      while ((line = commentRegEx.exec(commentBlock))) {
        let startPos = activeEditor.document.positionAt(
          match.index + line.index + line[2].length
        );
        let endPos = activeEditor.document.positionAt(
          match.index + line.index + line[0].length
        );
        let range = { range: new vscode.Range(startPos, endPos) };
        // Find which custom delimiter was used in order to add it to the collection
        let matchString = line[3];
        let matchTag = this.tags.find(
          (item) => item.tag.toLowerCase() === matchString.toLowerCase()
        );
        if (matchTag) {
          matchTag.ranges.push(range);
        }
      }
    }
  }

Indeedornot avatar Nov 21 '22 12:11 Indeedornot

Right now the workaround i found is to write a regex that allows everything up until a tag however I do not think it is in anyway long term thing

(^)+([ \t]*[ \t]*)(!|\?|\/\/|\*)([ ]*|[:])+([^*/][^!\?\/\/\*]*)

Indeedornot avatar Nov 21 '22 15:11 Indeedornot

Are you saying you would rather:

/* ! this is red
so is this
this too! */

instead of the current:

/* ! this is red
this is normal */

My expectation of behavior would be exactly this; style from the tag to the new line. with the behavior I believe you are implying, it would be impossible to color just a single line of a multiline comment without creating a custom tag for default styling and applying it on any line not intended to by styled within a multiline comment.

aarondill avatar Dec 06 '22 05:12 aarondill

I cannot link sadly to other issues mentioning this behavior to be expected currently due to being on mobile but prior to posting I've searched issues and have seen few mentioning the same problem about having to place token on each line of multiline comment.

I would have made a fork with such behavior being the default or a pull adding a new option to prevent changes for already using users. Only problem is i cannot figure out sane way to block out words in this regex as such it is limited to single characters

Indeedornot avatar Dec 06 '22 07:12 Indeedornot

I have gone and tagged here are people referencing this approach, might not be exactly the biggest bunch, but it is visible that some do want it

#226 #391 #163 #134 #262 #350

Indeedornot avatar Dec 06 '22 16:12 Indeedornot

So I have no color of any kind in multiline / block comments in TypeScript. My current workaround is to start the line of the block comment with // . That means the following is becoming a common pattern when using TSX: image

It would be really nice if it just worked for each line of a block comment so I can leave out the weird // and have my comment move 3 characters to the left. Also, a bit of a nitpick, but may be related, it now colors the whole remainder of the line instead of reverting to normal colours before or directly after the closing comment tag.

image

The following should be exactly following the example: image But alas! image

ThaJay avatar Jun 13 '23 12:06 ThaJay

@Indeedornot How do you modify this in the settings.json file when you are working with python in visual studio? I have searched everywhere but there isnt a way to access the parser.js file in visual studio or modify the inline comment characters.

saramkhalaf avatar Oct 01 '23 23:10 saramkhalaf

I do have the option "better-comments.multilineComments" enabled.

By the way I agree it would be logical to me to put the better-comments token on each line: image

And I just noticed somthing; does this look weird to anyone else? image I believe it is (or at least it was) pretty common to precede each line of a block comment with a * for the visuals. I would expect (want?) these lines to not be coloured by the first character * but rather the one after it: ! or *

About my previous comment: It only seems to ignore better-comments tokens on the first line of a block comment and keeps the colour until the end of the line instead of the end of the comment like: image

Checking the documentation, it even colours the character before the better comments token: better-comments

ThaJay avatar Oct 02 '23 13:10 ThaJay