Skip to content

Do not suggest bogus module names #3784

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 4 commits into from
Sep 2, 2023
Merged
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
19 changes: 11 additions & 8 deletions plugins/hls-module-name-plugin/src/Ide/Plugin/ModuleName.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.Except
import Control.Monad.Trans.Maybe
import Data.Aeson (toJSON)
import Data.Char (isLower)
import Data.List (intercalate, isPrefixOf,
minimumBy)
import Data.Char (isLower, isUpper)
import Data.List (intercalate, minimumBy,
stripPrefix, uncons)
import qualified Data.List.NonEmpty as NE
import qualified Data.Map as Map
import Data.Maybe (mapMaybe)
import Data.Ord (comparing)
import Data.String (IsString)
import qualified Data.Text as T
Expand Down Expand Up @@ -154,15 +155,17 @@ pathModuleNames recorder state normFilePath filePath
mdlPath <- liftIO $ makeAbsolute filePath
logWith recorder Debug (AbsoluteFilePath mdlPath)

let prefixes = filter (`isPrefixOf` mdlPath) paths
pure (map (moduleNameFrom mdlPath) prefixes)
let suffixes = mapMaybe (`stripPrefix` mdlPath) paths
pure (map moduleNameFrom suffixes)
where
moduleNameFrom mdlPath prefix =
moduleNameFrom =
T.pack
. intercalate "."
-- Do not suggest names whose components start from a lower-case char,
-- they are guaranteed to be malformed.
. filter (maybe False (isUpper . fst) . uncons)
. splitDirectories
. drop (length prefix)
$ dropExtension mdlPath
. dropExtension

-- | The module name, as stated in the module
codeModuleName :: IdeState -> NormalizedFilePath -> IO (Maybe (Range, T.Text))
Expand Down