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

Handle the line prefix correctly #780

Merged
merged 7 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
### Changed

### Fixed

- Wrong default HIndent configuration in [`README.md`] ([#750]).
- Fix the bug of panicking when the given source code has CPP lines and space-prefixed lines ([#780]).

### Removed

Expand Down Expand Up @@ -363,6 +365,7 @@ This version is accidentally pushlished, and is the same as 5.3.3.
[@uhbif19]: https://github.com/uhbif19
[@toku-sa-n]: https://github.com/toku-sa-n

[#780]: https://github.com/mihaimaruseac/hindent/pull/780
[#750]: https://github.com/mihaimaruseac/hindent/pull/750
[#742]: https://github.com/mihaimaruseac/hindent/pull/742
[#741]: https://github.com/mihaimaruseac/hindent/pull/741
Expand Down
18 changes: 18 additions & 0 deletions TESTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3411,6 +3411,24 @@ isDebug = False
#endif
```

Conditionals inside a `where` with empty lines and CPP

```haskell
-- https://github.com/mihaimaruseac/hindent/issues/779
foo = bar + baz
where

#if 0
bar = 1

baz = 1
#else
bar = 2

baz = 2
#endif
```

Macro definitions (`#define`)

```haskell
Expand Down
6 changes: 5 additions & 1 deletion src/HIndent.hs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ reformat config mexts mfilepath rawCode =
processBlock (HaskellSource yPos text) =
let ls = S8.lines text
prefix = findPrefix ls
code = unlines' (map (stripPrefix prefix) ls)
code = unlines' (map stripPrefixIfNotNull ls)
stripPrefixIfNotNull s =
if S.null s
then s
else stripPrefix prefix s
in case parseModule mfilepath allExts (UTF8.toString code) of
POk _ m ->
Right
Expand Down
2 changes: 1 addition & 1 deletion src/HIndent/Printer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module HIndent.Printer

import Control.Applicative
import Control.Monad
import Control.Monad.State.Strict
import Control.Monad.State.Strict
import Data.ByteString.Builder
import Data.Int (Int64)
import HIndent.Config
Expand Down
Loading