Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shell script: # followed by non-whitespace may or may not be comment #24

Open
J-Siu opened this issue Dec 27, 2024 · 8 comments
Open

Shell script: # followed by non-whitespace may or may not be comment #24

J-Siu opened this issue Dec 27, 2024 · 8 comments

Comments

@J-Siu
Copy link

J-Siu commented Dec 27, 2024

Version 1.13.1

When Remove All Comments, following line (from p10k.zsh):

(( $#branch > 32 )) && branch[13,-13]="…"  # <-- this line

becomes:

(( $

The correct outcome should be:

(( $#branch > 32 )) && branch[13,-13]="…"

Additional information:

  1. In shell script(posix), if # immediately followed by non-white-space, it may or may not not be comment.
  2. Complexity of shell script:
    # Following line does not contain comment
    cat file | grep -v -e ^# -e ^$
    # Following line is partial comment
    cat file | grep -v -e ^# -e ^$ ### This part is a comment
    
@J-Siu J-Siu changed the title Shell script: # followed by non-whitespace should not be treated as comment Shell script: # followed by non-whitespace may not be comment Dec 27, 2024
@J-Siu J-Siu changed the title Shell script: # followed by non-whitespace may not be comment Shell script: # followed by non-whitespace may or may not be comment Dec 27, 2024
@rioj7
Copy link
Owner

rioj7 commented Dec 28, 2024

@J-Siu I have tried these strings in VSC and found that syntax highlighting correctly detects the comments and command arguments.

I have looked a the TextMate Grammar and it is not easy to filter these # characters.
It looks like it has transformed a grammar for bash to a collection of begin-end regex and specify which non-terminals are possible after this begin, if non matches any more it must find the end.

If I read the TextMate Grammar correct it also accepts // as single line comment.

@J-Siu
Copy link
Author

J-Siu commented Dec 28, 2024

@rioj7 Understand. I think unless you can leverage some existing tool like "shell syntax", "shell format", or some parsing library, it will be very complicated.

One no so ideal workaround is add a "Remove All Single Line Comments", which ignore in-line comments, and add full support of shell script in future.

@rioj7
Copy link
Owner

rioj7 commented Dec 29, 2024

@J-Siu I can make the lines that have the first non whitespace a # the comment lines

@J-Siu
Copy link
Author

J-Siu commented Dec 29, 2024

@rioj7 I actually see there is a "Remove All Single Line Comments" already. So the workaround already exist. Not sure why I missed it before🤦‍♂️

@rioj7
Copy link
Owner

rioj7 commented Dec 29, 2024

@J-Siu A Single Line Comment is a comment that starts and ends on the same line, all comments with # in bash are single line comments. I have to define the comments with a regex to get the required behavior.

@J-Siu
Copy link
Author

J-Siu commented Dec 29, 2024

@rioj7 Yes. "Remove All Single Line Comments" currently also remove in-line comments.

@rioj7 rioj7 closed this as completed in 2291daa Jan 3, 2025
@rioj7
Copy link
Owner

rioj7 commented Jan 3, 2025

@J-Siu in v1.13.2 I have set that only lines starting with # are removed

@rioj7 rioj7 reopened this Jan 3, 2025
@J-Siu
Copy link
Author

J-Siu commented Jan 4, 2025

@rioj7 "Remove All Single Line Comments" is working correctly now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants