-
-
Notifications
You must be signed in to change notification settings - Fork 391
Resolve for explicit-imports #3682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
joyfulmantis
merged 19 commits into
haskell:master
from
joyfulmantis:explicit-imports-resolve
Jul 12, 2023
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
355e95c
Generic support for resolve in hls packages
joyfulmantis fb49c31
Add a new code action resolve helper that falls backs to commands
joyfulmantis d1d299b
add resolve capability set to hls-test-utils
joyfulmantis 735feca
Add code lens resolve support
joyfulmantis 89b71a2
Merge branch 'resolve-support' into explicit-imports-resolve
joyfulmantis 02262bd
Merge remote-tracking branch 'upstream/master' into explicit-imports-…
joyfulmantis 251c917
WIP
joyfulmantis ace6dcf
Finish up explicit imports and add tests
joyfulmantis af4c70f
michaelpj's suggestions
joyfulmantis 9fb0512
refine-imports --no-verify commit
joyfulmantis 20874be
Spelling mistakes and useWithStale for rule
joyfulmantis 5895174
Turn on -Werror and clean up var names
joyfulmantis c778379
Fix test warnings
joyfulmantis df8fda0
Move to maybeT and fix 8.10 flags issue
joyfulmantis b86b156
Added stale action test
joyfulmantis 905edd3
Address suggestions
joyfulmantis 57242a9
Merge branch 'master' into explicit-imports-resolve
joyfulmantis af46494
merge upstream/master
joyfulmantis 3f7f55c
fix merge mistake
joyfulmantis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
376 changes: 209 additions & 167 deletions
376
plugins/hls-explicit-imports-plugin/src/Ide/Plugin/ExplicitImports.hs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,40 @@ | ||
{-# LANGUAGE DisambiguateRecordFields #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE NamedFieldPuns #-} | ||
{-# LANGUAGE OverloadedLabels #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE TypeOperators #-} | ||
{-# LANGUAGE ViewPatterns #-} | ||
|
||
module Main | ||
( main | ||
) where | ||
|
||
import Data.Foldable (find, forM_) | ||
import Control.Lens ((^.)) | ||
import Data.Either.Extra | ||
import Data.Foldable (find) | ||
import Data.Row ((.+), (.==)) | ||
import Data.Text (Text) | ||
import qualified Data.Text as T | ||
import qualified Ide.Plugin.ExplicitImports as ExplicitImports | ||
import qualified Language.LSP.Protocol.Lens as L | ||
import Language.LSP.Protocol.Message | ||
import System.FilePath ((<.>), (</>)) | ||
import System.FilePath ((</>)) | ||
import Test.Hls | ||
|
||
explicitImportsPlugin :: PluginTestDescriptor ExplicitImports.Log | ||
explicitImportsPlugin = mkPluginTestDescriptor ExplicitImports.descriptor "explicitImports" | ||
|
||
longModule :: T.Text | ||
longModule = "F" <> T.replicate 80 "o" | ||
|
||
main :: IO () | ||
main = defaultTestRunner $ | ||
testGroup | ||
"Make imports explicit" | ||
[ codeActionGoldenTest "UsualCase" 3 0 | ||
[ codeActionAllGoldenTest "UsualCase" 3 0 | ||
, codeActionAllResolveGoldenTest "UsualCase" 3 0 | ||
, codeActionOnlyGoldenTest "OnlyThis" 3 0 | ||
, codeActionOnlyResolveGoldenTest "OnlyThis" 3 0 | ||
, codeLensGoldenTest "UsualCase" 0 | ||
, codeActionBreakFile "BreakFile" 4 0 | ||
, codeActionStaleAction "StaleAction" 4 0 | ||
, testCase "No CodeAction when exported" $ | ||
runSessionWithServer explicitImportsPlugin testDataDir $ do | ||
doc <- openDoc "Exported.hs" "haskell" | ||
|
@@ -65,12 +72,74 @@ main = defaultTestRunner $ | |
|
||
-- code action tests | ||
|
||
codeActionGoldenTest :: FilePath -> Int -> Int -> TestTree | ||
codeActionGoldenTest fp l c = goldenWithExplicitImports fp $ \doc -> do | ||
codeActionAllGoldenTest :: FilePath -> Int -> Int -> TestTree | ||
codeActionAllGoldenTest fp l c = goldenWithExplicitImports " code action" fp codeActionNoResolveCaps $ \doc -> do | ||
actions <- getCodeActions doc (pointRange l c) | ||
case find ((== Just "Make all imports explicit") . caTitle) actions of | ||
Just (InR x) -> executeCodeAction x | ||
_ -> liftIO $ assertFailure "Unable to find CodeAction" | ||
|
||
codeActionBreakFile :: FilePath -> Int -> Int -> TestTree | ||
codeActionBreakFile fp l c = goldenWithExplicitImports " code action" fp codeActionNoResolveCaps $ \doc -> do | ||
_ <- waitForDiagnostics | ||
changeDoc doc [edit] | ||
actions <- getCodeActions doc (pointRange l c) | ||
case find ((== Just "Make all imports explicit") . caTitle) actions of | ||
Just (InR x) -> executeCodeAction x | ||
_ -> liftIO $ assertFailure "Unable to find CodeAction" | ||
where edit = TextDocumentContentChangeEvent $ InL $ #range .== pointRange 2 21 | ||
.+ #rangeLength .== Nothing | ||
.+ #text .== "x" | ||
|
||
codeActionStaleAction :: FilePath -> Int -> Int -> TestTree | ||
codeActionStaleAction fp l c = goldenWithExplicitImports " code action" fp codeActionResolveCaps $ \doc -> do | ||
_ <- waitForDiagnostics | ||
actions <- getCodeActions doc (pointRange l c) | ||
changeDoc doc [edit] | ||
_ <- waitForDiagnostics | ||
case find ((== Just "Make this import explicit") . caTitle) actions of | ||
Just (InR x) -> | ||
maybeResolveCodeAction x >>= | ||
\case Just _ -> liftIO $ assertFailure "Code action still valid" | ||
Nothing -> pure () | ||
_ -> liftIO $ assertFailure "Unable to find CodeAction" | ||
where edit = TextDocumentContentChangeEvent $ InL $ #range .== Range (Position 6 0) (Position 6 0) | ||
.+ #rangeLength .== Nothing | ||
.+ #text .== "\ntesting = undefined" | ||
|
||
codeActionAllResolveGoldenTest :: FilePath -> Int -> Int -> TestTree | ||
codeActionAllResolveGoldenTest fp l c = goldenWithExplicitImports " code action resolve" fp codeActionResolveCaps $ \doc -> do | ||
actions <- getCodeActions doc (pointRange l c) | ||
Just (InR x) <- pure $ find ((== Just "Make all imports explicit") . caTitle) actions | ||
resolved <- resolveCodeAction x | ||
executeCodeAction resolved | ||
|
||
codeActionOnlyGoldenTest :: FilePath -> Int -> Int -> TestTree | ||
codeActionOnlyGoldenTest fp l c = goldenWithExplicitImports " code action" fp codeActionNoResolveCaps $ \doc -> do | ||
actions <- getCodeActions doc (pointRange l c) | ||
case find ((== Just "Make this import explicit") . caTitle) actions of | ||
Just (InR x) -> executeCodeAction x | ||
_ -> liftIO $ assertFailure "Unable to find CodeAction" | ||
|
||
codeActionOnlyResolveGoldenTest :: FilePath -> Int -> Int -> TestTree | ||
codeActionOnlyResolveGoldenTest fp l c = goldenWithExplicitImports " code action resolve" fp codeActionResolveCaps $ \doc -> do | ||
actions <- getCodeActions doc (pointRange l c) | ||
Just (InR x) <- pure $ find ((== Just "Make this import explicit") . caTitle) actions | ||
resolved <- resolveCodeAction x | ||
executeCodeAction resolved | ||
|
||
-- TODO: use the one from lsp-test once that's released | ||
resolveCodeAction :: CodeAction -> Session CodeAction | ||
joyfulmantis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
resolveCodeAction ca = do | ||
resolveResponse <- request SMethod_CodeActionResolve ca | ||
Right resolved <- pure $ resolveResponse ^. L.result | ||
joyfulmantis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pure resolved | ||
|
||
maybeResolveCodeAction :: CodeAction -> Session (Maybe CodeAction) | ||
maybeResolveCodeAction ca = do | ||
resolveResponse <- request SMethod_CodeActionResolve ca | ||
let resolved = resolveResponse ^. L.result | ||
pure $ eitherToMaybe resolved | ||
|
||
caTitle :: (Command |? CodeAction) -> Maybe Text | ||
caTitle (InR CodeAction {_title}) = Just _title | ||
|
@@ -79,18 +148,17 @@ caTitle _ = Nothing | |
-- code lens tests | ||
|
||
codeLensGoldenTest :: FilePath -> Int -> TestTree | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe these things could also go in |
||
codeLensGoldenTest fp codeLensIdx = goldenWithExplicitImports fp $ \doc -> do | ||
codeLens <- (!! codeLensIdx) <$> getCodeLensesBy isExplicitImports doc | ||
mapM_ executeCmd | ||
[c | CodeLens{_command = Just c} <- [codeLens]] | ||
|
||
getCodeLensesBy :: (CodeLens -> Bool) -> TextDocumentIdentifier -> Session [CodeLens] | ||
getCodeLensesBy f doc = filter f <$> getCodeLenses doc | ||
|
||
isExplicitImports :: CodeLens -> Bool | ||
isExplicitImports (CodeLens _ (Just (Command _ cmd _)) _) | ||
| ":explicitImports:" `T.isInfixOf` cmd = True | ||
isExplicitImports _ = False | ||
codeLensGoldenTest fp _ = goldenWithExplicitImports " code lens" fp codeActionNoResolveCaps $ \doc -> do | ||
(codeLens: _) <- getCodeLenses doc | ||
CodeLens {_command = Just c} <- resolveCodeLens codeLens | ||
executeCmd c | ||
|
||
-- TODO: use the one from lsp-test once that's released | ||
resolveCodeLens :: CodeLens -> Session CodeLens | ||
resolveCodeLens cl = do | ||
resolveResponse <- request SMethod_CodeLensResolve cl | ||
Right resolved <- pure $ resolveResponse ^. L.result | ||
pure resolved | ||
|
||
-- Execute command and wait for result | ||
executeCmd :: Command -> Session () | ||
|
@@ -102,8 +170,8 @@ executeCmd cmd = do | |
|
||
-- helpers | ||
|
||
goldenWithExplicitImports :: FilePath -> (TextDocumentIdentifier -> Session ()) -> TestTree | ||
goldenWithExplicitImports fp = goldenWithHaskellDoc explicitImportsPlugin (fp <> " (golden)") testDataDir fp "expected" "hs" | ||
goldenWithExplicitImports :: String -> FilePath -> ClientCapabilities -> (TextDocumentIdentifier -> Session ()) -> TestTree | ||
goldenWithExplicitImports title fp caps = goldenWithHaskellAndCaps caps explicitImportsPlugin (fp <> title <> " (golden)") testDataDir fp "expected" "hs" | ||
|
||
testDataDir :: String | ||
testDataDir = "test" </> "testdata" | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module B where | ||
|
||
b1 :: String | ||
b1 = "b1" | ||
|
||
b2 :: String | ||
b2 = "b2" |
6 changes: 6 additions & 0 deletions
6
plugins/hls-explicit-imports-plugin/test/testdata/BreakFile.expected.hs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{-# OPTIONS_GHC -Wall #-} | ||
module BreakFile whexe | ||
|
||
import A ( a1 ) | ||
|
||
main = putStrLn $ "hello " ++ a1 |
6 changes: 6 additions & 0 deletions
6
plugins/hls-explicit-imports-plugin/test/testdata/BreakFile.hs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{-# OPTIONS_GHC -Wall #-} | ||
module BreakFile where | ||
|
||
import A | ||
|
||
main = putStrLn $ "hello " ++ a1 |
7 changes: 7 additions & 0 deletions
7
plugins/hls-explicit-imports-plugin/test/testdata/OnlyThis.expected.hs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module OnlyThis where | ||
|
||
import A ( a1 ) | ||
import B | ||
|
||
main :: IO () | ||
main = putStrLn $ "hello " ++ a1 ++ b1 |
7 changes: 7 additions & 0 deletions
7
plugins/hls-explicit-imports-plugin/test/testdata/OnlyThis.hs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module OnlyThis where | ||
|
||
import A | ||
import B | ||
|
||
main :: IO () | ||
main = putStrLn $ "hello " ++ a1 ++ b1 |
8 changes: 8 additions & 0 deletions
8
plugins/hls-explicit-imports-plugin/test/testdata/StaleAction.expected.hs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{-# OPTIONS_GHC -Wall #-} | ||
module StaleAction where | ||
|
||
import A | ||
|
||
main = putStrLn $ "hello " ++ a1 | ||
|
||
testing = undefined |
6 changes: 6 additions & 0 deletions
6
plugins/hls-explicit-imports-plugin/test/testdata/StaleAction.hs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{-# OPTIONS_GHC -Wall #-} | ||
module StaleAction where | ||
|
||
import A | ||
|
||
main = putStrLn $ "hello " ++ a1 |
2 changes: 1 addition & 1 deletion
2
plugins/hls-explicit-imports-plugin/test/testdata/UsualCase.expected.hs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module Main where | ||
module UsualCase where | ||
|
||
import A ( a1 ) | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module Main where | ||
module UsualCase where | ||
|
||
import A | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
|
||
cradle: | ||
direct: | ||
arguments: | ||
- OnlyThis.hs | ||
- StaleAction.hs | ||
- UsualCase.hs | ||
- Exported.hs | ||
- A.hs | ||
- B.hs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test doesn't look quite right to me: shouldn't we be getting the code actions, picking one, then editing the file, then resolving the action we picked before and seeing it fail?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this test causes a parsing error in the file and then gets a code action and executes it. Because we are using "useWithStale" we can still operate on a file that has a parse fail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see, that's slightly different then. I think it might also be interesting to put an edit in between getting the actions and resolving one. Potentially one that doesn't cause a parse error, so it just mixes up the uniques, which should cause a failure still.