Skip to content
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

Add support for hover extensions #77

Merged
merged 26 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6169f30
Parse extensions from config
fwcd Dec 3, 2024
c4ed2aa
Normalize hover contents
fwcd Dec 3, 2024
4a052fd
Add mergeHovers utility function
fwcd Dec 3, 2024
e117249
Stub out extension hovers
fwcd Dec 3, 2024
5a708d5
Parse unknown extensions into catch-all ExtensionPoint
fwcd Dec 3, 2024
72b9b47
Allow IO in extension hovers
fwcd Dec 3, 2024
50e8680
Implement basic hover extensions
fwcd Dec 4, 2024
cb10bf1
Update extension hover formatting
fwcd Dec 4, 2024
0445022
Improve exit message
fwcd Dec 4, 2024
e3a8738
Run hover extensions without shell
fwcd Dec 4, 2024
3491d04
Manually define Extension ToJSON/FromJSON
fwcd Dec 4, 2024
245a61b
Add support for extension output formats
fwcd Dec 4, 2024
846a1ac
Format extension data type definition
fwcd Dec 4, 2024
b6b0ba2
Format hover extensions depending on output format
fwcd Dec 4, 2024
5500091
Add extension title to hover
fwcd Dec 4, 2024
5b701e6
Add missing line breaks
fwcd Dec 4, 2024
cd8e9df
Implement template substitutions
fwcd Dec 4, 2024
c71ecb4
Factor out applyParam
fwcd Dec 4, 2024
5464460
Add some more template parameters
fwcd Dec 4, 2024
db0aa41
Fix typo
fwcd Dec 4, 2024
ed04ed2
Update naming convention for template parameters
fwcd Dec 4, 2024
4514ced
Fix URI template parameter
fwcd Dec 4, 2024
7114e9e
Always provide extension hover
fwcd Dec 4, 2024
2c922d0
Support the `showOutputOnError` key in extensions
fwcd Dec 4, 2024
55a15a3
Expose more information to hover extensions
fwcd Dec 6, 2024
daa9490
Use resolveAtPos for hover extensions
fwcd Dec 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions curry-language-server.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ library
Curry.LanguageServer.CPM.Deps
Curry.LanguageServer.CPM.Monad
Curry.LanguageServer.CPM.Process
Curry.LanguageServer.Extension
Curry.LanguageServer.FileLoader
Curry.LanguageServer.Handlers
Curry.LanguageServer.Handlers.Cancel
Expand Down
5 changes: 5 additions & 0 deletions src/Curry/LanguageServer/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Curry.LanguageServer.Config
) where

import Colog.Core (Severity (..))
import Curry.LanguageServer.Extension (Extension (..))
import Data.Aeson
( FromJSON (..)
, ToJSON (..)
Expand All @@ -26,6 +27,7 @@ data Config = Config { forceRecompilation :: Bool
, logLevel :: LogLevel
, curryPath :: String
, useSnippetCompletions :: Bool
, extensions :: [Extension]
}
deriving (Show, Eq)

Expand All @@ -36,6 +38,7 @@ instance Default Config where
, logLevel = LogLevel Info
, curryPath = "pakcs"
, useSnippetCompletions = False
, extensions = []
}

instance FromJSON Config where
Expand All @@ -46,6 +49,7 @@ instance FromJSON Config where
logLevel <- l .:? "logLevel" .!= (def @Config).logLevel
curryPath <- l .:? "curryPath" .!= (def @Config).curryPath
useSnippetCompletions <- l .:? "useSnippetCompletions" .!= (def @Config).useSnippetCompletions
extensions <- l .:? "extensions" .!= (def @Config).extensions
return Config {..}

instance ToJSON Config where
Expand All @@ -56,6 +60,7 @@ instance ToJSON Config where
, "logLevel" .= logLevel
, "curryPath" .= curryPath
, "useSnippetCompletions" .= useSnippetCompletions
, "extensions" .= extensions
]

instance FromJSON LogLevel where
Expand Down
83 changes: 83 additions & 0 deletions src/Curry/LanguageServer/Extension.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{-# LANGUAGE OverloadedRecordDot, OverloadedStrings, RecordWildCards, TypeApplications #-}
module Curry.LanguageServer.Extension
( ExtensionPoint (..), ExtensionOutputFormat (..), Extension (..)
) where

import Data.Aeson (FromJSON (..), ToJSON (..), (.:?), (.!=), (.=), object, withObject)
import Data.Default (Default (..))
import qualified Data.Text as T

data ExtensionPoint = ExtensionPointHover
| ExtensionPointUnknown T.Text
deriving (Show, Eq)

data ExtensionOutputFormat = ExtensionOutputFormatPlaintext
| ExtensionOutputFormatMarkdown
| ExtensionOutputFormatUnknown T.Text
deriving (Show, Eq)

data Extension = Extension
{ name :: T.Text
, extensionPoint :: ExtensionPoint
, outputFormat :: ExtensionOutputFormat
, showOutputOnError :: Bool
, executable :: T.Text
, args :: [T.Text]
}
deriving (Show, Eq)

instance Default Extension where
def = Extension
{ name = "Anonymous Extension"
, extensionPoint = ExtensionPointHover
, outputFormat = ExtensionOutputFormatPlaintext
, showOutputOnError = False
, executable = "echo"
, args = []
}

instance FromJSON Extension where
parseJSON = withObject "Extension" $ \e -> do
name <- e .:? "name" .!= (def @Extension).name
extensionPoint <- e .:? "extensionPoint" .!= (def @Extension).extensionPoint
outputFormat <- e .:? "outputFormat" .!= (def @Extension).outputFormat
showOutputOnError <- e .:? "showOutputOnError" .!= (def @Extension).showOutputOnError
executable <- e .:? "executable" .!= (def @Extension).executable
args <- e .:? "args" .!= (def @Extension).args
return Extension {..}

instance ToJSON Extension where
toJSON Extension {..} = object
[ "name" .= name
, "extensionPoint" .= extensionPoint
, "outputFormat" .= outputFormat
, "showOutputOnError" .= showOutputOnError
, "executable" .= executable
, "args" .= args
]

instance FromJSON ExtensionPoint where
parseJSON v = do
s <- parseJSON v
return $ case s :: T.Text of
"hover" -> ExtensionPointHover
_ -> ExtensionPointUnknown s

instance ToJSON ExtensionPoint where
toJSON p = toJSON @T.Text $ case p of
ExtensionPointHover -> "hover"
ExtensionPointUnknown s -> s

instance FromJSON ExtensionOutputFormat where
parseJSON v = do
s <- parseJSON v
return $ case s :: T.Text of
"plaintext" -> ExtensionOutputFormatPlaintext
"markdown" -> ExtensionOutputFormatMarkdown
_ -> ExtensionOutputFormatUnknown s

instance ToJSON ExtensionOutputFormat where
toJSON p = toJSON @T.Text $ case p of
ExtensionOutputFormatPlaintext -> "plaintext"
ExtensionOutputFormatMarkdown -> "markdown"
ExtensionOutputFormatUnknown s -> s
117 changes: 104 additions & 13 deletions src/Curry/LanguageServer/Handlers/TextDocument/Hover.hs
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
{-# LANGUAGE FlexibleContexts, OverloadedStrings, OverloadedRecordDot, TypeOperators, ViewPatterns #-}
{-# LANGUAGE FlexibleContexts, NumericUnderscores, OverloadedStrings, OverloadedRecordDot, TypeOperators, ViewPatterns #-}
{-# OPTIONS_GHC -Wno-deprecations #-}
module Curry.LanguageServer.Handlers.TextDocument.Hover (hoverHandler) where

-- Curry Compiler Libraries + Dependencies

import Control.Applicative ((<|>))
import Control.Lens ((^.))
import Control.Monad.Extra (mapMaybeM)
import Control.Monad.IO.Class (MonadIO (..))
import Control.Monad.Trans (lift)
import Control.Monad.Trans.Maybe (MaybeT (..))
import qualified Curry.LanguageServer.Config as CFG
import qualified Curry.LanguageServer.Index.Store as I
import qualified Curry.LanguageServer.Index.Symbol as I
import Curry.LanguageServer.Utils.Convert (ppPredTypeToText, currySpanInfo2Range)
import Curry.LanguageServer.Extension (ExtensionPoint (..), ExtensionOutputFormat (..), Extension (..))
import Curry.LanguageServer.Utils.Convert (ppPredTypeToText, currySpanInfo2Range, ppToText)
import Curry.LanguageServer.Index.Resolve (resolveAtPos)
import Curry.LanguageServer.Utils.General (liftMaybe)
import Curry.LanguageServer.Utils.Logging (debugM, infoM)
import Curry.LanguageServer.Utils.Lookup (findTypeAtPos)
import Curry.LanguageServer.Index.Symbol (symbolParentIdent)
import Curry.LanguageServer.Utils.Syntax (moduleIdentifier)
import Curry.LanguageServer.Utils.Sema (ModuleAST, TypedSpanInfo (..))
import Curry.LanguageServer.Utils.Uri (normalizeUriWithPath)
import Curry.LanguageServer.Utils.Uri (normalizeUriWithPath, uriToFilePath)
import Curry.LanguageServer.Monad (LSM, getStore)
import Data.Maybe (listToMaybe)
import Data.Maybe (listToMaybe, maybeToList, fromMaybe)
import qualified Data.Text as T
import qualified Language.LSP.Server as S
import qualified Language.LSP.Protocol.Types as J
import qualified Language.LSP.Protocol.Lens as J
import qualified Language.LSP.Protocol.Message as J
import Language.LSP.Server (MonadLsp)
import System.Exit (ExitCode (..))
import System.Process (readCreateProcessWithExitCode, proc)
import System.Timeout (timeout)

hoverHandler :: S.Handlers LSM
hoverHandler = S.requestHandler J.SMethod_TextDocumentHover $ \req responder -> do
Expand All @@ -37,13 +42,16 @@ hoverHandler = S.requestHandler J.SMethod_TextDocumentHover $ \req responder ->
store <- getStore
hover <- runMaybeT $ do
entry <- I.getModule normUri
MaybeT $ fetchHover store entry pos
MaybeT $ fetchHover store entry pos uri
responder $ Right $ maybe (J.InR J.Null) J.InL hover

fetchHover :: (MonadIO m, MonadLsp CFG.Config m) => I.IndexStore -> I.ModuleStoreEntry -> J.Position -> m (Maybe J.Hover)
fetchHover store entry pos = runMaybeT $ do
fetchHover :: (MonadIO m, MonadLsp CFG.Config m) => I.IndexStore -> I.ModuleStoreEntry -> J.Position -> J.Uri -> m (Maybe J.Hover)
fetchHover store entry pos uri = runMaybeT $ do
ast <- liftMaybe entry.moduleAST
hover <- liftMaybe $ qualIdentHover store ast pos <|> typedSpanInfoHover ast pos
cfg <- lift S.getConfig
let baseHover = maybeToList $ qualIdentHover store ast pos <|> typedSpanInfoHover ast pos
extHovers <- mapMaybeM (extensionHover store ast pos uri) cfg.extensions
hover <- liftMaybe . joinHovers $ baseHover ++ extHovers
lift $ infoM $ "Found hover: " <> previewHover hover
return hover

Expand All @@ -65,10 +73,93 @@ typedSpanInfoHover ast@(moduleIdentifier -> mid) pos = do

return $ J.Hover contents range

extensionHover :: MonadIO m => I.IndexStore -> ModuleAST -> J.Position -> J.Uri -> Extension -> m (Maybe J.Hover)
extensionHover store ast@(moduleIdentifier -> mid) pos@(J.Position l c) uri e = case e.extensionPoint of
ExtensionPointHover -> runMaybeT $ do
let symbol = listToMaybe . fst =<< resolveAtPos store ast pos
timeoutSecs = 10
timeoutMicros = timeoutSecs * 1_000_000
templateParams = [ ("currentFile", T.pack (fromMaybe "" (uriToFilePath uri)))
, ("currentUri", J.getUri uri)
, ("currentModule", ppToText mid)
, ("line", T.pack (show l))
, ("column", T.pack (show c))
, ("type", fromMaybe "" ((.printedType) =<< symbol))
, ("identifier", maybe "" (.ident) symbol)
, ("module", maybe "" symbolParentIdent symbol)
] :: [(T.Text, T.Text)]
applyParam p = T.replace ("{" <> p <> "}")
evalTemplate t = foldr (uncurry applyParam) t templateParams
procOpts = proc (T.unpack e.executable) (T.unpack . (evalTemplate :: T.Text -> T.Text) <$> e.args)

(exitCode, out, err) <- MaybeT $ liftIO $ timeout timeoutMicros $ readCreateProcessWithExitCode procOpts ""

let simpleCodeBlock t
| T.null t = ""
| otherwise = "```\n" <> t <> "\n```"

text <- case exitCode of
ExitSuccess -> return $ T.unlines
[ "**" <> e.name <> "**"
, ""
, case e.outputFormat of
ExtensionOutputFormatMarkdown -> T.pack out
_ -> simpleCodeBlock (T.pack out)
]
_ | e.showOutputOnError -> return $ T.unlines
[ "_Extension **" <> e.name <> "** exited with " <> T.pack (show exitCode) <> "_"
, ""
, simpleCodeBlock (T.pack err)
]
| otherwise -> liftMaybe Nothing

let contents = J.InL $ J.MarkupContent J.MarkupKind_Markdown text

return $ J.Hover contents Nothing
_ -> return Nothing

previewHover :: J.Hover -> T.Text
previewHover ((^. J.contents) -> J.InL (J.MarkupContent k t)) = case k of J.MarkupKind_Markdown -> markdownToPlain t
J.MarkupKind_PlainText -> t
previewHover _ = "?"
previewHover = T.unlines . (previewMarkupContent <$>) . normalizeHoverContents . (^. J.contents)

previewMarkupContent :: J.MarkupContent -> T.Text
previewMarkupContent (J.MarkupContent k t) = case k of
J.MarkupKind_Markdown -> markdownToPlain t
J.MarkupKind_PlainText -> t

joinHovers :: [J.Hover] -> Maybe J.Hover
joinHovers [] = Nothing
joinHovers hs = Just $ foldr1 mergeHovers hs

mergeHovers :: J.Hover -> J.Hover -> J.Hover
mergeHovers (J.Hover (normalizeHoverContents -> cs1) r1) (J.Hover (normalizeHoverContents -> cs2) r2) =
J.Hover (J.InL (joinMarkupContent (cs1 ++ cs2))) (r1 <|> r2)

joinMarkupContent :: [J.MarkupContent] -> J.MarkupContent
joinMarkupContent [] = emptyMarkupContent
joinMarkupContent cs = foldr1 mergeMarkupContent cs

mergeMarkupContent :: J.MarkupContent -> J.MarkupContent -> J.MarkupContent
mergeMarkupContent (normalizeToMarkdown -> J.MarkupContent _ t1) (normalizeToMarkdown -> J.MarkupContent _ t2) =
J.MarkupContent J.MarkupKind_Markdown $ T.unlines [t1, "", "---", "", t2]

emptyMarkupContent :: J.MarkupContent
emptyMarkupContent = J.MarkupContent J.MarkupKind_PlainText ""

normalizeToMarkdown :: J.MarkupContent -> J.MarkupContent
normalizeToMarkdown (J.MarkupContent k t) = case k of
J.MarkupKind_Markdown -> J.MarkupContent k t
J.MarkupKind_PlainText -> J.mkMarkdownCodeBlock "text" t

normalizeHoverContents :: J.MarkupContent J.|? (J.MarkedString J.|? [J.MarkedString]) -> [J.MarkupContent]
normalizeHoverContents m = case m of
J.InL c -> [c]
J.InR (J.InL s) -> [markedStringToContent s]
J.InR (J.InR ss) -> markedStringToContent <$> ss

markedStringToContent :: J.MarkedString -> J.MarkupContent
markedStringToContent (J.MarkedString m) = case m of
J.InL t -> J.MarkupContent J.MarkupKind_PlainText t
J.InR (J.MarkedStringWithLanguage l t) -> J.mkMarkdownCodeBlock l t

markdownToPlain :: T.Text -> T.Text
markdownToPlain t = T.intercalate ", " $ filter includeLine $ T.lines t
Expand Down
6 changes: 5 additions & 1 deletion src/Curry/LanguageServer/Utils/Sema.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ untypedTopLevelDecls (CS.Module _ _ _ _ _ _ decls) = untypedDecls
where typeSigIdents = S.fromList [i | CS.TypeSig _ is _ <- decls, i <- is]
untypedDecls = [(spi, i, t) | CS.FunctionDecl spi t i _ <- decls, i `S.notMember` typeSigIdents]

data TypedSpanInfo a = TypedSpanInfo T.Text a CSPI.SpanInfo
data TypedSpanInfo a = TypedSpanInfo
{ exprText :: T.Text
, typeAnnotation :: a
, spanInfo :: CSPI.SpanInfo
}
deriving (Show, Eq)

class HasTypedSpanInfos e a where
Expand Down
Loading