packages feed

hsdev 0.1.8.1 → 0.1.8.2

raw patch · 3 files changed

+28/−5 lines, 3 filesdep ~hformat

Dependency ranges changed: hformat

Files

hsdev.cabal view
@@ -2,7 +2,7 @@ --  see http://haskell.org/cabal/users-guide/
 
 name:                hsdev
-version:             0.1.8.1
+version:             0.1.8.2
 synopsis:            Haskell development library and tool with support of autocompletion, symbol info, go to declaration, find references etc.
 description:
   Haskell development library and tool with support of autocompletion, symbol info, go to declaration, find references, hayoo search etc.
src/HsDev/Client/Commands.hs view
@@ -7,7 +7,7 @@ import Control.Applicative
 import Control.Concurrent.MVar
 import Control.Exception (displayException)
-import Control.Lens (view, preview, _Just)
+import Control.Lens (view, preview, _Just, (^..), each)
 import Control.Monad
 import Control.Monad.Except
 import Control.Monad.Reader
@@ -317,13 +317,13 @@ targetFilter :: CommandMonad m => TargetFilter -> m (ModuleId -> Bool)
 targetFilter f = case f of
 	TargetProject proj -> liftM inProject $ findProject proj
-	TargetFile file -> return $ inFile file
+	TargetFile file -> liftM inFile $ refineSourceFile file
 	TargetModule mname -> return $ inModule mname
 	TargetDepsOf dep -> liftM inDeps $ findDep dep
 	TargetPackageDb pdb -> return $ inPackageDb pdb
 	TargetCabal -> return $ inPackageDbStack userDb
 	TargetSandbox sbox -> liftM inPackageDbStack $ findSandbox sbox >>= sandboxPackageDbStack
-	TargetPackage pack -> return $ inPackage pack
+	TargetPackage pack -> liftM inPackage $ refinePackage pack
 	TargetSourced -> return byFile
 	TargetStandalone -> return standalone
 
@@ -344,11 +344,29 @@ 	sbox <- liftIO $ S.findSandbox fpath'
 	maybe (hsdevError $ FileNotFound fpath') return sbox
 
+-- | Get source file
+refineSourceFile :: (CommandMonad m, Functor m) => FilePath -> m FilePath
+refineSourceFile fpath = do
+	fpath' <- findPath fpath
+	db' <- getDb
+	maybe (hsdevError (NotInspected $ FileModule fpath' Nothing)) return $ do
+		m' <- lookupFile fpath' db'
+		preview (moduleLocation . moduleFile) m'
+
 -- | Get module by source
 refineSourceModule :: (CommandMonad m, Functor m) => FilePath -> m Module
 refineSourceModule fpath = do
+	fpath' <- findPath fpath
 	db' <- getDb
-	maybe (hsdevError (NotInspected $ FileModule fpath Nothing)) return $ lookupFile fpath db'
+	maybe (hsdevError (NotInspected $ FileModule fpath' Nothing)) return $ lookupFile fpath' db'
+
+-- | Ensure package exists
+refinePackage :: (CommandMonad m, Functor m) => String -> m String
+refinePackage pack = do
+	db' <- getDb
+	if pack `elem` (allPackages db' ^.. each . packageName)
+		then return pack
+		else hsdevError (PackageNotFound pack)
 
 -- | Get list of enumerated sandboxes
 getSandboxes :: (CommandMonad m, Functor m) => [FilePath] -> m [Sandbox]
src/HsDev/Types.hs view
@@ -20,6 +20,7 @@ 	FileNotFound FilePath |
 	ToolNotFound String |
 	ProjectNotFound String |
+	PackageNotFound String |
 	ToolError String String |
 	NotInspected ModuleLocation |
 	InspectError String |
@@ -37,6 +38,7 @@ 	rnf (FileNotFound f) = rnf f
 	rnf (ToolNotFound t) = rnf t
 	rnf (ProjectNotFound p) = rnf p
+	rnf (PackageNotFound p) = rnf p
 	rnf (ToolError t e) = rnf t `seq` rnf e
 	rnf (NotInspected mloc) = rnf mloc
 	rnf (InspectError e) = rnf e
@@ -53,6 +55,7 @@ 	show (FileNotFound f) = format "file '{}' not found" ~~ f
 	show (ToolNotFound t) = format "tool '{}' not found" ~~ t
 	show (ProjectNotFound p) = format "project '{}' not found" ~~ p
+	show (PackageNotFound p) = format "package '{}' not found" ~~ p
 	show (ToolError t e) = format "tool '{}' failed: {}" ~~ t ~~ e
 	show (NotInspected mloc) = "module not inspected: {}" ~~ show mloc
 	show (InspectError e) = format "failed to inspect: {}" ~~ e
@@ -74,6 +77,7 @@ 	toJSON (FileNotFound f) = jsonErr "file not found" ["file" .= f]
 	toJSON (ToolNotFound t) = jsonErr "tool not found" ["tool" .= t]
 	toJSON (ProjectNotFound p) = jsonErr "project not found" ["project" .= p]
+	toJSON (PackageNotFound p) = jsonErr "package not found" ["package" .= p]
 	toJSON (ToolError t e) = jsonErr "tool error" ["tool" .= t, "msg" .= e]
 	toJSON (NotInspected mloc) = jsonErr "module not inspected" ["module" .= mloc]
 	toJSON (InspectError e) = jsonErr "inspect error" ["msg" .= e]
@@ -93,6 +97,7 @@ 			"file not found" -> FileNotFound <$> v .: "file"
 			"tool not found" -> ToolNotFound <$> v .: "tool"
 			"project not found" -> ProjectNotFound <$> v .: "project"
+			"package not found" -> PackageNotFound <$> v .: "package"
 			"tool error" -> ToolError <$> v .: "tool" <*> v .: "msg"
 			"module not inspected" -> NotInspected <$> v .: "module"
 			"inspect error" -> InspectError <$> v .: "msg"