Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Use Brittany with hsimport when needed #783

Merged
merged 17 commits into from
Nov 11, 2018

Conversation

lukel97
Copy link
Collaborator

@lukel97 lukel97 commented Aug 25, 2018

This does a spartan check to see if the imports are formatted the way Brittany formats it, and if so then applies a coat of Brittany to the import code action.
Closes #772

Todo

  • Add LSP config option to disable this

return (any isFormattedLine ls)
-- Only use Brittany formatting if it's already formatted
isFormattedLine x
| "import qualified " `T.isPrefixOf` x = True
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty much like most people write the qualified import, so it is a potential for a false positive. Maybe check the presence of at least two spaces after the next block of text?

@Gurkenglas
Copy link
Contributor

Gurkenglas commented Aug 25, 2018

The obvious way to check if it's formatted with britanny is to format it with brittany and see if that changes anything.

return $ IdeResultOk (J.WorkspaceEdit mChanges mDocChanges)

where hasFormattedImports fp = do
ls <- T.lines <$> liftIO (T.readFile fp)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this PR, can we consider not using the read function from the Text package? I’ve had problem in the past with locale settings messing up the results.

An alternative is to used ByteString and do a lenient decode to utf8

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this same issue be causing #667?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's exactly how the problem manifests. Should we create a ticket to get this addressed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lorenzo We can probably just reuse that ticket, and add some comments explaining that it could be related to Data.Text.IO.readFile. I just did a quick search and hie does use it in quite a few locations

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran into similar issues when working on the PureScript IDE integration, if it's any help we're using these functions to read source files currently: https://github.com/purescript/purescript/blob/b07042fb7b4f6a68c0fdc68b7e51076b7106cfba/src/System/IO/UTF8.hs

@lukel97
Copy link
Collaborator Author

lukel97 commented Aug 28, 2018

Updated to simply always apply Brittany formatting

@lukel97 lukel97 changed the title Use Brittany with hsimport when needed [WIP]Use Brittany with hsimport when needed Sep 1, 2018
@alanz
Copy link
Collaborator

alanz commented Oct 28, 2018

I think this PR needs an update.

@alanz
Copy link
Collaborator

alanz commented Oct 28, 2018

Then we can probably merge it.

This allows for plugins to now access config options
It also streamlines the process of running the monads from Scheduler
Should be expanded upon to move VFS into
@lukel97
Copy link
Collaborator Author

lukel97 commented Nov 4, 2018

Not sure what I did differently but managed to add LspFuncs into IdeM, which now gives all plugins access to the Config so we can add an LSP option to disable this. This also turned out to have some great benefits and I was able to refactor Scheduler so that ideDispatcher and ghcDispatcher are symmetrical.
I plan on revising #861 so we can move VFS access straight into IdeM too!

@lukel97 lukel97 changed the title [WIP]Use Brittany with hsimport when needed Use Brittany with hsimport when needed Nov 5, 2018
@lukel97 lukel97 requested review from alanz and lorenzo November 5, 2018 00:51
@lukel97
Copy link
Collaborator Author

lukel97 commented Nov 5, 2018

Ready for re-review

@lukel97 lukel97 added type: enhancement type: refactor Refactor and tidy up internals. labels Nov 5, 2018
Copy link
Collaborator

@lorenzo lorenzo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like this refactoring. Left a few comments on things that could be improved

]

-- ---------------------------------------------------------------------

-- | For the diagnostic providers in the config, return a map of
-- current enabled state, indexed by the plugin id.
getDiagnosticProvidersConfig :: Config -> Map.Map PluginId Bool
getDiagnosticProvidersConfig :: Config -> Map.Map T.Text Bool
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this different?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PluginId is defined inside PluginsIdeMonads, which now imports Config so its to avoid a circular dependency. This is probably a sign that we should split the module up a bit

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, makes sense

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we not keep it as a PluginId, just define it in a different place? I am not all that happy with it sudennly being Text, we are losing coherence.

Copy link
Collaborator Author

@lukel97 lukel97 Nov 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this function be moved into LspStdio.hs to avoid the circular dependency? It feels like it belongs in Config.hs though. I couldn't really find a logical grouping of Plugin related types to move out PluginsIdeMonads since they all reference IdeGhcM etc.
Another possibility is that PluginDescriptor gets moved into PluginsIdeMonads where the actual PluginDescriptor type lives

Just lf -> return (Core.rootPath lf)
Nothing -> return Nothing

data IdeEnv = IdeEnv (Maybe (Core.LspFuncs Config))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make this a newtype instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant to make this more than a wrapper for LspFuncs, for things that shouldn't change during the execution of IdeM/IdeGhcM/IdeDeferM. Updated it to include the PID and plugins

-> IO void
ideDispatcher stateVar caps env errorHandler callbackHandler pin =
-> IdeM void
ideDispatcher env errorHandler callbackHandler pin =
-- TODO: AZ run a single ReaderT, with a composite R.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This TODO should be moved to the place where the code was moved to

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fixed

@lukel97
Copy link
Collaborator Author

lukel97 commented Nov 6, 2018

Looks like chocolately is down, AppVeyor build should be fine otherwise

@lukel97
Copy link
Collaborator Author

lukel97 commented Nov 10, 2018

@alanz @lorenzo is this ready to merge?

@lorenzo
Copy link
Collaborator

lorenzo commented Nov 10, 2018

I’m ok with merging this

@alanz
Copy link
Collaborator

alanz commented Nov 10, 2018

LGTM, merge when CI passes

@alanz
Copy link
Collaborator

alanz commented Nov 10, 2018

BTW, I have seen intermittent failures with that test on various branches / ghc versions recently. I think we should try to make it more robust.

@lukel97 lukel97 merged commit 8514a5b into haskell:master Nov 11, 2018
@alanz alanz added this to the prehistory milestone Feb 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: enhancement type: refactor Refactor and tidy up internals.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants