[clang-format] Trying to use libFormat for code indentation

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

[clang-format] Trying to use libFormat for code indentation

via cfe-dev

Hi,


libFormat and clang-format are great for formatting but I want to use libFormat a bit differently - to provide the indentation for the next line (inside IDE, when you press CR in code editor).


Probably I miss some handy flag which helps with that but so far I've found only workarounds to do what I need:

 - add some stupid expression to the new line (something like "a;") to force libFormat indent it

 - play with clang-format flags to prevent it from shrinking the new line into the previous one

 - set ColumnLimit to the length of the previous line also to prevent shrinking


All of them work completely unreliably. Also it's often some incomplete code in the middle of typing so it might have unfinished expressions.


So I want to ask if somebody has/had similar use cases and knows how to deal with them before adding some special flag to clang-format and trying to solve my issues there.


Kind regards,

Ivan


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: [clang-format] Trying to use libFormat for code indentation

via cfe-dev

Hi Ivan,


take a look at this one: https://clang.llvm.org/doxygen/classclang_1_1Lexer.html#a9102a4b3ccbed3505f6d002502193793

The Lexer provides a function that gives the indendation for a line, given that you seem to know the line of interest you can probably query the Lexer there.


Best Jonas


Am 05.10.2018 um 13:31 schrieb Ivan Donchevskii via cfe-dev:

Hi,


libFormat and clang-format are great for formatting but I want to use libFormat a bit differently - to provide the indentation for the next line (inside IDE, when you press CR in code editor).


Probably I miss some handy flag which helps with that but so far I've found only workarounds to do what I need:

 - add some stupid expression to the new line (something like "a;") to force libFormat indent it

 - play with clang-format flags to prevent it from shrinking the new line into the previous one

 - set ColumnLimit to the length of the previous line also to prevent shrinking


All of them work completely unreliably. Also it's often some incomplete code in the middle of typing so it might have unfinished expressions.


So I want to ask if somebody has/had similar use cases and knows how to deal with them before adding some special flag to clang-format and trying to solve my issues there.


Kind regards,

Ivan



_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: [clang-format] Trying to use libFormat for code indentation

via cfe-dev

Hi Jonas,


Lexer returns only the indentation which is already in the source code but not the calculated according to the .clang-format file.

So far I've found the WhitespaceManager in libFormat but it seems to work only together with other formatting.


Ivan


From: Jonas Toth <[hidden email]>
Sent: Friday, October 5, 2018 3:16:51 PM
To: Ivan Donchevskii; cfe-dev
Subject: Re: [cfe-dev] [clang-format] Trying to use libFormat for code indentation
 

Hi Ivan,


take a look at this one: https://clang.llvm.org/doxygen/classclang_1_1Lexer.html#a9102a4b3ccbed3505f6d002502193793

The Lexer provides a function that gives the indendation for a line, given that you seem to know the line of interest you can probably query the Lexer there.


Best Jonas


Am 05.10.2018 um 13:31 schrieb Ivan Donchevskii via cfe-dev:

Hi,


libFormat and clang-format are great for formatting but I want to use libFormat a bit differently - to provide the indentation for the next line (inside IDE, when you press CR in code editor).


Probably I miss some handy flag which helps with that but so far I've found only workarounds to do what I need:

 - add some stupid expression to the new line (something like "a;") to force libFormat indent it

 - play with clang-format flags to prevent it from shrinking the new line into the previous one

 - set ColumnLimit to the length of the previous line also to prevent shrinking


All of them work completely unreliably. Also it's often some incomplete code in the middle of typing so it might have unfinished expressions.


So I want to ask if somebody has/had similar use cases and knows how to deal with them before adding some special flag to clang-format and trying to solve my issues there.


Kind regards,

Ivan



_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: [clang-format] Trying to use libFormat for code indentation

