Skip to content

Commit

Permalink
Merge pull request #157 from kadena-io/rsoeldner/lsp-docs
Browse files Browse the repository at this point in the history
Revise LSP hover details to produce new documentation
  • Loading branch information
jmcardon authored Jul 10, 2024
2 parents 12e9fe4 + 78c2b65 commit 5d76b93
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 1,782 deletions.
9 changes: 5 additions & 4 deletions pact-lsp/Pact/Core/LanguageServer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import Pact.Core.IR.Term
import Pact.Core.LanguageServer.Utils
import Pact.Core.LanguageServer.Renaming
import Pact.Core.Repl.BuiltinDocs
import Pact.Core.Repl.BuiltinDocs.Internal
import Pact.Core.Repl.UserDocs
import Pact.Core.Names
import qualified Pact.Core.IR.ModuleHashing as MHash
Expand Down Expand Up @@ -304,19 +305,19 @@ documentHoverRequestHandler = requestHandler SMethod_TextDocumentHover $ \req re
case getMatch pos =<< view (lsTopLevel . at nuri) st of
Just tlm -> case tlm of
TermMatch (Builtin builtin i) -> let
docs = fromMaybe "No docs available"
docs = fromMaybe (MarkdownDoc "*No docs available*")
(M.lookup (replCoreBuiltinToUserText builtin) builtinDocs)

mc = MarkupContent MarkupKind_PlainText docs
mc = MarkupContent MarkupKind_Markdown (_markdownDoc docs)
range = spanInfoToRange i
hover = Hover (InL mc) (Just range)
in resp (Right (InL hover))

TermMatch (Var (Name n (NTopLevel mn _)) _) ->
-- Access user-annotated documentation using the @doc command.
let qn = QualifiedName n mn
toHover d = Hover (InL $ MarkupContent MarkupKind_PlainText d) Nothing
doc = preview (lsReplState . at nuri . _Just . replUserDocs . ix qn) st
toHover d = Hover (InL $ MarkupContent MarkupKind_Markdown (_markdownDoc d)) Nothing
doc = MarkdownDoc <$> preview (lsReplState . at nuri . _Just . replUserDocs . ix qn) st
in resp (Right (maybeToNull (toHover <$> doc)))
_ -> resp (Right (InR Null))
Nothing -> do
Expand Down
36 changes: 36 additions & 0 deletions pact-tests/Pact/Core/Test/DocsTests.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Pact.Core.Test.DocsTests (tests) where

import Control.Monad
import qualified Data.Set as S
import Data.Text (Text)
import qualified Data.Text as T
import Pact.Core.Builtin
import Pact.Core.Repl.BuiltinDocs.Internal
import System.Directory
import System.FilePath
import Test.Tasty
import Test.Tasty.HUnit

tests :: IO TestTree
tests = do
let baseDir = "docs/builtins"
cats <- listDirectory baseDir
builtinWithDocs <- join <$> traverse (\cat -> map (T.pack . takeBaseName) <$> listDirectory (baseDir </> cat)) cats
pure $
testGroup
"Documentation Tests"
[ docsExistsTest builtinWithDocs
]

