never executed always true always false
    1 module Conjure.LSP.Documentation where
    2 
    3 import Conjure.Prelude
    4 import Conjure.Language.Validator (DocType (..), RegionType (Documentation))
    5 
    6 import Language.LSP.Types (MarkupKind (MkMarkdown), MarkupContent (MarkupContent))
    7 import qualified Data.Text as T
    8 import Data.Text.Encoding ( decodeUtf8 )
    9 
   10 -- from: https://stackoverflow.com/a/60793739/463977
   11 import Network.HTTP.Client          -- package http-client
   12 import Network.HTTP.Client.TLS      -- package http-client-tls
   13 import qualified Data.ByteString.Lazy as BL
   14 
   15 getDocsForBuiltin :: RegionType -> IO (Maybe MarkupContent)
   16 getDocsForBuiltin (Documentation prefix (T.unpack -> name)) = do
   17     let
   18         category = case prefix of
   19           OperatorD -> "operator"
   20           FunctionD -> "function"
   21           KeywordD -> "keyword"
   22           TypeD -> "type"
   23           AttributeD -> "attribute"
   24 
   25         download = do
   26             -- create a connection manager
   27             manager <- newManager tlsManagerSettings
   28             -- create the request
   29             request <- parseRequest (readUrl category name)
   30             -- make the request
   31             r <- httpLbs request manager
   32             -- get the contents (as a lazy ByteString)
   33             let contents = decodeUtf8 $ BL.toStrict $ responseBody r
   34             if contents == "404: Not Found"
   35                 then return $ Just $ fallbackMsg category name
   36                 else return $ Just $ MarkupContent MkMarkdown $ T.concat
   37                     [ contents
   38                     , "\n\n -- \n\n"
   39                     , "[Edit this doc](",editURL category name,")"
   40                     ]
   41 
   42         handler :: HttpException -> IO (Maybe MarkupContent)
   43         handler _ = return $ Just $ MarkupContent MkMarkdown "No internet connection"
   44 
   45     download `catch` handler
   46 getDocsForBuiltin _ = pure Nothing 
   47 
   48 
   49 fallbackMsg :: String -> String -> MarkupContent
   50 fallbackMsg c n = MarkupContent MkMarkdown $ T.concat ["[Create this doc](", createURL c n,")"]
   51 
   52 branch :: String
   53 branch = "main"
   54 
   55 readUrl :: String -> String -> String
   56 readUrl category name = concat
   57     [ "https://raw.githubusercontent.com/conjure-cp/conjure/" ++ branch ++ "/docs/bits/"
   58     , category
   59     , "/"
   60     , name
   61     , ".md"
   62     ]
   63 
   64 
   65 createURL :: String -> String -> Text
   66 createURL category name = T.pack $ concat [
   67             "https://github.com/conjure-cp/conjure/new/" ++ branch ++ "/docs/bits/"
   68              ,category
   69              ,"?filename="
   70              , name
   71              ,".md"
   72             ]
   73 
   74 
   75 editURL :: String -> String -> Text
   76 editURL category name = T.pack $ concat [
   77             "https://github.com/conjure-cp/conjure/edit/" ++ branch ++ "/docs/bits/"
   78              ,category
   79              ,"/"
   80              , name
   81              ,".md"
   82             ]