-
-
Notifications
You must be signed in to change notification settings - Fork 402
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
37
commits 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 2 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
7a54a1d
WIP: add basic boilerplate for signature help plugin
jian-lin 9168b74
WIP: finish signature help plugin MVP
jian-lin 4d7fa57
WIP: remove unused dependencies
jian-lin 622c8ec
WIP: fix func-tests
jian-lin 62fbccf
WIP: add basic tests
jian-lin a6635ca
Change expected test results considering the cursor shape
jian-lin bf0b4d5
Replace maybe with case for better readability
jian-lin c95d6e4
Call extractInfoFromSmallestContainingFunctionApplicationAst once
jian-lin 3dc1ec8
Add missing ParameterInformation for multi-line tests
jian-lin dca1311
Add a signature help test for type constraint with kind signatures
jian-lin 471958f
Add a signature help test for higher-order function
jian-lin d603ec4
Show more types: each type as one signature help
jian-lin faa4e48
Adjust import for the removal of hie-compat (#4613)
jian-lin 9bea0e3
Use structured type and type string to generate signature help
jian-lin db5e59e
Add signature help test: 2 type constraints
jian-lin fdd5acd
Add signature help test: middle =>
jian-lin c6ece42
Add signature help test: => in argument
jian-lin fe2d618
Add 2 signature help tests: RankNTypes(forall in middle)
jian-lin c3224d7
Add signature help test: LinearTypes
jian-lin 081ea8f
Add another signature help tests: RankNTypes(forall in middle)
jian-lin a522e88
Show function documentation in signature help
jian-lin d826d06
Show function argument documentation in signature help
jian-lin 62f2e41
Rename a local variable
jian-lin 35399e7
Do not error if doc is not available
jian-lin c0f7844
Add a helper function to create markdown doc
jian-lin eeeb283
Make signature helps reproducible before comparing them
jian-lin f1d19ba
Do not show uris in the argument documentation
jian-lin 433a8ad
Add a signature help test: imported function with argument doc
jian-lin d3d7d12
Add a signature help test: imported function with no doc
jian-lin e0a7e8b
Remove an outdated TODO item
jian-lin e6702b7
Remember previous active signature
jian-lin 3d7a67b
Update a TODO item
jian-lin 800f908
Show signature help even when there are type applications
jian-lin cb84480
Fix some tests for ghc > 9.8 due to changed doc
jian-lin c7931a2
Fix some tests for ghc > 9.8 due to changed type: Any -> ZonkAny 0
jian-lin b6d4617
Add jian-lin as codeowner of hls-signature-help-plugin
jian-lin 9a1de4e
Add hls-signature-help-plugin to some documentation files
jian-lin 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
208 changes: 208 additions & 0 deletions
208
plugins/hls-signature-help-plugin/src/Ide/Plugin/SignatureHelp.hs
jian-lin marked this conversation as resolved.
Show resolved
Hide resolved
|
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,208 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE GADTs #-} | ||
|
||
module Ide.Plugin.SignatureHelp (descriptor) where | ||
|
||
import Control.Arrow ((>>>)) | ||
import Data.Bifunctor (bimap) | ||
import qualified Data.Map.Strict as M | ||
import Data.Maybe (mapMaybe) | ||
import qualified Data.Set as S | ||
import Data.Text (Text) | ||
import qualified Data.Text as T | ||
import Development.IDE (GetHieAst (GetHieAst), | ||
HieAstResult (HAR, hieAst, hieKind), | ||
HieKind (..), | ||
IdeState (shakeExtras), | ||
Pretty (pretty), | ||
Recorder, WithPriority, | ||
printOutputable) | ||
import Development.IDE.Core.PluginUtils (runIdeActionE, | ||
useWithStaleFastE) | ||
import Development.IDE.Core.PositionMapping (fromCurrentPosition) | ||
import Development.IDE.GHC.Compat (ContextInfo (Use), | ||
FastStringCompat, HieAST, | ||
HieASTs, | ||
IdentifierDetails, Name, | ||
RealSrcSpan, SDoc, | ||
getAsts, | ||
getSourceNodeIds, | ||
hieTypeToIface, | ||
hie_types, identInfo, | ||
identType, | ||
isAnnotationInNodeInfo, | ||
mkRealSrcLoc, | ||
mkRealSrcSpan, | ||
nodeChildren, nodeSpan, | ||
ppr, recoverFullType, | ||
smallestContainingSatisfying, | ||
sourceNodeInfo) | ||
import Development.IDE.GHC.Compat.Util (LexicalFastString (LexicalFastString)) | ||
import GHC.Data.Maybe (rightToMaybe) | ||
import GHC.Types.SrcLoc (isRealSubspanOf) | ||
import Ide.Plugin.Error (getNormalizedFilePathE) | ||
import Ide.Types (PluginDescriptor (pluginHandlers), | ||
PluginId, | ||
PluginMethodHandler, | ||
defaultPluginDescriptor, | ||
mkPluginHandler) | ||
import Language.LSP.Protocol.Message (Method (Method_TextDocumentSignatureHelp), | ||
SMethod (SMethod_TextDocumentSignatureHelp)) | ||
import Language.LSP.Protocol.Types (Null (Null), | ||
ParameterInformation (ParameterInformation), | ||
Position (Position), | ||
SignatureHelp (SignatureHelp), | ||
SignatureHelpParams (SignatureHelpParams), | ||
SignatureInformation (SignatureInformation), | ||
TextDocumentIdentifier (TextDocumentIdentifier), | ||
UInt, | ||
type (|?) (InL, InR)) | ||
|
||
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") | ||
{ Ide.Types.pluginHandlers = mkPluginHandler SMethod_TextDocumentSignatureHelp signatureHelpProvider | ||
} | ||
|
||
-- TODO(@linj) get doc | ||
signatureHelpProvider :: PluginMethodHandler IdeState Method_TextDocumentSignatureHelp | ||
signatureHelpProvider ideState _pluginId (SignatureHelpParams (TextDocumentIdentifier uri) position _mProgreeToken _mContext) = do | ||
nfp <- getNormalizedFilePathE uri | ||
mResult <- runIdeActionE "signatureHelp" (shakeExtras ideState) $ do | ||
-- TODO(@linj) why HAR {hieAst} may have more than one AST? | ||
(HAR {hieAst, hieKind}, positionMapping) <- useWithStaleFastE GetHieAst nfp | ||
case fromCurrentPosition positionMapping position of | ||
Nothing -> pure Nothing | ||
Just oldPosition -> do | ||
let functionName = | ||
jian-lin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
extractInfoFromSmallestContainingFunctionApplicationAst | ||
oldPosition | ||
hieAst | ||
(\span -> getLeftMostNode >>> getNodeName span) | ||
functionType = | ||
extractInfoFromSmallestContainingFunctionApplicationAst | ||
oldPosition | ||
hieAst | ||
(\span -> getLeftMostNode >>> getNodeType hieKind span) | ||
argumentNumber = | ||
extractInfoFromSmallestContainingFunctionApplicationAst | ||
oldPosition | ||
hieAst | ||
getArgumentNumber | ||
pure $ Just (functionName, functionType, argumentNumber) | ||
case mResult of | ||
-- TODO(@linj) what do non-singleton lists mean? | ||
Just (functionName : _, functionType : _, argumentNumber : _) -> do | ||
pure $ InL $ mkSignatureHelp functionName functionType (fromIntegral argumentNumber - 1) | ||
_ -> pure $ InR Null | ||
|
||
mkSignatureHelp :: Name -> Text -> UInt -> SignatureHelp | ||
mkSignatureHelp functionName functionType argumentNumber = | ||
let functionNameLabelPrefix = printOutputable (ppr functionName) <> " :: " | ||
in SignatureHelp | ||
[ SignatureInformation | ||
(functionNameLabelPrefix <> functionType) | ||
Nothing | ||
(Just $ mkArguments (fromIntegral $ T.length functionNameLabelPrefix) functionType) | ||
(Just $ InL argumentNumber) | ||
] | ||
(Just 0) | ||
(Just $ InL argumentNumber) | ||
jian-lin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
-- TODO(@linj) can type string be a multi-line string? | ||
mkArguments :: UInt -> Text -> [ParameterInformation] | ||
jian-lin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
mkArguments offset functionType = | ||
let separator = " -> " | ||
separatorLength = fromIntegral $ T.length separator | ||
splits = T.breakOnAll separator functionType | ||
prefixes = fst <$> splits | ||
prefixLengths = fmap (T.length >>> fromIntegral) prefixes | ||
ranges = | ||
[ ( if previousPrefixLength == 0 then 0 else previousPrefixLength + separatorLength, | ||
currentPrefixLength | ||
) | ||
| (previousPrefixLength, currentPrefixLength) <- zip (0: prefixLengths) prefixLengths | ||
] | ||
in [ ParameterInformation (InR range) Nothing | ||
| range <- bimap (+offset) (+offset) <$> ranges | ||
] | ||
|
||
extractInfoFromSmallestContainingFunctionApplicationAst :: | ||
Position -> HieASTs a -> (RealSrcSpan -> HieAST a -> Maybe b) -> [b] | ||
extractInfoFromSmallestContainingFunctionApplicationAst position hieAsts extractInfo = | ||
M.elems $ flip M.mapMaybeWithKey (getAsts hieAsts) $ \hiePath hieAst -> | ||
smallestContainingSatisfying (positionToSpan hiePath position) (nodeHasAnnotation ("HsApp", "HsExpr")) hieAst | ||
>>= extractInfo (positionToSpan hiePath position) | ||
where | ||
positionToSpan hiePath position = | ||
let loc = mkLoc hiePath position in mkRealSrcSpan loc loc | ||
mkLoc (LexicalFastString hiePath) (Position line character) = | ||
mkRealSrcLoc hiePath (fromIntegral line + 1) (fromIntegral character + 1) | ||
|
||
type Annotation = (FastStringCompat, FastStringCompat) | ||
|
||
nodeHasAnnotation :: Annotation -> HieAST a -> Bool | ||
nodeHasAnnotation annotation = sourceNodeInfo >>> maybe False (isAnnotationInNodeInfo annotation) | ||
jian-lin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
-- TODO(@linj): the left most node may not be the function node. example: (if True then f else g) x | ||
jian-lin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
getLeftMostNode :: HieAST a -> HieAST a | ||
getLeftMostNode thisNode = | ||
case nodeChildren thisNode of | ||
[] -> thisNode | ||
leftChild: _ -> getLeftMostNode leftChild | ||
|
||
getNodeName :: RealSrcSpan -> HieAST a -> Maybe Name | ||
getNodeName _span hieAst = | ||
if nodeHasAnnotation ("HsVar", "HsExpr") hieAst | ||
then | ||
case mapMaybe extractName $ M.keys $ M.filter isUse $ getSourceNodeIds hieAst of | ||
[name] -> Just name -- TODO(@linj) will there be more than one name? | ||
jian-lin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_ -> Nothing | ||
else Nothing -- TODO(@linj) must function node be HsVar? | ||
where | ||
extractName = rightToMaybe | ||
|
||
-- TODO(@linj) share code with getNodeName | ||
getNodeType :: HieKind a -> RealSrcSpan -> HieAST a -> Maybe Text | ||
getNodeType (hieKind :: HieKind a) _span hieAst = | ||
if nodeHasAnnotation ("HsVar", "HsExpr") hieAst | ||
then | ||
case M.elems $ M.filter isUse $ getSourceNodeIds hieAst of | ||
[identifierDetails] -> identType identifierDetails >>= (prettyType >>> Just) | ||
jian-lin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_ -> Nothing -- TODO(@linj) will there be more than one identifierDetails? | ||
else Nothing | ||
where | ||
-- modified from Development.IDE.Spans.AtPoint.atPoint | ||
prettyType :: a -> Text | ||
prettyType = expandType >>> printOutputable | ||
|
||
expandType :: a -> SDoc | ||
expandType t = case hieKind of | ||
HieFresh -> ppr t | ||
HieFromDisk hieFile -> ppr $ hieTypeToIface $ recoverFullType t (hie_types hieFile) | ||
|
||
isUse :: IdentifierDetails a -> Bool | ||
isUse = identInfo >>> S.member Use | ||
|
||
-- Just 1 means the first argument | ||
getArgumentNumber :: RealSrcSpan -> HieAST a -> Maybe Integer | ||
getArgumentNumber span hieAst = | ||
if nodeHasAnnotation ("HsApp", "HsExpr") hieAst | ||
then | ||
case nodeChildren hieAst of | ||
[leftChild, _] -> | ||
if span `isRealSubspanOf` nodeSpan leftChild | ||
then Nothing | ||
else getArgumentNumber span leftChild >>= \argumentNumber -> Just (argumentNumber + 1) | ||
_ -> Nothing -- impossible | ||
else | ||
case nodeChildren hieAst of | ||
[] -> Just 0 -- the function is found | ||
[child] -> getArgumentNumber span child -- ignore irrelevant nodes | ||
_ -> Nothing -- TODO(@linj) handle more cases such as `if` |
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!