Merge branch 'master' of https://github.com/Bubba/haskell-lsp-test
authorLuke Lau <luke_lau@icloud.com>
Tue, 14 Aug 2018 21:34:58 +0000 (22:34 +0100)
committerLuke Lau <luke_lau@icloud.com>
Tue, 14 Aug 2018 21:34:58 +0000 (22:34 +0100)
.gitignore
.travis.yml
ChangeLog.md
lsp-test.cabal
src/Language/Haskell/LSP/Test.hs
src/Language/Haskell/LSP/Test/Server.hs
test/Test.hs
test/data/renamePass/session.log

index 21a619508fe5119a0b25209eaed928395ab3196d..b057681729a1b050fbeb1191b01f9e355558d9fa 100644 (file)
@@ -15,3 +15,6 @@ example/.ghc.environment.*
 
 # used for rerunning failed hspec tests
 .hspec-failures
+
+# downloaded by .download-hie.sh on travis
+hie
index 25a5e69462fbd1bd4f16fa8b534cd4fd595cefd8..bb1cd2cd206fef916fbdd89ac88cb3eeb610ceec 100644 (file)
@@ -1,14 +1,15 @@
-# language: haskell
-# ghc:
-#   - "8.2.2"
-#   - "8.4.2"
-language: c
+language: haskell
+ghc:
+  - "8.2.2"
+  - "8.4.3"
 
 sudo: false
 
 # Caching so the next build will be fast too.
 cache:
   directories:
+  - dist
+  - dist-newstyle
   - $HOME/.stack
   - $HOME/haskell-ide-engine/.stack-work
 
@@ -18,31 +19,37 @@ addons:
     - npm
 
 before_install:
-  # Download and unpack the stack executable
+  - printenv
   - mkdir -p ~/.local/bin
   - export PATH=$HOME/.local/bin:$PATH
   - travis_retry curl -L https://get.haskellstack.org/stable/linux-x86_64.tar.gz | tar xz --wildcards --strip-components=1 -C ~/.local/bin '*/stack'
-    
-  - cd $HOME
-  # It's ok to fail
-  - cd haskell-ide-engine
+  - stack config set system-ghc --global true
+  # - export PATH=/opt/ghc/8.4.3/bin:$PATH
+  # - sh .download-hie.sh
+  # - export PATH=$TRAVIS_BUILD_DIR:$PATH
+  # - hie version
+  - mkdir -p haskell-ide-engine
+  - cd $HOME/haskell-ide-engine
   - git init
-  - git remote add origin https://github.com/Bubba/haskell-ide-engine.git
-  - git pull origin heirarchal-document-symbols
+  - git remote add origin https://github.com/haskell/haskell-ide-engine.git
+  - git pull origin master
   - git submodule init
   - git submodule sync
-  - git submodule update
-  # - cabal new-update
-  # - cabal new-configure
-  # - cabal new-build
-  # - export PATH=$HOME/haskell-ide-engine/dist-newstyle/build/*/ghc-*/haskell-ide-engine-*/x/hie/build:$PATH
-  - stack --no-terminal --skip-ghc-check install -j2
+  - git submodule update --init
+  - stack --no-terminal --skip-ghc-check -j2 install --stack-yaml=stack-$TRAVIS_HASKELL_VERSION.yaml
   - stack exec hoogle generate
   - cd $TRAVIS_BUILD_DIR
+  - hie --version
+  # - git submodule init
+  # - git submodule sync
+  # - git submodule update
+  # - cabal new-configure
+  # - cabal new-build -j2
+  # - export PATH=$HOME/haskell-ide-engine/dist-newstyle/build/*/ghc-*/haskell-ide-engine-*/c/hie/build/hie/hie:$PATH
   - npm update
   - npm i -g javascript-typescript-langserver
 
 script:
- - stack test
+  - stack test --no-terminal --skip-ghc-check -j2
   # - cabal configure --enable-tests
-  # - cabal test -j2 --show-details=streaming
+  # - cabal test --show-details=streaming
index ca5db5f1d8d8196dc99ce2df5926be64c5c45202..5a87f0d3dc2a79d5d0f404264f68aede25ee5104 100644 (file)
@@ -1,5 +1,10 @@
 # Revision history for lsp-test
 
+## 0.2.1.0 -- 2018-08-14
+
+* Add getCodeActions
+* Add getCurrentDiagnostics
+
 ## 0.2.0.0 -- 2018-08-06
 
 * Update to haskell-lsp 0.6.0.0
index 9c422d4d50a9246f4ded796f65ab83eafb8b2c00..85ade4ce95df79fcc679c4b6062980b9bc00e8bf 100644 (file)
@@ -1,5 +1,5 @@
 name:                lsp-test
