1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE RankNTypes #-}
3 {-# LANGUAGE OverloadedStrings #-}
4 module Language.Haskell.LSP.Test.Files
10 import Language.Haskell.LSP.Capture
11 import Language.Haskell.LSP.Types hiding ( error )
12 import Language.Haskell.LSP.Messages
14 import qualified Data.HashMap.Strict as HM
15 import qualified Data.Text as T
17 import System.Directory
18 import System.FilePath
20 swapFiles :: FilePath -> [Event] -> IO [Event]
21 swapFiles relCurBaseDir msgs = do
22 let capturedBaseDir = rootDir msgs
24 curBaseDir <- (</> relCurBaseDir) <$> getCurrentDirectory
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
33 rootDir :: [Event] -> FilePath
34 rootDir (FromClient _ (ReqInitialize req):_) =
35 fromMaybe (error "Couldn't find root dir") $ do
36 rootUri <- req ^. params .rootUri
38 rootDir _ = error "Couldn't find initialize request in session"
40 mapUris :: (Uri -> Uri) -> Event -> Event
43 FromClient t msg -> FromClient t (fromClientMsg msg)
44 FromServer t msg -> FromServer t (fromServerMsg msg)
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
58 fromServerMsg :: FromServerMessage -> FromServerMessage
59 fromServerMsg (ReqApplyWorkspaceEdit r) =
60 ReqApplyWorkspaceEdit $ params . edit .~ swapWorkspaceEdit (r ^. params . edit) $ r
62 fromServerMsg (NotPublishDiagnostics n) = NotPublishDiagnostics $ swapUri params n
64 fromServerMsg (RspDocumentSymbols r) =
65 let newSymbols = fmap (fmap (swapUri location)) $ r ^. result
66 in RspDocumentSymbols $ result .~ newSymbols $ r
68 fromServerMsg (RspRename r) =
69 let oldResult = r ^. result :: Maybe WorkspaceEdit
70 newResult = fmap swapWorkspaceEdit oldResult
71 in RspRename $ result .~ newResult $ r
75 swapWorkspaceEdit :: WorkspaceEdit -> WorkspaceEdit
77 let newDocChanges = fmap (fmap (swapUri textDocument)) $ e ^. documentChanges
78 newChanges = fmap (swapKeys f) $ e ^. changes
79 in WorkspaceEdit newChanges newDocChanges
81 swapKeys :: (Uri -> Uri) -> HM.HashMap Uri b -> HM.HashMap Uri b
82 swapKeys f = HM.foldlWithKey' (\acc k v -> HM.insert (f k) v acc) HM.empty
84 swapUri :: HasUri b Uri => Lens' a b -> a -> a
86 let newUri = f (x ^. lens . uri)
87 in (lens . uri) .~ newUri $ x
89 -- | Transforms rootUri/rootPath.
90 transformInit :: InitializeParams -> InitializeParams
92 let newRootUri = fmap f (x ^. rootUri)
94 fp <- T.unpack <$> x ^. rootPath
95 let uri = filePathToUri fp
96 T.pack <$> uriToFilePath (f uri)
97 in (rootUri .~ newRootUri) $ (rootPath .~ newRootPath) x