Navigation Menu

Skip to content

dakrone/emacs-java-imports

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Emacs Java Imports

Introduction

https://travis-ci.org/dakrone/emacs-java-imports.svg http://melpa.org/packages/java-imports-badge.svg

I needed a way to import java classes easily in Emacs. I also didn’t want a package that was tied to a particular tool like maven or gradle (especially a potentially long startup time tool).

This adds the import-java-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.

It does this because I didn’t want to drop into Intellij to add all the import statements for my Java code, I just want to hit a key and have an import statement added for the class under the point.

Installation

java-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 java-imports RET to install java-imports.

Usage

(require 'java-imports)

;; whatever you want to bind it to
(define-key java-mode-map (kbd "M-I") 'java-imports-add-import-dwim)

;; See customization below for where to put java imports
(setq java-imports-find-block-function 'java-imports-find-place-sorted-block)

I also recommend having java-imports automatically add any seen imports to the import cache by adding:

(add-hook 'java-mode-hook 'java-imports-scan-file)

Functions

Functions you may want to bind to a key in Java-mode:

FunctionUse
java-imports-add-import-dwimAdd import for the symbol at point (or ask if none)
java-imports-add-importAdd import for symbol at point, confirming class first
java-imports-scan-fileScan imports in the file, adding them to the cache

Other useful functions for writing your own tools:

Function
java-imports-add-import-with-package
java-imports-list-imports

Customization

Saving buffer automatically after adding an import

java-imports will default to saving the buffer after adding an import, but you can customize java-imports-save-buffer-after-import-added to change this.

Caching

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 java-imports-add-import with the prefix key (C-u).

To disable caching, set java-imports-use-cache to nil.

Import style

You can customize java-imports-find-block-function, either setting it to a custom function, or one of the included ones:

  • java-imports-find-place-after-last-import (default)

    Simply appends the import to the end of the list of imports

  • java-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>

public class Whatever {
  ...
}

For example:

package org.writequit;

import org.writequit.Strings;

import java.util.ArrayList;
import java.util.List;

class Foo {
    public void main() {
        String[] s = Strings.EMPTY_ARRAY;
        List<String> = new ArrayList<>();
    }
}

Cache name

By default java-imports will use ”java-imports” as the name of the cache of class->package names, however, if you want to have separate caches per project, you can customize java-imports-cache-name to have a separate String name (perhaps in a .dir-locals.el for per-project imports).

Things to do:

  • [X] Avoid importing packages that already have import statements
  • [X] Handle annotations correctly
  • [ ] Handle * imports
  • [ ] Inner classes?
  • [X] Scan java files for classes and add to the cache
  • [X] Add tests
  • [X] Hook up to travis-ci