-
Notifications
You must be signed in to change notification settings - Fork 47
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
Allow comments in selective imports. #403
Conversation
tokenLength() does not consider comments as valid tokens, so get their length explicitly. Fixes issue 384, allowing comments inbetween the symbols of selective imports.
I don't remember why |
Haven't tried that. I didn't dare changing |
I can't easily run the tests because I am on Windows a.t.m. and |
I was about to push a test to your fork but actually the fix doesn't work. It addresses the assertion failure but formating is not good: import std.algorithm, std.stdio : readln,
// comment
writeln; gives import std.algorithm, std.stdio : readln, // comment
writeln; |
With this patch (applied to the PR in its current state) the formatting is correct although there must be better ways @@ -525,10 +525,21 @@ private:
writeToken();
write(" ");
}
else if (currentIs(tok!","))
{
+ if (index + 1 < tokens.length && tokens[index + 1].type == tok!"comment")
+ {
+ writeToken();
+ write(" ");
+ writeToken();
+ simpleNewline();
+ if (!indentLevel)
+ indentLevel++;
+ indent();
+ continue;
+ }
// compute length until next ',' or ';'
int lengthOfNextChunk;
for (size_t i = index + 1; i < tokens.length; i++)
{
if (tokens[i].type == tok!"," || tokens[i].type == tok!";") |
This is how I feel about almost all the code in this project. |
@bbasile what does your proposed change do when the comment following the comma is a |
Nothing and maybe indentation is wrongly handled too (should it be always incremented ?) |
Anyway, @veelo , you can use the online tests and amend, force push, until it becomes green. |
To be frank, I have noticed the formatting issues as well, more on that below. In my eyes however, a formatting bug is a lot less severe than aborting the program and discarding most of the source (especially when used About formatting: in my quest to resolve the formatting I quickly digressed into other parts of the code like I have worked with this test file: import std.stdio : readln, writeln;
import std.stdio : readln,
writeln;
import std.stdio : readln,
writeln;
import std.stdio : readln, /* comment1 */ writeln;
import std.stdio : readln, // comment2
writeln;
import std.stdio : readln,
// comment3
writeln;
import std.stdio : readln,
/* comment4 */
writeln;
/*
123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
10 20 30 40 50 60 70 80
*/
import std.stdio : readln, readln, readln, readln, readln, readln, readln, readln, readln, readln, readln,
writeln;
import std.stdio : readln, readln, readln, readln, readln, /* comment5 -------- */ readln, readln, readln, readln, readln, readln,
writeln;
import std.stdio : readln, readln, readln, readln, readln, readln, readln, readln, readln, readln, readln,
// comment6
writeln;
import std.stdio : readln, readln, readln, readln, readln, readln, // comment7
// comment8
writeln; which is formatted, by the PR in its current state, using import std.stdio : readln, writeln;
import std.stdio : readln, writeln;
import std.stdio : readln, writeln;
import std.stdio : readln, /* comment1 */ writeln;
import std.stdio : readln, // comment2
writeln;
import std.stdio : readln, // comment3
writeln;
import std.stdio : readln, /* comment4 */
writeln;
/*
123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
10 20 30 40 50 60 70 80
*/
import std.stdio : readln, readln, readln, readln, readln, readln,
readln, readln, readln, readln, readln, writeln;
import std.stdio : readln, readln, readln, readln, readln, /* comment5 -------- */ readln,
readln, readln, readln, readln, readln, writeln;
import std.stdio : readln, readln, readln, readln, readln, readln,
readln, readln, readln, readln, readln, // comment6
writeln;
import std.stdio : readln, readln, readln, readln, readln, readln, // comment7
// comment8
writeln; As you see, the statements with In principle, the formatting of selective imports is much simpler than other code: the indenting is fixed, the types of tokens are very limited (just identifiers and comments and an occasional alias). This should be easy... I'd almost just grab |
Thought: Can't we just reuse the same code for the formatting of the definition of enumerations, inside |
Yes indeed, enum formating seems to handle comments enough closely to what's expected for imports. |
Not much is gained by using `tokenLength` anyway, use the slice length for everything. `len` is now unsigned so the assert is obsolete.
But enum members are put on separate lines, which is not how selective imports are formatted currently. I'm on to something though, hang on. |
- Let a single `pushWrapIndex` take care of indexing. - Remove chunk length calculation entirely. - Comment spacing is funny, fix by special casing. Bonus: comments that appear on a separate line are kept on a separate line.
What my last commit does not fix is line length violations where comments are involved: /*
123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
10 20 30 40 50 60 70 80
*/
import std.stdio : readln, readln, /* commentA */ readln, readln, readln, readln, readln, readln, readln, readln, readln,
writeln;
import std.stdio : readln, readln, readln, readln, readln, /* commentB -------- */ readln, readln, readln, readln, readln, readln,
writeln;
import std.stdio : readln, readln, readln, readln, readln, readln, readln, // commentC
// commentD
writeln;
void fun(int EEEEEEE1, int EEEEEEE2, int EEEEEEE3, int EEEEEEE4, int EEEEEEE5, /* comment */ int EEEEEEE6) {}
void gun(int EEEEEEE1, int EEEEEEE2, int EEEEEEE3, int EEEEEEE4, // commmmmmmmmmmmmmmmmmmmment
int EEEEEEE5) {} is formatted as (EDIT: with /*
123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
10 20 30 40 50 60 70 80
*/
import std.stdio : readln, readln, /* commentA */ readln, readln, readln, readln, readln,
readln, readln, readln, readln, writeln;
import std.stdio : readln, readln, readln, readln, readln, /* commentB -------- */ readln, readln,
readln, readln, readln, readln, writeln;
import std.stdio : readln, readln, readln, readln, readln, readln, readln, // commentC
// commentD
writeln;
void fun(int EEEEEEE1, int EEEEEEE2, int EEEEEEE3, int EEEEEEE4, int EEEEEEE5, /* comment */ int EEEEEEE6)
{
}
void gun(int EEEEEEE1, int EEEEEEE2, int EEEEEEE3, int EEEEEEE4, // commmmmmmmmmmmmmmmmmmmment
int EEEEEEE5)
{
} Function arguments share this defect so it should be considered another issue, I'd say. |
This is not a regression, as the added `import` shows. It is just the normal line braking logic doing its thing.
Agreed and merged. |
Thanks. Can we have a new release, please? :-) |
Allow comments in selective imports. (dlang-community#403)
tokenLength() does not consider comments as valid tokens, so get their length explicitly.
Fixes issue 384, allowing comments inbetween the symbols of selective imports.