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 function to indent things in blocks for nix-indent-line #61

Merged
merged 1 commit into from Jan 3, 2019
Merged

Conversation

etu
Copy link
Collaborator

@etu etu commented Jan 2, 2019

Add a function nix-indent-first-line-in-block that checks if the last word on the previous line is one of "let, import, [, =, (, {". Then it adds two spaces of indent to the one of the previous line.

This is a shot at improving based on the examples is #60.

It works with all examples in #60 and just by going through my configuration.nix it seems a lot better than before.

It's not perfect in all the cases like this:

let
  splitString = _sep: _s: builtins.filter
    (x: builtins.typeOf x == "string")
    (builtins.split _sep _s);
in # whatever

But it wasn't before either, in fact it behaves exactly the same as before in that case as far as I can see.

@grahamc
Copy link
Member

grahamc commented Jan 2, 2019

What does it do in this case?

let
  splitString = _sep: _s: builtins.filter
    (x: builtins.typeOf x == "string")
    (builtins.split _sep _s);
in # whatever

@etu
Copy link
Collaborator Author

etu commented Jan 2, 2019

@grahamc
It becomes this:

let
  splitString = _sep: _s: builtins.filter
  (x: builtins.typeOf x == "string")
  (builtins.split _sep _s);
in # whatever

But before that would result in:

let
splitString = _sep: _s: builtins.filter
(x: builtins.typeOf x == "string")
(builtins.split _sep _s);
in # whatever

for me, so still better than before.

@grahamc
Copy link
Member

grahamc commented Jan 2, 2019

Confirming it fixes these cases:

{
  x86 = [
    {
      name = "t1.small.x86";
    }
  ];
}
let
  x = [
    (let y = 1; in y)
    { foo = 1; }
    [ 1 2 3 ]
    x
  ];
in x

(though I note on the first line, tabbing will indent let "forever")

let
  mozilla-overlay =
    import
  (
    builtins.fetchTarball
    https://github.com/mozilla/nixpkgs-mozilla/archive/master.tar.gz
  );
in mozilla-overlay

Looks like #60 can be closed with this merge!

@etu
Copy link
Collaborator Author

etu commented Jan 3, 2019

@grahamc I have now fixed the regression that it endlessly indented the first line.

I've also made some more comments to make it easier to understand and moved logic around to not have weird things (like return -2 because I add +2 at a later place to get 0, instead I return 0 in one place and return a value that is already +2 added to in the other place).

@etu
Copy link
Collaborator Author

etu commented Jan 3, 2019

One case it doesn't handle I realized is if there's an empty line in between like this:

let

  x = [

    (let y = 1; in y)
    { foo = 1; }
    [ 1 2 3 ]
    x
  ];
in x

EDIT: And that is now fixed.

Add a function `nix-indent-first-line-in-block` that checks if the
last word on the previous line is one of "let, import, [, =, (, {".
Then it adds two spaces of indent to the one of the previous line.

And if you stand on the first line of a file, it defaults to indenting
the line to zero spaces.
@etu etu mentioned this pull request Jan 3, 2019
@matthewbauer
Copy link
Member

Awesome! Thanks

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

3 participants