via cfe-dev
In reply to this post by via cfe-dev
Hi Ivan,
We're also interested in as-you-type formatting like indentation for clangd.
But I don't think anyone's deeply explored what would be needed from libFormat to allow this.
Cheers, Sam

On Fri, Oct 5, 2018 at 1:31 PM Ivan Donchevskii via cfe-dev <[hidden email]> wrote:

Hi,


libFormat and clang-format are great for formatting but I want to use libFormat a bit differently - to provide the indentation for the next line (inside IDE, when you press CR in code editor).


Probably I miss some handy flag which helps with that but so far I've found only workarounds to do what I need:

 - add some stupid expression to the new line (something like "a;") to force libFormat indent it

 - play with clang-format flags to prevent it from shrinking the new line into the previous one

 - set ColumnLimit to the length of the previous line also to prevent shrinking


All of them work completely unreliably. Also it's often some incomplete code in the middle of typing so it might have unfinished expressions.


So I want to ask if somebody has/had similar use cases and knows how to deal with them before adding some special flag to clang-format and trying to solve my issues there.


Kind regards,

Ivan

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: [clang-format] Trying to use libFormat for code indentation

via cfe-dev

Hi!


I've decided not to wait but to try something myself :)

Check this out - https://reviews.llvm.org/D53072

This fixes some problems for me and I only need to add some string to the new empty line not to have indentation removed.


Kind regards,

Ivan


From: Sam McCall <[hidden email]>
Sent: Friday, October 12, 2018 9:04:24 AM
To: Ivan Donchevskii
Cc: clang developer list
Subject: Re: [cfe-dev] [clang-format] Trying to use libFormat for code indentation
 
Hi Ivan,
We're also interested in as-you-type formatting like indentation for clangd.
But I don't think anyone's deeply explored what would be needed from libFormat to allow this.
Cheers, Sam

On Fri, Oct 5, 2018 at 1:31 PM Ivan Donchevskii via cfe-dev <[hidden email]> wrote:

Hi,


libFormat and clang-format are great for formatting but I want to use libFormat a bit differently - to provide the indentation for the next line (inside IDE, when you press CR in code editor).


Probably I miss some handy flag which helps with that but so far I've found only workarounds to do what I need:

 - add some stupid expression to the new line (something like "a;") to force libFormat indent it

 - play with clang-format flags to prevent it from shrinking the new line into the previous one

 - set ColumnLimit to the length of the previous line also to prevent shrinking


All of them work completely unreliably. Also it's often some incomplete code in the middle of typing so it might have unfinished expressions.


So I want to ask if somebody has/had similar use cases and knows how to deal with them before adding some special flag to clang-format and trying to solve my issues there.


Kind regards,

Ivan

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: [clang-format] Trying to use libFormat for code indentation

via cfe-dev
Cool! We were just chatting over breakfast about inserting a token to preserve the whitespace :-)
Does this work well in practice with unmatched parens/braces?

