-
-
Notifications
You must be signed in to change notification settings - Fork 396
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
Draft
jian-lin
wants to merge
1
commit into
haskell:master
Choose a base branch
from
linj-fork:pr/signature-help
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Implement signature help #4626
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
plugins/hls-signature-help-plugin/src/Ide/Plugin/SignatureHelp.hs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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!