This is a fork of emacs-java-imports that is modified to work with Groovy programming language. Code is mostly the same (all credits go to original author of emacs-java-imports), except that it deals with the fact that semicolons are optional in groovy.
This package adds the import-groovy-class
function, which will prompt for the
class at point, then the package, then add the required import statement for the
class and cache a “class-name -> package” relationship for any future importing
of the class.
groovy-imports
is available in the MELPA repository.
Do this, if MELPA isn’t already in your sources:
(require 'package)
(add-to-list 'package-archives
'("MELPA" . "https://melpa.org/packages/" ))
Then run M-x package-refresh-contents
to load the contents of the new
repository, and M-x package-install RET groovy-imports RET
to install
groovy-imports
.
(require 'groovy-imports)
;; whatever you want to bind it to
(define-key groovy-mode-map (kbd "M-I") 'groovy-imports-add-import-dwim)
;; See customization below for where to put groovy imports
(setq groovy-imports-find-block-function 'groovy-imports-find-place-sorted-block)
I also recommend having groovy-imports automatically add any seen imports to the import cache by adding:
(add-hook 'groovy-mode-hook 'groovy-imports-scan-file)
Functions you may want to bind to a key in Groovy-mode:
Function | Use |
---|---|
groovy-imports-add-import-dwim | Add import for the symbol at point (or ask if none) |
groovy-imports-add-import | Add import for symbol at point, confirming class first |
groovy-imports-scan-file | Scan imports in the file, adding them to the cache |
Other useful functions for writing your own tools:
Function |
---|
groovy-imports-add-import-with-package |
groovy-imports-list-imports |
groovy-imports
will default to saving the buffer after adding an import, but you
can customize groovy-imports-save-buffer-after-import-added
to change this.
By default packages are cached the first time they’re manually entered, if you
want to overwrite what’s in the cache you can invoke groovy-imports-add-import
with the prefix key (C-u
).
To disable caching, set groovy-imports-use-cache
to nil
.
You can customize groovy-imports-find-block-function
, either setting it to a
custom function, or one of the included ones:
groovy-imports-find-place-after-last-import
(default)Simply appends the import to the end of the list of imports
groovy-imports-find-place-sorted-block
Places the import alphabetically sorted into the list of imports, so they will go into:
<package declaration>
<alphabetical non-JDK imports>
<alphabetical JDK imports>
class Whatever {
...
}
By default groovy-imports will use ”groovy-imports
” as the name of the cache of
class->package names, however, if you want to have separate caches per project,
you can customize groovy-imports-cache-name
to have a separate String name
(perhaps in a .dir-locals.el
for per-project imports).
You’ll need: https://github.com/cask/cask
Run in the terminal:
make install
make test