jde integration with hippie completion
James Ahlborn <[email protected]> Wed, 20 Sep 2006 12:53:34 -0400
| Newsgroups | gmane.emacs.jdee |
|---|---|
| Organization | Health Market Science |
| Message-ID | <[email protected]> |
i use hippie completion in emacs, which does everything that the standard completion (dabbrev) does, and so much more. ever since i first tried it, i loved it. anyway, always bothered me that jde completion had no hippie completion integration. so, i finally got around to writing it. i'm passing this on so others can enjoy hippie goodness with jde completion. i wrote it against 2.3.5 version of jde, so hopefully it'll work with 2.3.5.1. maybe it could be integrated into the jde-complete package... -james :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: James Ahlborn Software Developer Health Market Science, Inc. direct: (610) 994-5171 email: [email protected] Visit our new site: www.healthmarketscience.com :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
jde-hippie.el
(text/x-emacs-lisp, 9.9 KB)
;;; jde-hippie.el --- Integrates JDEE with hippie expansion ;; Copyright (C) 2006 Free Software Foundation, Inc. ;; Author: James Ahlborn <[email protected]> ;; Keywords: convenience, abbrev ;; This file is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation; either version 2, or (at your option) ;; any later version. ;; This file is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with GNU Emacs; see the file COPYING. If not, write to ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, ;; Boston, MA 02111-1307, USA. ;;; Commentary: ;; This package provides a few functions for integrating jde completion with ;; hippie completion. There are four functions available: ;; `jdeh-try-expand-partially-fast' : attempts to complete as many unique ;; characters as possible. However, completion will only be attempted if ;; the preceding chars match the expression '[.]\\w*'. This narrows the ;; overhead of jde completion to only stuff which is likely to complete. ;; `jdeh-try-expand-fast' : attempts to complete the current expression, ;; cycling through the available completions. However, completion will ;; only be attempted if the preceding chars match the expression ;; '[.]\\w*'. This narrows the overhead of jde completion to only stuff ;; which is likely to complete. You generally want to put ;; `jdeh-try-expand-partially-fast' in the hippie function list before ;; this function. ;; `jdeh-try-expand-partially' : like `jdeh-try-expand-partially-fast', but ;; always attempts expansion. This can be slow if run on every completion ;; attempt, so you may want to put this later in the hippie function list. ;; `jdeh-try-expand' : like `jdeh-try-expand-fast', but ;; always attempts expansion. This can be slow if run on every completion ;; attempt, so you may want to put this later in the hippie function list. ;; You generally want to put `jdeh-try-expand-partially' in the hippie ;; function list before this function. ;; ;;; Code: (require 'hippie-exp) (require 'jde-complete) (defvar jdeh-complete-current-list nil) (defvar jdeh-complete-current-prefix "") (defun jdeh-try-expand-fast (old) "Try to complete java expression using contextual information about the class of the object at point. This function will only attempt completion if the characters before the point match the expression '[.]\\w*'. The argument OLD has to be nil the first call of this function, and t for subsequent calls (for further possible completions of the same string). It returns t if a new completion is found, nil otherwise." (jdeh-try-expand-internal old t)) (defun jdeh-try-expand-partially-fast (old) "Try to complete as many characters as are unique for java expression using contextual information about the class of the object at point. This function will only attempt completion if the characters before the point match the expression '[.]\\w*'. The argument OLD has to be nil the first call of this function, and t for subsequent calls (for further possible completions of the same string). It returns t if a new completion is found, nil otherwise." (jdeh-try-expand-partially-internal old t)) (defun jdeh-try-expand (old) "Try to complete java expression using contextual information about the class of the object at point. This function will always attempt completion, which can be slow. The argument OLD has to be nil the first call of this function, and t for subsequent calls (for further possible completions of the same string). It returns t if a new completion is found, nil otherwise." (jdeh-try-expand-internal old nil)) (defun jdeh-try-expand-partially (old) "Try to complete as many characters as are unique for java expression using contextual information about the class of the object at point. This function will always attempt completion, which can be slow. The argument OLD has to be nil the first call of this function, and t for subsequent calls (for further possible completions of the same string). It returns t if a new completion is found, nil otherwise." (jdeh-try-expand-partially-internal old nil)) ;; modified version of jde-complete-pair which only builds list, nothing ;; else (also, swaps cons cells of result list) (defun jdeh-complete-build-completions (pair) (let ((access (jde-complete-get-access pair)) completion-list) (progn (if access (setq completion-list (jde-complete-find-completion-for-pair pair nil access)) (setq completion-list (jde-complete-find-completion-for-pair pair))) ;;if the completion list is nil check if the method is in the ;;current class(this) (if (null completion-list) (setq completion-list (jde-complete-find-completion-for-pair (list (concat "this." (car pair)) "") nil jde-complete-private))) ;;if completions is still null check if the method is in the ;;super class (if (null completion-list) (setq completion-list (jde-complete-find-completion-for-pair (list (concat "super." (car pair)) "") nil jde-complete-protected))) ;; swap pairs (put code first, pretty name second) (setq jdeh-complete-current-list (mapcar '(lambda (x) (cons (cdr x) (car x))) completion-list)) jdeh-complete-current-list))) ;; common setup function for jdeh-try-expand and jdeh-try-expand-partially (defun jdeh-try-expand-init (old fast) (if (not old) (progn ;;resetting jdeh-complete-current-list (setq jdeh-complete-current-list nil) (setq jdeh-complete-current-prefix "") ;; if in "fast" mode, only attempt completion if we are currently at a ;; point where the preceding expression matches ".\\w*" (if (or (not fast) (save-excursion (re-search-backward "[.]\\w*\\=" nil t))) ;; setup expansions (ripped from jde-complete-generic) (let* ((pair (jde-parse-java-variable-at-point)) jde-parse-attempted-to-import) (if (and pair ;; find completions (jdeh-complete-build-completions (jde-complete-get-pair pair nil))) (let ((start-pos (- (point) (length (cadr pair))))) ;; if we are at the "." char, the string to replace is ;; empty, which hippie doesn't handle well. in that case, ;; include the "." in the string to replace. (if (= start-pos (point)) (progn (setq start-pos (1- start-pos)) (setq jdeh-complete-current-prefix (buffer-substring-no-properties start-pos (point))))) ;; initialize the string to replace (he-init-string start-pos (point))) )))))) ;; integrate full jde expansion with hippie expansion (defun jdeh-try-expand-internal (old fast) ;; do common setup (jdeh-try-expand-init old fast) ;; trim already tried values from the list (while (and jdeh-complete-current-list (he-string-member (concat jdeh-complete-current-prefix (caar jdeh-complete-current-list)) he-tried-table)) (setq jdeh-complete-current-list (cdr jdeh-complete-current-list))) ;; use existing list (if (null jdeh-complete-current-list) (progn (if old (he-reset-string)) nil) (progn (he-substitute-string (concat jdeh-complete-current-prefix (caar jdeh-complete-current-list))) (setq jdeh-complete-current-list (cdr jdeh-complete-current-list)) t))) ;; integrate prefix jde expansion with hippie expansion (defun jdeh-try-expand-partially-internal (old fast) ;; do common setup (jdeh-try-expand-init old fast) (let (expansion) (if (and (not old) jdeh-complete-current-list) (progn ;; make sure we account for any extra chars we add to the ;; beginning of the completion (if (not (string= he-search-string jdeh-complete-current-prefix)) (setq expansion (try-completion (substring he-search-string (length jdeh-complete-current-prefix)) jdeh-complete-current-list))) (if (stringp expansion) (setq expansion (concat jdeh-complete-current-prefix expansion))) (if (or (eq expansion t) (string= expansion he-search-string) (he-string-member expansion he-tried-table)) (setq expansion nil)))) ;; insert expansion if we found one (if (not expansion) (progn (if old (he-reset-string)) nil) (progn (he-substitute-string expansion) t)))) (provide 'jde-hippie) ;;; jde-hippie.el ends here