Skip to content

Commit

Permalink
Merge pull request #264 from georgefst/fourmolu
Browse files Browse the repository at this point in the history
Add fourmolu plugin (attempt 2) and add Brittany for ghc-8.10.1
  • Loading branch information
lukel97 committed Aug 8, 2020
2 parents cb430d3 + 3df2bbc commit d74d111
Show file tree
Hide file tree
Showing 18 changed files with 232 additions and 60 deletions.
9 changes: 8 additions & 1 deletion cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ packages:
./
ghcide

source-repository-package
type: git
location: https://github.com/bubba/brittany.git
tag: c59655f10d5ad295c2481537fc8abf0a297d9d1c

tests: true

package *
Expand All @@ -14,4 +19,6 @@ package ghcide

write-ghc-environment-files: never

index-state: 2020-07-27T12:40:45Z
index-state: 2020-08-07T11:45:57Z

allow-newer: data-tree-print:base
6 changes: 3 additions & 3 deletions exe/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ import Ide.Plugin.Example as Example
import Ide.Plugin.Example2 as Example2
import Ide.Plugin.GhcIde as GhcIde
import Ide.Plugin.Floskell as Floskell
import Ide.Plugin.Fourmolu as Fourmolu
import Ide.Plugin.Ormolu as Ormolu
import Ide.Plugin.StylishHaskell as StylishHaskell
import Ide.Plugin.Retrie as Retrie
#if AGPL && !MIN_VERSION_ghc(8,10,1)
#if AGPL
import Ide.Plugin.Brittany as Brittany
#endif
import Ide.Plugin.Pragmas as Pragmas
Expand Down Expand Up @@ -102,15 +103,14 @@ idePlugins includeExamples = pluginDescToIdePlugins allPlugins
GhcIde.descriptor "ghcide"
, Pragmas.descriptor "pragmas"
, Floskell.descriptor "floskell"
, Fourmolu.descriptor "fourmolu"
-- , genericDescriptor "generic"
-- , ghcmodDescriptor "ghcmod"
, Ormolu.descriptor "ormolu"
, StylishHaskell.descriptor "stylish-haskell"
, Retrie.descriptor "retrie"
#if AGPL
#if !MIN_VERSION_ghc(8,10,1)
, Brittany.descriptor "brittany"
#endif
#endif
, Eval.descriptor "eval"
]
Expand Down
15 changes: 8 additions & 7 deletions haskell-language-server.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ library
Ide.Plugin.Eval
Ide.Plugin.Example
Ide.Plugin.Example2
Ide.Plugin.Fourmolu
Ide.Plugin.GhcIde
Ide.Plugin.Ormolu
Ide.Plugin.Pragmas
Expand Down Expand Up @@ -72,6 +73,7 @@ library
, extra
, filepath
, floskell == 0.10.*
, fourmolu ^>= 0.1
, ghc
, ghc-boot-th
, ghcide >= 0.1
Expand Down Expand Up @@ -101,11 +103,10 @@ library
else
build-depends: unix
if flag(agpl)
if impl(ghc < 8.10)
build-depends:
brittany
exposed-modules:
Ide.Plugin.Brittany
build-depends:
brittany
exposed-modules:
Ide.Plugin.Brittany

ghc-options:
-Wall
Expand Down Expand Up @@ -223,7 +224,7 @@ common hls-test-utils
, hslogger
, hspec
, hspec-core
, lsp-test >= 0.11.0.3
, lsp-test >= 0.11.0.4
, stm
, tasty-hunit
, temporary
Expand Down Expand Up @@ -252,7 +253,7 @@ test-suite func-test
, haskell-lsp
, haskell-lsp-types
, lens
, lsp-test >= 0.11.0.3
, lsp-test >= 0.11.0.4
, tasty
, tasty-ant-xml >= 1.1.6
, tasty-expected-failure
Expand Down
1 change: 1 addition & 0 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ let defaultCompiler = "ghc" + lib.replaceStrings ["."] [""] haskellPackages.ghc.
data-default-instances-old-locale
extra
floskell
fourmolu
fuzzy
generic-deriving
ghc-check
Expand Down
95 changes: 95 additions & 0 deletions src/Ide/Plugin/Fourmolu.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PackageImports #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

module Ide.Plugin.Fourmolu
(
descriptor
, provider
)
where

import Control.Exception
import qualified Data.Text as T
import Development.IDE.Core.Rules
import Development.IDE.Core.RuleTypes (GhcSession (GhcSession))
import Development.IDE.Core.Shake (use)
import Development.IDE.GHC.Util (hscEnv)
import Development.IDE.Types.Diagnostics as D
import Development.IDE.Types.Location
import qualified DynFlags as D
import qualified EnumSet as S
import GHC
import GHC.LanguageExtensions.Type
import GhcPlugins (HscEnv (hsc_dflags))
import Ide.Plugin.Formatter
import Ide.PluginUtils
import Ide.Types
import Language.Haskell.LSP.Core (LspFuncs (withIndefiniteProgress),
ProgressCancellable (Cancellable))
import Language.Haskell.LSP.Types
import "fourmolu" Ormolu
import System.FilePath (takeFileName)
import Text.Regex.TDFA.Text ()

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

descriptor :: PluginId -> PluginDescriptor
descriptor plId = (defaultPluginDescriptor plId)
{ pluginFormattingProvider = Just provider
}

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

provider :: FormattingProvider IO
provider lf ideState typ contents fp _ = withIndefiniteProgress lf title Cancellable $ do
let
fromDyn :: DynFlags -> IO [DynOption]
fromDyn df =
let
pp =
let p = D.sPgm_F $ D.settings df
in if null p then [] else ["-pgmF=" <> p]
pm = map (("-fplugin=" <>) . moduleNameString) $ D.pluginModNames df
ex = map showExtension $ S.toList $ D.extensionFlags df
in
return $ map DynOption $ pp <> pm <> ex

ghc <- runAction "Fourmolu" ideState $ use GhcSession fp
let df = hsc_dflags . hscEnv <$> ghc
fileOpts <- case df of
Nothing -> return []
Just df -> fromDyn df

let
fullRegion = RegionIndices Nothing Nothing
rangeRegion s e = RegionIndices (Just $ s + 1) (Just $ e + 1)
mkConf o region = do
printerOpts <- loadConfigFile True (Just fp') defaultPrinterOpts
return $ defaultConfig
{ cfgDynOptions = o
, cfgRegion = region
, cfgDebug = True
, cfgPrinterOpts = printerOpts
}
fmt :: T.Text -> Config RegionIndices -> IO (Either OrmoluException T.Text)
fmt cont conf =
try @OrmoluException (ormolu conf fp' $ T.unpack cont)
fp' = fromNormalizedFilePath fp

case typ of
FormatText -> ret <$> (fmt contents =<< mkConf fileOpts fullRegion)
FormatRange (Range (Position sl _) (Position el _)) ->
ret <$> (fmt contents =<< mkConf fileOpts (rangeRegion sl el))
where
title = T.pack $ "Formatting " <> takeFileName (fromNormalizedFilePath fp)
ret :: Either OrmoluException T.Text -> Either ResponseError (List TextEdit)
ret (Left err) = Left
(responseError (T.pack $ "fourmoluCmd: " ++ show err) )
ret (Right new) = Right (makeDiffTextEdit contents new)

showExtension :: Extension -> String
showExtension Cpp = "-XCPP"
showExtension other = "-X" ++ show other
3 changes: 2 additions & 1 deletion src/Ide/Plugin/Ormolu.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PackageImports #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
Expand Down Expand Up @@ -29,7 +30,7 @@ import Ide.Types
import Language.Haskell.LSP.Core (LspFuncs (withIndefiniteProgress),
ProgressCancellable (Cancellable))
import Language.Haskell.LSP.Types
import Ormolu
import "ormolu" Ormolu
import System.FilePath (takeFileName)
import Text.Regex.TDFA.Text ()

Expand Down
19 changes: 11 additions & 8 deletions stack-8.10.1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,37 @@ ghc-options:
"$everything": -haddock

extra-deps:
- aeson-1.5.2.0
- github: bubba/brittany
commit: c59655f10d5ad295c2481537fc8abf0a297d9d1c
- Cabal-3.0.2.0
- hie-bios-0.6.1
- cabal-plan-0.7.0.0
- clock-0.7.2
- floskell-0.10.3
- data-tree-print-0.1.0.2
- floskell-0.10.4
- fourmolu-0.1.0.0@rev:1
- ghc-exactprint-0.6.3
- HsYAML-aeson-0.2.0.0@rev:2
- lens-4.19.1
- lsp-test-0.11.0.3
- lsp-test-0.11.0.4
- monad-dijkstra-0.1.1.2
- optics-core-0.3
- ormolu-0.1.2.0
- retrie-0.1.1.1
- stylish-haskell-0.11.0.0
- stylish-haskell-0.11.0.3
- semigroups-0.18.5
- temporary-1.2.1.1
- these-1.1

flags:
haskell-language-server:
pedantic: true
# We want to let agpl be the default value in .cabal (True)
# but brittany is not usable with ghc-8.10.1
# see https://github.com/lspitzner/brittany/issues/269
agpl: false
retrie:
BuildExecutable: false

# allow-newer: true
# for data-tree-print's bounds on base (>=4.8 && <4.14); using base-4.14.0.0.
allow-newer: true

nix:
packages: [ icu libcxx zlib ]
Expand Down
17 changes: 11 additions & 6 deletions stack-8.6.4.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ ghc-options:
"$everything": -haddock

extra-deps:
- aeson-1.5.2.0
- ansi-terminal-0.10.3
- base-compat-0.11.0
- brittany-0.12.1.1@rev:2
- base-compat-0.10.5
- github: bubba/brittany
commit: c59655f10d5ad295c2481537fc8abf0a297d9d1c
- butcher-1.3.3.1
- Cabal-3.0.2.0
- cabal-plan-0.6.2.0
- clock-0.7.2
- extra-1.7.3
- floskell-0.10.3
- floskell-0.10.4
- fourmolu-0.1.0.0@rev:1
- fuzzy-0.1.0.0
# - ghcide-0.1.0
- ghc-check-0.5.0.1
Expand All @@ -30,26 +33,28 @@ extra-deps:
- haskell-lsp-types-0.22.0.0
- hie-bios-0.6.1
- HsYAML-0.2.1.0@rev:1
- HsYAML-aeson-0.2.0.0@rev:1
- HsYAML-aeson-0.2.0.0@rev:2
- indexed-profunctors-0.1
- lens-4.18
- lsp-test-0.11.0.3
- lsp-test-0.11.0.4
- monad-dijkstra-0.1.1.2
- opentelemetry-0.4.2
- optics-core-0.2
- optparse-applicative-0.15.1.0
- ormolu-0.1.2.0
- parser-combinators-1.2.1
- primitive-0.7.1.0
- regex-base-0.94.0.0
- regex-pcre-builtin-0.95.1.1.8.43
- regex-tdfa-1.3.1.0
- retrie-0.1.1.1
- semialign-1.1
# - github: wz1000/shake
# commit: fb3859dca2e54d1bbb2c873e68ed225fa179fbef
- stylish-haskell-0.11.0.0
- stylish-haskell-0.11.0.3
- tasty-rerun-1.1.17
- temporary-1.2.1.1
- these-1.1.1.1
- type-equality-1
- topograph-1

Expand Down
17 changes: 11 additions & 6 deletions stack-8.6.5.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ ghc-options:
"$everything": -haddock

extra-deps:
- aeson-1.5.2.0
- ansi-terminal-0.10.3
- base-compat-0.11.0
- brittany-0.12.1.1@rev:2
- base-compat-0.10.5
- github: bubba/brittany
commit: c59655f10d5ad295c2481537fc8abf0a297d9d1c
- butcher-1.3.3.1
- Cabal-3.0.2.0
- cabal-plan-0.6.2.0
- clock-0.7.2
- extra-1.7.3
- floskell-0.10.3
- floskell-0.10.4
- fourmolu-0.1.0.0@rev:1
- fuzzy-0.1.0.0
# - ghcide-0.1.0
- ghc-check-0.5.0.1
Expand All @@ -29,26 +32,28 @@ extra-deps:
- haskell-lsp-types-0.22.0.0
- hie-bios-0.6.1
- HsYAML-0.2.1.0@rev:1
- HsYAML-aeson-0.2.0.0@rev:1
- HsYAML-aeson-0.2.0.0@rev:2
- indexed-profunctors-0.1
- lens-4.18
- lsp-test-0.11.0.3
- lsp-test-0.11.0.4
- monad-dijkstra-0.1.1.2
- opentelemetry-0.4.2
- optics-core-0.2
- optparse-applicative-0.15.1.0
- ormolu-0.1.2.0
- parser-combinators-1.2.1
- primitive-0.7.1.0
- regex-base-0.94.0.0
- regex-pcre-builtin-0.95.1.1.8.43
- regex-tdfa-1.3.1.0
- retrie-0.1.1.1
- semialign-1.1
# - github: wz1000/shake
# commit: fb3859dca2e54d1bbb2c873e68ed225fa179fbef
- stylish-haskell-0.11.0.0
- stylish-haskell-0.11.0.3
- tasty-rerun-1.1.17
- temporary-1.2.1.1
- these-1.1.1.1
- type-equality-1
- topograph-1

Expand Down
Loading

0 comments on commit d74d111

Please sign in to comment.