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?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Citrisin
  • 176
  • 5
  • Have you found an answer to this? because I'm facing the same problem and need to know how to do that. – m4l490n Sep 04 '15 at 20:38
  • Unfortunately not. It seems to not be possible with gnu indent. I didn't find it in documentation. – Citrisin Nov 05 '15 at 22:39

2 Answers2

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

Zombo
  • 1
  • 5
  • 43
  • 62
  • 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 09:00
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
Jedi
  • 249
  • 1
  • 9