+;; -*- lexical-binding: t; -*-
+
;;; javaimp.el --- Add and reorder Java import statements in Maven projects
;; Copyright (C) 2014 Free Software Foundation, Inc.
(defcustom javaimp-import-default-order 50
"Defines the order of classes which were not matched by
-`javaimp-import-group-alist'.")
+`javaimp-import-group-alist'")
(defcustom javaimp-jdk-home nil
- "Path to the JDK.")
+ "Path to the JDK")
(defcustom javaimp-mvn-program "mvn"
- "Path to the `mvn' program.")
+ "Path to the `mvn' program")
(defcustom javaimp-cygpath-program "cygpath"
- "Path to the `cygpath' program.")
+ "Path to the `cygpath' program")
(defcustom javaimp-jar-program "jar"
"Path to the `jar' program")
;;; Variables and constants
(defvar javaimp-maven-root-modules nil
- "Loaded root Maven modules.")
+ "Loaded root Maven modules")
(defvar javaimp-jar-classes-cache nil
- "Jar classes cache.")
+ "Jar classes cache")
(defconst javaimp-debug-buf-name "*javaimp-debug*")
;;;###autoload
(defun javaimp-maven-visit-root (path)
- (interactive "DVisit maven root project: ")
"Loads all modules starting from root module identified by
PATH. PATH should point to a directory."
+ (interactive "DVisit maven root project: ")
(let ((root-pom (expand-file-name
(concat (file-name-as-directory path) "pom.xml")))
- modules root-module)
+ modules existing-module)
(unless (file-readable-p root-pom)
(error "Cannot read root pom: %s" root-pom))
(setq modules (javaimp-maven-load-module-tree root-pom))
(javaimp-call-mvn
pom "help:effective-pom"
(lambda ()
- (let (xml-tree xml-start-pos xml-end-pos project-extractor)
+ (let (xml-start-pos xml-end-pos project-extractor)
(goto-char (point-min))
(search-forward "<?xml")
(setq xml-start-pos (match-beginning 0))
(let* ((artifact-id (car (cddr (assq 'artifactId (cddr proj)))))
(pom-file-path (cdr (assoc artifact-id artifact-pomfile-alist)))
(source-dir (car (cddr (assq 'sourceDirectory
- (cddr (assq 'build (cddr proj)))))))
+ (cddr (assq 'build (cddr proj)))))))
(test-source-dir (car (cddr (assq 'testSourceDirectory
- (cddr (assq 'build (cddr proj))))))))
+ (cddr (assq 'build (cddr proj))))))))
(push (javaimp-make-mod
artifact-id pom-file-path
(file-name-as-directory
(if (eq system-type 'cygwin)
- (car (process-lines javaimp-cygpath-program "-u"
+ (car (process-lines javaimp-cygpath-program "-u"
source-dir))
source-dir))
(file-name-as-directory
(defun javaimp-call-mvn (pom-file target handler)
"Runs Maven target TARGET on POM-FILE, then calls HANDLER in
-the temporary buffer and returns its result."
+the temporary buffer and returns its result"
(message "Calling \"mvn %s\" on pom: %s" target pom-file)
(with-temp-buffer
(let* ((pom-file (if (eq system-type 'cygwin)
;;; Reading and caching dependencies
(defun javaimp-maven-fetch-module-deps (module)
- "Returns list of dependency jars for MODULE."
+ "Returns list of dependency jars for MODULE"
(javaimp-call-mvn
(javaimp-get-mod-pom-file module) "dependency:build-classpath"
(lambda ()
(split-string deps-line (concat "[" path-separator "\n" "]+") t)))))
(defun javaimp-get-dep-jars-cached (module)
- "Returns a list of dependency jar file paths for a MODULE."
+ "Returns a list of dependency jar file paths for a MODULE"
(let ((current-pom-file-mod-ts
(nth 5 (file-attributes (javaimp-get-mod-pom-file module)))))
(unless (and (javaimp-get-mod-pom-mod-ts module)
(defun javaimp-get-jdk-jars ()
"Returns list of jars from the jre/lib subdirectory of the JDK
-directory."
+directory"
(when javaimp-jdk-home
(directory-files (concat (file-name-as-directory javaimp-jdk-home)
(file-name-as-directory "jre/lib"))
(setq result (append (javaimp-get-jar-classes-cached jar) result)))))
(defun javaimp-determine-module (file)
- "Returns a module in which the source file FILE resides."
+ "Returns a module in which the source file FILE resides"
(let ((root-modules javaimp-maven-root-modules)
result)
(while (and root-modules (not result))
(defun javaimp-determine-module-from-root (file root-module)
"Searches a hierarchy of modules starting at ROOT-MODULE for
-source file FILE."
+source file FILE"
(let ((modules (cdr root-module))
result)
(while (and modules (not result))
(push (concat prefix (file-name-sans-extension (car file))) result)))
result))
-(defun javaimp-add-to-import-group (class group-sym)
- "Subroutine of `javaimp-organize-imports'."
- (let* ((order (or (assoc-default class javaimp-import-group-alist
+(defun javaimp-add-to-import-groups (new-class groups)
+ "Subroutine of `javaimp-organize-imports'"
+ (let* ((order (or (assoc-default new-class javaimp-import-group-alist
'string-match)
javaimp-import-default-order))
- (group (assoc order (symbol-value group-sym))))
+ (group (assoc order groups)))
(if group
- ;; check if this class is already added
- (unless (member class (cdr group))
- (setcdr group (cons class (cdr group))))
- (set group-sym (cons (cons order (list class))
- (symbol-value group-sym))))))
+ (progn
+ ;; add only if this class is not already there
+ (unless (member new-class (cdr group))
+ (setcdr group (cons new-class (cdr group))))
+ groups)
+ (cons (cons order (list new-class)) groups))))
(defun javaimp-insert-import-groups (groups static-p)
"Inserts all imports in GROUPS. Non-nil STATIC-P means that
(while (re-search-forward
"^\\s-*import\\s-+\\(static\\s-+\\)?\\([._[:word:]]+\\)"
nil t)
- (javaimp-add-to-import-group (match-string 2)
- (if (null (match-string 1))
- 'import-groups
- 'static-import-groups))
+ (if (null (match-string 1))
+ (setq import-groups
+ (javaimp-add-to-import-groups (match-string 2)
+ import-groups))
+ (setq static-import-groups
+ (javaimp-add-to-import-groups (match-string 2)
+ static-import-groups)))
(beginning-of-line)
(unless old-imports-start (setq old-imports-start (point)))
(kill-line)
(delete-region old-imports-start (point))))
;; new imports
(dolist (class new-classes)
- (javaimp-add-to-import-group class 'import-groups))
+ (setq import-groups (javaimp-add-to-import-groups class import-groups)))
;; insert all
(if (or import-groups static-import-groups)
(progn
;;;###autoload
(defun javaimp-invalidate-jar-classes-cache ()
+ "Resets jar classes cache (debugging only)"
(interactive)
- "(for debug) Resets jar classes cache"
(setq javaimp-jar-classes-cache nil))
;;;###autoload
(defun javaimp-forget-all-visited-modules ()
+ "Resets `javaimp-maven-root-modules' (debugging only)"
(interactive)
- "(for debug) Resets `javaimp-maven-root-modules'"
(setq javaimp-maven-root-modules nil))
;;;###autoload
(defun javaimp-reset ()
+ "Resets all data (debugging only)"
(interactive)
(javaimp-forget-all-visited-modules)
(javaimp-invalidate-jar-classes-cache))