Skip to content

Commit

Permalink
Fix compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
infinisil committed Mar 13, 2024
1 parent 1fbf81e commit 72ea134
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 31 deletions.
22 changes: 8 additions & 14 deletions src/Nixfmt/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ import qualified Text.Megaparsec.Char.Lexer as L (decimal)
import Nixfmt.Lexer (lexeme, pushTrivia, takeTrivia, whole)
import Nixfmt.Parser.Float (floatParse)
import Nixfmt.Types
(Ann, Binder(..), Expression(..), File, Fixity(..), Item(..), Items(..), Leaf,
(Ann(..), Binder(..), Expression(..), File, Fixity(..), Item(..), Items(..), Leaf,
Operator(..), ParamAttr(..), Parameter(..), Parser, Path, Selector(..),
SimpleSelector(..), StringPart(..), Term(..), Token(..), Trivium(..),
SimpleSelector(..), StringPart(..), Term(..), Token(..), Trivium(..), Whole(..),
operators, tokenText)
import Nixfmt.Util
(commonIndentation, identChar, isSpaces, manyP, manyText, pathChar,
Expand Down Expand Up @@ -116,18 +116,12 @@ interpolation = Interpolation <$>

-- Interpolation, but only allowing identifiers and simple strings inside
interpolationRestricted :: Parser StringPart
interpolationRestricted = Interpolation <$>
symbol TInterOpen <*>
-- simple string without dynamic interpolations
(Term <$> String <$> do
str <- string
guard $ not $ containsInterpolation str
return str
) <*>
rawSymbol TInterClose
where
containsInterpolation (Ann str _ _) =
any (\part -> case part of { Interpolation _ _ _ -> True; _ -> False }) $ concat str
interpolationRestricted = do
interpol <- interpolation
case interpol of
-- Interpolation (Whole (Term (Token (Ann _ (Identifier _) _))) _) -> pure interpol
Interpolation (Whole (Term (SimpleString _)) _) -> pure interpol
_ -> empty

simpleStringPart :: Parser StringPart
simpleStringPart = TextPart <$> someText (
Expand Down
8 changes: 4 additions & 4 deletions src/Nixfmt/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ prettyTerm (Parenthesized paropen expr (Ann closePre parclose closePost))
-- Parenthesized application
(Application f a) -> prettyApp True mempty True f a
-- Same thing for selections
(Term (Selection t _)) | isAbsorbable t -> line' <> group expr <> line'
(Term (Selection _ _)) -> group expr <> line'
(Term (Selection t _ _)) | isAbsorbable t -> line' <> group expr <> line'
(Term (Selection _ _ _)) -> group expr <> line'
-- Start on a new line for the others
_ -> line' <> group expr <> line'

Expand Down Expand Up @@ -659,8 +659,8 @@ instance Pretty [StringPart] where
-- Parenthesized application
(Application f a) -> prettyApp True mempty True f a
-- Same thing for selections
(Term (Selection t _)) | isAbsorbable t -> line' <> group expr <> line'
(Term (Selection _ _)) -> group expr <> line'
(Term (Selection t _ _)) | isAbsorbable t -> line' <> group expr <> line'
(Term (Selection _ _ _)) -> group expr <> line'
-- Start on a new line for the others
_ -> line' <> group expr <> line'

Expand Down
25 changes: 12 additions & 13 deletions src/Nixfmt/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -222,17 +222,12 @@ instance LanguageElement SimpleSelector where
(StringSelector str) -> [Term (SimpleString str)]

instance LanguageElement Selector where
mapFirstToken' f = \case
(Selector Nothing ident def) -> first (\ident' -> Selector Nothing ident' def) $ mapFirstToken' f ident
(Selector (Just dot) ident def) -> first (\dot' -> Selector (Just dot') ident def) $ mapFirstToken' f dot
mapFirstToken' f (Selector Nothing ident) = first (\ident' -> Selector Nothing ident') $ mapFirstToken' f ident
mapFirstToken' f (Selector (Just dot) ident) = first (\dot' -> Selector (Just dot') ident) $ mapFirstToken' f dot

mapLastToken' f = \case
(Selector dot ident Nothing) -> first (\ident' -> Selector dot ident' Nothing) $ mapLastToken' f ident
(Selector dot ident (Just (qmark, def))) -> first (Selector dot ident . Just . (qmark,)) $ mapLastToken' f def
mapLastToken' f (Selector dot ident) = first (\ident' -> Selector dot ident') $ mapLastToken' f ident

walkSubprograms = \case
(Selector _ ident Nothing) -> walkSubprograms ident
(Selector _ ident (Just (_, def))) -> (Term def) : walkSubprograms ident
walkSubprograms (Selector _ ident) = walkSubprograms ident

instance LanguageElement ParamAttr where
mapFirstToken' _ _ = error "unreachable"
Expand Down Expand Up @@ -268,7 +263,7 @@ instance LanguageElement Term where
(List open items close) -> first (\open' -> List open' items close) (f open)
(Set (Just rec) open items close) -> first (\rec' -> Set (Just rec') open items close) (f rec)
(Set Nothing open items close) -> first (\open' -> Set Nothing open' items close) (f open)
(Selection term selector) -> first (\term' -> Selection term' selector) (mapFirstToken' f term)
(Selection term selector def) -> first (\term' -> Selection term' selector def) (mapFirstToken' f term)
(Parenthesized open expr close) -> first (\open' -> Parenthesized open' expr close) (f open)

mapLastToken' f = \case
Expand All @@ -278,8 +273,11 @@ instance LanguageElement Term where
(Path path) -> first Path (f path)
(List open items close) -> first (List open items) (f close)
(Set rec open items close) -> first (Set rec open items) (f close)
(Selection term []) -> first (\term' -> Selection term' []) (mapLastToken' f term)
(Selection term sels) -> first (Selection term . NonEmpty.toList) (mapLastToken' f $ NonEmpty.fromList sels)
(Selection term sels (Just (orToken, def))) -> first (\def' -> Selection term sels (Just (orToken, def'))) (mapLastToken' f def)
(Selection term sels Nothing) ->
case NonEmpty.nonEmpty sels of
Just nonEmptySels -> first (\sels' -> Selection term (NonEmpty.toList sels') Nothing) (mapLastToken' f nonEmptySels)
Nothing -> first (\term' -> Selection term' [] Nothing) (mapLastToken' f term)
(Parenthesized open expr close) -> first (Parenthesized open expr) (f close)

walkSubprograms = \case
Expand All @@ -304,7 +302,8 @@ instance LanguageElement Term where
(CommentedItem comment item) ->
[ Term (Set Nothing (ann TBraceOpen) (Items [CommentedItem comment item]) (ann TBraceClose)) ]
(DetachedComments c) -> [ emptySet c ]
(Selection term sels) -> Term term : (sels >>= walkSubprograms)
(Selection term sels Nothing) -> Term term : (sels >>= walkSubprograms)
(Selection term sels (Just (_, def))) -> Term term : (sels >>= walkSubprograms) ++ [ Term def ]
(Parenthesized _ expr _) -> [expr]
-- The others are already minimal
_ -> []
Expand Down

0 comments on commit 72ea134

Please sign in to comment.