Add PrettyPrinter That Returns Both Errors & Text #351

Open
prikhi wants to merge 1 commits from prikhi/add-alternative-coreio into master
prikhi commented 2021-07-10 02:18:59 +02:00 (Migrated from github.com)

Hi! Currently haskell-language-server uses parsePrintModule in it's brittany plugin to format source code.

This works well unless the source code causes brittany to throw warnings. See https://github.com/haskell/haskell-language-server/issues/2005

I added a new pPrintText function the Brittany.Main module, that emulates the coreIO function but only relies on a Config and the input Text, & returns the list of warnings/errors & either the original or formatted text.

This is pretty hacky, lots of code is copy-pasted & trimmed down from the stuff in coreIO. Not sure if Main is the correct place for this, if pPrintText is a decent name for this, or if I should refactor a bit to reduce duplication. Feedback welcome & feel free to just toss this away and do it correctly 😉.

It would be great if we could get a new release pushed after this sort of functionality is added - it'd mean we can bring back HLS auto-brittany-formatting to projects that heavily use constructs that cause brittany to raise warnings(like TypeOperators).

Commit message follows.


Add a new pPrintText function to the Brittany.Main module that behaves
similarly to the coreIO function, but instead only take a config &
input string & always returns a list of errors & either the unaltered or
the pretty printed text.

This new function will always check the generated text & ignores all
tracing and printing to stdout.

This a useful function for utility programs, like haskell-language-server,
that want to feed text to brittany & manipulate the output.

Hi! Currently `haskell-language-server` uses `parsePrintModule` in it's brittany plugin to format source code. This works well unless the source code causes brittany to throw warnings. See https://github.com/haskell/haskell-language-server/issues/2005 I added a new `pPrintText` function the `Brittany.Main` module, that emulates the `coreIO` function but only relies on a `Config` and the input `Text`, & returns the list of warnings/errors & either the original or formatted text. This is pretty hacky, lots of code is copy-pasted & trimmed down from the stuff in `coreIO`. Not sure if `Main` is the correct place for this, if `pPrintText` is a decent name for this, or if I should refactor a bit to reduce duplication. Feedback welcome & feel free to just toss this away and do it correctly :wink:. It would be great if we could get a new release pushed after this sort of functionality is added - it'd mean we can bring back HLS auto-brittany-formatting to projects that heavily use constructs that cause brittany to raise warnings(like `TypeOperators`). Commit message follows. --- Add a new `pPrintText` function to the Brittany.Main module that behaves similarly to the `coreIO` function, but instead only take a config & input string & always returns a list of errors & either the unaltered or the pretty printed text. This new function will always check the generated text & ignores all tracing and printing to stdout. This a useful function for utility programs, like haskell-language-server, that want to feed text to brittany & manipulate the output.
This pull request has changes conflicting with the target branch.
  • src/Language/Haskell/Brittany/Main.hs
You can also view command line instructions.

Step 1:

From your project repository, check out a new branch and test the changes.
git checkout -b prikhi/add-alternative-coreio master
git pull origin prikhi/add-alternative-coreio

Step 2:

Merge the changes and update on Gitea.
git checkout master
git merge --no-ff prikhi/add-alternative-coreio
git push origin master
Sign in to join this conversation.
There is no content yet.