1 {-# LANGUAGE OverloadedStrings #-}
2 module Language.Haskell.LSP.Test.Decoding where
4 import Prelude hiding ( id )
7 import Control.Exception
9 import qualified Data.ByteString.Lazy.Char8 as B
12 import System.IO.Error
13 import Language.Haskell.LSP.Types
14 import Language.Haskell.LSP.Types.Lens
15 import Language.Haskell.LSP.Messages
16 import Language.Haskell.LSP.Test.Exceptions
17 import qualified Data.HashMap.Strict as HM
19 getAllMessages :: Handle -> IO [B.ByteString]
25 msg <- getNextMessage h
27 (msg :) <$> getAllMessages h
29 -- | Fetches the next message bytes based on
30 -- the Content-Length header
31 getNextMessage :: Handle -> IO B.ByteString
33 headers <- getHeaders h
34 case read . init <$> lookup "Content-Length" headers of
35 Nothing -> throw NoContentLengthHeader
36 Just size -> B.hGet h size
38 addHeader :: B.ByteString -> B.ByteString
39 addHeader content = B.concat
41 , B.pack $ show $ B.length content
47 getHeaders :: Handle -> IO [(String, String)]
49 l <- catch (hGetLine h) eofHandler
50 let (name, val) = span (/= ':') l
51 if null val then return [] else ((name, drop 2 val) :) <$> getHeaders h
53 | isEOFError e = throw UnexpectedServerTermination
56 type RequestMap = HM.HashMap LspId ClientMethod
58 newRequestMap :: RequestMap
59 newRequestMap = HM.empty
61 updateRequestMap :: RequestMap -> LspId -> ClientMethod -> RequestMap
62 updateRequestMap reqMap id method = HM.insert id method reqMap
64 getRequestMap :: [FromClientMessage] -> RequestMap
65 getRequestMap = foldl helper HM.empty
67 helper acc msg = case msg of
68 (ReqInitialize val) -> insert val acc
69 (ReqShutdown val) -> insert val acc
70 (ReqHover val) -> insert val acc
71 (ReqCompletion val) -> insert val acc
72 (ReqCompletionItemResolve val) -> insert val acc
73 (ReqSignatureHelp val) -> insert val acc
74 (ReqDefinition val) -> insert val acc
75 (ReqTypeDefinition val) -> insert val acc
76 (ReqFindReferences val) -> insert val acc
77 (ReqDocumentHighlights val) -> insert val acc
78 (ReqDocumentSymbols val) -> insert val acc
79 (ReqWorkspaceSymbols val) -> insert val acc
80 (ReqCodeAction val) -> insert val acc
81 (ReqCodeLens val) -> insert val acc
82 (ReqCodeLensResolve val) -> insert val acc
83 (ReqDocumentFormatting val) -> insert val acc
84 (ReqDocumentRangeFormatting val) -> insert val acc
85 (ReqDocumentOnTypeFormatting val) -> insert val acc
86 (ReqRename val) -> insert val acc
87 (ReqExecuteCommand val) -> insert val acc
88 (ReqDocumentLink val) -> insert val acc
89 (ReqDocumentLinkResolve val) -> insert val acc
90 (ReqWillSaveWaitUntil val) -> insert val acc
92 insert m = HM.insert (m ^. id) (m ^. method)
94 matchResponseMsgType :: ClientMethod -> B.ByteString -> FromServerMessage
95 matchResponseMsgType req = case req of
96 Initialize -> RspInitialize . decoded
97 Shutdown -> RspShutdown . decoded
98 TextDocumentHover -> RspHover . decoded
99 TextDocumentCompletion -> RspCompletion . decoded
100 CompletionItemResolve -> RspCompletionItemResolve . decoded
101 TextDocumentSignatureHelp -> RspSignatureHelp . decoded
102 TextDocumentDefinition -> RspDefinition . decoded
103 TextDocumentTypeDefinition -> RspTypeDefinition . decoded
104 TextDocumentReferences -> RspFindReferences . decoded
105 TextDocumentDocumentHighlight -> RspDocumentHighlights . decoded
106 TextDocumentDocumentSymbol -> RspDocumentSymbols . decoded
107 WorkspaceSymbol -> RspWorkspaceSymbols . decoded
108 TextDocumentCodeAction -> RspCodeAction . decoded
109 TextDocumentCodeLens -> RspCodeLens . decoded
110 CodeLensResolve -> RspCodeLensResolve . decoded
111 TextDocumentFormatting -> RspDocumentFormatting . decoded
112 TextDocumentRangeFormatting -> RspDocumentRangeFormatting . decoded
113 TextDocumentOnTypeFormatting -> RspDocumentOnTypeFormatting . decoded
114 TextDocumentRename -> RspRename . decoded
115 WorkspaceExecuteCommand -> RspExecuteCommand . decoded
116 TextDocumentDocumentLink -> RspDocumentLink . decoded
117 DocumentLinkResolve -> RspDocumentLinkResolve . decoded
118 TextDocumentWillSaveWaitUntil -> RspWillSaveWaitUntil . decoded
119 x -> error . ((show x ++ " is not a request: ") ++) . show
120 where decoded x = fromMaybe (error $ "Couldn't decode response for the request type: "
121 ++ show req ++ "\n" ++ show x)
124 decodeFromServerMsg :: RequestMap -> B.ByteString -> FromServerMessage
125 decodeFromServerMsg reqMap bytes =
126 case HM.lookup "method" obj of
127 Just methodStr -> case fromJSON methodStr of
128 Success method -> case method of
129 -- We can work out the type of the message
130 TextDocumentPublishDiagnostics -> NotPublishDiagnostics $ fromJust $ decode bytes
131 WindowShowMessage -> NotShowMessage $ fromJust $ decode bytes
132 WindowLogMessage -> NotLogMessage $ fromJust $ decode bytes
133 CancelRequestServer -> NotCancelRequestFromServer $ fromJust $ decode bytes
135 fromJust $ asum [NotWorkDoneProgressBegin <$> decode bytes, NotWorkDoneProgressReport <$> decode bytes, NotWorkDoneProgressEnd <$> decode bytes]
136 WindowWorkDoneProgressCreate -> ReqWorkDoneProgressCreate $ fromJust $ decode bytes
137 TelemetryEvent -> NotTelemetry $ fromJust $ decode bytes
138 WindowShowMessageRequest -> ReqShowMessage $ fromJust $ decode bytes
139 ClientRegisterCapability -> ReqRegisterCapability $ fromJust $ decode bytes
140 ClientUnregisterCapability -> ReqUnregisterCapability $ fromJust $ decode bytes
141 WorkspaceApplyEdit -> ReqApplyWorkspaceEdit $ fromJust $ decode bytes
142 WorkspaceWorkspaceFolders -> error "ReqWorkspaceFolders not supported yet"
143 WorkspaceConfiguration -> error "ReqWorkspaceConfiguration not supported yet"
145 | "id" `HM.member` obj && "method" `HM.member` obj -> ReqCustomServer $ fromJust $ decode bytes
146 | "id" `HM.member` obj -> RspCustomServer $ fromJust $ decode bytes
147 | otherwise -> NotCustomServer $ fromJust $ decode bytes
151 Nothing -> case decode bytes :: Maybe (ResponseMessage Value) of
152 Just msg -> case HM.lookup (requestId $ msg ^. id) reqMap of
153 Just req -> matchResponseMsgType req bytes -- try to decode it to more specific type
154 Nothing -> error "Couldn't match up response with request"
155 Nothing -> error "Couldn't decode message"
156 where obj = fromJust $ decode bytes :: Object