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(c_sharp): add indent query #5344

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions queries/c_sharp/indents.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
[
(property_declaration)
; (field_declaration)
(variable_declaration)
(accessor_declaration)

; (declaration_list)

(parameter_list)
(type_parameter_list)
(accessor_list)
(attribute_argument_list)
(base_list)
(argument_list)

(binary_expression)
(expression_statement)
(return_statement)
(local_declaration_statement)
(local_function_statement)
(yield_statement)
(fixed_statement)
(unsafe_statement)
(labeled_statement)
(for_each_statement)
(for_statement)
(if_statement)
lucario387 marked this conversation as resolved.
Show resolved Hide resolved
(switch_expression)
(switch_section)
(switch_statement)
(while_statement)
(lambda_expression)
(do_statement)
(checked_statement)
(try_statement)
(using_statement)
(local_function_statement)
(object_creation_expression)
(array_creation_expression)
(implicit_object_creation_expression)
lucario387 marked this conversation as resolved.
Show resolved Hide resolved

] @indent.begin

(method_declaration body: _ @indent.begin)
(namespace_declaration body: _ @indent.begin)
(record_declaration body: _ @indent.begin)
(class_declaration body: _ @indent.begin)
(struct_declaration body: _ @indent.begin)
(constructor_declaration body: _ @indent.begin)
(interface_declaration body: _ @indent.begin)
(enum_declaration body: _ @indent.begin)
lucario387 marked this conversation as resolved.
Show resolved Hide resolved

(property_declaration (accessor_list "{" @indent.branch) @indent.dedent)

(switch_section
(case_switch_label) (case_switch_label) @indent.branch)
lucario387 marked this conversation as resolved.
Show resolved Hide resolved


(if_statement (block "{" @indent.branch))
(if_statement alternative: (if_statement) @indent.dedent)
Comment on lines +59 to +60
Copy link
Member

@lucario387 lucario387 Sep 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The if_statement should be similar enough to C that I suggest you can use the C's indent queries version, with nodes modification (i.e. (compound_statement) -> (block))

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember I was looking onto C's version, but I understood only like half of it, so i did this, which I undestand. I think there is subtle difference in tree for C and C#. I generally had hard time to debug the thing so I ended up with kind of first working solution.


(while_statement
(block "{" @indent.branch))

(array_creation_expression
(initializer_expression "{" @indent.branch))
(object_creation_expression
(initializer_expression "{" @indent.branch))
(implicit_object_creation_expression
(initializer_expression "{" @indent.branch))

(for_each_statement
body: (block "{" @indent.branch))

(lambda_expression
body: (block "{" @indent.branch))

(for_statement
body: (block "{" @indent.branch))

(switch_expression "{" @indent.branch)

(switch_statement
(switch_body "{" @indent.branch))

(try_statement
body: (block "{" @indent.branch))
(catch_clause
body: (block "{" @indent.branch))
(finally_clause
(block "{" @indent.branch))

(fixed_statement
(block "{" @indent.branch))

(using_statement
body: (block "{" @indent.branch))
lucario387 marked this conversation as resolved.
Show resolved Hide resolved

["catch" "finally" "else" "}" "]" ")" ] @indent.branch

["}" ")" "]" ] @indent.end

(expression_statement ";" @indent.end)

(local_function_statement ";" @indent.end)
(local_declaration_statement ";" @indent.end)

; this is fucked up, because grammer doesn't match # as part of the token
[
(if_directive)
(else_directive)
(elif_directive)
(endif_directive)
(nullable_directive)
(pragma_directive)
(undef_directive)
] @indent.zero
Copy link
Member

@theHamsta theHamsta Sep 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some tests? At least there should be some test files on how this is expected to work.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I started writing something. But I probably don't get how it is supposed to work. I managed to start the test. I wrote some example files. But no matter what I put in those files, whole_file always passes. I'd expect it to fail when the file isn't indented in harmony with the query. newline fails sometimes, but obviously puts different indetation compared to == on such line. So, is it for testing opening new line and indentation triggers? Or just one of them?