-version:             0.2.0.0
+version:             0.2.1.0
 synopsis:            Functional test framework for LSP servers.
 description:
   A test framework for writing tests against 
@@ -78,9 +78,9 @@ test-suite tests
   build-depends:       base >= 4.7 && < 5
                      , hspec
                      , lens
-                     , data-default
-                     , haskell-lsp >= 0.6
+                     , haskell-lsp
                      , lsp-test
+                     , data-default
                      , aeson
                      , unordered-containers
                      , text
index 04fcc21a7f9510a10f4acc0c0eb012a24321db21..ec290ff9833b0d79af1e861f825d7850a407caf3 100644 (file)
@@ -52,9 +52,11 @@ module Language.Haskell.LSP.Test
   , waitForDiagnostics
   , waitForDiagnosticsSource
   , noDiagnostics
+  , getCurrentDiagnostics
   -- ** Commands
   , executeCommand
   -- ** Code Actions
+  , getCodeActions
   , getAllCodeActions
   , executeCodeAction
   -- ** Completions
@@ -358,15 +360,24 @@ getDocumentSymbols doc = do
     Just (DSSymbolInformation (List xs)) -> return (Right xs)
     Nothing -> Prelude.error "No result and no error in DocumentSymbolsResponse"
 
+-- | Returns the code actions in the specified range.
+getCodeActions :: TextDocumentIdentifier -> Range -> Session [CAResult]
+getCodeActions doc range = do
+  ctx <- getCodeActionContext doc
+  rsp <- request TextDocumentCodeAction (CodeActionParams doc range ctx)
+
+  case rsp ^. result of
+    Just (List xs) -> return xs
+    _ -> throw (UnexpectedResponseError (rsp ^. LSP.id) (fromJust $ rsp ^. LSP.error))
+
 -- | Returns all the code actions in a document by 
 -- querying the code actions at each of the current 
 -- diagnostics' positions.
 getAllCodeActions :: TextDocumentIdentifier -> Session [CAResult]
 getAllCodeActions doc = do
-  curDiags <- fromMaybe [] . Map.lookup (doc ^. uri) . curDiagnostics <$> get
-  let ctx = CodeActionContext (List curDiags) Nothing
+  ctx <- getCodeActionContext doc
 
-  foldM (go ctx) [] curDiags
+  foldM (go ctx) [] =<< getCurrentDiagnostics doc
 
   where
     go :: CodeActionContext -> [CAResult] -> Diagnostic -> Session [CAResult]
@@ -379,6 +390,16 @@ getAllCodeActions doc = do
           let Just (List cmdOrCAs) = mRes
             in return (acc ++ cmdOrCAs)
 
+getCodeActionContext :: TextDocumentIdentifier -> Session CodeActionContext
+getCodeActionContext doc = do
+  curDiags <- getCurrentDiagnostics doc
+  return $ CodeActionContext (List curDiags) Nothing
+
+-- | Returns the current diagnostics that have been sent to the client.
+-- Note that this does not wait for more to come in.
+getCurrentDiagnostics :: TextDocumentIdentifier -> Session [Diagnostic]
+getCurrentDiagnostics doc = fromMaybe [] . Map.lookup (doc ^. uri) . curDiagnostics <$> get
+
 -- | Executes a command.
 executeCommand :: Command -> Session ()
 executeCommand cmd = do
index 7d00f2382900e454b5b67b32de1a33ffc71a4caa..bd5bdb959f670652a5e910d5d0942b43fcb0c494 100644 (file)
@@ -1,6 +1,7 @@
 module Language.Haskell.LSP.Test.Server (withServer) where
 
 import Control.Concurrent
+import Control.Exception
 import Control.Monad
 import Language.Haskell.LSP.Test.Compat
 import System.IO
@@ -21,8 +22,6 @@ withServer serverExe logStdErr f = do
 
   pid <- getProcessID serverProc
 
-  result <- f serverIn serverOut pid
-
+  finally (f serverIn serverOut pid) $ do
     killThread errSinkThread
     terminateProcess serverProc
-  return result
index d524ee4ffe15c666c8feaabdd5c518e804782a58..88c6852eadd43d885bc5cef1217bee08cffe18e8 100644 (file)
@@ -176,6 +176,13 @@ main = hspec $ do
         liftIO $ contents `shouldBe` "main :: IO Int\nmain = return 42\n"
         noDiagnostics
 
+  describe "getCodeActions" $
+    it "works" $ runSession "hie" fullCaps "test/data/refactor" $ do
+      doc <- openDoc "Main.hs" "haskell"
+      waitForDiagnostics
+      [CACodeAction action] <- getCodeActions doc (Range (Position 1 14) (Position 1 18))
+      liftIO $ action ^. title `shouldBe` "Apply hint:Redundant bracket"
+
   describe "getAllCodeActions" $
     it "works" $ runSession "hie --lsp" fullCaps "test/data/refactor" $ do
       doc <- openDoc "Main.hs" "haskell"
index 08ee03793d6ad0afebbe3eaf0e3122ecc8526587..e03f29c3382d25cb90a83f7855f2f779f8c09130 100644 (file)
@@ -9,7 +9,7 @@
 {"tag":"FromServer","contents":["2018-06-03T04:08:39.524239Z",{"tag":"NotPublishDiagnostics","contents":{"jsonrpc":"2.0","params":{"uri":"file:///Users/luke/Desktop/simple.hs","diagnostics":[]},"method":"textDocument/publishDiagnostics"}}]}
 {"tag":"FromServer","contents":["2018-06-03T04:08:39.714012Z",{"tag":"NotPublishDiagnostics","contents":{"jsonrpc":"2.0","params":{"uri":"file:///Users/luke/Desktop/simple.hs","diagnostics":[]},"method":"textDocument/publishDiagnostics"}}]}
 {"tag":"FromClient","contents":["2018-06-03T04:08:40.844374Z",{"tag":"ReqDocumentSymbols","contents":{"jsonrpc":"2.0","params":{"textDocument":{"uri":"file:///Users/luke/Desktop/simple.hs"}},"method":"textDocument/documentSymbol","id":25}}]}
