-
Notifications
You must be signed in to change notification settings - Fork 152
Use ghc-lib-parser with stylish-haskell #293
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
Closed
Closed
Changes from all commits
Commits
Show all changes
81 commits
Select commit
Hold shift + click to select a range
3946b64
Add initial parsing logic for using ghc-lib-parser instead of haskell…
felixmulder e9f63dc
Add comments parsing capability
felixmulder 4278c11
Restructure interface to be a bit more similar to old stylish
felixmulder 0005bba
Only do module header in stylish step
felixmulder 97a9c57
Print both imports and module header in stylish regular path
felixmulder dc92f92
Hook up old stylish to new one
felixmulder 03e3e39
Finish implemntation of module header
felixmulder bf38e53
Print more helpful error message when encountering a parser error
felixmulder 196f445
Implement missing printer for IEThingWith
felixmulder f2a0cd6
Make sure that imports respect grouping
felixmulder 58424cd
Fix issue where 'where' might be repeated due to not being a part of AST
felixmulder 0c7072f
Start moving printers into steps
felixmulder 627448a
Remove unused decl printer
felixmulder bb8e157
Move imports printer to step
felixmulder 5ea2bbe
Enable more of the old steps and list which one need to be fixed
felixmulder f66b9f2
Make sure that module imports don't bother rest of file
felixmulder d36cced
Factor out function to associate comments with groups of expressions
felixmulder b989066
Make sure imports respect groups and preserve comments
felixmulder 8032ebb
Add ability to save return value of running printer
felixmulder da96a3b
Add GHC module for utility functions
felixmulder a1e7cf9
Make sure imports step respects groupings
felixmulder 0516749
Fix hiding getting an extra space
felixmulder 9e72c96
Move baseDynFlags to GHC util module
felixmulder 30c68a0
Fix printing of exports that are symbolic
felixmulder d02f0b6
Add span to error message for hint on where error is
felixmulder f555e4f
Add config option to emit non-zero exit code when reformatting
felixmulder 3beaf57
Implement data decl step
felixmulder abae160
Add records step to list of finished steps
felixmulder afb226e
Implement deriving via printing
felixmulder be7cdb9
Fix indentation on enums with deriving clauses
felixmulder db7c1fa
Fix multi decl formatting and newtype formatting
felixmulder 2e9a291
Handle infix constructors when printing data decls
felixmulder 24e7c44
Make sure docstrings are included if they precede `=` sign
felixmulder 257e24a
Homogenize handling of comments between type name and `=` sign
felixmulder 92068f0
Make sure newtypes are broken before `=` sign if indent eq is set
felixmulder cdcd2c5
Add support for indenting via clauses
felixmulder 9152671
Fix function printing in data decl step
felixmulder 5d63611
Print type literals in a uniform way
felixmulder 137d048
Print type operators in a uniform way
felixmulder f022ac2
Make sure nested type literals are properly printed
felixmulder 01a8dbd
Fix printing of promoted constructors
felixmulder 4be10fa
Move type printing to printer module
felixmulder 34c5c2b
Move around and document a few functions
felixmulder 2822e25
Fix double space after infix type name
felixmulder 6b636c8
Allow old steps to be run alongside new ones
felixmulder b8677cd
Fix issue where comment is not printed after type name and newline
felixmulder 8826da3
Add missing EOL comment printing to constructors
felixmulder 126577e
Rename step to show that it's using ghc-lib-parser
felixmulder 1749866
Fix incorrect error string in parseModuleHSE
felixmulder 0271e8a
Fix off-by-one error
felixmulder e406fa1
Work around edge-case where there are comments _in_ a deriving block
felixmulder a8f91c9
Make sure previous fix is idempotent
felixmulder ecb69c9
Reformulate predicate for inserting comments before eq to single binding
felixmulder a9d6248
Implement language pragma step with ghc-lib-parser
felixmulder 58a12b4
Add SYB query function for `Module`
felixmulder ff3990c
Implement redundant view/bang pattern functions using SYB query API
felixmulder 313bc77
Fix remaining configuration tests
felixmulder 3b77027
Bump haskell docker image to get newer version of stack
felixmulder 32385f4
Remove outputable helper export from printer module
felixmulder 0250597
Document Module module
felixmulder e39bfd1
Correct comment on language string length
felixmulder e5d5a0e
Add comment for `putAllSpanComments`
felixmulder ff35fb3
Rename `putModulePrefix` to `putModuleName`
felixmulder 0dda96b
Document new configuration options
felixmulder 115325a
Fix issue with incomplete pattern match on AST
felixmulder b47d606
Add missing documentation on how to enable module header formatting
felixmulder 46ca865
Add configurability on curried context and implement simple GADT support
felixmulder 3f16447
Fix typo in config file: 'parsre' -> 'parse'
felixmulder 0611d91
Remove redundant '$'
felixmulder b46676d
Use `on` for compare function
felixmulder 99ceb58
Rename `Imports'` to `ImportsGHC`
felixmulder 98e82ee
Use CPP to get rid of monoid warning for newer GHC versions
felixmulder 34f333b
Implement equality for `Import` decl
felixmulder a556f59
Implement ability to merge imports in ImportsGHC
felixmulder e1b7a64
Add formatting for long deriving clauses
felixmulder 18f96cb
Pass max columns to ImportsGHC step
felixmulder fe97c30
Add ability to split import lines that are too long
felixmulder f9d5d77
Add testing function that has sane expected/got for long lines
felixmulder 14cc117
Fix issue with multiple groups in due to abusing editor functions
felixmulder 4208e1b
Don't split 'hiding' imports since it changes the scope
felixmulder d11358a
Make sure that nullary imports are not deleted
felixmulder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ workflows: | |
jobs: | ||
build: | ||
docker: | ||
- image: 'haskell:8.6' | ||
- image: 'haskell:8.8.3' | ||
|
||
steps: | ||
- checkout | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# OPTIONS_GHC -Wno-missing-fields #-} | ||
-- | Utility functions for working with the GHC AST | ||
module Language.Haskell.Stylish.GHC | ||
( dropAfterLocated | ||
, dropBeforeLocated | ||
, dropBeforeAndAfter | ||
-- * Unsafe getters | ||
, getEndLineUnsafe | ||
, getStartLineUnsafe | ||
-- * Standard settings | ||
, baseDynFlags | ||
-- * Positions | ||
, unLocated | ||
-- * Outputable operators | ||
, showOutputable | ||
, compareOutputable | ||
) where | ||
|
||
-------------------------------------------------------------------------------- | ||
import Data.Function (on) | ||
|
||
-------------------------------------------------------------------------------- | ||
import DynFlags (Settings(..), defaultDynFlags) | ||
import qualified DynFlags as GHC | ||
import FileSettings (FileSettings(..)) | ||
import GHC.Fingerprint (fingerprint0) | ||
import GHC.Platform | ||
import GHC.Version (cProjectVersion) | ||
import GhcNameVersion (GhcNameVersion(..)) | ||
import PlatformConstants (PlatformConstants(..)) | ||
import SrcLoc (GenLocated(..), SrcSpan(..)) | ||
import SrcLoc (Located, RealLocated) | ||
import SrcLoc (srcSpanStartLine, srcSpanEndLine) | ||
import ToolSettings (ToolSettings(..)) | ||
import qualified Outputable as GHC | ||
|
||
getStartLineUnsafe :: Located a -> Int | ||
getStartLineUnsafe = \case | ||
(L (RealSrcSpan s) _) -> srcSpanStartLine s | ||
_ -> error "could not get start line of block" | ||
|
||
getEndLineUnsafe :: Located a -> Int | ||
getEndLineUnsafe = \case | ||
(L (RealSrcSpan s) _) -> srcSpanEndLine s | ||
_ -> error "could not get end line of block" | ||
|
||
dropAfterLocated :: Maybe (Located a) -> [RealLocated b] -> [RealLocated b] | ||
dropAfterLocated loc xs = case loc of | ||
Just (L (RealSrcSpan rloc) _) -> | ||
filter (\(L x _) -> srcSpanEndLine rloc >= srcSpanStartLine x) xs | ||
_ -> xs | ||
|
||
dropBeforeLocated :: Maybe (Located a) -> [RealLocated b] -> [RealLocated b] | ||
dropBeforeLocated loc xs = case loc of | ||
Just (L (RealSrcSpan rloc) _) -> | ||
filter (\(L x _) -> srcSpanStartLine rloc <= srcSpanEndLine x) xs | ||
_ -> xs | ||
|
||
dropBeforeAndAfter :: Located a -> [RealLocated b] -> [RealLocated b] | ||
dropBeforeAndAfter loc = dropBeforeLocated (Just loc) . dropAfterLocated (Just loc) | ||
|
||
baseDynFlags :: GHC.DynFlags | ||
baseDynFlags = defaultDynFlags fakeSettings llvmConfig | ||
where | ||
fakeSettings = GHC.Settings | ||
{ sGhcNameVersion = GhcNameVersion "stylish-haskell" cProjectVersion | ||
, sFileSettings = FileSettings {} | ||
, sToolSettings = ToolSettings | ||
{ toolSettings_opt_P_fingerprint = fingerprint0, | ||
toolSettings_pgm_F = "" | ||
} | ||
, sPlatformConstants = PlatformConstants | ||
{ pc_DYNAMIC_BY_DEFAULT = False | ||
, pc_WORD_SIZE = 8 | ||
} | ||
, sTargetPlatform = Platform | ||
{ platformMini = PlatformMini | ||
{ platformMini_arch = ArchUnknown | ||
, platformMini_os = OSUnknown | ||
} | ||
, platformWordSize = PW8 | ||
, platformUnregisterised = True | ||
, platformHasIdentDirective = False | ||
, platformHasSubsectionsViaSymbols = False | ||
, platformIsCrossCompiling = False | ||
} | ||
, sPlatformMisc = PlatformMisc {} | ||
, sRawSettings = [] | ||
} | ||
|
||
llvmConfig = GHC.LlvmConfig [] [] | ||
|
||
unLocated :: Located a -> a | ||
unLocated (L _ a) = a | ||
|
||
showOutputable :: GHC.Outputable a => a -> String | ||
showOutputable = GHC.showPpr baseDynFlags | ||
|
||
compareOutputable :: GHC.Outputable a => a -> a -> Ordering | ||
compareOutputable = compare `on` showOutputable |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have any other orderings in use? If not maybe it would make sense to extract this to an orphan
Ord
instance forGHC.Outputable
and not use this one explicitly?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is mostly used in the imports step. I think it might be a good idea here to invent our type for imports that carries the information we need. This'll probably be nice when we add support for merging imports - so then we add the ordering instance for that type.
WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I did two things, I changed the newtype representation of:
This makes things a bit easier to reason about, it was possible to define equality for the import individually and I think it'll give us a more intuitive API down the line to work with imports. E.g. I added a helper function
canMergeImport
which is used in the equality instance. This helper could be used in theImports
step to merge imports - and get better ordering... I think I'm gonna play with that a bit todayThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lukasz-golebiewski @felixmulder
This thread caught my eye as I perused this ticket. It looks to me that these lines are covering the same ground as
ghc-lib-parser-ex
'sHsExtendInstances
wrapper. I've mused before thatghc-lib-parser-ex
might be useful for stylish-haskell to use/extend on its conversion from haskell-src-exts toghc-lib-parser
- would be nice to share these sorts of common utilities with e.g. HLint!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm c.f.
showOutputable :: GHC.Outputable a => a -> String
withunsafePrettyPrint
(fromLanguage.Haskell.GhclibParserEx.GHC.Utils.Outputable
) too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that'd be neat 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's let this land and revisit. If you're open to it moving some of this down to
ghc-lib-parser-ex
, I think we can eliminate this file at least in entirety.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see also that
Module.hs
has quite a lot of overlap with code underGHC/Util
in HLint (e.g. parsing out language pragmas); most code in there hasn't been deemed general enough before now to get pulled down intoghc-lib-parser-ex
but I'm starting to see opportunities to coalesce on it now.