docsExistsTest :: [Text] -> TestTree
docsExistsTest b = testCase "Builtins should have docs" $ do
let normBuiltins = builtinToNormalizedName <$> replCoreBuiltinNames
let diff = (S.fromList normBuiltins `S.difference` excluded) `S.difference` S.fromList b
assertEqual "Missing builtins should be empty" S.empty diff
where
excluded = S.fromList
["cond", "env-ask-gasmodel", "env-chain-data", "env-exec-config"
,"env-gaslog", "env-gasmodel-fixed", "env-milligas", "env-module-admin"
,"env-set-milligas", "env-stackframe", "env-verifiers", "negate"
,"pact-state", "print", "reset-pact-state", "rollback-tx", "show"
,"sig-keyset", "test-capability"]
7 changes: 4 additions & 3 deletions pact-tests/Pact/Core/Test/LanguageServer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import Pact.Core.Builtin
import Data.Maybe (isJust)
import qualified Data.Map.Strict as M
import Pact.Core.Repl.BuiltinDocs
import Pact.Core.Repl.BuiltinDocs.Internal
import Data.Either (isLeft)
import Data.Text.Encoding
import qualified Data.ByteString.Lazy as LBS
Expand Down Expand Up @@ -66,7 +67,7 @@ userDocHoverTests = [hoverTest "defun my-fun" 11 "This is my-fun documentation"]
liftIO $ do
assertBool "Return hover information" (isJust h)
let Just hov' = h
assertEqual "Match builtin docs" (view contents hov') (InL $ MarkupContent MarkupKind_PlainText expected)
assertEqual "Match builtin docs" (view contents hov') (InL $ MarkupContent MarkupKind_Markdown expected)


definitionRequestTests :: [TestTree]
Expand Down Expand Up @@ -106,8 +107,8 @@ builtinHoverTests
assertBool "Return hover information" (isJust h)
let
Just hov' = h
Just expectedDocs = M.lookup (replCoreBuiltinToUserText b) builtinDocs
assertEqual "Match builtin docs" (view contents hov') (InL $ MarkupContent MarkupKind_PlainText expectedDocs)
Just (MarkdownDoc expectedDocs) = M.lookup (replCoreBuiltinToUserText b) builtinDocs
assertEqual "Match builtin docs" (view contents hov') (InL $ MarkupContent MarkupKind_Markdown expectedDocs)

overloadBuiltinHoverTests :: [TestTree]
overloadBuiltinHoverTests
Expand Down
3 changes: 3 additions & 0 deletions pact-tests/PactCoreTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ import qualified Pact.Core.Test.LanguageServer as LanguageServer
import qualified Pact.Core.Test.GasGolden as GasGolden
import qualified Pact.Core.Test.SizeOfTests as SizeOfTests
import qualified Pact.Core.Test.ConTagGolden as ConTagGoldenTests
import qualified Pact.Core.Test.DocsTests as DocsTests

main :: IO ()
main = do
replTests <- ReplTests.tests
gasGolden <- GasGolden.tests
legacyTests <- LegacySerialiseTests.tests
commandTests <- CommandTests.tests
docsTests <- DocsTests.tests
defaultMain $ testGroup "pactTests"
[ replTests
, LexerTests.tests
Expand All @@ -38,5 +40,6 @@ main = do
, SizeOfTests.tests
, commandTests
, ConTagGoldenTests.tests
, docsTests
]

13 changes: 11 additions & 2 deletions pact-tng.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ extra-source-files:
cbits/musl/log_data.h
cbits/musl/pow_data.h
cbits/musl/sqrt_data.h
docs/builtins/**/*.md

flag with-crypto
description: Enable crypto primitives
Expand Down Expand Up @@ -192,7 +193,9 @@ library
cbits/musl/sqrt.c
cbits/musl/sqrt_data.c

other-modules: PackageInfo_pact_tng
other-modules:
PackageInfo_pact_tng
Paths_pact_tng

exposed-modules:
Pact.Core.Compile
Expand Down Expand Up @@ -272,6 +275,7 @@ library
Pact.Core.Repl.Runtime.ReplBuiltin
Pact.Core.Repl.Compile
Pact.Core.Repl.BuiltinDocs
Pact.Core.Repl.BuiltinDocs.Internal
Pact.Core.Repl.UserDocs

-- Serialization
Expand Down Expand Up @@ -362,7 +366,9 @@ executable pact
default-language: Haskell2010

-- beware of the autogen modules. Remember to `cabal clean`!
other-modules: PackageInfo_pact_tng
other-modules:
PackageInfo_pact_tng
Paths_pact_tng

benchmark bench
type: exitcode-stdio-1.0
Expand Down Expand Up @@ -495,6 +501,7 @@ test-suite core-tests
, pact-tng:test-utils
, lsp-test
, lsp-types

other-modules:
, Pact.Core.Test.CommandTests
, Pact.Core.Test.ReplTests
Expand All @@ -511,6 +518,8 @@ test-suite core-tests
, Pact.Core.Test.LanguageServer
, Pact.Core.Test.GasGolden
, Pact.Core.Test.ConTagGolden
, Pact.Core.Test.DocsTests
, Paths_pact_tng
if (flag(with-crypto))
build-depends: pact-tng:pact-crypto

Expand Down
Loading

0 comments on commit 5d76b93

Please sign in to comment.