From 386bf8a83815a9d04d801da4ac988adf637341d1 Mon Sep 17 00:00:00 2001 From: Fendor Date: Thu, 9 Jun 2022 18:31:37 +0200 Subject: [PATCH] Add example plugin --- exe/Plugins.hs | 2 + .../default/src/Ide/Plugin/ExampleCabal.hs | 51 +++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/exe/Plugins.hs b/exe/Plugins.hs index 5e7bb29ca1a..49e602a3604 100644 --- a/exe/Plugins.hs +++ b/exe/Plugins.hs @@ -13,6 +13,7 @@ import Development.IDE (IdeState) import qualified Development.IDE.Plugin.HLS.GhcIde as GhcIde import qualified Ide.Plugin.Example as Example import qualified Ide.Plugin.Example2 as Example2 +import qualified Ide.Plugin.ExampleCabal as ExampleCabal -- haskell-language-server optional plugins #if qualifyImportedNames @@ -204,4 +205,5 @@ idePlugins recorder includeExamples = pluginDescToIdePlugins allPlugins examplePlugins = [Example.descriptor pluginRecorder "eg" ,Example2.descriptor pluginRecorder "eg2" + ,ExampleCabal.descriptor pluginRecorder "ec" ] diff --git a/plugins/default/src/Ide/Plugin/ExampleCabal.hs b/plugins/default/src/Ide/Plugin/ExampleCabal.hs index d09d4e6547e..8a4c1e37cfa 100644 --- a/plugins/default/src/Ide/Plugin/ExampleCabal.hs +++ b/plugins/default/src/Ide/Plugin/ExampleCabal.hs @@ -1,2 +1,53 @@ +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE DeriveGeneric #-} +{-# LANGUAGE DeriveAnyClass #-} +{-# LANGUAGE OverloadedStrings #-} module Ide.Plugin.ExampleCabal where +import Data.Aeson +import qualified Data.HashMap.Strict as Map +import qualified Data.Text as T +import Development.IDE as D +import qualified Development.IDE.Core.Shake as Shake +import GHC.Generics +import Ide.Types +import Language.LSP.Server +import Language.LSP.Types +import Text.Regex.TDFA.Text () + + +newtype Log = LogShake Shake.Log deriving Show + +instance Pretty Log where + pretty = \case + LogShake log -> pretty log + +descriptor :: Recorder (WithPriority Log) -> PluginId -> PluginDescriptor IdeState +descriptor _recorder plId = (defaultCabalPluginDescriptor plId) + { pluginCommands = [PluginCommand "codelens.todo" "example adding" addTodoCmd] + } + +-- --------------------------------------------------------------------- +-- | Parameters for the addTodo PluginCommand. +data AddTodoParams = AddTodoParams + { file :: Uri -- ^ Uri of the file to add the pragma to + , todoText :: T.Text + } + deriving (Show, Eq, Generic, ToJSON, FromJSON) + +addTodoCmd :: CommandFunction IdeState AddTodoParams +addTodoCmd _ide (AddTodoParams uri todoText) = do + let + pos = Position 3 0 + textEdits = List + [TextEdit (Range pos pos) + ("-- TODO:" <> todoText <> "\n") + ] + res = WorkspaceEdit + (Just $ Map.singleton uri textEdits) + Nothing + Nothing + _ <- sendRequest SWorkspaceApplyEdit (ApplyWorkspaceEditParams Nothing res) (\_ -> pure ()) + return $ Right Null + +-- ---------------------------------------------------------------------