Skip to content

Instantly share code, notes, and snippets.

@tdavis
Created January 3, 2014 19:54
Show Gist options
  • Select an option

  • Save tdavis/8245344 to your computer and use it in GitHub Desktop.

Select an option

Save tdavis/8245344 to your computer and use it in GitHub Desktop.
;;; my-java.el --- Set up eclim and work around some of its current issues.
;;; Commentary:
;;;
;;;
;;; Code:
(eval-when-compile (require 'cl))
(require 'eclim)
(require 'eclim-problems)
(require 'eclimd)
(require 'auto-complete-config)
(require 'ac-emacs-eclim-source)
(require 'javadoc-lookup)
(global-eclim-mode)
(ac-config-default)
(ac-emacs-eclim-config)
(defun eclim-next-problem-in-buffer (arg)
"Jump to the next problem in the buffer, if there is one. With ARG, start at (point-min)."
(interactive "P")
(let ((next-position nil))
(save-excursion
(if arg (goto-char (point-min)))
(while (not (or (eobp) next-position))
(goto-char (next-char-property-change (point)))
(if (member 'eclim-problem (mapcar (lambda (o) (overlay-get o 'category)) (overlays-at (point))))
(setq next-position (point)))))
(if (not (eq next-position nil))
(goto-char next-position)
(message "No more problems."))))
(defun eclim-java-constructor ()
"Enhanced version: mark some properties to create a custom constructor.
The built-in version is lazy and creates an empty constructor (how useful!)"
(interactive)
(if (use-region-p)
(let ((class (eclim--java-current-class-name)) (ids ()))
(save-restriction
(narrow-to-region (region-beginning) (region-end))
(goto-char (point-min))
(while (>= (point-max) (+ 1 (point)))
(forward-word)
(let ((id (cdr (eclim--java-identifier-at-point))))
(unless (or (string= id "") (string= (substring id 0 1) "@"))
(eclim/with-results hits ("java_search" ("-p" (format "%s.%s" class id)) ("-t" "field") ("-x" "declarations") ("-s" "project"))
(if (not (eq hits [])) (add-to-list 'ids id)))))))
(eclim/execute-command "java_constructor" "-p" "-f" "-o" ("-r" (format "[%s]" (mapconcat 'identity (reverse ids) ",")))))
(eclim/execute-command "java_constructor" "-p" "-f" "-o")))
(defun eclim-run-test ()
"Run test class at point."
(interactive)
(let ((working-dir (assoc-default 'path
(find eclim--project-name (eclim/project-list)
:key (lambda (e) (assoc-default 'name e))
:test #'string=))))
(if (not (string= major-mode "java-mode"))
(message "Sorry cannot run current buffer.")
(compile (concat eclim-executable " -command java_junit -p " eclim--project-name
" -t " (eclim-package-and-class) )))))
(defun my-eclim-problems-correct (correction-num)
"Correct problem at POINT. With CORRECTION-NUM, use that correction (C-u = 0)."
(interactive "P")
(let ((p (eclim--problems-get-current-problem)))
(if (not (string-match "\.java$" (cdr (assoc 'filename p))))
(error "Not a Java file; corrections are currently supported only for Java")
(if (equal correction-num nil) (eclim-problems-open-current))
(eclim-java-correct (cdr (assoc 'line p)) (eclim--byte-offset))
(if (not (equal correction-num nil))
(let ((n (if (sequencep correction-num) "0" (prin1-to-string correction-num)))) (eclim-java-correct-choose n))))))
(defun eclim--problems-get-current-problem ()
"Fix bug in `find-if' call..."
(let ((buf (get-buffer "*eclim: problems*")))
(if (eq buf (current-buffer))
;; we are in the problems buffer
(let ((problems (eclim--problems-filtered))
(index (1- (line-number-at-pos))))
(if (>= index (length problems))
(error "No problem on this line")
(aref problems index)))
;; we need to figure out which problem corresponds to this pos
(save-restriction
(widen)
(let ((line (line-number-at-pos))
(col (current-column)))
(or (find-if (lambda (p) (and (string= (assoc-default 'filename p) ((lambda () (file-truename buffer-file-name))))
(= (assoc-default 'line p) line)))
eclim--problems-list)
(error "No problem on this line")))))))
(defun my-java-mode-hook ()
"Initialize auto-complete; add some mode functions."
(auto-complete-mode 1)
(define-key java-mode-map (kbd "C-<tab>") 'auto-complete)
(define-key eclim-mode-map (kbd "C-c C-e f j") 'eclim-run-test)
(define-key eclim-mode-map (kbd "C-c C-e n") 'eclim-next-problem-in-buffer)
(define-key eclim-mode-map (kbd "C-c C-e c") 'my-eclim-problems-correct)
;; 2 spaces at HubSpot!
(setq c-basic-offset 2
tab-width 2
indent-tabs-mode nil)
)
(add-hook 'java-mode-hook 'my-java-mode-hook)
(javadoc-add-roots "/usr/share/playframework2/documentation/api/java")
(setq help-at-pt-display-when-idle t)
(setq help-at-pt-timer-delay 0.1)
(help-at-pt-set-timer)
;; being able to open jar:file resources isn't necessarily useful but meh
(setq eclim--compressed-urls-regexp "^\\(\\(?:jar:file\\|jar\\|file\\|zip\\)://\\)")
(defadvice compile-goto-error (around eclim-goto-error)
"Make `compile-goto-error' smart enough to find class files."
(if (string= (buffer-name) "*eclim: find")
(let ((parts (split-string (buffer-substring-no-properties (line-beginning-position) (line-end-position)) ":")))
(eclim--find-file (mapconcat 'identity (subseq parts 0 -3) ":"))
(goto-line (string-to-number (nth 2 parts)))
(recenter 'top)
ad-do-it))
ad-do-it)
(ad-activate 'compile-goto-error)
(defadvice eclim-quit-window (after really-quit-window)
"This puppy doesn't normally delete the window."
(delete-window))
(ad-activate 'eclim-quit-window)
(provide 'my-java)
;;; my-java.el ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment