|
| 1 | +-- | goToDeclaration, goToDefinition, and goToImplementation are equivalent except for the input/return wrappers |
| 2 | +module Unison.LSP.GoToDefinition |
| 3 | + ( goToDefinitionHandler, |
| 4 | + goToDeclarationHandler, |
| 5 | + goToImplementationHandler, |
| 6 | + ) |
| 7 | +where |
| 8 | + |
| 9 | +import Control.Lens hiding (List) |
| 10 | +import Data.IntervalMap.Lazy qualified as IM |
| 11 | +import Language.LSP.Protocol.Lens |
| 12 | +import Language.LSP.Protocol.Message qualified as Msg |
| 13 | +import Language.LSP.Protocol.Types |
| 14 | +import Unison.LSP.FileAnalysis qualified as FileAnalysis |
| 15 | +import Unison.LSP.Types |
| 16 | +import Unison.Prelude |
| 17 | + |
| 18 | +-- | Go to Definition handler |
| 19 | +goToDefinitionHandler :: Msg.TRequestMessage 'Msg.Method_TextDocumentDefinition -> (Either Msg.ResponseError (Msg.MessageResult 'Msg.Method_TextDocumentDefinition) -> Lsp ()) -> Lsp () |
| 20 | +goToDefinitionHandler m respond = do |
| 21 | + respond . Right . maybe (InR . InL $ []) (InR . InL) =<< runMaybeT do |
| 22 | + let pos = (m ^. params . position) |
| 23 | + targetRange <- locationInfo (m ^. params . textDocument . uri) pos |
| 24 | + let originLoc = Nothing -- Just use default |
| 25 | + let targetUri = m ^. params . textDocument . uri |
| 26 | + pure $ |
| 27 | + [DefinitionLink (LocationLink originLoc targetUri targetRange targetRange)] |
| 28 | + |
| 29 | +-- | Go to Declaration handler |
| 30 | +goToDeclarationHandler :: Msg.TRequestMessage 'Msg.Method_TextDocumentDeclaration -> (Either Msg.ResponseError (Msg.MessageResult 'Msg.Method_TextDocumentDeclaration) -> Lsp ()) -> Lsp () |
| 31 | +goToDeclarationHandler m respond = do |
| 32 | + respond . Right . maybe (InR . InL $ []) (InR . InL) =<< runMaybeT do |
| 33 | + let pos = (m ^. params . position) |
| 34 | + targetRange <- locationInfo (m ^. params . textDocument . uri) pos |
| 35 | + let originLoc = Nothing -- Just use default |
| 36 | + let targetUri = m ^. params . textDocument . uri |
| 37 | + pure $ |
| 38 | + [DeclarationLink (LocationLink originLoc targetUri targetRange targetRange)] |
| 39 | + |
| 40 | +goToImplementationHandler :: Msg.TRequestMessage 'Msg.Method_TextDocumentImplementation -> (Either Msg.ResponseError (Msg.MessageResult 'Msg.Method_TextDocumentImplementation) -> Lsp ()) -> Lsp () |
| 41 | +goToImplementationHandler m respond = do |
| 42 | + respond . Right . maybe (InR . InL $ []) (InR . InL) =<< runMaybeT do |
| 43 | + let pos = (m ^. params . position) |
| 44 | + targetRange <- locationInfo (m ^. params . textDocument . uri) pos |
| 45 | + let originLoc = Nothing -- Just use default |
| 46 | + let targetUri = m ^. params . textDocument . uri |
| 47 | + pure $ |
| 48 | + [DefinitionLink (LocationLink originLoc targetUri targetRange targetRange)] |
| 49 | + |
| 50 | +locationInfo :: Uri -> Position -> MaybeT Lsp Range |
| 51 | +locationInfo uri pos = do |
| 52 | + FileAnalysis {localBindingInfo} <- FileAnalysis.getFileAnalysis uri |
| 53 | + (_interval, (_typ, range)) <- hoistMaybe $ IM.lookupMin $ IM.intersecting localBindingInfo (IM.ClosedInterval pos pos) |
| 54 | + pure range |
| 55 | + where |
| 56 | + hoistMaybe :: Maybe a -> MaybeT Lsp a |
| 57 | + hoistMaybe = MaybeT . pure |
0 commit comments