hsimport: Extend the import list of a Haskell source file

[ bsd3, development, library, program, utils ] [ Propose Tags ] [ Report a vulnerability ]

A command line program for extending the import list of a Haskell source file.


[Skip to Readme]

Downloads

Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1, 0.1.1, 0.2, 0.2.1, 0.2.2, 0.2.3, 0.2.4, 0.2.5, 0.2.6, 0.2.6.1, 0.2.6.2, 0.2.6.3, 0.2.6.4, 0.2.6.5, 0.2.6.6, 0.2.6.7, 0.2.6.8, 0.2.7, 0.2.8, 0.2.9, 0.2.10, 0.2.11, 0.3, 0.4, 0.5, 0.5.1, 0.5.2, 0.6, 0.6.1, 0.6.2, 0.6.3, 0.6.4, 0.6.5, 0.6.6, 0.6.7, 0.7, 0.7.1, 0.8.0, 0.8.1, 0.8.2, 0.8.3, 0.8.4, 0.8.5, 0.8.6, 0.8.8, 0.9.0, 0.10.0, 0.11.0
Change log CHANGELOG
Dependencies attoparsec (>=0.10.4.0 && <0.14), base (>=3 && <5), cmdargs (>=0.10.5 && <0.11), directory (>=1.2.0.1 && <1.4), dyre (>=0.8 && <0.9), haskell-src-exts (>=1.18.0 && <1.24), hsimport, ilist (>=0.1 && <0.5), microlens (>=0.4 && <0.5), mtl (>=2.1.2 && <2.3), split (>=0.2.2 && <0.3), text (>=0.11.3.1 && <1.3) [details]
Tested with ghc ==7.6.2, ghc ==7.6.3, ghc ==7.8.3, ghc ==7.10.1, ghc ==7.10.2, ghc ==8.0.1, ghc ==8.2.1, ghc ==8.4.2, ghc ==8.6.3
License BSD-3-Clause
Author Daniel Trstenjak
Maintainer power.walross@gmail.com
Revised Revision 3 made by fendor at 2020-06-22T08:42:21Z
Category Utils, Development
Source repo head: git clone https://github.com/fendor/hsimport
Uploaded by DanielTrstenjak at 2019-09-15T08:44:09Z
Distributions
Reverse Dependencies 1 direct, 0 indirect [details]
Executables hsimport
Downloads 37680 total (59 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2019-09-15 [all 1 reports]

Readme for hsimport-0.11.0

[back to package description]

hsimport

A command line program for extending the import list of a Haskell source file.

hsimport gets the module name and the symbol name to import as arguments, parses the given source file using the library haskell-src-exts and then tries to only extend the import list if it's necessary. If the symbol is already imported or if the whole module is already imported, then the given source file isn't changed.

Installation

cabal install hsimport

Examples

$> hsimport -m 'Control.Monad' SomeSource.hs
=> import Control.Monad

$> hsimport -m 'Control.Monad' -s 'when' SomeSource.hs
=> import Control.Monad (when)

$> hsimport -m 'Control.Monad' -q 'CM' SomeSource.hs
=> import qualified Control.Monad as CM

$> hsimport -m 'Control.Monad' --as 'CM' SomeSource.hs
=> import Control.Monad as CM

$> hsimport -m 'Data.Maybe' -s 'Maybe'
=> import Data.Maybe (Maybe)

$> hsimport -m 'Data.Maybe' -s 'Maybe' -a
=> import Data.Maybe (Maybe(..))

$> hsimport -m 'Data.Maybe' -s 'Maybe' -w 'Just'
=> import Data.Maybe (Maybe(Just))

$> hsimport -m 'Data.Maybe' -s 'Maybe' -w 'Just' -w 'Nothing'
=> import Data.Maybe (Maybe(Just, Nothing))

Configuration

The easiest way to configure hsimport is by creating a cabal project. An example for this is here.

The other way - which most likely isn't worth the hassle - is by writting a ~/.config/hsimport/hsimport.hs file:

-- ~/.config/hsimport/hsimport.hs
import qualified Language.Haskell.Exts as HS
import HsImport

main :: IO ()
main = hsimport $ defaultConfig { prettyPrint = prettyPrint, findImportPos = findImportPos }
   where
      -- This is a bogus implementation of prettyPrint, because it doesn't handle the
      -- qualified import case nor does it considers any explicitely imported or hidden symbols.
      prettyPrint :: ImportDecl -> String
      prettyPrint (ImportDecl { HS.importModule = HS.ModuleName _ modName }) =
         "import " ++ modName

      -- This findImportPos implementation will always add the new import declaration
      -- at the end of the current ones. The data type ImportPos has the two constructors
      -- After and Before.
      findImportPos :: ImportDecl -> [ImportDecl] -> Maybe ImportPos
      findImportPos         _             [] = Nothing
      findImportPos newImport currentImports = Just . After . last $ currentImports

The position of the configuration file depends on the result of getUserConfigDir "hsimport", which is a function from the package xdg-basedir, on linux like systems it is ~/.config/hsimport/hsimport.hs.

If you've modified the configuration file, then the next call of hsimport will ensure a rebuild. If you've installed hsimport with cabal install, without using a sandbox, then this should just work.

If you've build hsimport inside of a sandbox, then you most likely have to temporary modify the GHC_PACKAGE_PATH for the next call of hsimport, to point ghc to the global database and to the package database of the sandbox.

# global package database
$> export GLOBAL_PKG_DB=/usr/lib/ghc/package.conf.d/

# hsimport sandbox package database
$> export SANDBOX_PKG_DB=/home/you/hsimport-build-dir/.cabal-sandbox/*-packages.conf.d/

$> GHC_PACKAGE_PATH=$GLOBAL_PKG_DB:$SANDBOX_PKG_DB hsimport --help

Text Editor Integration

vim-hsimport

Command Line Usage

$> hsimport --help
hsimport [OPTIONS] [SOURCEFILE]
  A command line program for extending the import list of a Haskell source
  file.

Common flags:
  -m --modulename=ITEM     The module to import
  -s --symbolname=ITEM     The symbol to import, if empty, the entire module
                           is imported
  -a --all                 All constructors or methods of the symbol should
                           be imported: 'Symbol(..)'
  -w --with=ITEM           The constructors or methods of the symbol
                           should be imported: 'Symbol(With)'
  -q --qualifiedname=ITEM  The name to use for a qualified module import
  -o --outputsrcfile=FILE  Save modified source file to file, if empty, the
                           source file is modified inplace
  -h --help                Display help message
  -v --version             Print version information

Issues

There is some rudimentarily handling for code using CPP, but the import statements might be added at the wrong place, because the lines containing CPP directives are ignored and therefore they aren't considered in the source line count.