patch to jde-import.el

"Jeff Peck" <[email protected]>
Newsgroups gmane.emacs.jdee.devel
Message-ID <CB39887585434E6B8C10328B58976A81@blue>
Continuing to make JDEE safe for users of inner-classes,
I offer this patch to jde-import.el

(defcustom jde-import-exclude-inner-imports t
  "Exclude imports for classes that appear to be included as inner-classes or by import some.package.*
This avoids offers for some.package.Outer.Inner when some.package.Outer is already imported.
Which is correct if your code refers to Outer.Inner, rather than just Inner;
in the latter case, supplying the no-exclude argument to `jde-import-all' will find all the classes."
  :group 'jde-project
  :type 'boolean)

So if you have:
import foo.Bar;

and in the code you reference: Bar.Baz or Bar.Baz.class or Bar.Baz.ENUM or Bar.Baz.method(...), etc.
JDEE will not insist on adding another: import foo.Bar.Baz;

the same filter also recognizes if import foo.*; then no need to import foo.Bar;

Note: I have made the default for this to be 'true' even though that may change existing experience.
Because I believe the prior behaviour is a bug, and so the default is to fix the bug.
As noted, if you need/want the old behaviour in a particular case, just supply the prefix-arg.

Also fixed a bug where the import insertion point did not go to the next line after the last import.
so if you had a comment on the last import, jde would insert before that comment...

------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev

_______________________________________________
jdee-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jdee-devel
jde-import.patch (application/octet-stream, 4.7 KB)
Index: jde-import.el
===================================================================
--- jde-import.el	(revision 181)
+++ jde-import.el	(working copy)
@@ -165,6 +165,15 @@
   :group 'jde-project
   :type 'boolean)
 
+(defcustom jde-import-exclude-inner-imports t
+  "Exclude imports for classes that appear to be included as inner-classes or by import some.package.*
+This avoids offers for some.package.Outer.Inner when some.package.Outer is already imported.
+Which is correct if your code refers to Outer.Inner, rather than just Inner;
+in the latter case, supplying the no-exclude argument to `jde-import-all' will find all the classes."
+  :group 'jde-project
+  :type 'boolean)
+
+
 (defun jde-import-current-package-p (class)
   "Returns non-nil if the fully qualified classname CLASS belongs to
 the same package as the class in the current buffer."
@@ -241,7 +250,10 @@
 	    (setq insertion-point 1)))
      (save-excursion
        (goto-char insertion-point)
-       (unless (and (bolp) (eolp)) (insert "\n")))
+       (forward-line 1)
+       (setq insertion-point (point))
+       (unless (and (bolp) (eolp)) (insert "\n"))
+       )
      insertion-point))
 
 (defun jde-import-import (class)
@@ -924,6 +936,51 @@
 		       (member name imported-classes))
 		(add-to-list 'classes-to-import  name t)))))))))
 
+(defun jde-import-is-included0(name import0)
+  "check single qualified name against a single qualified class name."
+  (and import0 
+       (let* ((len0 (length import0))
+	      (dotstar (eq t (compare-strings import0 (- len0 2) len0 ".*" nil nil nil)))
+	      (import (if dotstar (substring import0 0 (- len0 2)) import0))
+	      (len (length import)))
+	 (or
+	  (string-equal import name)	; name.equals(import)
+	  (and 
+	   (eq t (compare-strings name 0 len import nil nil nil))  ; name.startsWith(import)
+	   (eq t (compare-strings name len (1+ len) "." nil nil )) ; name[len] == "."
+	   ))
+	 )))
+
+(defun jde-import-is-included1 (name classes) 
+  "check single qualified name against list of qualified classes"
+  (and name
+       (do* ((imports classes (cdr imports))
+	     (import (car imports) (car imports))
+	     (incl (jde-import-is-included0 name import) (jde-import-is-included0 name import)))
+	   ((or (null import) incl) incl)
+	 )))
+
+(defun jde-import-is-included (names classes) 
+  "check single or list of qualified names against qualified classes"
+  (if (listp names) 
+      (do* ((nlist names (cdr nlist))
+	    (name (car nlist) (car nlist))
+	    (incl (jde-import-is-included1 name classes) (jde-import-is-included1 name classes))
+	    )
+	  ((or (null name) incl) incl))
+    (jde-import-is-included1 names classes)
+    ))
+
+(defun jde-import-filter-inner-imports (qualified-names) 
+  "remove names that are imported by outer classes or some.package.*" 
+  (let* ((import-tags (semantic-brute-find-tag-by-class 'include (current-buffer)))
+	 (imported-classes (mapcar (lambda (import-tag) (semantic-tag-name import-tag)) import-tags))
+	 (imports nil))
+    (dolist (qnames qualified-names imports)
+      (if (not (jde-import-is-included qnames imported-classes)) 
+	  (setq imports (cons qnames imports))))
+    ))
+
 (defun jde-import-all-show ()
   "Display a list of the class names referenced in this
 buffer that are not declared or explicitly imported into this
@@ -941,7 +998,10 @@
 (defun jde-import-all-filter (unqualified-imports &optional no-exclude)
   "Generate a list of fully qualified names of classes to
 import from UNQUALIFIED-IMPORTS, excluding classes specified
-by `jde-import-exclude-imports' if NO-EXCLUDE is nil."
+by `jde-import-exclude-imports' if NO-EXCLUDE is nil.
+If `jde-import-exclude-inner-imports' is non-nil, then also remove
+any classes that appear to be included by outer-class imports."
+  (let ((imports
   (mapcar
    (lambda (unqualified-class)
      (let ((qualified-imports (jde-import-get-qualified-names unqualified-class)))
@@ -949,6 +1009,10 @@
 	   qualified-imports
 	 (jde-import-exclude-imports qualified-imports))))
    unqualified-imports))
+	)
+    (if (or no-exclude (not jde-import-exclude-inner-imports))
+	imports
+      (jde-import-filter-inner-imports imports))))
 
 (defun jde-import-all-unique ()
   "Import all classes uniquely referenced by unqualified class
Index: jde-util.el
===================================================================
--- jde-util.el	(revision 181)
+++ jde-util.el	(working copy)
@@ -93,9 +93,9 @@
 
 (if (not (fboundp 'replace-in-string))
     (defun replace-in-string  (string regexp newtext &optional literal)
-      "Replace REGEXP with NEWTEXT in STRING."
+      "Replace REGEXP with NEWTEXT in STRING. see: `replace-match'"
       (if (string-match regexp string)
-	  (replace-match newtext nil nil string)
+	  (replace-match newtext nil literal string)
 	string)))
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.