7

Is there a way to convince GNU indent to break long comment and change it to multiline comment? Something like this:

// Very long comment, longer than 80 characters. Just imagine that.

To this:

/*
 * Very long comment, longer than 80 characters. Just
 * imagine that.
 */

I know that formatting second one can be done simply by '-cdb -sc', but I don't know how to ensure line break and comment type change. Is it even possible?

  • Have you found an answer to this? because I'm facing the same problem and need to know how to do that. – m4l490n Sep 4 '15 at 20:38
  • Unfortunately not. It seems to not be possible with gnu indent. I didn't find it in documentation. – Citrisin Nov 5 '15 at 22:39
1
sed -i '\_//_{s_//_/* _g;s_$_ */_g}' file.c
indent -fc1 -fca -sc -cdb file.c
sed -i 's/\/\*\(.*\)\*\/$/\/\/\1/' file.c

does what you requested, but I would be cautious as it probably misses some edge cases

Input:

// Lorem ipsum dolor sit amet, apeirian constituam interpretaris no his, soluta salutandi persequeris vel ne, facete impedit contentiones te eam. Ut utamur habemus qualisque usu
#include <stdio.h>
int main () // damn
{
  puts ("hello world" /* sheeet */); }              /* oh shit */

Output:

/*
 * Lorem ipsum dolor sit amet, apeirian constituam interpretaris no his,
 * soluta salutandi persequeris vel ne, facete impedit contentiones te eam.
 * Ut utamur habemus qualisque usu
 */
#include <stdio.h>
int
main ()             // damn
{
  puts ("hello world" /* sheeet */ );
}               // oh shit
1

I got this to work, for some reason it requires both -fc1 and -fca options:

indent -fc1 -fca j.c

Alternatively, you can use the Berkeley style:

indent -orig j.c

Input:

// Lorem ipsum dolor sit amet, apeirian constituam interpretaris no his, soluta salutandi persequeris vel ne, facete impedit contentiones te eam. Ut utamur habemus qualisque usu
#include <stdio.h>
int main(){puts("hello world");}

Output:

// Lorem ipsum dolor sit amet, apeirian constituam interpretaris no his,
// soluta salutandi persequeris vel ne, facete impedit contentiones te eam.
// Ut utamur habemus qualisque usu
#include <stdio.h>
int
main ()
{
  puts ("hello world");
}

Example

  • Partly this is some sort of solution, but it breaks style (multi line comment should be /* */). Idea was that it wouldn't only break to new line but also changed comment type. I will accept this answer after while if no one founds better solution, but it is not exactly what I needed. – Citrisin Jun 15 '16 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.