guibou / PyF

Haskell QuasiQuoter for String Formatting

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Whitespace trimming

guibou opened this issue · comments

PyF does not trim whitespaces. So multiline string most of the time looks weird with respect to indentation, such as:

main = do
   putStrLn [fmt|\
- a
- b
|]

There are many possible design in order to do that:

Trim common lines

We can ignore common whitespaces and "blank" lines at the beginning / end, such as:

main = do
   putStrLn [fmt|
      - a
      - b
    |]

Will results in the string -a\n-b\n.

  • What to do with the last line break?
  • This does not allows leading white space on all lines. We can force them with an empty {""} replacement field.

Align with the |

main = do
   putStrLn [fmt|
                          - a
                          - b
                        |]
  • This is super painful.

Add an alignment marker

main = do
   putStrLn [fmt|
                   | - a
                   | - b
                   |]

Here the | is used as an alignement marker.

Where to put the new function?

Should we put the new function in a separated module (i.e. PyF.Strip.fmt) or in the same module with a new name, such as PyF.fmtStripped?

Should we change the default?

PyF.fmt may be the one with whitespaces stripping, whe PyF.fmtRaw won't have whitespace stripping.

I'm in favor of this solution, however that's a breaking change. Considering the popularity of PyF right now, it is only used in krank (my project) and envstatus (@gbataille project). So changing the default won't harm many people, but it may harm people in non published projects.

We'll change the major PyF version, so users will be aware of some "breaking" changes.

It may be possible to have a smooth transition. The whitespacing is happening at compile time. So more logic at that time won't impact performance. I propose the following design:

When formatting, we check if the trimmed format string is similar to the untrimmed one. If yes, do nothing. If not, emit a warning. The warning may be disable with an environment variable.

Closed by #75.

It uses common indentation, following the description in Python.

import PyF.Trimmed

main = do
   putStrLn [fmt|
      - a
      - b
    |]

will outputs

- a
- b

Init and trailing whitespaces are ignored. The qq is now in a new module PyF.trimmed.