Skip to content
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

Consider all root paths when suggesting module name change. #2195

Merged
merged 3 commits into from
Sep 16, 2021
Merged
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
39 changes: 22 additions & 17 deletions plugins/hls-module-name-plugin/src/Ide/Plugin/ModuleName.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import Control.Monad.Trans.Maybe
import Data.Aeson (Value (Null), toJSON)
import Data.Char (isLower)
import qualified Data.HashMap.Strict as HashMap
import Data.List (find, intercalate, isPrefixOf)
import Data.List (intercalate, isPrefixOf, minimumBy)
import Data.Maybe (maybeToList)
import Data.String (IsString)
import qualified Data.Text as T
Expand All @@ -44,6 +44,7 @@ import Language.LSP.VFS (virtualFileText)
import System.Directory (canonicalizePath)
import System.FilePath (dropExtension, splitDirectories,
takeFileName)
import Data.Ord (comparing)

-- |Plugin descriptor
descriptor :: PluginId -> PluginDescriptor IdeState
Expand Down Expand Up @@ -97,36 +98,40 @@ action state uri =
contents <- lift . getVirtualFile $ toNormalizedUri uri
let emptyModule = maybe True (T.null . T.strip . virtualFileText) contents

correctName <- MaybeT . liftIO $ traceAs "correctName" <$> pathModuleName state nfp fp
correctNames <- liftIO $ traceAs "correctNames" <$> pathModuleNames state nfp fp
let bestName = minimumBy (comparing T.length) correctNames

statedNameMaybe <- liftIO $ traceAs "statedName" <$> codeModuleName state nfp
case statedNameMaybe of
Just (nameRange, statedName)
| correctName /= statedName ->
pure $ Replace uri nameRange ("Set module name to " <> correctName) correctName
| statedName `notElem` correctNames ->
pure $ Replace uri nameRange ("Set module name to " <> bestName) bestName
Nothing
| emptyModule ->
let code = "module " <> correctName <> " where\n"
let code = "module " <> bestName <> " where\n"
in pure $ Replace uri (Range (Position 0 0) (Position 0 0)) code code
_ -> MaybeT $ pure Nothing

-- | The module name, as derived by the position of the module in its source directory
pathModuleName :: IdeState -> NormalizedFilePath -> String -> IO (Maybe T.Text)
pathModuleName state normFilePath filePath
| isLower . head $ takeFileName filePath = return $ Just "Main"
-- | Possible module names, as derived by the position of the module in the
-- source directories. There may be more than one possible name, if the source
-- directories are nested inside each other.
pathModuleNames :: IdeState -> NormalizedFilePath -> String -> IO [T.Text]
pathModuleNames state normFilePath filePath
| isLower . head $ takeFileName filePath = return ["Main"]
| otherwise = do
session <- runAction "ModuleName.ghcSession" state $ use_ GhcSession normFilePath
srcPaths <- evalGhcEnv (hscEnvWithImportPaths session) $ importPaths <$> getSessionDynFlags
paths <- mapM canonicalizePath srcPaths
mdlPath <- canonicalizePath filePath
pure $ do
prefix <- find (`isPrefixOf` mdlPath) paths
pure
. T.pack
. intercalate "."
. splitDirectories
. drop (length prefix + 1)
$ dropExtension mdlPath
let prefixes = filter (`isPrefixOf` mdlPath) paths
pure (map (moduleNameFrom mdlPath) prefixes)
where
moduleNameFrom mdlPath prefix =
T.pack
. intercalate "."
. splitDirectories
. drop (length prefix + 1)
$ dropExtension mdlPath

-- | The module name, as stated in the module
codeModuleName :: IdeState -> NormalizedFilePath -> IO (Maybe (Range, T.Text))
Expand Down
5 changes: 5 additions & 0 deletions plugins/hls-module-name-plugin/test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ tests =
[CodeLens { _command = Just c }] <- getCodeLenses doc
executeCommand c
void $ skipManyTill anyMessage (message SWorkspaceApplyEdit)

, goldenWithModuleName "Fix wrong module name in nested directory" "subdir/TWrongModuleName" $ \doc -> do
[CodeLens { _command = Just c }] <- getCodeLenses doc
executeCommand c
void $ skipManyTill anyMessage (message SWorkspaceApplyEdit)
]

goldenWithModuleName :: TestName -> FilePath -> (TextDocumentIdentifier -> Session ()) -> TestTree
Expand Down
1 change: 1 addition & 0 deletions plugins/hls-module-name-plugin/test/testdata/hie.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
cradle:
direct:
arguments:
- "-isubdir"
- "TEmptyModule"
- "TWrongModuleName"
- "mainlike"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module TWrongModuleName
( x
)
where

x :: Integer
x = 11
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module BadName
( x
)
where

x :: Integer
x = 11