Skip to content

[plugin][refine-import] Do not refine to Internal module #1832

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Control.Monad.IO.Class (liftIO)
import Data.Aeson.Types
import qualified Data.HashMap.Strict as HashMap
import Data.IORef (readIORef)
import Data.List (intercalate)
import Data.List (intercalate, isSuffixOf)
import qualified Data.Map.Strict as Map
import Data.Maybe (catMaybes, fromMaybe)
import qualified Data.Set as S
Expand Down Expand Up @@ -187,13 +187,20 @@ refineImportsRule = define $ \RefineImports nfp -> do
-- for modules/symbols actually got used
(imports, mbMinImports) <- liftIO $ extractMinimalImports hsc tmr

-- Internal module is the convention that no module should import
-- directly
let notContainInternalModule :: [AvailInfo] -> Bool
notContainInternalModule = not . any (\a ->
"Internal" `isSuffixOf` prettyPrint (availName a))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't be too hard to add configuration for the internal suffix - @berberman and @isovector are the experts

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, an example of Properties in type lenses plugin:

descriptor :: PluginId -> PluginDescriptor IdeState
descriptor plId =
(defaultPluginDescriptor plId)
{ pluginHandlers = mkPluginHandler STextDocumentCodeLens codeLensProvider
, pluginCommands = [PluginCommand (CommandId typeLensCommandId) "adds a signature" commandHandler]
, pluginRules = rules
, pluginConfigDescriptor = defaultConfigDescriptor {configCustomConfig = mkCustomConfig properties}
}
properties :: Properties '[ 'PropertyKey "mode" ('TEnum Mode)]
properties = emptyProperties
& defineEnumProperty #mode "Control how type lenses are shown"
[ (Always, "Always displays type lenses of global bindings")
, (Exported, "Only display type lenses of exported global bindings")
, (Diagnostics, "Follows error messages produced by GHC about missing signatures")
] Always
codeLensProvider ::
IdeState ->
PluginId ->
CodeLensParams ->
LSP.LspM Config (Either ResponseError (List CodeLens))
codeLensProvider ideState pId CodeLensParams{_textDocument = TextDocumentIdentifier uri} = do
mode <- usePropertyLsp #mode pId properties

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rayshih had the example above helped to implement the config of the internal suffix?


let filterByImport
:: LImportDecl GhcRn
-> Map.Map ModuleName [AvailInfo]
-> Maybe (Map.Map ModuleName [AvailInfo])
filterByImport (L _ ImportDecl{ideclHiding = Just (_, L _ names)}) avails =
let importedNames = S.fromList $ map (ieName . unLoc) names
res = flip Map.filter avails $ \a ->
availsExcludingInternal = Map.filter notContainInternalModule avails
res = flip Map.filter availsExcludingInternal $ \a ->
any (`S.member` importedNames)
$ concatMap availNamesWithSelectors a
allFilteredAvailsNames = S.fromList
Expand Down
1 change: 1 addition & 0 deletions plugins/hls-refine-imports-plugin/test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ main = defaultTestRunner $
"Refine Imports"
[ codeActionGoldenTest "WithOverride" 3 1
, codeLensGoldenTest "UsualCase" 1
, codeLensGoldenTest "DontUseInternal" 1
]

refineImportsPlugin :: PluginDescriptor IdeState
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Main where

import H
import E ( e2 )
import Data.List (intercalate)

main :: IO ()
main = putStrLn
$ "hello "
<> intercalate ", " [h, e2]
10 changes: 10 additions & 0 deletions plugins/hls-refine-imports-plugin/test/testdata/DontUseInternal.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Main where

import H
import D
import Data.List (intercalate)

main :: IO ()
main = putStrLn
$ "hello "
<> intercalate ", " [h, e2]
3 changes: 3 additions & 0 deletions plugins/hls-refine-imports-plugin/test/testdata/H.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module H (module H.Internal) where

import H.Internal
4 changes: 4 additions & 0 deletions plugins/hls-refine-imports-plugin/test/testdata/H/Internal.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module H.Internal where

h :: String
h = "h"
5 changes: 4 additions & 1 deletion plugins/hls-refine-imports-plugin/test/testdata/hie.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ cradle:
arguments:
- UsualCase.hs
- WithOverride.hs
- DontUseInternal.hs
- A.hs
- B.hs
- C.hs
- D.hs
- E.hs
- F.hs
- G.hs
- G.hs
- H.hs
- H/Internal.hs