|
| 1 | +{-# LANGUAGE DataKinds #-} |
| 2 | +{-# LANGUAGE DuplicateRecordFields #-} |
| 3 | + |
| 4 | +module Ide.Plugin.SignatureHelp (descriptor) where |
| 5 | + |
| 6 | +import Control.Monad.Trans (lift) |
| 7 | +import qualified Data.List.NonEmpty as NL |
| 8 | +import qualified Data.Text as T |
| 9 | +import Development.IDE |
| 10 | +import Development.IDE.Core.PluginUtils (runIdeActionE, |
| 11 | + useWithStaleFastE) |
| 12 | +import Development.IDE.Spans.AtPoint (getNamesAtPoint) |
| 13 | +import Ide.Plugin.Error |
| 14 | +import Ide.Types |
| 15 | +import Language.LSP.Protocol.Message |
| 16 | +import Language.LSP.Protocol.Types |
| 17 | +import Text.Regex.TDFA ((=~)) |
| 18 | + |
| 19 | +data Log = LogDummy |
| 20 | + |
| 21 | +instance Pretty Log where |
| 22 | + pretty = \case |
| 23 | + LogDummy -> "TODO(@linj) remove this dummy log" |
| 24 | + |
| 25 | +descriptor :: Recorder (WithPriority Log) -> PluginId -> PluginDescriptor IdeState |
| 26 | +descriptor _recorder pluginId = |
| 27 | + (defaultPluginDescriptor pluginId "Provides signature help of something callable") |
| 28 | + { pluginHandlers = mkPluginHandler SMethod_TextDocumentSignatureHelp signatureHelpProvider |
| 29 | + } |
| 30 | + |
| 31 | +-- get src info |
| 32 | +-- function |
| 33 | +-- which arg is under the cursor |
| 34 | +-- get function type (and arg doc) |
| 35 | +-- assemble result |
| 36 | +-- TODO(@linj) |
| 37 | +signatureHelpProvider :: PluginMethodHandler IdeState Method_TextDocumentSignatureHelp |
| 38 | +signatureHelpProvider ideState _pluginId (SignatureHelpParams (TextDocumentIdentifier uri) position _mProgreeToken _mContext) = do |
| 39 | + nfp <- getNormalizedFilePathE uri |
| 40 | + names <- runIdeActionE "signatureHelp" (shakeExtras ideState) $ do |
| 41 | + (HAR {hieAst}, positionMapping) <- useWithStaleFastE GetHieAst nfp |
| 42 | + let ns = getNamesAtPoint hieAst position positionMapping |
| 43 | + pure ns |
| 44 | + mRangeAndDoc <- |
| 45 | + runIdeActionE |
| 46 | + "signatureHelp.getDoc" |
| 47 | + (shakeExtras ideState) |
| 48 | + (lift (getAtPoint nfp position)) |
| 49 | + let (_mRange, contents) = case mRangeAndDoc of |
| 50 | + Just (mRange, contents) -> (mRange, contents) |
| 51 | + Nothing -> (Nothing, []) |
| 52 | + |
| 53 | + pure $ |
| 54 | + InL $ |
| 55 | + SignatureHelp |
| 56 | + ( case mkSignatureHelpLabel names contents of |
| 57 | + Just label -> |
| 58 | + [ SignatureInformation |
| 59 | + label |
| 60 | + Nothing |
| 61 | + (Just [ParameterInformation (InR (5, 8)) Nothing]) |
| 62 | + Nothing |
| 63 | + ] |
| 64 | + Nothing -> [] |
| 65 | + ) |
| 66 | + (Just 0) |
| 67 | + (Just $ InL 0) |
| 68 | + where |
| 69 | + mkSignatureHelpLabel names types = |
| 70 | + case (chooseName $ printName <$> names, chooseType types >>= showType) of |
| 71 | + (Just name, Just typ) -> Just $ T.pack name <> " :: " <> typ |
| 72 | + _ -> Nothing |
| 73 | + chooseName names = case names of |
| 74 | + [] -> Nothing |
| 75 | + name : names' -> Just $ NL.last (name NL.:| names') |
| 76 | + chooseType types = case types of |
| 77 | + [] -> Nothing |
| 78 | + [t] -> Just t |
| 79 | + _ -> Just $ types !! (length types - 2) |
| 80 | + showType typ = getMatchedType $ typ =~ ("\n```haskell\n(.*) :: (.*)\n```\n" :: T.Text) |
| 81 | + getMatchedType :: (T.Text, T.Text, T.Text, [T.Text]) -> Maybe T.Text |
| 82 | + getMatchedType (_, _, _, [_, t]) = Just t |
| 83 | + getMatchedType _ = Nothing |
0 commit comments