|
| 1 | +-- Copyright (c) 2019 The DAML Authors. All rights reserved. |
| 2 | +-- SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | + |
| 5 | +-- | Display information on hover. |
| 6 | +module Development.IDE.LSP.HoverDefinition |
| 7 | + ( setHandlersHover |
| 8 | + , setHandlersDefinition |
| 9 | + ) where |
| 10 | + |
| 11 | +import Development.IDE.Core.Rules |
| 12 | +import Development.IDE.Core.Service |
| 13 | +import Development.IDE.LSP.Server |
| 14 | +import Development.IDE.Types.Location |
| 15 | +import Development.IDE.Types.Logger |
| 16 | +import Development.Shake |
| 17 | +import qualified Language.Haskell.LSP.Core as LSP |
| 18 | +import Language.Haskell.LSP.Messages |
| 19 | +import Language.Haskell.LSP.Types |
| 20 | + |
| 21 | +import qualified Data.Text as T |
| 22 | + |
| 23 | +gotoDefinition :: IdeState -> TextDocumentPositionParams -> IO LocationResponseParams |
| 24 | +hover :: IdeState -> TextDocumentPositionParams -> IO (Maybe Hover) |
| 25 | +gotoDefinition = request "Definition" getDefinition (MultiLoc []) SingleLoc |
| 26 | +hover = request "Hover" getAtPoint Nothing foundHover |
| 27 | + |
| 28 | +foundHover :: (Maybe Range, [T.Text]) -> Maybe Hover |
| 29 | +foundHover (mbRange, contents) = |
| 30 | + Just $ Hover (HoverContents $ MarkupContent MkMarkdown $ T.intercalate sectionSeparator contents) mbRange |
| 31 | + |
| 32 | +setHandlersDefinition, setHandlersHover :: PartialHandlers |
| 33 | +setHandlersDefinition = PartialHandlers $ \WithMessage{..} x -> |
| 34 | + return x{LSP.definitionHandler = withResponse RspDefinition $ const gotoDefinition} |
| 35 | +setHandlersHover = PartialHandlers $ \WithMessage{..} x -> |
| 36 | + return x{LSP.hoverHandler = withResponse RspHover $ const hover} |
| 37 | + |
| 38 | +-- | Respond to and log a hover or go-to-definition request |
| 39 | +request |
| 40 | + :: T.Text |
| 41 | + -> (NormalizedFilePath -> Position -> Action (Maybe a)) |
| 42 | + -> b |
| 43 | + -> (a -> b) |
| 44 | + -> IdeState |
| 45 | + -> TextDocumentPositionParams |
| 46 | + -> IO b |
| 47 | +request label getResults notFound found ide (TextDocumentPositionParams (TextDocumentIdentifier uri) pos _) = do |
| 48 | + mbResult <- case uriToFilePath' uri of |
| 49 | + Just path -> logAndRunRequest label getResults ide pos path |
| 50 | + Nothing -> pure Nothing |
| 51 | + pure $ maybe notFound found mbResult |
| 52 | + |
| 53 | +logAndRunRequest :: T.Text -> (NormalizedFilePath -> Position -> Action b) -> IdeState -> Position -> String -> IO b |
| 54 | +logAndRunRequest label getResults ide pos path = do |
| 55 | + let filePath = toNormalizedFilePath path |
| 56 | + logInfo (ideLogger ide) $ |
| 57 | + label <> " request at position " <> T.pack (showPosition pos) <> |
| 58 | + " in file: " <> T.pack path |
| 59 | + runAction ide $ getResults filePath pos |
0 commit comments