hsimport 0.8.0 → 0.8.1
raw patch · 6 files changed
+38/−87 lines, 6 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- CHANGELOG +4/−0
- hsimport.cabal +1/−1
- lib/HsImport/ImportChange.hs +10/−9
- lib/HsImport/Main.hs +8/−15
- lib/HsImport/Parse.hs +0/−54
- lib/HsImport/Utils.hs +15/−8
CHANGELOG view
@@ -1,3 +1,7 @@+0.8.1+-----+* Use SrcSpan info of haskell-src-exts instead of parsing import declarations by hand+ 0.8.0 ----- * Changes for haskell-src-exts 1.18.0
hsimport.cabal view
@@ -1,5 +1,5 @@ name: hsimport-version: 0.8.0+version: 0.8.1 cabal-version: >=1.9.2 build-type: Simple license: BSD3
lib/HsImport/ImportChange.hs view
@@ -20,12 +20,13 @@ type HsModule = HS.Module HS.SrcSpanInfo -- | How the import declarations should be changed-data ImportChange = ReplaceImportAt SrcLine HsImportDecl -- ^ replace the import declaration at SrcLine- | AddImportAfter SrcLine HsImportDecl -- ^ add import declaration after SrcLine- | AddImportAtEnd HsImportDecl -- ^ add import declaration at end of source file- | FindImportPos HsImportDecl -- ^ search for an insert position for the import declaration- | NoImportChange -- ^ no changes of the import declarations- deriving (Show)+data ImportChange+ = ReplaceImportAt HS.SrcSpan HsImportDecl -- ^ replace the import declaration at SrcSpan+ | AddImportAfter SrcLine HsImportDecl -- ^ add import declaration after SrcLine+ | AddImportAtEnd HsImportDecl -- ^ add import declaration at end of source file+ | FindImportPos HsImportDecl -- ^ search for an insert position for the import declaration+ | NoImportChange -- ^ no changes of the import declarations+ deriving (Show) importChanges :: Module -> Maybe Symbol -> HsModule -> [ImportChange]@@ -67,7 +68,7 @@ then NoImportChange else case find hasImportedSymbols matching of Just impDecl ->- ReplaceImportAt (importDeclSrcLine impDecl) (addSymbol impDecl symbol)+ ReplaceImportAt (srcSpan impDecl) (addSymbol impDecl symbol) Nothing -> FindImportPos $ importDeclWithSymbol moduleName symbol@@ -242,11 +243,11 @@ where newSrcLine srcSpan imports decls | not $ null imports- = Just (importDeclSrcLine $ last imports)+ = Just (firstSrcLine $ last imports) | (decl:_) <- decls , sLoc <- declSrcLoc decl- , HS.srcLine sLoc >= spanSrcLine srcSpan+ , HS.srcLine sLoc >= HS.startLine srcSpan = Just $ max 0 (HS.srcLine sLoc - 1) | otherwise
lib/HsImport/Main.hs view
@@ -13,13 +13,13 @@ import qualified Data.Text as T import qualified Data.Text.IO as TIO import qualified Config.Dyre as Dyre+import qualified Language.Haskell.Exts as HS import HsImport.ImportChange import HsImport.ImportSpec import HsImport.ImportPos (ImportPos(..)) import qualified HsImport.Args as Args import HsImport.Config import HsImport.Utils-import qualified HsImport.Parse as P #if __GLASGOW_HASKELL__ < 710 import Control.Applicative ((<$>))@@ -67,13 +67,13 @@ where applyChanges = foldl' applyChange - applyChange srcLines (ReplaceImportAt srcLine importDecl) =- let numTakes = max 0 (srcLine - 1)- numDrops = lastImportSrcLine srcLine srcLines+ applyChange srcLines (ReplaceImportAt srcSpan importDecl) =+ let numTakes = max 0 (HS.srcSpanStartLine srcSpan - 1)+ numDrops = HS.srcSpanEndLine srcSpan in take numTakes srcLines ++ [prettyPrint importDecl] ++ drop numDrops srcLines applyChange srcLines (AddImportAfter srcLine importDecl) =- let numTakes = lastImportSrcLine srcLine srcLines+ let numTakes = srcLine numDrops = numTakes in take numTakes srcLines ++ [prettyPrint importDecl] ++ drop numDrops srcLines @@ -82,11 +82,11 @@ applyChange srcLines (FindImportPos importDecl) = case findImportPos importDecl allImportDecls of- Just (After impDecl) -> applyChange srcLines (AddImportAfter (importDeclSrcLine impDecl)+ Just (After impDecl) -> applyChange srcLines (AddImportAfter (lastSrcLine impDecl) importDecl)- Just (Before impDecl) -> applyChange srcLines (AddImportAfter (max 0 (importDeclSrcLine impDecl - 1))+ Just (Before impDecl) -> applyChange srcLines (AddImportAfter (max 0 (firstSrcLine impDecl - 1)) importDecl)- _ -> applyChange srcLines (AddImportAfter (importDeclSrcLine . last $ allImportDecls)+ _ -> applyChange srcLines (AddImportAfter (lastSrcLine . last $ allImportDecls) importDecl) applyChange srcLines NoImportChange = srcLines@@ -94,12 +94,5 @@ outputFile spec | Just file <- saveToFile spec = file | otherwise = sourceFile spec-- lastImportSrcLine fstLine srcLines- | Just lastLine <- P.lastImportSrcLine $ drop (max 0 (fstLine - 1)) srcLines- = fstLine + (lastLine - 1)-- | otherwise- = fstLine allImportDecls = importDecls $ parsedSrcFile spec
lib/HsImport/Parse.hs view
@@ -2,7 +2,6 @@ module HsImport.Parse ( parseFile- , lastImportSrcLine ) where import qualified Data.Text.IO as TIO@@ -40,59 +39,6 @@ if "#" `isPrefixOf` line then "-- fake hsimport comment" else line---type SrcLine = Int---- | Expects that '[String]' starts with an import declaration and returns--- the last source line of the import declaration, so this function--- is for the handling of multine line import declarations.-lastImportSrcLine :: [String] -> Maybe SrcLine-lastImportSrcLine srcLines- | null srcLines- = Nothing-- | "import" `isPrefixOf` head srcLines- = parseImport 1-- | otherwise- = Nothing-- where- parseImport lastLine- | lastLine <= numSrcLines- = case parseFileContents source of- HS.ParseOk module_- | oneImportDeclWithoutSymbols module_ && startsWithImportDeclSymbols (lastLine + 1)- -> parseImport (lastLine + 1)-- | otherwise- -> Just lastLine-- HS.ParseFailed _ _ -> parseImport (lastLine + 1)-- | otherwise- = Nothing-- where- source = unlines $ take lastLine srcLines-- numSrcLines = length srcLines-- -- | Returns True if the module contains one ImportDecl without any explicitely- -- listed symbols.- oneImportDeclWithoutSymbols (HS.Module _ _ _ [HS.ImportDecl {HS.importSpecs = Nothing}] _) = True- oneImportDeclWithoutSymbols _ = False-- -- | Returns True if the line represents the starting of a ImportDecl symbol list.- startsWithImportDeclSymbols lineNum- | line : _ <- drop (lineNum - 1) srcLines- , ' ' : _ <- line- , '(' : _ <- dropWhile (== ' ') line- = True-- | otherwise- = False -- | tries to find the maximal part of the source file (from the beginning) that contains
lib/HsImport/Utils.hs view
@@ -1,23 +1,30 @@ module HsImport.Utils- ( importDeclSrcLine- , spanSrcLine+ ( firstSrcLine+ , lastSrcLine+ , srcSpan , declSrcLoc , importDecls ) where import qualified Language.Haskell.Exts as HS -type SrcLine = Int+type SrcLine = Int+type HsImportDecl = HS.ImportDecl HS.SrcSpanInfo+type HsModule = HS.Module HS.SrcSpanInfo -importDeclSrcLine :: HS.ImportDecl HS.SrcSpanInfo -> SrcLine-importDeclSrcLine = spanSrcLine . HS.importAnn+firstSrcLine :: HsImportDecl -> SrcLine+firstSrcLine = HS.startLine . HS.importAnn -spanSrcLine :: HS.SrcSpanInfo -> SrcLine-spanSrcLine = HS.srcSpanStartLine . HS.srcInfoSpan+srcSpan :: HsImportDecl -> HS.SrcSpan+srcSpan = HS.srcInfoSpan . HS.importAnn +lastSrcLine :: HsImportDecl -> SrcLine+lastSrcLine = HS.srcSpanEndLine . srcSpan++ declSrcLoc :: HS.Decl HS.SrcSpanInfo -> HS.SrcLoc declSrcLoc decl = HS.SrcLoc srcFile srcLine srcCol where@@ -27,7 +34,7 @@ srcCol = HS.srcSpanStartColumn srcSpan -importDecls :: HS.Module HS.SrcSpanInfo -> [HS.ImportDecl HS.SrcSpanInfo]+importDecls :: HsModule -> [HsImportDecl] importDecls (HS.Module _ _ _ imports _) = imports importDecls (HS.XmlPage _ _ _ _ _ _ _) = [] importDecls (HS.XmlHybrid _ _ _ imports _ _ _ _ _) = imports