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

[feat] add C/C++ support #5

Closed
ViRu-ThE-ViRuS opened this issue Nov 22, 2022 · 10 comments
Closed

[feat] add C/C++ support #5

ViRu-ThE-ViRuS opened this issue Nov 22, 2022 · 10 comments
Labels
enhancement New feature or request

Comments

@ViRu-ThE-ViRuS
Copy link
Contributor

hey! amazing work on this plugin, works very well for me in some of the languages I have tested it on!

I wanted to request support for C/C++ as well, as it would be very useful and the fact that there are very few options (if any) for these languages for this level of functionality. I am aware that it might be a complicated endeavor (as suggested by discussions in splitjoin.vim plugin), but its good to start a new discussion.

I think it would be a great addition to this plugin, and will attract many more devs and appreciation!

@ViRu-ThE-ViRuS ViRu-ThE-ViRuS changed the title [feat] add C++ support [feat] add C/C++ support Nov 22, 2022
@Wansmer
Copy link
Owner

Wansmer commented Nov 23, 2022

Hi! Thanks for feedback. Can you send few examples of c++: 'result of SPLIT'/'result of JOIN' (like in tests)?
I didn't use c++ in my work and no know its syntax. I think, if c++ have similar structure, TreeSJ will handle it.

Or you can try configured c++ according README.md and open a PR.

@Wansmer Wansmer added the enhancement New feature or request label Nov 24, 2022
@ViRu-ThE-ViRuS
Copy link
Contributor Author

ViRu-ThE-ViRuS commented Nov 24, 2022

sure! I can see what I can get this working with, just starting to get familiar with Treesitter.
but for a reference, checkout this issue on splitjoin.vim

AndrewRadev/splitjoin.vim#33 (comment)
AndrewRadev/splitjoin.vim#33 (comment)

@ViRu-ThE-ViRuS
Copy link
Contributor Author

ViRu-ThE-ViRuS commented Nov 24, 2022

just from a quick view, progress would require support for multiple separators configurable via a list like no_format_with is. eg: this would enable and, or, &&, ||, and so on to be used together in as one separator type.

also, there is a crash, its hard to explain how to reproduce, but it might be a missing bounds check which can just be patched:

|| Error executing Lua callback: ...e/nvim/site/pack/packer/opt/treesj/lua/treesj/format.lua:67: Cursor position outside buffer
|| stack traceback:
|| 	[C]: in function 'nvim_win_set_cursor'
|| 	...e/nvim/site/pack/packer/opt/treesj/lua/treesj/format.lua:67: in function '_format'
|| 	...are/nvim/site/pack/packer/opt/treesj/lua/treesj/init.lua:20: in function <...are/nvim/site/pack/packer/opt/treesj/lua/treesj/init.lua:19>

@Wansmer
Copy link
Owner

Wansmer commented Nov 24, 2022

Later, I'll look at the tree structure that the treesitter gives. I think that there should be no problems for c/c++ support.

@Wansmer
Copy link
Owner

Wansmer commented Nov 24, 2022

just from a quick view, progress would require support for multiple separators configurable via a list like no_format_with is. eg: this would enable and, or, &&, ||, and so on to be used together in as one separator type.

also, there is a crash, its hard to explain how to reproduce, but it might be a missing bounds check which can just be patched:

|| Error executing Lua callback: ...e/nvim/site/pack/packer/opt/treesj/lua/treesj/format.lua:67: Cursor position outside buffer
|| stack traceback:
|| 	[C]: in function 'nvim_win_set_cursor'
|| 	...e/nvim/site/pack/packer/opt/treesj/lua/treesj/format.lua:67: in function '_format'
|| 	...are/nvim/site/pack/packer/opt/treesj/lua/treesj/init.lua:20: in function <...are/nvim/site/pack/packer/opt/treesj/lua/treesj/init.lua:19>

Can you open a separate issue and send a code, preset and cursor position where you call a plugin?

@ViRu-ThE-ViRuS
Copy link
Contributor Author

ViRu-ThE-ViRuS commented Nov 24, 2022

Later, I'll look at the tree structure that the treesitter gives. I think that there should be no problems for c/c++ support.

I agree, some of the use cases are very easily implemented:

        cpp = {
            parameter_list = treesj_utils.set_preset_for_args(),
            argument_list = treesj_utils.set_preset_for_args(),
            initializer_list = treesj_utils.set_preset_for_list()
        },

ill keep looking at this, adding more cases as I find time and use.

EDIT:

the above config works for:

  1. parameter lists
void test3(int a, int b, float c) {}
---
void test3(
  int a,
  int b,
  float c
) {}
  1. argument lists
  test3(1, 2, 3.0f);
--- 
  test3(
    1,
    2,
    3.0f
  );
  1. initializer lists
  std::vector<int> table = { 1, 2, 3, 4, 5 };
---
  std::vector<int> table = {
    1,
    2,
    3,
    4,
    5,
  };

@Wansmer
Copy link
Owner

Wansmer commented Nov 24, 2022

@ViRu-ThE-ViRuS, send a code (block where you call plugin) too, please

@Wansmer
Copy link
Owner

Wansmer commented Nov 24, 2022

AndrewRadev/splitjoin.vim#33 (comment)

In the first fragment, it is a binary expression. I don't think what is the best way to format it with treesitter (because it is binary inside binary). But I also think, how implement this, but not in TreeSJ.
The second fragment you already configured in your preset. Or implied need similar formatting, when first argument stay on first line?

AndrewRadev/splitjoin.vim#33 (comment)

About this, not sure. In my todos I have task to adapt TreeSJ for split/join blocks without brackets, but here separator stay before named nodes. I'll keep that scenario in mind for future development.

@ViRu-ThE-ViRuS
Copy link
Contributor Author

AndrewRadev/splitjoin.vim#33 (comment)

In the first fragment, it is a binary expression. I don't think what is the best way to format it with treesitter (because it is binary inside binary). But I also think, how implement this, but not in TreeSJ. The second fragment you already configured in your preset. Or implied need similar formatting, when first argument stay on first line?

For the first fragment, it would make sense if clauses within ( ) are on one line, and if no grouping via ( ) is given, then just split them all into new lines. EG:

if (a && b || c) {}
---
if (
    a &&
    b || 
    c
) {}

NOTE: One would prefer to have the first clause on the first line, and the last clause inline with ) {} but I think thats a nit pick and its okay to not handle this edge case.
The main point is to split the conditions into readable lines, after which any auto formatter can take care of the first and last lines as per its configuration.

if((a && b) || c) {}
---
if (
    (a && b) ||
     c
) {}

If these groupings can be handled correctly by TreeSJ, it would be an amazing addition.
I totally understand if cases like this are beyond the scope of this plugin.

@Wansmer
Copy link
Owner

Wansmer commented Feb 7, 2023

#58, #59

@Wansmer Wansmer closed this as completed Feb 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants