1

I am a C programmer and I am looking for a plugin that automatically adds semicolons when necessary before writing changes to a file. For example, look at the following example.

printf("%s\n", string)

This should be turned into:

printf("%s\n", string);

It would be nice if the same plugin also did this for other languages that require semicolons, like CSS.

4
  • 1
    Maybe with treesitter it's possible. Otherwise with regex-style substitution, it'd be a hell to come up with the correct regex for a C-style statement. (My unprofessional advice is to just put the semicolon yourself.)
    – 3N4N
    Aug 12, 2022 at 4:45
  • Okay thanks :).
    – Amarakon
    Aug 12, 2022 at 17:19
  • 1
    This is not directly related to the question, but, one can quickly add a semicolon to the end of the current line with the keys A ; Esc... and repeat this at another line with ..
    – husB
    Aug 13, 2022 at 15:25
  • Don't get me wrong. Your request is valid. It's just that as far as I understand, the solution would be very complicated (as illustrated below by Martin). The cost-benefit ratio just wouldn't be feasible.
    – 3N4N
    Aug 15, 2022 at 8:14

1 Answer 1

3

Languages with automatic semicolon insertion such as JavaScript and Go have as bunch of edge-cases where they do unexpected things, and any C solution will probably be no different. In C macros in particular make it extra hard:

#define IF    if(
#define THEN  ){
#define ELSE  } else {
#define ELIF  } else if (
#define FI    ;}

IF foo THEN
   stmt();
FI

This is a bit of a silly thing to do, but people do use macros like that. I've seen "macro abuse" like this in more modern/current codebases too.

Merely inserting a semi-colon after a )\n won't really work either:

int value = (cond == 5)
    ? (this_value)
    : (other_value);

if (foo)
{
}

if (foo) bar();  // Do need it here!

So you really need deep knowledge of the C code, and as mentioned, even with that it's pretty tricky to get right. There's an open feature request for clangd language server.

For CSS it might be a little bit more doable as the syntax is simpler, but I wouldn't be surprised if there are edge cases there too.

My advice: just type the semicolons.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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