-
-
Notifications
You must be signed in to change notification settings - Fork 391
Not suggest exported imports #2329
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
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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,89 @@ | ||
{-# LANGUAGE DisambiguateRecordFields #-} | ||
{-# LANGUAGE NamedFieldPuns #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE TypeOperators #-} | ||
{-# LANGUAGE ViewPatterns #-} | ||
|
||
module Main | ||
( main | ||
) where | ||
|
||
import Data.Foldable (find, forM_) | ||
import Data.Text (Text) | ||
import qualified Data.Text as T | ||
import qualified Ide.Plugin.ExplicitImports as ExplicitImports | ||
import System.FilePath ((<.>), (</>)) | ||
import Test.Hls | ||
|
||
explicitImportsPlugin :: PluginDescriptor IdeState | ||
explicitImportsPlugin = ExplicitImports.descriptor "explicitImports" | ||
|
||
|
||
main :: IO () | ||
main = defaultTestRunner $ | ||
testGroup | ||
"Refine Imports" | ||
[ codeActionGoldenTest "UsualCase" 3 0 | ||
, codeLensGoldenTest "UsualCase" 0 | ||
, testCase "No CodeAction when exported" $ | ||
runSessionWithServer explicitImportsPlugin testDataDir $ do | ||
doc <- openDoc "Exported.hs" "haskell" | ||
action <- getCodeActions doc (pointRange 3 0) | ||
liftIO $ action @?= [] | ||
, testCase "No CodeLens when exported" $ | ||
runSessionWithServer explicitImportsPlugin testDataDir $ do | ||
doc <- openDoc "Exported.hs" "haskell" | ||
lenses <- getCodeLenses doc | ||
liftIO $ lenses @?= [] | ||
] | ||
|
||
-- code action tests | ||
|
||
codeActionGoldenTest :: FilePath -> Int -> Int -> TestTree | ||
codeActionGoldenTest fp l c = goldenWithExplicitImports fp $ \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" | ||
|
||
caTitle :: (Command |? CodeAction) -> Maybe Text | ||
caTitle (InR CodeAction {_title}) = Just _title | ||
caTitle _ = Nothing | ||
|
||
-- code lens tests | ||
|
||
codeLensGoldenTest :: FilePath -> Int -> TestTree | ||
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 | ||
|
||
-- Execute command and wait for result | ||
executeCmd :: Command -> Session () | ||
executeCmd cmd = do | ||
executeCommand cmd | ||
_resp <- skipManyTill anyMessage (message SWorkspaceApplyEdit) | ||
-- liftIO $ print _resp | ||
return () | ||
|
||
-- helpers | ||
|
||
goldenWithExplicitImports :: FilePath -> (TextDocumentIdentifier -> Session ()) -> TestTree | ||
goldenWithExplicitImports fp = goldenWithHaskellDoc explicitImportsPlugin (fp <> " (golden)") testDataDir fp "expected" "hs" | ||
|
||
testDataDir :: String | ||
testDataDir = "test" </> "testdata" | ||
|
||
pointRange :: Int -> Int -> Range | ||
pointRange | ||
(subtract 1 -> line) | ||
(subtract 1 -> col) = | ||
Range (Position line col) (Position line $ col + 1) |
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 A where | ||
|
||
a1 :: String | ||
a1 = "a1" | ||
|
||
a2 :: String | ||
a2 = "a2" |
6 changes: 6 additions & 0 deletions
6
plugins/hls-explicit-imports-plugin/test/testdata/Exported.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 @@ | ||
module Exported (module A) where | ||
|
||
import A | ||
|
||
main :: IO () | ||
main = putStrLn $ "hello " ++ a1 |
6 changes: 6 additions & 0 deletions
6
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module Main where | ||
|
||
import A ( a1 ) | ||
|
||
main :: IO () | ||
main = putStrLn $ "hello " ++ a1 |
6 changes: 6 additions & 0 deletions
6
plugins/hls-explicit-imports-plugin/test/testdata/UsualCase.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 @@ | ||
module Main where | ||
|
||
import A | ||
|
||
main :: IO () | ||
main = putStrLn $ "hello " ++ 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
cradle: | ||
direct: | ||
arguments: | ||
- UsualCase.hs | ||
- Exported.hs | ||
- A.hs |
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.
Wow the plugin had no test suite??? Many thanks for adding it, a great contribution in its own
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.
It would be great to add it to the github workflow to run them in ci, adding an entry like
haskell-language-server/.github/workflows/test.yml
Lines 207 to 209 in a0cd84b
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.
OK. I'll fix.