|
| 1 | +{-# LANGUAGE LambdaCase #-} |
| 2 | +{-# LANGUAGE DeriveGeneric #-} |
| 3 | +{-# LANGUAGE DeriveAnyClass #-} |
| 4 | +{-# LANGUAGE OverloadedStrings #-} |
1 | 5 | module Ide.Plugin.ExampleCabal where
|
2 | 6 |
|
| 7 | +import Data.Aeson |
| 8 | +import qualified Data.HashMap.Strict as Map |
| 9 | +import qualified Data.Text as T |
| 10 | +import Development.IDE as D |
| 11 | +import qualified Development.IDE.Core.Shake as Shake |
| 12 | +import GHC.Generics |
| 13 | +import Ide.Types |
| 14 | +import Language.LSP.Server |
| 15 | +import Language.LSP.Types |
| 16 | +import Text.Regex.TDFA.Text () |
| 17 | + |
| 18 | + |
| 19 | +newtype Log = LogShake Shake.Log deriving Show |
| 20 | + |
| 21 | +instance Pretty Log where |
| 22 | + pretty = \case |
| 23 | + LogShake log -> pretty log |
| 24 | + |
| 25 | +descriptor :: Recorder (WithPriority Log) -> PluginId -> PluginDescriptor IdeState |
| 26 | +descriptor _recorder plId = (defaultCabalPluginDescriptor plId) |
| 27 | + { pluginCommands = [PluginCommand "codelens.todo" "example adding" addTodoCmd] |
| 28 | + } |
| 29 | + |
| 30 | +-- --------------------------------------------------------------------- |
| 31 | +-- | Parameters for the addTodo PluginCommand. |
| 32 | +data AddTodoParams = AddTodoParams |
| 33 | + { file :: Uri -- ^ Uri of the file to add the pragma to |
| 34 | + , todoText :: T.Text |
| 35 | + } |
| 36 | + deriving (Show, Eq, Generic, ToJSON, FromJSON) |
| 37 | + |
| 38 | +addTodoCmd :: CommandFunction IdeState AddTodoParams |
| 39 | +addTodoCmd _ide (AddTodoParams uri todoText) = do |
| 40 | + let |
| 41 | + pos = Position 3 0 |
| 42 | + textEdits = List |
| 43 | + [TextEdit (Range pos pos) |
| 44 | + ("-- TODO:" <> todoText <> "\n") |
| 45 | + ] |
| 46 | + res = WorkspaceEdit |
| 47 | + (Just $ Map.singleton uri textEdits) |
| 48 | + Nothing |
| 49 | + Nothing |
| 50 | + _ <- sendRequest SWorkspaceApplyEdit (ApplyWorkspaceEditParams Nothing res) (\_ -> pure ()) |
| 51 | + return $ Right Null |
| 52 | + |
| 53 | +-- --------------------------------------------------------------------- |
0 commit comments