-{"tag":"FromServer","contents":["2018-06-03T04:08:40.859268Z",{"tag":"RspDocumentSymbols","contents":{"result":[{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":3,"character":0},"end":{"line":3,"character":4}}},"kind":12,"name":"main"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":7,"character":5},"end":{"line":7,"character":9}}},"kind":5,"name":"Item"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":8,"character":5},"end":{"line":8,"character":10}}},"kind":5,"name":"Items"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":10,"character":5},"end":{"line":10,"character":12}}},"kind":5,"name":"Command"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":10,"character":15},"end":{"line":10,"character":19}}},"kind":9,"containerName":"Command","name":"Quit"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":11,"character":15},"end":{"line":11,"character":27}}},"kind":9,"containerName":"Command","name":"DisplayItems"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":12,"character":15},"end":{"line":12,"character":22}}},"kind":9,"containerName":"Command","name":"AddItem"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":13,"character":15},"end":{"line":13,"character":25}}},"kind":9,"containerName":"Command","name":"RemoveItem"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":14,"character":15},"end":{"line":14,"character":19}}},"kind":9,"containerName":"Command","name":"Help"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":16,"character":5},"end":{"line":16,"character":10}}},"kind":5,"name":"Error"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":19,"character":0},"end":{"line":19,"character":12}}},"kind":12,"name":"parseCommand"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":28,"character":0},"end":{"line":28,"character":7}}},"kind":12,"name":"addItem"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":31,"character":0},"end":{"line":31,"character":12}}},"kind":12,"name":"displayItems"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":34,"character":0},"end":{"line":34,"character":10}}},"kind":12,"name":"removeItem"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":37,"character":9},"end":{"line":37,"character":14}}},"kind":6,"containerName":"removeItem","name":"front"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":37,"character":16},"end":{"line":37,"character":20}}},"kind":6,"containerName":"removeItem","name":"back"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":38,"character":8},"end":{"line":38,"character":14}}},"kind":12,"containerName":"removeItem","name":"result"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":41,"character":0},"end":{"line":41,"character":16}}},"kind":12,"name":"interactWithUser"}],"jsonrpc":"2.0","id":25}}]}
+{"tag":"FromServer","contents":["2018-06-03T04:08:40.859268Z",{"tag":"RspDocumentSymbols","contents":{"result":[{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":3,"character":0},"end":{"line":3,"character":4}}},"kind":12,"name":"main"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":7,"character":5},"end":{"line":7,"character":9}}},"kind":5,"name":"Item"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":8,"character":5},"end":{"line":8,"character":10}}},"kind":5,"name":"Items"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":10,"character":5},"end":{"line":10,"character":12}}},"kind":5,"name":"Command"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":10,"character":15},"end":{"line":10,"character":19}}},"kind":9,"containerName":"Command","name":"Quit"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":11,"character":15},"end":{"line":11,"character":27}}},"kind":9,"containerName":"Command","name":"DisplayItems"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":12,"character":15},"end":{"line":12,"character":22}}},"kind":9,"containerName":"Command","name":"AddItem"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":13,"character":15},"end":{"line":13,"character":25}}},"kind":9,"containerName":"Command","name":"RemoveItem"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":14,"character":15},"end":{"line":14,"character":19}}},"kind":9,"containerName":"Command","name":"Help"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":16,"character":5},"end":{"line":16,"character":10}}},"kind":5,"name":"Error"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":19,"character":0},"end":{"line":19,"character":12}}},"kind":12,"name":"parseCommand"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":28,"character":0},"end":{"line":28,"character":7}}},"kind":12,"name":"addItem"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":31,"character":0},"end":{"line":31,"character":12}}},"kind":12,"name":"displayItems"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":34,"character":0},"end":{"line":34,"character":10}}},"kind":12,"name":"removeItem"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":37,"character":9},"end":{"line":37,"character":14}}},"kind":13,"containerName":"removeItem","name":"front"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":37,"character":16},"end":{"line":37,"character":20}}},"kind":13,"containerName":"removeItem","name":"back"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":38,"character":8},"end":{"line":38,"character":14}}},"kind":12,"containerName":"removeItem","name":"result"},{"location":{"uri":"file:///Users/luke/Desktop/simple.hs","range":{"start":{"line":41,"character":0},"end":{"line":41,"character":16}}},"kind":12,"name":"interactWithUser"}],"jsonrpc":"2.0","id":25}}]}
 {"tag":"FromClient","contents":["2018-06-03T04:08:46.24927Z",{"tag":"ReqRename","contents":{"jsonrpc":"2.0","params":{"newName":"arseCommand","textDocument":{"uri":"file:///Users/luke/Desktop/simple.hs"},"position":{"line":19,"character":0}},"method":"textDocument/rename","id":32}}]}
 {"tag":"FromServer","contents":["2018-06-03T04:08:46.528715Z",{"tag":"RspRename","contents":{"result":{"changes":{"file:///Users/luke/Desktop/simple.hs":[{"range":{"start":{"line":43,"character":0},"end":{"line":43,"character":27}},"newText":"  case arseCommand line of"},{"range":{"start":{"line":18,"character":0},"end":{"line":19,"character":38}},"newText":"arseCommand :: String -> Either Error Command\narseCommand line = case words line of"}]}},"jsonrpc":"2.0","id":32}}]}
 {"tag":"FromClient","contents":["2018-06-03T04:08:48.300837Z",{"tag":"NotDidChangeTextDocument","contents":{"jsonrpc":"2.0","params":{"contentChanges":[{"text":"module Main where\n\nmain :: IO ()\nmain = do\n  let initialList = []\n  interactWithUser initialList\n\ntype Item = String\ntype Items = [Item]\n\ndata Command = Quit\n             | DisplayItems\n             | AddItem String\n             | RemoveItem Int\n             | Help\n\ntype Error = String\n\narseCommand :: String -> Either Error Command\narseCommand line = case words line of\n  [\"quit\"] -> Right Quit\n  [\"items\"] -> Right DisplayItems\n  \"add\" : item -> Right $ AddItem $ unwords item\n  \"remove\" : i -> Right $ RemoveItem $ read $ unwords i\n  [\"help\"] -> Right Help\n  _ -> Left \"Unknown command\"\n\naddItem :: Item -> Items -> Items\naddItem = (:)\n\ndisplayItems :: Items -> String\ndisplayItems = unlines . map (\"- \" ++)\n\nremoveItem :: Int -> Items -> Either Error Items\nremoveItem i items\n  | i < 0 || i >= length items = Left \"Out of range\"\n  | otherwise = Right result\n  where (front, back) = splitAt (i + 1) items\n        result = init front ++ back\n\ninteractWithUser :: Items -> IO ()\ninteractWithUser items = do\n  line <- getLine\n  case arseCommand line of\n    Right DisplayItems -> do\n      putStrLn $ displayItems items\n      interactWithUser items\n\n    Right (AddItem item) -> do\n      let newItems = addItem item items\n      putStrLn \"Added\"\n      interactWithUser newItems\n\n    Right (RemoveItem i) ->\n      case removeItem i items of\n        Right newItems -> do\n          putStrLn $ \"Removed \" ++ items !! i\n          interactWithUser newItems\n        Left err -> do\n          putStrLn err\n          interactWithUser items\n\n\n    Right Quit -> return ()\n\n    Right Help -> do\n      putStrLn \"Commands:\"\n      putStrLn \"help\"\n      putStrLn \"items\"\n      putStrLn \"add\"\n      putStrLn \"quit\"\n      interactWithUser items\n\n    Left err -> do\n      putStrLn $ \"Error: \" ++ err\n      interactWithUser items\n"}],"textDocument":{"uri":"file:///Users/luke/Desktop/simple.hs","version":1}},"method":"textDocument/didChange"}}]}