-
-
Notifications
You must be signed in to change notification settings - Fork 434
feat: add hls-document-link-plugin to support LSP document links
#4898
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
Open
jetjinser
wants to merge
9
commits into
haskell:master
Choose a base branch
from
jetjinser:documentLink
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.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1967271
feat: add hls-document-link-plugin
jetjinser 7ee648a
test: hls-document-link-plugin golden test
jetjinser 382b87a
feat: add documentLinkOn config
jetjinser a8c3492
test: update default-config
jetjinser 0353713
doc(features): support documentLink
jetjinser fbe109e
refactor: use mkDocLink to reduce template code
jetjinser e644d72
refactor(hls-document-link-plugin): not goldenTest
jetjinser 93d2836
test: more import test
jetjinser 24826fc
refactor: random improve
jetjinser 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
129 changes: 129 additions & 0 deletions
129
plugins/hls-document-link-plugin/src/Ide/Plugin/DocumentLink.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,129 @@ | ||
| {-# LANGUAGE DataKinds #-} | ||
| {-# LANGUAGE DisambiguateRecordFields #-} | ||
| {-# LANGUAGE LambdaCase #-} | ||
| {-# LANGUAGE OverloadedStrings #-} | ||
| {-# LANGUAGE PatternSynonyms #-} | ||
| {-# LANGUAGE StrictData #-} | ||
| {-# LANGUAGE TypeFamilies #-} | ||
|
|
||
| module Ide.Plugin.DocumentLink (descriptor, Log(..)) where | ||
|
|
||
| import Control.DeepSeq (NFData) | ||
| import Control.Monad.Trans.Maybe (MaybeT (runMaybeT), | ||
| hoistMaybe) | ||
| import Data.Hashable (Hashable) | ||
| import qualified Data.Map.Strict as Map | ||
| import Data.Maybe (maybeToList) | ||
| import Development.IDE (DocAndTyThingMap (DKMap, getDocMap), | ||
| GetDocMap (GetDocMap), | ||
| GetHieAst (GetHieAst), | ||
| HieAstResult (HAR, hieAst), | ||
| IdeState (shakeExtras), | ||
| Pretty (pretty), Range, | ||
| Recorder, RuleResult, Rules, | ||
| Uri (Uri), WithPriority, | ||
| cmapWithPrio, | ||
| defineNoDiagnostics, | ||
| fromNormalizedFilePath, | ||
| realSrcSpanToRange) | ||
| import Development.IDE.Core.PluginUtils (runIdeActionE, useMT, | ||
| useWithStaleFastE) | ||
| import qualified Development.IDE.Core.Shake as Shake | ||
| import Development.IDE.GHC.Compat (lookupNameEnv) | ||
| import Development.IDE.GHC.Compat.Util (mkFastString) | ||
| import Development.IDE.Spans.Common (DocMap, | ||
| SpanDoc (SpanDocString, SpanDocText), | ||
| spanDocUriDoc) | ||
| import GHC.Generics (Generic) | ||
| import GHC.Iface.Ext.Types (HieAST (nodeChildren, nodeSpan, sourcedNodeInfo), | ||
| HieASTs (getAsts), | ||
| Identifier, | ||
| NodeInfo (nodeIdentifiers), | ||
| NodeOrigin (SourceInfo), | ||
| SourcedNodeInfo (getSourcedNodeInfo), | ||
| Span, pattern HiePath) | ||
| import Ide.Plugin.Error (getNormalizedFilePathE) | ||
| import Ide.Types (PluginDescriptor (pluginHandlers), | ||
| PluginId, | ||
| PluginMethodHandler, | ||
| defaultPluginDescriptor, | ||
| mkPluginHandler, pluginRules) | ||
| import Language.LSP.Protocol.Message (Method (Method_TextDocumentDocumentLink), | ||
| SMethod (SMethod_TextDocumentDocumentLink)) | ||
| import Language.LSP.Protocol.Types (DocumentLink (..), | ||
| DocumentLinkParams (DocumentLinkParams), | ||
| TextDocumentIdentifier (TextDocumentIdentifier), | ||
| type (|?) (InL)) | ||
|
|
||
| newtype Log = LogShake Shake.Log | ||
|
|
||
| instance Pretty Log where | ||
| pretty = \case | ||
| LogShake shakeLog -> pretty shakeLog | ||
|
|
||
| descriptor :: Recorder (WithPriority Log) -> PluginId -> PluginDescriptor IdeState | ||
| descriptor recorder pluginId = | ||
| (defaultPluginDescriptor pluginId "Provide document link of symbols") | ||
| { Ide.Types.pluginHandlers = mkPluginHandler SMethod_TextDocumentDocumentLink documentLinkProvider, | ||
| Ide.Types.pluginRules = getDocumentLinkRule recorder | ||
| } | ||
|
|
||
| documentLinkProvider :: PluginMethodHandler IdeState Method_TextDocumentDocumentLink | ||
| documentLinkProvider ideState _pluginId (DocumentLinkParams _ _ (TextDocumentIdentifier uri)) = do | ||
| nfp <- getNormalizedFilePathE uri | ||
| ((DocumentLinks uris), _pm) <- runIdeActionE "DocumentLink" (shakeExtras ideState) $ useWithStaleFastE GetDocumentLinks nfp | ||
| pure $ InL (map mkDocumentLink uris) | ||
|
|
||
| mkDocumentLink :: (Range, Uri) -> DocumentLink | ||
| mkDocumentLink (range, target) = | ||
| DocumentLink | ||
| { _range = range, | ||
| _target = Just target, | ||
| _tooltip = Nothing, | ||
| _data_ = Nothing | ||
| } | ||
|
|
||
| data GetDocumentLinks = GetDocumentLinks | ||
| deriving (Eq, Show, Generic) | ||
|
|
||
| instance Hashable GetDocumentLinks | ||
|
|
||
| instance NFData GetDocumentLinks | ||
|
|
||
| newtype DocumentLinks = DocumentLinks [(Range, Uri)] | ||
| deriving (Show, NFData) | ||
|
|
||
| type instance RuleResult GetDocumentLinks = DocumentLinks | ||
|
|
||
| getDocumentLinkRule :: Recorder (WithPriority Log) -> Rules () | ||
| getDocumentLinkRule recorder = | ||
| defineNoDiagnostics (cmapWithPrio LogShake recorder) $ \GetDocumentLinks nfp -> runMaybeT $ do | ||
| HAR {hieAst} <- useMT GetHieAst nfp | ||
| DKMap {getDocMap} <- useMT GetDocMap nfp | ||
| ast <- hoistMaybe $ getAsts hieAst Map.!? HiePath (mkFastString $ fromNormalizedFilePath nfp) | ||
| let lookup = lookupDoc getDocMap | ||
| pure $ DocumentLinks (foldAst lookup ast) | ||
|
|
||
| -- | Recursively traverses the HieAST in depth-first order to collect information | ||
| -- from leaf nodes. For each leaf, it extracts all identifiers and their source | ||
| -- spans, applies the lookup function, and aggregates the results using the | ||
| -- Monoid instance. | ||
| foldAst :: forall a t. Monoid a => ((Identifier, Span) -> a) -> HieAST t -> a | ||
| foldAst lookup ast = case nodeChildren ast of | ||
| [] -> visitLeaf ast | ||
| asts -> foldMap (foldAst lookup) asts | ||
| where | ||
| visitLeaf leaf = | ||
| let span = nodeSpan leaf | ||
| mNodeInfo = Map.lookup SourceInfo $ getSourcedNodeInfo (sourcedNodeInfo leaf) | ||
| in flip foldMap mNodeInfo $ \nodeInfo -> | ||
| foldMap (\ident -> lookup (ident, span)) (Map.keys $ nodeIdentifiers nodeInfo) | ||
|
|
||
| lookupDoc :: DocMap -> (Identifier, Span) -> [(Range, Uri)] | ||
| lookupDoc dm (identifier, span) = do | ||
| Right name <- [identifier] | ||
| doc <- maybeToList $ lookupNameEnv dm name | ||
| uris <- maybeToList $ spanDocUriDoc $ case doc of | ||
| SpanDocString _ uris -> uris | ||
| SpanDocText _ uris -> uris | ||
| pure (realSrcSpanToRange span, Uri uris) |
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,106 @@ | ||
| {-# LANGUAGE DerivingStrategies #-} | ||
| {-# LANGUAGE LambdaCase #-} | ||
| {-# LANGUAGE OverloadedStrings #-} | ||
| {-# LANGUAGE QuasiQuotes #-} | ||
|
|
||
| import Control.Exception (throw) | ||
| import Control.Lens hiding ((<.>)) | ||
| import Control.Monad (void) | ||
| import Data.Maybe (fromJust, fromMaybe) | ||
| import qualified Data.Text as T | ||
| import Ide.Plugin.DocumentLink (descriptor) | ||
| import qualified Ide.Plugin.DocumentLink as DL | ||
| import qualified Language.LSP.Protocol.Lens as L | ||
| import System.FilePath | ||
| import Test.Hls | ||
| import qualified Test.Hls.FileSystem as FS | ||
|
|
||
| testDataDir :: FilePath | ||
| testDataDir = "plugins" </> "hls-document-link-plugin" </> "test" </> "testdata" | ||
|
|
||
| main :: IO () | ||
| main = defaultTestRunner tests | ||
|
|
||
| tests :: TestTree | ||
| tests = testGroup "documentLink" | ||
| [ mkTest "no document links" "NoDocumentLinks" [] | ||
| , mkTest "links of primitive types" "Definition" | ||
| [ (mkDocLink (mkRange 0 10 0 13) (Uri "GHC-Types.html#t:Int")) | ||
| , (mkDocLink (mkRange 2 10 2 14) (Uri "GHC-Types.html#t:Bool")) | ||
| , (mkDocLink (mkRange 3 9 3 13) (Uri "GHC-Types.html#v:True")) | ||
| ] | ||
| , mkTest "links from modules" "ImportModule" | ||
| [ (mkDocLink (mkRange 0 20 0 28) (Uri "GHC-Internal-Data-Maybe.html#v:fromJust")) | ||
| , (mkDocLink (mkRange 0 30 0 39) (Uri "GHC-Internal-Data-Maybe.html#v:fromMaybe")) | ||
| , (mkDocLink (mkRange 0 41 0 46) (Uri "GHC-Internal-Maybe.html#t:Maybe")) | ||
| , (mkDocLink (mkRange 0 47 0 51) (Uri "GHC-Internal-Maybe.html#v:Just")) | ||
| , (mkDocLink (mkRange 0 53 0 60) (Uri "GHC-Internal-Maybe.html#v:Nothing")) | ||
| , (mkDocLink (mkRange 1 20 1 26) (Uri "GHC-Internal-Data-Either.html#t:Either")) | ||
| , (mkDocLink (mkRange 2 5 2 13) (Uri "GHC-Internal-Data-Maybe.html#v:fromJust")) | ||
| , (mkDocLink (mkRange 2 15 2 24) (Uri "GHC-Internal-Data-Maybe.html#v:fromMaybe")) | ||
| ] | ||
| ] | ||
|
|
||
| mkDocLink :: Range -> Uri -> SimilarDocumentLink | ||
| mkDocLink range uri = | ||
| SimilarDocumentLink (DocumentLink range (Just uri) Nothing Nothing) | ||
|
|
||
| mkTest :: TestName -> FilePath -> [SimilarDocumentLink] -> TestTree | ||
| mkTest title file expected = testCase title $ runWithDocumentLink filehs $ do | ||
| adoc <- openDoc filehs "haskell" | ||
| void waitForBuildQueue | ||
| documentLink <- getDocumentLink adoc | ||
| liftIO $ ((fmap . fmap) SimilarDocumentLink documentLink) @?= Just expected | ||
| where filehs = file <.> "hs" | ||
|
|
||
| runWithDocumentLink :: FilePath -> Session a -> IO a | ||
| runWithDocumentLink file = runSessionWithServerInTmpDir def plugin (mkFs $ FS.directProject file) | ||
| where plugin :: PluginTestDescriptor DL.Log | ||
| plugin = mkPluginTestDescriptor descriptor "documentLink" | ||
| mkFs :: [FS.FileTree] -> FS.VirtualFileTree | ||
| mkFs = FS.mkVirtualFileTree testDataDir | ||
|
|
||
| getDocumentLink :: TextDocumentIdentifier -> Session (Maybe [DocumentLink]) | ||
| getDocumentLink doc = | ||
| let params = DocumentLinkParams Nothing Nothing doc | ||
| in nullToMaybe . getResponseResult <$> request SMethod_TextDocumentDocumentLink params | ||
|
|
||
|
|
||
| getResponseResult :: (Show (ErrorData m)) => TResponseMessage m -> MessageResult m | ||
| getResponseResult rsp = | ||
| case rsp ^. L.result of | ||
| Right x -> x | ||
| Left err -> throw $ UnexpectedResponseError (fromJust $ rsp ^. L.id) err | ||
|
|
||
| newtype SimilarDocumentLink = SimilarDocumentLink DocumentLink | ||
| deriving newtype (Show) | ||
|
|
||
| -- custom Eq to ignore some details, such as specific URI | ||
| -- not symmetry | ||
| instance Eq SimilarDocumentLink where | ||
| SimilarDocumentLink actualDocumentLink@( DocumentLink | ||
| actualRange | ||
| actualUri | ||
| actualTooltip | ||
| actualData ) | ||
| == SimilarDocumentLink expectedDocumentLink@( DocumentLink | ||
| expectRange | ||
| expectUri | ||
| expectToolTip | ||
| expectData ) | ||
| | actualDocumentLink == expectedDocumentLink = True | ||
| | actualRange == expectRange | ||
| && actualTooltip == expectToolTip | ||
| && actualData == expectData | ||
| = actualUri ~= expectUri | ||
| | otherwise = False | ||
|
|
||
| class IsSimilar a where | ||
| (~=) :: a -> a -> Bool | ||
|
|
||
| instance (IsSimilar a) => IsSimilar (Maybe a) where | ||
| m1 ~= m2 = fromMaybe False $ liftA2 (~=) m1 m2 | ||
|
|
||
| instance IsSimilar Uri where | ||
| (Uri actual) ~= (Uri except) | ||
| = except `T.isSuffixOf` actual | ||
Oops, something went wrong.
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.