From 4fcddfcc80e2c81988b8a1b00d627698cb58d607 Mon Sep 17 00:00:00 2001 From: Zubin Duggal Date: Sat, 13 Aug 2022 13:33:20 +0530 Subject: [PATCH] Allow plugins to use GetAnnotatedParsedSource by linking it in when depended on --- exe/Plugins.hs | 2 +- .../src/Ide/Plugin/CodeRange.hs | 6 +++- .../src/Ide/Plugin/HaddockComments.hs | 6 ++-- .../hls-haddock-comments-plugin/test/Main.hs | 2 +- .../src/Development/IDE/Plugin/CodeAction.hs | 30 ++++++++++--------- 5 files changed, 27 insertions(+), 19 deletions(-) diff --git a/exe/Plugins.hs b/exe/Plugins.hs index 6ffd212807b..9046866cc54 100644 --- a/exe/Plugins.hs +++ b/exe/Plugins.hs @@ -177,7 +177,7 @@ idePlugins recorder includeExamples = pluginDescToIdePlugins allPlugins Class.descriptor pluginRecorder "class" : #endif #if hls_haddockComments - HaddockComments.descriptor "haddockComments" : + HaddockComments.descriptor pluginRecorder "haddockComments" : #endif #if hls_eval Eval.descriptor pluginRecorder "eval" : diff --git a/plugins/hls-code-range-plugin/src/Ide/Plugin/CodeRange.hs b/plugins/hls-code-range-plugin/src/Ide/Plugin/CodeRange.hs index 0a48a3467b1..23a02cfb60a 100644 --- a/plugins/hls-code-range-plugin/src/Ide/Plugin/CodeRange.hs +++ b/plugins/hls-code-range-plugin/src/Ide/Plugin/CodeRange.hs @@ -31,6 +31,8 @@ import Development.IDE.Core.PositionMapping (PositionMapping, fromCurrentPosition, toCurrentRange) import Development.IDE.Types.Logger (Pretty (..)) +import qualified Development.IDE.GHC.ExactPrint as E +import Development.IDE.Plugin.CodeAction import Ide.Plugin.CodeRange.Rules (CodeRange (..), GetCodeRange (..), codeRangeRule) @@ -55,7 +57,7 @@ import Language.LSP.Types (List (List), import Prelude hiding (log, span) descriptor :: Recorder (WithPriority Log) -> PluginId -> PluginDescriptor IdeState -descriptor recorder plId = (defaultPluginDescriptor plId) +descriptor recorder plId = mkExactprintPluginDescriptor (cmapWithPrio LogExactPrint recorder) $ (defaultPluginDescriptor plId) { pluginHandlers = mkPluginHandler STextDocumentSelectionRange selectionRangeHandler -- TODO @sloorush add folding range -- <> mkPluginHandler STextDocumentFoldingRange foldingRangeHandler @@ -63,10 +65,12 @@ descriptor recorder plId = (defaultPluginDescriptor plId) } data Log = LogRules Rules.Log + | LogExactPrint E.Log instance Pretty Log where pretty log = case log of LogRules codeRangeLog -> pretty codeRangeLog + LogExactPrint exactPrintLog -> pretty exactPrintLog selectionRangeHandler :: IdeState -> PluginId -> SelectionRangeParams -> LspM c (Either ResponseError (List SelectionRange)) selectionRangeHandler ide _ SelectionRangeParams{..} = do diff --git a/plugins/hls-haddock-comments-plugin/src/Ide/Plugin/HaddockComments.hs b/plugins/hls-haddock-comments-plugin/src/Ide/Plugin/HaddockComments.hs index cdc2813403f..29932198937 100644 --- a/plugins/hls-haddock-comments-plugin/src/Ide/Plugin/HaddockComments.hs +++ b/plugins/hls-haddock-comments-plugin/src/Ide/Plugin/HaddockComments.hs @@ -15,8 +15,10 @@ import qualified Data.Map as Map import qualified Data.Text as T import Development.IDE hiding (pluginHandlers) import Development.IDE.GHC.Compat +import Development.IDE.Plugin.CodeAction import Development.IDE.GHC.Compat.ExactPrint import Development.IDE.GHC.ExactPrint (GetAnnotatedParsedSource (..)) +import qualified Development.IDE.GHC.ExactPrint as E import Ide.Types import Language.Haskell.GHC.ExactPrint import Language.Haskell.GHC.ExactPrint.Types hiding (GhcPs) @@ -24,8 +26,8 @@ import Language.Haskell.GHC.ExactPrint.Utils import Language.LSP.Types ----------------------------------------------------------------------------- -descriptor :: PluginId -> PluginDescriptor IdeState -descriptor plId = +descriptor :: Recorder (WithPriority E.Log) -> PluginId -> PluginDescriptor IdeState +descriptor recorder plId = mkExactprintPluginDescriptor recorder $ (defaultPluginDescriptor plId) { pluginHandlers = mkPluginHandler STextDocumentCodeAction codeActionProvider } diff --git a/plugins/hls-haddock-comments-plugin/test/Main.hs b/plugins/hls-haddock-comments-plugin/test/Main.hs index 3eadb934162..22189c25904 100644 --- a/plugins/hls-haddock-comments-plugin/test/Main.hs +++ b/plugins/hls-haddock-comments-plugin/test/Main.hs @@ -19,7 +19,7 @@ main :: IO () main = defaultTestRunner tests haddockCommentsPlugin :: PluginDescriptor IdeState -haddockCommentsPlugin = HaddockComments.descriptor "haddockComments" +haddockCommentsPlugin = HaddockComments.descriptor mempty "haddockComments" tests :: TestTree tests = diff --git a/plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs b/plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs index 1438db7703f..2be6f9a5f74 100644 --- a/plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs +++ b/plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs @@ -21,6 +21,7 @@ module Development.IDE.Plugin.CodeAction ( + mkExactprintPluginDescriptor, iePluginDescriptor, typeSigsPluginDescriptor, bindingsPluginDescriptor, @@ -158,39 +159,40 @@ iePluginDescriptor recorder plId = , wrap suggestExportUnusedTopBinding ] plId - in old {pluginHandlers = pluginHandlers old <> mkPluginHandler STextDocumentCodeAction codeAction - , pluginRules = getAnnotatedParsedSourceRule recorder - } + in mkExactprintPluginDescriptor recorder $ old {pluginHandlers = pluginHandlers old <> mkPluginHandler STextDocumentCodeAction codeAction } typeSigsPluginDescriptor :: Recorder (WithPriority E.Log) -> PluginId -> PluginDescriptor IdeState -typeSigsPluginDescriptor recorder plId = - (mkGhcideCAsPlugin [ +typeSigsPluginDescriptor recorder plId = mkExactprintPluginDescriptor recorder $ + mkGhcideCAsPlugin [ wrap $ suggestSignature True , wrap suggestFillTypeWildcard , wrap removeRedundantConstraints , wrap suggestAddTypeAnnotationToSatisfyContraints , wrap suggestConstraint ] - plId) { pluginRules = getAnnotatedParsedSourceRule recorder } + plId bindingsPluginDescriptor :: Recorder (WithPriority E.Log) -> PluginId -> PluginDescriptor IdeState -bindingsPluginDescriptor recorder plId = - (mkGhcideCAsPlugin [ +bindingsPluginDescriptor recorder plId = mkExactprintPluginDescriptor recorder $ + mkGhcideCAsPlugin [ wrap suggestReplaceIdentifier , wrap suggestImplicitParameter , wrap suggestNewDefinition , wrap suggestDeleteUnusedBinding ] - plId) { pluginRules = getAnnotatedParsedSourceRule recorder } + plId fillHolePluginDescriptor :: Recorder (WithPriority E.Log) -> PluginId -> PluginDescriptor IdeState -fillHolePluginDescriptor recorder plId = (mkGhcideCAPlugin (wrap suggestFillHole) plId) { pluginRules = getAnnotatedParsedSourceRule recorder } +fillHolePluginDescriptor recorder plId = mkExactprintPluginDescriptor recorder (mkGhcideCAPlugin (wrap suggestFillHole) plId) extendImportPluginDescriptor :: Recorder (WithPriority E.Log) -> PluginId -> PluginDescriptor IdeState -extendImportPluginDescriptor recorder plId = (defaultPluginDescriptor plId) - { pluginCommands = [extendImportCommand] - , pluginRules = getAnnotatedParsedSourceRule recorder - } +extendImportPluginDescriptor recorder plId = mkExactprintPluginDescriptor recorder $ (defaultPluginDescriptor plId) + { pluginCommands = [extendImportCommand] } + + +-- | Add the ability for a plugin to call GetAnnotatedParsedSource +mkExactprintPluginDescriptor :: Recorder (WithPriority E.Log) -> PluginDescriptor a -> PluginDescriptor a +mkExactprintPluginDescriptor recorder desc = desc { pluginRules = pluginRules desc >> getAnnotatedParsedSourceRule recorder } -------------------------------------------------------------------------------------------------