Skip to content

Commit 1391769

Browse files
committed
Fail with PluginError instead of pattern match error
1 parent f34d064 commit 1391769

File tree

5 files changed

+15
-18
lines changed

5 files changed

+15
-18
lines changed

.hlint.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@
107107
- Ide.Plugin.Eval.Util
108108
- Ide.Plugin.Floskell
109109
- Ide.Plugin.ModuleName
110-
- Ide.Plugin.Rename
111110
- Ide.Plugin.Class.ExactPrint
112111
- TExpectedActual
113112
- TRigidType

ghcide/src/Development/IDE/Spans/AtPoint.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ foiReferencesAtPoint file pos (FOIReferences asts) =
9595
adjustedLocs = HM.foldr go [] asts
9696
go (HAR _ _ rf tr _, goMapping) xs = refs ++ typerefs ++ xs
9797
where
98-
refs = mapMaybe (toCurrentLocation goMapping . realSrcSpanToLocation . fst)
99-
$ concat $ mapMaybe (\n -> M.lookup (Right n) rf) names
100-
typerefs = mapMaybe (toCurrentLocation goMapping . realSrcSpanToLocation)
101-
$ concat $ mapMaybe (`M.lookup` tr) names
98+
refs = concatMap (mapMaybe (toCurrentLocation goMapping . realSrcSpanToLocation . fst))
99+
(mapMaybe (\n -> M.lookup (Right n) rf) names)
100+
typerefs = concatMap (mapMaybe (toCurrentLocation goMapping . realSrcSpanToLocation))
101+
(mapMaybe (`M.lookup` tr) names)
102102
in (names, adjustedLocs,map fromNormalizedFilePath $ HM.keys asts)
103103

104104
getNamesAtPoint :: HieASTs a -> Position -> PositionMapping -> [Name]

haskell-language-server.cabal

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,6 @@ library hls-rename-plugin
486486
build-depends:
487487
, base >=4.12 && <5
488488
, containers
489-
, extra
490489
, ghcide == 2.6.0.0
491490
, hashable
492491
, hiedb

hls-plugin-api/src/Ide/Plugin/Properties.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ data SomePropertyKeyWithMetaData
101101
SomePropertyKeyWithMetaData (SPropertyKey k) (MetaData t)
102102

103103
-- | 'Properties' is a partial implementation of json schema, without supporting union types and validation.
104-
-- In hls, it defines a set of properties which used in dedicated configuration of a plugin.
104+
-- In hls, it defines a set of properties used in dedicated configuration of a plugin.
105105
-- A property is an immediate child of the json object in each plugin's "config" section.
106106
-- It was designed to be compatible with vscode's settings UI.
107107
-- Use 'emptyProperties' and 'useProperty' to create and consume 'Properties'.

plugins/hls-rename-plugin/src/Ide/Plugin/Rename.hs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ import Control.Monad.Except (ExceptT, throwError)
1515
import Control.Monad.IO.Class (MonadIO, liftIO)
1616
import Control.Monad.Trans.Class (lift)
1717
import Data.Bifunctor (first)
18+
import Data.Foldable (fold)
1819
import Data.Generics
1920
import Data.Hashable
2021
import Data.HashSet (HashSet)
2122
import qualified Data.HashSet as HS
22-
import Data.List.Extra hiding (length)
23+
import Data.List.NonEmpty (NonEmpty ((:|)),
24+
groupWith)
2325
import qualified Data.Map as M
2426
import Data.Maybe
2527
import Data.Mod.Word
@@ -62,7 +64,7 @@ descriptor recorder pluginId = mkExactprintPluginDescriptor recorder $ (defaultP
6264
}
6365

6466
renameProvider :: PluginMethodHandler IdeState Method_TextDocumentRename
65-
renameProvider state pluginId (RenameParams _prog (TextDocumentIdentifier uri) pos newNameText) = do
67+
renameProvider state pluginId (RenameParams _prog (TextDocumentIdentifier uri) pos newNameText) = do
6668
nfp <- getNormalizedFilePathE uri
6769
directOldNames <- getNamesAtPos state nfp pos
6870
directRefs <- concat <$> mapM (refsAtName state nfp) directOldNames
@@ -71,8 +73,8 @@ renameProvider state pluginId (RenameParams _prog (TextDocumentIdentifier uri) p
7173
indirect references through punned names. To find the transitive closure, we do a pass of
7274
the direct references to find the references for any punned names.
7375
See the `IndirectPuns` test for an example. -}
74-
indirectOldNames <- concat . filter notNull <$>
75-
mapM (uncurry (getNamesAtPos state)) (mapMaybe locToFilePos directRefs)
76+
indirectOldNames <- concat . filter ((>1) . length) <$>
77+
(mapM (uncurry (getNamesAtPos state)) =<< mapM locToFilePos directRefs)
7678
let oldNames = filter matchesDirect indirectOldNames ++ directOldNames
7779
matchesDirect n = occNameFS (nameOccName n) `elem` directFS
7880
where
@@ -91,7 +93,7 @@ renameProvider state pluginId (RenameParams _prog (TextDocumentIdentifier uri) p
9193
verTxtDocId <- lift $ getVersionedTextDoc (TextDocumentIdentifier uri)
9294
getSrcEdit state verTxtDocId (replaceRefs newName locations)
9395
fileEdits <- mapM getFileEdit filesRefs
94-
pure $ InL $ foldl' (<>) mempty fileEdits
96+
pure $ InL $ fold fileEdits
9597

9698
-- | Limit renaming across modules.
9799
failWhenImportOrExport ::
@@ -217,9 +219,8 @@ removeGenerated HAR{..} = HAR{hieAst = go hieAst,..}
217219
HieASTs (fmap goAst (getAsts hf))
218220
goAst (Node nsi sp xs) = Node (SourcedNodeInfo $ M.restrictKeys (getSourcedNodeInfo nsi) (S.singleton SourceInfo)) sp (map goAst xs)
219221

220-
-- head is safe since groups are non-empty
221222
collectWith :: (Hashable a, Eq b) => (a -> b) -> HashSet a -> [(b, HashSet a)]
222-
collectWith f = map (\a -> (f $ head a, HS.fromList a)) . groupOn f . HS.toList
223+
collectWith f = map (\(a :| as) -> (f a, HS.fromList (a:as))) . groupWith f . HS.toList
223224

224225
locToUri :: Location -> Uri
225226
locToUri (Location uri _) = uri
@@ -230,10 +231,8 @@ unsafeSrcSpanToLoc srcSpan =
230231
Nothing -> error "Invalid conversion from UnhelpfulSpan to Location"
231232
Just location -> location
232233

233-
locToFilePos :: Location -> Maybe (NormalizedFilePath, Position)
234-
locToFilePos (Location uri (Range pos _)) = (,pos) <$> nfp
235-
where
236-
nfp = uriToNormalizedFilePath $ toNormalizedUri uri
234+
locToFilePos :: Monad m => Location -> ExceptT PluginError m (NormalizedFilePath, Position)
235+
locToFilePos (Location uri (Range pos _)) = (,pos) <$> getNormalizedFilePathE uri
237236

238237
replaceModName :: Name -> Maybe ModuleName -> Module
239238
replaceModName name mbModName =

0 commit comments

Comments
 (0)