rowtype-yoga / pose

24 stars 1 forks source link

codeformatter deletes code when indentation of `where` is incorrect #8

Closed FloWi closed 2 years ago

FloWi commented 2 years ago

when formatting this code (with a wrong indentation of the where in the go function)

findLastIndex :: forall a. (a -> Boolean) -> List a -> Maybe Int
findLastIndex _ Nil = Nothing
findLastIndex pred xs = go 0 Nothing xs
  where
  go :: Int -> Maybe Int -> List a -> Maybe Int
  go _ res Nil = res
  go i res (y : ys) = go (i + 1) newRes ys
  where
    newRes = if (pred y) then Just i else res

the whole body of the go function is removed.

findLastIndex :: forall a. (a -> Boolean) -> List a -> Maybe Int
findLastIndex _ Nil = Nothing
findLastIndex pred xs = go 0 Nothing xs
  where
  newRes = if (pred y) then Just i else res

correct code

findLastIndex :: forall a. (a -> Boolean) -> List a -> Maybe Int
findLastIndex _ Nil = Nothing
findLastIndex pred xs = go 0 Nothing xs
  where
  go :: Int -> Maybe Int -> List a -> Maybe Int
  go _ res Nil = res
  go i res (y : ys) = go (i + 1) newRes ys
    where
    newRes = if (pred y) then Just i else res

This might not be the best purescript code, since I'm still learning purescript, but I don't expect the formatter deleting nonempty lines.

FloWi commented 2 years ago

whole file

module Ch5 where

import Data.List (List(..), (:))
import Data.Maybe (Maybe(..))
import Effect (Effect)
import Effect.Class.Console (log)
import Prelude (Unit, discard, negate, show, ($), (+), (==))

test :: Effect Unit
test = do
  log """log $ show $ findLastIndex (_ == 10) (Nil :: List Int)"""
  log $ show $ findLastIndex (_ == 10) (Nil :: List Int)
  log """log $ show $ findLastIndex (_ == 10) (10 : 5 : 10 : -1 : 2 : 10 : Nil) """
  log $ show $ findLastIndex (_ == 10) (10 : 5 : 10 : -1 : 2 : 10 : Nil)
  log """log $ show $ findLastIndex (_ == 10) (10 : 5 : 10 : -1 : 2 : 10 : 5 : Nil) """
  log $ show $ findLastIndex (_ == 10) (10 : 5 : 10 : -1 : 2 : 10 : 5 : Nil)
  log """log $ show $ findLastIndex (_ == 10) (11 : 12 : Nil)"""
  log $ show $ findLastIndex (_ == 10) (11 : 12 : Nil)

findLastIndex :: forall a. (a -> Boolean) -> List a -> Maybe Int
findLastIndex _ Nil = Nothing
findLastIndex pred xs = go 0 Nothing xs
  where
  go :: Int -> Maybe Int -> List a -> Maybe Int
  go _ res Nil = res
  go i res (y : ys) = go (i + 1) newRes ys
  where
    newRes = if (pred y) then Just i else res
i-am-the-slime commented 2 years ago

Good catch! I don't know why this would happen. If the parser can't parse the code the formatter won't format. I wonder if this could be a problem with the parser.

natefaubion commented 2 years ago

This is an issue with the parser. https://github.com/natefaubion/purescript-language-cst-parser/issues/24

FloWi commented 2 years ago

That's good news, @natefaubion. I'm glad my newbieness to a language with significant whitespaces could bring a bug to the surface :)

i-am-the-slime commented 2 years ago

@FloWi this should work now with version 0.11.2 of the formatter.

FloWi commented 2 years ago

Yes, it works fine. The formatter doesn't delete anything and the where is now highlighted as an unexpected token. Thanks for the fix @natefaubion and for the update @i-am-the-slime!