Skip to content

Commit 5b8c7a9

Browse files
Bodigrimmichaelpj
andauthored
Do not suggest bogus module names (#3784)
* hls-module-name-plugin: use stripPrefix instead of isPrefixOf * hls-module-name-plugin: do not suggest module names whose components start from a lower-case char --------- Co-authored-by: Michael Peyton Jones <[email protected]>
1 parent 645bb34 commit 5b8c7a9

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

plugins/hls-module-name-plugin/src/Ide/Plugin/ModuleName.hs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ import Control.Monad.Trans.Class (lift)
2525
import Control.Monad.Trans.Except
2626
import Control.Monad.Trans.Maybe
2727
import Data.Aeson (toJSON)
28-
import Data.Char (isLower)
29-
import Data.List (intercalate, isPrefixOf,
30-
minimumBy)
28+
import Data.Char (isLower, isUpper)
29+
import Data.List (intercalate, minimumBy,
30+
stripPrefix, uncons)
3131
import qualified Data.List.NonEmpty as NE
3232
import qualified Data.Map as Map
33+
import Data.Maybe (mapMaybe)
3334
import Data.Ord (comparing)
3435
import Data.String (IsString)
3536
import qualified Data.Text as T
@@ -154,15 +155,17 @@ pathModuleNames recorder state normFilePath filePath
154155
mdlPath <- liftIO $ makeAbsolute filePath
155156
logWith recorder Debug (AbsoluteFilePath mdlPath)
156157

157-
let prefixes = filter (`isPrefixOf` mdlPath) paths
158-
pure (map (moduleNameFrom mdlPath) prefixes)
158+
let suffixes = mapMaybe (`stripPrefix` mdlPath) paths
159+
pure (map moduleNameFrom suffixes)
159160
where
160-
moduleNameFrom mdlPath prefix =
161+
moduleNameFrom =
161162
T.pack
162163
. intercalate "."
164+
-- Do not suggest names whose components start from a lower-case char,
165+
-- they are guaranteed to be malformed.
166+
. filter (maybe False (isUpper . fst) . uncons)
163167
. splitDirectories
164-
. drop (length prefix)
165-
$ dropExtension mdlPath
168+
. dropExtension
166169

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

0 commit comments

Comments
 (0)