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 "same" or "until here" indent capture #3196

Closed
MDeiml opened this issue Jul 26, 2022 · 4 comments · Fixed by #5355
Closed

Add "same" or "until here" indent capture #3196

MDeiml opened this issue Jul 26, 2022 · 4 comments · Fixed by #5355
Labels
A-tree-sitter Area: Tree-sitter C-enhancement Category: Improvements

Comments

@MDeiml
Copy link
Contributor

MDeiml commented Jul 26, 2022

Describe your feature request

In some language (of the top of my mind Haskell and Markdown) you want to indent the next line as much as the previous one or indent it up to a certain point in the previous line.

Examples

haskell

f x = some more code
  where foo = y
        bar = z
        foobar = y * z

markdown

* multiline
  list item

This could be solved by adding a new capture @indent-until-here that causes all subsequent lines to be indented as far as the end (or maybe start, I guess that's up to discussion) of the captured node.

Something else that might be useful is allowing to set the exact string of the added indentation for all indent captures. This way multline comment markers could be handled as part of indentation. (See #1730).

Example

/// this is a doc comment
/// that is not yet finished|
fn foo() {}

If the cursor is at the | and I press enter we might want to automatically add /// to the beginning of the new line.

@MDeiml MDeiml added the C-enhancement Category: Improvements label Jul 26, 2022
@kirawi kirawi added the A-tree-sitter Area: Tree-sitter label Jul 26, 2022
@CaptainDrewBoy
Copy link

I want to concur that this would be a hugely useful addition - currently I'm using neovim and indents along these lines would make me switch.

@Triton171
Copy link
Contributor

I definitely agree that aligning syntax nodes is a much-needed feature. I think the system needs to be slightly more general than what you describe, though. For example in C/C++, if a function declaration doesn't fit on a single line, the parameter list is often split into several lines that are all aligned:

void func_with_many_args(int some_first_argument,
                         int some_second_argument,
                         int some_third_argument);

Note that even though the ( is the start of the parameter_list tree-sitter node, the following lines are aligned not to it, but to the first argument. Ideally, we could write a query like this, aligning the whole parameter_list to the first argument:

(parameter_list
    . (parameter_declaration) @anchor) @align

For other use cases, one could simply set @anchor and @align to be the same node.

Continuing comments could also nicely integrate in the indentation system. One could add a fieldpost_fix the the struct Indentation in helix-core/src/indent.rs. This would just append some string after the indentation. A query for rustdoc comments might then look something like this:

(
    (line_comment) @postfix
    (#set! "postfix-text" "///")
    (#match? @postfix "///.*")
)

The additional regex match is needed here since the rust tree-sitter grammar doesn't distinguish between comments starting with // and ///.

@Triton171
Copy link
Contributor

I'm currently prototyping @align captures and for C/C++ it's already working well. I noticed that neither haskell nor markdown currently have indent queries. For markdown, we probably don't want to add them as the formatting of markdown files is very opinionated. I don't have a lot of experience with haskell, so I don't know if I'll be able to write good queries for it. @MDeiml do you want to work on that? Alternatively, if you could send me a well-formatted (preferably large) haskell file I can also try to do it myself.

@MDeiml
Copy link
Contributor Author

MDeiml commented Dec 27, 2022

I'm also not too familiar with haskell and haven't written any in years now. The most popular formatting tool for it seems to be "hident" though, and it has a lot of examples on how code is formatted: https://github.com/mihaimaruseac/hindent/blob/master/TESTS.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tree-sitter Area: Tree-sitter C-enhancement Category: Improvements
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants