c_formatter_42 icon indicating copy to clipboard operation
c_formatter_42 copied to clipboard

Add support for multiple assignment on a single line

Open mdabir1203 opened this issue 1 year ago • 11 comments

I used the c_formatter manually and was looking at it with different examples. I found it doesn't work with

  1. Multiple decalarations on a single line
  2. Declaration and assignation on the same line
  3. Variable declaration followed by a new line.
image

Probable Solution :

  1. 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

mdabir1203 avatar Apr 15 '23 10:04 mdabir1203

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);
}

cacharle avatar Apr 15 '23 10:04 cacharle

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);
}

mdabir1203 avatar Apr 15 '23 10:04 mdabir1203

image

mdabir1203 avatar Apr 15 '23 10:04 mdabir1203

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;
}

cacharle avatar Apr 15 '23 10:04 cacharle

#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

mdabir1203 avatar Apr 15 '23 11:04 mdabir1203

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;
}

cacharle avatar Apr 15 '23 11:04 cacharle

Yes I do. I used it today only. I installed it manually though . let me try the extension version :)

mdabir1203 avatar Apr 15 '23 11:04 mdabir1203

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

cacharle avatar Apr 15 '23 11:04 cacharle

I looked at the clang-format documentation and this stackoverflow thread but it doesn't seem possible to fix this with clang-format 😕

cacharle avatar Apr 16 '23 07:04 cacharle

image

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

mdabir1203 avatar Apr 16 '23 12:04 mdabir1203

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

cacharle avatar Apr 16 '23 12:04 cacharle