Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multiple assignment on a single line #49

Open
mdabir1203 opened this issue Apr 15, 2023 · 11 comments
Open

Add support for multiple assignment on a single line #49

mdabir1203 opened this issue Apr 15, 2023 · 11 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@mdabir1203
Copy link

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
@cacharle
Copy link
Collaborator

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

@mdabir1203
Copy link
Author

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
Copy link
Author

image

@cacharle
Copy link
Collaborator

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 cacharle changed the title Reformating doesn't work properly Add support for multiple assignment on a single line Apr 15, 2023
@cacharle cacharle added enhancement New feature or request help wanted Extra attention is needed labels Apr 15, 2023
@mdabir1203
Copy link
Author

mdabir1203 commented Apr 15, 2023

#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

@cacharle
Copy link
Collaborator

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

@mdabir1203
Copy link
Author

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

@cacharle
Copy link
Collaborator

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
Copy link
Collaborator

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

@mdabir1203
Copy link
Author

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

@cacharle
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants