Start work on moving to new session file format
[lsp-test.git] / src / Language / Haskell / LSP / Test / Files.hs
index f59551a3719ae3e2dcf9f24b321ef7aa569eb1c1..9f78ec1855a4c356da81846a0d2f9184992a293a 100644 (file)
@@ -7,68 +7,76 @@ module Language.Haskell.LSP.Test.Files
   )
 where
 
+import           Language.Haskell.LSP.Capture
 import           Language.Haskell.LSP.Types hiding ( error )
+import           Language.Haskell.LSP.Messages
 import           Control.Lens
 import           Data.Aeson
-import           Data.Aeson.Types
 import qualified Data.ByteString.Lazy.Char8    as B
+import qualified Data.HashMap.Strict           as HM
 import qualified Data.Text                     as T
-import qualified Data.HashMap.Strict           as HashMap
 import           Data.Maybe
 import           System.Directory
 import           System.FilePath
 
-swapFiles :: FilePath -> FilePath -> [B.ByteString] -> IO [B.ByteString]
-swapFiles recBaseDir relCurBaseDir msgs = do
+swapFiles :: FilePath -> [Event] -> IO [Event]
+swapFiles relCurBaseDir msgs = do
+  let capturedBaseDir = rootDir msgs
+
   curBaseDir <- (</> relCurBaseDir) <$> getCurrentDirectory
   let transform uri =
         let fp = fromMaybe (error "Couldn't transform uri") (uriToFilePath uri)
-            newFp = curBaseDir </> makeRelative recBaseDir fp
+            newFp = curBaseDir </> makeRelative capturedBaseDir fp
           in filePathToUri newFp
-      newMsgs = map (mapUris transform) msgs :: [B.ByteString]
+      newMsgs = map (mapUris transform) msgs
 
   return newMsgs
 
-rootDir :: [B.ByteString] -> FilePath
-rootDir msgs = fromMaybe (error "Couldn't find root dir") $ do
-  req <- decode (head msgs) :: Maybe InitializeRequest
+rootDir :: [Event] -> FilePath
+rootDir (FromClient _ (ReqInitialize req):_) =
+  fromMaybe (error "Couldn't find root dir") $ do
     rootUri <- req ^. params .rootUri
     uriToFilePath rootUri
+rootDir _ = error "Couldn't find initialize request in session"
 
-mapUris :: (Uri -> Uri) -> B.ByteString -> B.ByteString
-mapUris f msg =
-  case decode msg :: Maybe Object of
-    Just obj -> encode $ HashMap.map (mapValue f) obj
-    Nothing -> error "Couldn't decode message"
+mapUris :: (Uri -> Uri) -> Event -> Event
+mapUris f event =
+  case event of
+    FromClient t msg -> FromClient t (fromClientMsg msg)
+    FromServer t msg -> FromServer t (fromServerMsg msg)
 
   where
-    mapValue :: (Uri -> Uri) -> Value -> Value
-    mapValue f x = case parse parseJSON x :: Result VersionedTextDocumentIdentifier of
-      Success doc -> transform doc
-      Error _ -> case parse parseJSON x :: Result TextDocumentIdentifier of
-        Success doc -> transform doc
-        Error _ -> case parse parseJSON x :: Result InitializeParams of
-          Success params -> transformInit params
-          Error _ -> case parse parseJSON x :: Result Object of
-            Success obj -> Object $ HashMap.map (mapValue f) obj
-            Error _ -> x
+    fromClientMsg (NotDidOpenTextDocument n) = NotDidOpenTextDocument $ swapUri (params . textDocument) n
+    fromClientMsg (NotDidChangeTextDocument n) = NotDidChangeTextDocument $ swapUri (params . textDocument) n
+    fromClientMsg (NotWillSaveTextDocument n) = NotWillSaveTextDocument $ swapUri (params . textDocument) n
+    fromClientMsg (NotDidSaveTextDocument n) = NotDidSaveTextDocument $ swapUri (params . textDocument) n
+    fromClientMsg (NotDidCloseTextDocument n) = NotDidCloseTextDocument $ swapUri (params . textDocument) n
+    fromClientMsg (ReqInitialize r) = ReqInitialize $ params .~ (transformInit (r ^. params)) $ r
+    fromClientMsg x = x
+
+    fromServerMsg :: FromServerMessage -> FromServerMessage
+    fromServerMsg (ReqApplyWorkspaceEdit r) =
+      let newDocChanges = fmap (fmap (swapUri textDocument)) $ r ^. params . edit . documentChanges
+          r1 = (params . edit . documentChanges) .~ newDocChanges $ r
+          newChanges = fmap (swapKeys f) $ r1 ^. params . edit . changes
+          r2 = (params . edit . changes) .~ newChanges $ r1
+      in ReqApplyWorkspaceEdit r2
+    fromServerMsg x = x
 
-    -- parsing with just JSON
-    -- mapValueWithKey :: (Uri -> Uri) -> T.Text -> Value -> Value
-    -- mapValueWithKey f "uri" (String s) = fromMaybe (error "Couldn't convert uri") $ do
-    --   let uri = filePathToUri $ T.unpack s
-    --   String <$> (fmap T.pack (uriToFilePath $ f uri))
-    -- mapValueWithKey f _ (Array xs) = Array $ fmap (mapValue f) xs
-    -- mapValueWithKey f _ (Object x) = Object $ HashMap.mapWithKey (mapValueWithKey f) x
+    swapKeys :: (Uri -> Uri) -> HM.HashMap Uri b -> HM.HashMap Uri b
+    swapKeys f = HM.foldlWithKey' (\acc k v -> HM.insert (f k) v acc) HM.empty
 
-    transform x = toJSON $ x & uri .~ f (x ^. uri)
+    swapUri :: HasUri b Uri => Lens' a b -> a -> a
+    swapUri lens x =
+      let newUri = f (x ^. lens . uri)
+        in (lens . uri) .~ newUri $ x
 
-    -- transform rootUri/rootPath
-    transformInit :: InitializeParams -> Value
+    -- | Transforms rootUri/rootPath.
+    transformInit :: InitializeParams -> InitializeParams
     transformInit x =
       let newRootUri = fmap f (x ^. rootUri)
           newRootPath = do
             fp <- T.unpack <$> x ^. rootPath
             let uri = filePathToUri fp
             T.pack <$> uriToFilePath (f uri)
-        in toJSON $ (rootUri .~ newRootUri) $ (rootPath .~ newRootPath) x
+        in (rootUri .~ newRootUri) $ (rootPath .~ newRootPath) x