never executed always true always false
1 {-# LANGUAGE TypeFamilies #-}
2 {-# LANGUAGE PolyKinds #-}
3 {-# LANGUAGE DataKinds #-}
4 {-# LANGUAGE OverloadedStrings #-}
5 module Conjure.LSP.Handlers.File where
6 import Conjure.Prelude
7 import Language.LSP.Server (notificationHandler, Handlers, LspM, publishDiagnostics, getVirtualFile)
8 import Language.LSP.Types (SMethod (..), toNormalizedUri, Uri)
9 import Data.Text (pack)
10 import Control.Lens
11 import Language.LSP.VFS
12 import Language.LSP.Types.Lens (HasTextDocument(textDocument), HasParams (..), HasUri (uri))
13 import Conjure.LSP.Util (getErrorsFromText,sendErrorMessage, getDiagnostics)
14 import Language.LSP.Diagnostics (partitionBySource)
15 import Prettyprinter
16 fileHandlers :: Handlers (LspM ())
17 fileHandlers = mconcat [fileOpenedHandler,fileChangedHandler,fileClosedHandler]
18
19 unhandled :: [Handlers (LspM ())]
20 unhandled = [ notificationHandler SCancelRequest $ \ _ -> pure ()
21 , notificationHandler STextDocumentDidSave $ \ _ -> pure ()
22 ]
23
24 fileOpenedHandler :: Handlers (LspM ())
25 fileOpenedHandler = notificationHandler STextDocumentDidOpen $ \ req -> do
26 let td = req^.params.textDocument
27 doDiagForDocument td
28 pure ()
29
30
31
32 fileChangedHandler :: Handlers (LspM ())
33 fileChangedHandler = notificationHandler STextDocumentDidChange $ \ req -> do
34 let td = req^.params.textDocument
35 doDiagForDocument td
36 pure ()
37 --handle this only to suppress not implemented message
38 fileClosedHandler :: Handlers (LspM ())
39 fileClosedHandler = notificationHandler STextDocumentDidClose $ \ _ -> pure ()
40
41
42
43 doDiagForDocument ::( HasUri a Uri,Show a) => a -> LspM () ()
44 doDiagForDocument d = do
45 let td = d^.uri
46 let doc = toNormalizedUri td
47 mdoc <- getVirtualFile doc
48 case mdoc of
49 Just vf@(VirtualFile _ version _rope) -> do
50 errs <- getErrorsFromText $ virtualFileText vf
51 case errs of
52 Left msg -> sendErrorMessage "An error occured:details incoming" >>sendErrorMessage msg
53 Right file -> publishDiagnostics 10000 doc (Just $ fromIntegral version) $ partitionBySource $ getDiagnostics file
54 _ -> sendErrorMessage $ pack. show $ "No virtual file found for: " <> stringToDoc (show d)