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

Indent modifications #93

Merged
merged 5 commits into from
May 26, 2024
Merged

Conversation

CabalCrow
Copy link
Contributor

Brief Description

Fixes indentation issues for function parameters & pipes stacking indentation. Also provides an alternative indentation to pipes.

Fix Details

Indentation for function parameters.

There was no indentation provided for function parameters within the [] block. Examples

  • before:
export def function_name [
parameter: type   # Documentation
optional?: type   # Documentation
]: [string -> table] {
  • now:
export def function_name [
    parameter: type   # Documentation
    optional?: type   # Documentation
]: [string -> table] {

Pipe stacking indentation.

Sometimes pipes nodes can stack on each other on double (triple & so on) the indentation. This generally happens in this pattern:

{
    cmd
} {
        cmd
    }

This would probably occur most commonly in this scenario:

if true {
    cmd
} else {
        cmd
    }

There isn't a straight forward fix to this, so it goes towards opinionated territory.
What I've done is remove pipeline indentation alltogether. This fixes the issue & I've seen only 1 place where the behaviour changes. Normally if you do a command at the start of a line, if you go to the next line you will automatically indent. This is because even a command at the start is consider a pipeline. I view this as a positive effect. However it would also remove indentation for the next line in cases you have a pipe at the end of the line.

That being said I think the indentation provided by (pipeline) nodes overall does more damage than provide convenience, so I think the best course of action is to simply remove its indentation effect & try and replicate it via other methods(the method I attempted is provided below).

Proposed change

Adding @indent.immediate to "|" for pipeline indentation. This creates python line indentation if you have an open pipe "|" at the end of the line. This approach works well with 2 caveats:

  • comments in the middle of a pipe destroy the indent level:
cmd |
    properly_indented_command |
# comment kills indentation
imporperly_indented_command
  • indent persist until a command is inputed on a line. If you enter empty lines the indent would be preserved until you input a command:
cmd |
    proper_indent |


    another_command

the above code is not valid syntax, as a naked pipe requires a command on the very next line (or a comment line). I view this as a minor issue, as even if the indent was not there if someone enters a couple of empty lines and then a command after a naken pipe it would still be invalid syntax. So it is an indent issue only when someone is writing invalid code in the first place.

In short, this approach gives you proper indent after naked pipes at the cost of breaking indentation within a pipeline if you use non-inline comments in the middle of a pipe.

Help required.

Someone should test the indentation file in helix. I personally wasn't able to get the indents.scm file to have any change in helix to properly test it.

Add indenting on parameter_bracks.
Remove the indent to pipelines. An alternative should be suggested.
Add a python style indent. The issue is it won't properly manage
comments inbetween pipes. It can also allow multiple indents between
commands after pipes, which is not premited by the language syntax.
@CabalCrow
Copy link
Contributor Author

After further testing the alternative indent do not work all the time as well, so I removed it. Now this PR only fixes the indent of function parameters & removes the indent to pipes (to fix the issues created by it).

@fdncred fdncred merged commit 8233e8b into nushell:main May 26, 2024
3 checks passed
@fdncred
Copy link
Collaborator

fdncred commented May 26, 2024

thanks

@CabalCrow CabalCrow deleted the indent_modifications branch May 26, 2024 18:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants