|
| 1 | +{-# LANGUAGE DataKinds #-} |
| 2 | +{-# LANGUAGE DeriveAnyClass #-} |
| 3 | +{-# LANGUAGE DeriveGeneric #-} |
| 4 | +{-# LANGUAGE DuplicateRecordFields #-} |
| 5 | +{-# LANGUAGE FlexibleContexts #-} |
| 6 | +{-# LANGUAGE FlexibleInstances #-} |
| 7 | +{-# LANGUAGE LambdaCase #-} |
| 8 | +{-# LANGUAGE OverloadedStrings #-} |
| 9 | +{-# LANGUAGE RecordWildCards #-} |
| 10 | +{-# LANGUAGE TupleSections #-} |
| 11 | +{-# LANGUAGE TypeFamilies #-} |
| 12 | +{-# LANGUAGE ViewPatterns #-} |
| 13 | + |
| 14 | +module Ide.Plugin.Cabal where |
| 15 | + |
| 16 | +import Control.Monad.IO.Class |
| 17 | +import Data.Aeson |
| 18 | +import qualified Data.Text as T |
| 19 | +import Development.IDE as D |
| 20 | +import GHC.Generics |
| 21 | +import Ide.PluginUtils |
| 22 | +import Ide.Types |
| 23 | +import Language.LSP.Types |
| 24 | + |
| 25 | + |
| 26 | +newtype Log = LogText T.Text deriving Show |
| 27 | + |
| 28 | +instance Pretty Log where |
| 29 | + pretty = \case |
| 30 | + LogText log -> pretty log |
| 31 | + |
| 32 | +descriptor :: Recorder (WithPriority Log) -> PluginId -> PluginDescriptor IdeState |
| 33 | +descriptor recorder plId = (defaultCabalPluginDescriptor plId) |
| 34 | + { pluginHandlers = mkPluginHandler STextDocumentCodeLens (codeLens recorder) |
| 35 | + } |
| 36 | + |
| 37 | +-- --------------------------------------------------------------------- |
| 38 | + |
| 39 | +codeLens :: Recorder (WithPriority Log) -> PluginMethodHandler IdeState TextDocumentCodeLens |
| 40 | +codeLens recorder _ideState plId CodeLensParams{_textDocument=TextDocumentIdentifier uri} = liftIO $ do |
| 41 | + log Debug $ LogText "ExampleCabal.codeLens entered (ideLogger)" |
| 42 | + case uriToFilePath' uri of |
| 43 | + Just (toNormalizedFilePath -> _filePath) -> do |
| 44 | + let |
| 45 | + title = "Add TODO Item via Code Lens" |
| 46 | + range = Range (Position 3 0) (Position 4 0) |
| 47 | + let cmdParams = AddTodoParams uri "do abc" |
| 48 | + cmd = mkLspCommand plId "codelens.todo" title (Just [toJSON cmdParams]) |
| 49 | + pure $ Right $ List [ CodeLens range (Just cmd) Nothing ] |
| 50 | + Nothing -> pure $ Right $ List [] |
| 51 | + where |
| 52 | + log = logWith recorder |
| 53 | +-- --------------------------------------------------------------------- |
| 54 | + |
| 55 | +data AddTodoParams = AddTodoParams |
| 56 | + { file :: Uri -- ^ Uri of the file to add the pragma to |
| 57 | + , todoText :: T.Text |
| 58 | + } |
| 59 | + deriving (Show, Eq, Generic, ToJSON, FromJSON) |
0 commit comments