From 2c6f39e1a33ac3b8c139d944f9cf3e4464792587 Mon Sep 17 00:00:00 2001 From: Ryan Hendrickson Date: Thu, 25 Jul 2024 19:50:57 -0400 Subject: [PATCH] Support pipe operators --- src/Nixfmt/Parser.hs | 5 ++--- src/Nixfmt/Types.hs | 8 +++++++- test/diff/operation/in.nix | 13 +++++++++++++ test/diff/operation/out.nix | 13 +++++++++++++ 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/Nixfmt/Parser.hs b/src/Nixfmt/Parser.hs index f424876f..ddfb558d 100644 --- a/src/Nixfmt/Parser.hs +++ b/src/Nixfmt/Parser.hs @@ -17,7 +17,7 @@ import Data.Maybe (fromMaybe, mapMaybe, maybeToList) import Data.Text (Text) import qualified Data.Text as Text import Data.Void (Void) -import Nixfmt.Lexer (lexeme, pushTrivia, takeTrivia, whole) +import Nixfmt.Lexer (lexeme, takeTrivia, whole) import Nixfmt.Parser.Float (floatParse) import Nixfmt.Types ( Ann (..), @@ -38,7 +38,6 @@ import Nixfmt.Types ( StringPart (..), Term (..), Token (..), - Trivium (..), Whole (..), operators, tokenText, @@ -470,7 +469,7 @@ operator t = TMinus -> ">" TMul -> "/" TDiv -> "/*" - TLess -> "=" + TLess -> "=|" TGreater -> "=" TNot -> "=" _ -> "" diff --git a/src/Nixfmt/Types.hs b/src/Nixfmt/Types.hs index c41e5841..4af6d939 100644 --- a/src/Nixfmt/Types.hs +++ b/src/Nixfmt/Types.hs @@ -424,6 +424,8 @@ data Token | TLessEqual | TNot | TUnequal + | TPipeTo + | TPipeFrom | SOF deriving (Eq, Show) @@ -466,7 +468,9 @@ operators = ], [Op InfixL TAnd], [Op InfixL TOr], - [Op InfixL TImplies] + [Op InfixL TImplies], + [Op InfixL TPipeTo], + [Op InfixR TPipeFrom] ] tokenText :: Token -> Text @@ -519,4 +523,6 @@ tokenText TLess = "<" tokenText TLessEqual = "<=" tokenText TNot = "!" tokenText TUnequal = "!=" +tokenText TPipeTo = "|>" +tokenText TPipeFrom = "<|" tokenText SOF = "" diff --git a/test/diff/operation/in.nix b/test/diff/operation/in.nix index 44d3ba92..70ec4563 100644 --- a/test/diff/operation/in.nix +++ b/test/diff/operation/in.nix @@ -156,4 +156,17 @@ z = 30; } ) + + # Experimental pipe operators + ( + a // b + |> f "very long argument should justify splitting this over multiple lines" + |> g { } + ) + + ( + g { } <| + f "very long argument should justify splitting this over multiple lines" <| + a // b + ) ] diff --git a/test/diff/operation/out.nix b/test/diff/operation/out.nix index f094d0d2..f032b244 100644 --- a/test/diff/operation/out.nix +++ b/test/diff/operation/out.nix @@ -273,4 +273,17 @@ z = 30; } ) + + # Experimental pipe operators + ( + a // b + |> f "very long argument should justify splitting this over multiple lines" + |> g { } + ) + + ( + g { } + <| f "very long argument should justify splitting this over multiple lines" + <| a // b + ) ]