better-comments
better-comments copied to clipboard
Multiline Comments not working - Workaround Found
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,
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);
}
}
}
}
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]*)(!|\?|\/\/|\*)([ ]*|[:])+([^*/][^!\?\/\/\*]*)
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.
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
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
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:
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.
The following should be exactly following the example:
But alas!
@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.
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:
And I just noticed somthing; does this look weird to anyone else?
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:
Checking the documentation, it even colours the character before the better comments token: