c_formatter_42
c_formatter_42 copied to clipboard
Add support for multiple assignment on a single line
I used the c_formatter manually and was looking at it with different examples. I found it doesn't work with
- Multiple decalarations on a single line
- Declaration and assignation on the same line
- Variable declaration followed by a new line.

Probable Solution :
- Maybe parsed to check if there's any variable with data type then print a newline. I don't where you have implemented it. Maybe I could help
Multiple declaration on a single line isn't supported (feel free to add it yourself if you want that feature).
Declaration and assignment on the same line should work:
int main()
{
int a = 1;
return 0;
}
$ c_formatter_42 < t.c
int main(void)
{
int a;
a = 1;
return (0);
}
Please provide minimal examples of code that isn't formatted property for those two cases because it seems to work for me.
Variable declaration followed by a newline (I guess newline means an empty line) should work aswell:
int main()
{
int a;
int b;
return 0;
}
$ c_formatter_42 < t.c
int main(void)
{
int a;
int b;
return (0);
}
char *get_next_line(int fd)
{
char *buffer;
char *line;
static char *storage;
if (fd < 0 || BUFFER_SIZE <= 0)
return (0);
buffer = (char *)malloc(sizeof(char) * (BUFFER_SIZE + 1));
line = read_all(fd, buffer, storage);
free(buffer);
if (!line)
{
free(storage);
storage = 0;
return (storage);
}
storage = ft_trim(line);
return (line);
}

The first example works fine for me:
$ c_formatter_42 < t.c
char *get_next_line(int fd)
{
char *buffer;
char *line;
static char *storage;
if (fd < 0 || BUFFER_SIZE <= 0)
return (0);
buffer = (char *)malloc(sizeof(char) * (BUFFER_SIZE + 1));
line = read_all(fd, buffer, storage);
free(buffer);
if (!line)
{
free(storage);
storage = 0;
return (storage);
}
storage = ft_trim(line);
return (line);
}
The second one (please don't ever take screenshot of code ever again) doesn't work indeed but that's only because of multiple assignments on a single line, if I put them on multiple lines, you get the expected behavior:
$ c_formatter_42 < t.c
#include <stdio.h>
int main(void)
{
int a;
int b;
a = 0;
b = 0;
while (1000)
a += b * 100;
}
#include <stdio.h>
int main(void)
{
int a;
int b;
a = 0;
b = 0;
while (1000)
a += b;
}
Same issue. But yes multiple assignments when written in separate lines could work. Also I tried over and over again it doesn't again. The only difference I found is if there's lot of functions - I think then it makes sense that it doesn't work for the first code
Do you have the latest version?
That example works for me:
$ c_formatter_42 < t.c
#include <stdio.h>
int main(void)
{
int a;
int b;
a = 0;
b = 0;
while (1000)
a += b;
}
$ cat t.c
#include <stdio.h>
int main(void)
{
int a;
int b;
a = 0;
b = 0;
while (1000)
a += b;
}
Yes I do. I used it today only. I installed it manually though . let me try the extension version :)
Maybe the latest version on pypi.org isnt' the same as the one on this repo:
git clone [email protected]:dawnbeen/c_formatter_42.git
cd c_formatter_42
python -m venv venv
. venv/bin/activate
pip install -e .
c_formatter -h
I looked at the clang-format documentation and this stackoverflow thread but it doesn't seem possible to fix this with clang-format 😕

I got this from bing. Can we try this out. I could have done it myself although I don't know much about the code structure
I don't think those options do what we want (look at the examples):
The clang-format configuration is at https://github.com/dawnbeen/c_formatter_42/blob/master/c_formatter_42/data/.clang-format