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

[Feature Request] Remove blank lines from stripped comments #3

Open
SpriteOvO opened this issue Sep 7, 2023 · 3 comments
Open

[Feature Request] Remove blank lines from stripped comments #3

SpriteOvO opened this issue Sep 7, 2023 · 3 comments

Comments

@SpriteOvO
Copy link

For example:

#include <stdio.h>

// Let's say
// If we have multiple comments
// Like the example shown
// After using scc
// This will leave a lot of blank lines

int main() {
  printf("hello, world\n");
}

Run ./scc example.c, get result:

#include <stdio.h>







int main() {
  printf("hello, world\n");
}

It would be nice to add an option (or even by default) to remove these blank lines, for the example, I would expect more of this:

#include <stdio.h>

int main() {
  printf("hello, world\n");
}

Anyways, this is a great tool, it has helped me a lot! Thank you!

@jleffler
Copy link
Owner

jleffler commented Sep 7, 2023 via email

@SpriteOvO SpriteOvO changed the title [Feature Requres] Remove blank lines from stripped comments [Feature Request] Remove blank lines from stripped comments Sep 7, 2023
@jleffler
Copy link
Owner

jleffler commented Dec 9, 2023 via email

@SpriteOvO
Copy link
Author

I haven't read scc code carefully, so I don't know much about the implementation details, however I will try my best to give my opinions on the questions based on my understanding.

Should the output simply reduce all sequences of 2 or more consecutive blank lines (3 or more consecutive newline characters) to one blank line (2 newline characters)? Or should there be two blank lines because they were both there originally.

As a user, I would want only 1 blank line to be kept in my example because the blocks in the original (with comments) are also separated by only 1 blank line.

Should the middle comment line (line 5) be eliminated, even though there's a space in that line?

ATM, SCC reduces that to an empty line, which I think is the correct behaviour, and the 'compressing newlines' code should eliminate it too.

I agree with that. Keeping spaces between them doesn't make much sense to me.

Also, should one of lines 10 and 11 be eliminated?

IMO, I think scc should focus on stuff about/around comment, not formatting code. For the latter, we already have clang-format. So I would more expect scc not to touch anything other than comment, it might be better to keep lines 10 and 11.

I think it is going to be easier to reduce all occurrences of multiple adjacent blank lines to a single blank line than to do it only when there was a comment eliminated.

Thus, it would be fairly easy to eliminates lines 3-8 and 11 and harder to only eliminate lines 3-7. Or, at least, there's a technique that makes it fairly easy.

What about we add a new CLI option to "squash blank lines around deleted comments"? If the option is not specified, the original blank lines will be kept, otherwise the original blank lines will be squashed into only 1 blank lines.

To demonstrate what I mean, for example

 1: #include <stdio.h>
 2:
 3:
 4: // This is a comment, above are 2 blank lines
 5: // This is a comment, below are 3 blank lines
 6:
 7:
 8:
 9: int main() {
10:
11:
12:   printf("hello, world\n");
13: }

If the new CLI option is not specified, the 5 blank lines around the comments are still kept, the 2 blank lines that are not around the comment are always kept whether or not the CLI option is specified.

 1: #include <stdio.h>
 2:
 3:
 4:
 5:
 6:
 7: int main() {
 8:
 9:
10:   printf("hello, world\n");
11: }

If the new CLI option is specified, the 5 blank lines will be squashed into 1 blank line.

 1: #include <stdio.h>
 2:
 3: int main() {
 4:
 5:
 6:   printf("hello, world\n");
 7: }

I'm not sure how hard it would be, since I'm not thinking specifically about implementation details. But if you have more questions feel free to discuss them with me, I'd be happy to help in some way.

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