-
-
Notifications
You must be signed in to change notification settings - Fork 401
Implement signature help #4626
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Implement signature help #4626
Changes from 1 commit
7a54a1d
9168b74
4d7fa57
622c8ec
62fbccf
a6635ca
bf0b4d5
c95d6e4
3dc1ec8
dca1311
471958f
d603ec4
faa4e48
9bea0e3
db5e59e
fdd5acd
c6ece42
fe2d618
c3224d7
081ea8f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a bit offtopic: Is 2-space indentation instead of 4 allowed for new code? The .editorconfig says 4. However, some newly added plugins use 2: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Follow the |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE DuplicateRecordFields #-} | ||
|
||
module Ide.Plugin.SignatureHelp (descriptor) where | ||
|
||
import Control.Monad.Trans (lift) | ||
import qualified Data.List.NonEmpty as NL | ||
import qualified Data.Text as T | ||
import Development.IDE | ||
import Development.IDE.Core.PluginUtils (runIdeActionE, | ||
useWithStaleFastE) | ||
import Development.IDE.Spans.AtPoint (getNamesAtPoint) | ||
import Ide.Plugin.Error | ||
import Ide.Types | ||
import Language.LSP.Protocol.Message | ||
import Language.LSP.Protocol.Types | ||
import Text.Regex.TDFA ((=~)) | ||
|
||
data Log = LogDummy | ||
|
||
instance Pretty Log where | ||
pretty = \case | ||
LogDummy -> "TODO(@linj) remove this dummy log" | ||
|
||
descriptor :: Recorder (WithPriority Log) -> PluginId -> PluginDescriptor IdeState | ||
descriptor _recorder pluginId = | ||
(defaultPluginDescriptor pluginId "Provides signature help of something callable") | ||
{ pluginHandlers = mkPluginHandler SMethod_TextDocumentSignatureHelp signatureHelpProvider | ||
} | ||
|
||
-- get src info | ||
-- function | ||
-- which arg is under the cursor | ||
-- get function type (and arg doc) | ||
-- assemble result | ||
-- TODO(@linj) | ||
signatureHelpProvider :: PluginMethodHandler IdeState Method_TextDocumentSignatureHelp | ||
signatureHelpProvider ideState _pluginId (SignatureHelpParams (TextDocumentIdentifier uri) position _mProgreeToken _mContext) = do | ||
nfp <- getNormalizedFilePathE uri | ||
names <- runIdeActionE "signatureHelp" (shakeExtras ideState) $ do | ||
(HAR {hieAst}, positionMapping) <- useWithStaleFastE GetHieAst nfp | ||
let ns = getNamesAtPoint hieAst position positionMapping | ||
pure ns | ||
mRangeAndDoc <- | ||
runIdeActionE | ||
"signatureHelp.getDoc" | ||
(shakeExtras ideState) | ||
(lift (getAtPoint nfp position)) | ||
let (_mRange, contents) = case mRangeAndDoc of | ||
Just (mRange, contents) -> (mRange, contents) | ||
Nothing -> (Nothing, []) | ||
|
||
pure $ | ||
InL $ | ||
SignatureHelp | ||
( case mkSignatureHelpLabel names contents of | ||
Just label -> | ||
[ SignatureInformation | ||
label | ||
Nothing | ||
(Just [ParameterInformation (InR (5, 8)) Nothing]) | ||
Nothing | ||
] | ||
Nothing -> [] | ||
) | ||
(Just 0) | ||
(Just $ InL 0) | ||
where | ||
mkSignatureHelpLabel names types = | ||
case (chooseName $ printName <$> names, chooseType types >>= showType) of | ||
(Just name, Just typ) -> Just $ T.pack name <> " :: " <> typ | ||
_ -> Nothing | ||
chooseName names = case names of | ||
[] -> Nothing | ||
name : names' -> Just $ NL.last (name NL.:| names') | ||
chooseType types = case types of | ||
[] -> Nothing | ||
[t] -> Just t | ||
_ -> Just $ types !! (length types - 2) | ||
Check failure on line 79 in plugins/hls-signature-help-plugin/src/Ide/Plugin/SignatureHelp.hs
|
||
showType typ = getMatchedType $ typ =~ ("\n```haskell\n(.*) :: (.*)\n```\n" :: T.Text) | ||
getMatchedType :: (T.Text, T.Text, T.Text, [T.Text]) -> Maybe T.Text | ||
getMatchedType (_, _, _, [_, t]) = Just t | ||
getMatchedType _ = Nothing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is reasonable. Really
combineResponses
should have a way to return an error. There are a bunch of methods where it really only makes sense if we have one handler.We could try to combine responses: we would combine the signatures, and so long as only one of them indicated an active signature it would be okay. But that's a bit sketchy and I doubt we'll have several anyway!