Like when typing:
void foo() {
  auto Lambda = [&](int bar) {
     ^

The idea that you disable *all* line collapsing seems like a big hammer, is it possible to set a property on the dummy token that forces a wrap before it?

I do wonder whether that makes sense as a top-level option, or whether libFormat should grow an API to format-while-typing around the cursor: would take care of inserting/removing the token, setting options about collapsing lines etc.

Happy to take this discussion to the code review.

On Fri, Oct 12, 2018 at 9:15 AM Ivan Donchevskii <[hidden email]> wrote:

Hi!


I've decided not to wait but to try something myself :)

Check this out - https://reviews.llvm.org/D53072

This fixes some problems for me and I only need to add some string to the new empty line not to have indentation removed.


Kind regards,

Ivan


From: Sam McCall <[hidden email]>
Sent: Friday, October 12, 2018 9:04:24 AM
To: Ivan Donchevskii
Cc: clang developer list
Subject: Re: [cfe-dev] [clang-format] Trying to use libFormat for code indentation
 
Hi Ivan,
We're also interested in as-you-type formatting like indentation for clangd.
But I don't think anyone's deeply explored what would be needed from libFormat to allow this.
Cheers, Sam

On Fri, Oct 5, 2018 at 1:31 PM Ivan Donchevskii via cfe-dev <[hidden email]> wrote:

Hi,


libFormat and clang-format are great for formatting but I want to use libFormat a bit differently - to provide the indentation for the next line (inside IDE, when you press CR in code editor).


Probably I miss some handy flag which helps with that but so far I've found only workarounds to do what I need:

 - add some stupid expression to the new line (something like "a;") to force libFormat indent it

 - play with clang-format flags to prevent it from shrinking the new line into the previous one

 - set ColumnLimit to the length of the previous line also to prevent shrinking


All of them work completely unreliably. Also it's often some incomplete code in the middle of typing so it might have unfinished expressions.


So I want to ask if somebody has/had similar use cases and knows how to deal with them before adding some special flag to clang-format and trying to solve my issues there.


Kind regards,

Ivan

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: [clang-format] Trying to use libFormat for code indentation

via cfe-dev

Disabling lines collapsing was the simplest thing to do since I'm not deep in libFormat code :)


And after all it's possible to handle only one range from reformat() return if you need. So it does not really matter what happens with the code outside of it.


And I will be glad if somebody with better code understanding implements it better and makes some kind of auto insertion of token inside libFormat.


From: Sam McCall <[hidden email]>
Sent: Friday, October 12, 2018 9:36:13 AM
To: Ivan Donchevskii
Cc: clang developer list
Subject: Re: [cfe-dev] [clang-format] Trying to use libFormat for code indentation
 
Cool! We were just chatting over breakfast about inserting a token to preserve the whitespace :-)
Does this work well in practice with unmatched parens/braces?

Like when typing:
void foo() {
  auto Lambda = [&](int bar) {
     ^

The idea that you disable *all* line collapsing seems like a big hammer, is it possible to set a property on the dummy token that forces a wrap before it?

I do wonder whether that makes sense as a top-level option, or whether libFormat should grow an API to format-while-typing around the cursor: would take care of inserting/removing the token, setting options about collapsing lines etc.

Happy to take this discussion to the code review.

On Fri, Oct 12, 2018 at 9:15 AM Ivan Donchevskii <[hidden email]> wrote:

Hi!


I've decided not to wait but to try something myself :)

Check this out - https://reviews.llvm.org/D53072

This fixes some problems for me and I only need to add some string to the new empty line not to have indentation removed.


Kind regards,

Ivan


From: Sam McCall <[hidden email]>
Sent: Friday, October 12, 2018 9:04:24 AM
To: Ivan Donchevskii
Cc: clang developer list
Subject: Re: [cfe-dev] [clang-format] Trying to use libFormat for code indentation
 
Hi Ivan,
We're also interested in as-you-type formatting like indentation for clangd.
But I don't think anyone's deeply explored what would be needed from libFormat to allow this.
Cheers, Sam

On Fri, Oct 5, 2018 at 1:31 PM Ivan Donchevskii via cfe-dev <[hidden email]> wrote:

Hi,


libFormat and clang-format are great for formatting but I want to use libFormat a bit differently - to provide the indentation for the next line (inside IDE, when you press CR in code editor).


Probably I miss some handy flag which helps with that but so far I've found only workarounds to do what I need:

 - add some stupid expression to the new line (something like "a;") to force libFormat indent it

 - play with clang-format flags to prevent it from shrinking the new line into the previous one

 - set ColumnLimit to the length of the previous line also to prevent shrinking


All of them work completely unreliably. Also it's often some incomplete code in the middle of typing so it might have unfinished expressions.


So I want to ask if somebody has/had similar use cases and knows how to deal with them before adding some special flag to clang-format and trying to solve my issues there.


Kind regards,

Ivan

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev