Skip to content

Commit 8cebdd5

Browse files
committed
added option to always insert pragmas at top of file
1 parent 0241c47 commit 8cebdd5

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

haskell-language-server.cabal

+3-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,9 @@ common moduleName
229229
common pragmas
230230
if flag(pragmas) || flag(all-plugins)
231231
hs-source-dirs: plugins/default/src
232-
build-depends: fuzzy
232+
build-depends:
233+
, fuzzy
234+
, data-default
233235
other-modules: Ide.Plugin.Pragmas
234236
cpp-options: -Dpragmas
235237

hls-plugin-api/src/Ide/Plugin/Config.hs

+4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ data Config =
5555
, diagnosticsDebounceDuration :: !Int
5656
, liquidOn :: !Bool
5757
, formatOnImportOn :: !Bool
58+
, pragmaInsertAfterComments :: !Bool
5859
, formattingProvider :: !T.Text
5960
, maxCompletions :: !Int
6061
, plugins :: !(Map.Map T.Text PluginConfig)
@@ -69,6 +70,7 @@ instance Default Config where
6970
, diagnosticsDebounceDuration = 350000
7071
, liquidOn = False
7172
, formatOnImportOn = True
73+
, pragmaInsertAfterComments = True
7274
-- , formattingProvider = "brittany"
7375
, formattingProvider = "ormolu"
7476
-- , formattingProvider = "floskell"
@@ -93,6 +95,7 @@ parseConfig defValue = A.withObject "Config" $ \v -> do
9395
<*> o .:? "diagnosticsDebounceDuration" .!= diagnosticsDebounceDuration defValue
9496
<*> o .:? "liquidOn" .!= liquidOn defValue
9597
<*> o .:? "formatOnImportOn" .!= formatOnImportOn defValue
98+
<*> o .:? "pragmaInsertAfterComments" .!= pragmaInsertAfterComments defValue
9699
<*> o .:? "formattingProvider" .!= formattingProvider defValue
97100
<*> o .:? "maxCompletions" .!= maxCompletions defValue
98101
<*> o .:? "plugin" .!= plugins defValue
@@ -108,6 +111,7 @@ instance A.ToJSON Config where
108111
, "diagnosticsDebounceDuration" .= diagnosticsDebounceDuration
109112
, "liquidOn" .= liquidOn
110113
, "formatOnImportOn" .= formatOnImportOn
114+
, "pragmaInsertAfterComments" .= pragmaInsertAfterComments
111115
, "formattingProvider" .= formattingProvider
112116
, "maxCompletions" .= maxCompletions
113117
, "plugin" .= plugins

plugins/default/src/Ide/Plugin/Pragmas.hs

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
{-# LANGUAGE DuplicateRecordFields #-}
23
{-# LANGUAGE NamedFieldPuns #-}
34
{-# LANGUAGE OverloadedStrings #-}
@@ -10,6 +11,8 @@ import Control.Applicative ((<|>))
1011
import Control.Lens hiding (List)
1112
import Control.Monad (join)
1213
import Control.Monad.IO.Class
14+
import Ide.Plugin.Config
15+
import Data.Default
1316
import qualified Data.HashMap.Strict as H
1417
import Data.List.Extra (nubOrdOn)
1518
import Data.Maybe (catMaybes, listToMaybe)
@@ -42,13 +45,17 @@ data Pragma = LangExt T.Text | OptGHC T.Text
4245

4346
codeActionProvider :: PluginMethodHandler IdeState TextDocumentCodeAction
4447
codeActionProvider state _plId (CodeActionParams _ _ docId _ (J.CodeActionContext (J.List diags) _monly)) = do
45-
let mFile = docId ^. J.uri & uriToFilePath <&> toNormalizedFilePath'
46-
uri = docId ^. J.uri
47-
pm <- liftIO $ fmap join $ runAction "Pragmas.GetParsedModule" state $ getParsedModule `traverse` mFile
48-
let dflags = ms_hspp_opts . pm_mod_summary <$> pm
49-
insertRange = maybe (Range (Position 0 0) (Position 0 0)) endOfModuleHeader pm
50-
pedits = nubOrdOn snd . concat $ suggest dflags <$> diags
51-
return $ Right $ List $ pragmaEditToAction uri insertRange <$> pedits
48+
let mFile = docId ^. J.uri & uriToFilePath <&> toNormalizedFilePath'
49+
uri = docId ^. J.uri
50+
pm <- liftIO $ fmap join $ runAction "Pragmas.GetParsedModule" state $ getParsedModule `traverse` mFile
51+
config <- liftIO $ runAction "getConfig" state (getClientConfigAction def)
52+
let dflags = ms_hspp_opts . pm_mod_summary <$> pm
53+
pedits = nubOrdOn snd . concat $ suggest dflags <$> diags
54+
insertAfterComments = pragmaInsertAfterComments config
55+
return $ Right $ List $ pragmaEditToAction uri (getInsertRange insertAfterComments pm) <$> pedits
56+
where
57+
getInsertRange True (Just pm) = endOfModuleHeader pm
58+
getInsertRange _ _ = Range (Position 0 0) (Position 0 0)
5259

5360
-- | Add a Pragma to the given URI at the top of the file.
5461
-- It is assumed that the pragma name is a valid pragma,

0 commit comments

Comments
 (0)