Update haskell-lsp to 0.6
[opengl.git] / src / Language / Haskell / LSP / Test / Files.hs
1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE RankNTypes #-}
3 {-# LANGUAGE OverloadedStrings #-}
4 module Language.Haskell.LSP.Test.Files
5   ( swapFiles
6   , rootDir
7   )
8 where
9
10 import           Language.Haskell.LSP.Capture
11 import           Language.Haskell.LSP.Types hiding ( error )
12 import           Language.Haskell.LSP.Messages
13 import           Control.Lens
14 import qualified Data.HashMap.Strict           as HM
15 import qualified Data.Text                     as T
16 import           Data.Maybe
17 import           System.Directory
18 import           System.FilePath
19
20 swapFiles :: FilePath -> [Event] -> IO [Event]
21 swapFiles relCurBaseDir msgs = do
22   let capturedBaseDir = rootDir msgs
23
24   curBaseDir <- (</> relCurBaseDir) <$> getCurrentDirectory
25   let transform uri =
26         let fp = fromMaybe (error "Couldn't transform uri") (uriToFilePath uri)
27             newFp = curBaseDir </> makeRelative capturedBaseDir fp
28           in filePathToUri newFp
29       newMsgs = map (mapUris transform) msgs
30
31   return newMsgs
32
33 rootDir :: [Event] -> FilePath
34 rootDir (FromClient _ (ReqInitialize req):_) =
35   fromMaybe (error "Couldn't find root dir") $ do
36     rootUri <- req ^. params .rootUri
37     uriToFilePath rootUri
38 rootDir _ = error "Couldn't find initialize request in session"
39
40 mapUris :: (Uri -> Uri) -> Event -> Event
41 mapUris f event =
42   case event of
43     FromClient t msg -> FromClient t (fromClientMsg msg)
44     FromServer t msg -> FromServer t (fromServerMsg msg)
45
46   where
47     --TODO: Handle all other URIs that might need swapped
48     fromClientMsg (NotDidOpenTextDocument n) = NotDidOpenTextDocument $ swapUri (params . textDocument) n
49     fromClientMsg (NotDidChangeTextDocument n) = NotDidChangeTextDocument $ swapUri (params . textDocument) n
50     fromClientMsg (NotWillSaveTextDocument n) = NotWillSaveTextDocument $ swapUri (params . textDocument) n
51     fromClientMsg (NotDidSaveTextDocument n) = NotDidSaveTextDocument $ swapUri (params . textDocument) n
52     fromClientMsg (NotDidCloseTextDocument n) = NotDidCloseTextDocument $ swapUri (params . textDocument) n
53     fromClientMsg (ReqInitialize r) = ReqInitialize $ params .~ transformInit (r ^. params) $ r
54     fromClientMsg (ReqDocumentSymbols r) = ReqDocumentSymbols $ swapUri (params . textDocument) r
55     fromClientMsg (ReqRename r) = ReqRename $ swapUri (params . textDocument) r
56     fromClientMsg x = x
57
58     fromServerMsg :: FromServerMessage -> FromServerMessage
59     fromServerMsg (ReqApplyWorkspaceEdit r) =
60       ReqApplyWorkspaceEdit $ params . edit .~ swapWorkspaceEdit (r ^. params . edit) $ r
61
62     fromServerMsg (NotPublishDiagnostics n) = NotPublishDiagnostics $ swapUri params n
63
64     fromServerMsg (RspDocumentSymbols r) =
65       let newSymbols = case r ^. result of
66             Just (DSSymbolInformation si) -> Just (DSSymbolInformation (fmap (swapUri location) si))
67             x -> x
68       in RspDocumentSymbols $ result .~ newSymbols $ r
69
70     fromServerMsg (RspRename r) =
71       let oldResult = r ^. result :: Maybe WorkspaceEdit
72           newResult = fmap swapWorkspaceEdit oldResult
73       in RspRename $ result .~ newResult $ r
74
75     fromServerMsg x = x
76
77     swapWorkspaceEdit :: WorkspaceEdit -> WorkspaceEdit
78     swapWorkspaceEdit e =
79       let newDocChanges = fmap (fmap (swapUri textDocument)) $ e ^. documentChanges
80           newChanges = fmap (swapKeys f) $ e ^. changes
81       in WorkspaceEdit newChanges newDocChanges
82
83     swapKeys :: (Uri -> Uri) -> HM.HashMap Uri b -> HM.HashMap Uri b
84     swapKeys f = HM.foldlWithKey' (\acc k v -> HM.insert (f k) v acc) HM.empty
85
86     swapUri :: HasUri b Uri => Lens' a b -> a -> a
87     swapUri lens x =
88       let newUri = f (x ^. lens . uri)
89         in (lens . uri) .~ newUri $ x
90
91     -- | Transforms rootUri/rootPath.
92     transformInit :: InitializeParams -> InitializeParams
93     transformInit x =
94       let newRootUri = fmap f (x ^. rootUri)
95           newRootPath = do
96             fp <- T.unpack <$> x ^. rootPath
97             let uri = filePathToUri fp
98             T.pack <$> uriToFilePath (f uri)
99         in (rootUri .~ newRootUri) $ (rootPath .~ newRootPath) x