Emacs + clojure. Autocompletion data from all source files in project

2.6k views Asked by At

I've been developing C# applications for a long time. Commercial IDEs and tools provide extremely good code completion features. I'm now learning clojure and I really miss familiar workflow.

So, about emacs. I've installed nrepl, ac-nrepl and clojure-mode. Auto completion works fine in repl. It also works for symbols in current buffer. But not for:

  1. symbols from other project files
  2. external libraries (managed with leiningen).

Is there any existing package that fully covers cases 1 and 2?

My clojure-related config:

;;;;;;;;;;;;;;;
;;; clojure ;;;
;;;;;;;;;;;;;;;

(require 'nrepl)

;; Configure nrepl.el
(setq nrepl-hide-special-buffers t)
(setq nrepl-popup-stacktraces-in-repl t)
(setq nrepl-history-file "~/.emacs.d/nrepl-history")

;; Some default eldoc facilities
(add-hook 'nrepl-connected-hook
      (defun pnh-clojure-mode-eldoc-hook ()
        (add-hook 'clojure-mode-hook 'turn-on-eldoc-mode)
        (add-hook 'nrepl-interaction-mode-hook 'nrepl-turn-on-eldoc-mode)
        (nrepl-enable-on-existing-clojure-buffers)))

;; Repl mode hook
(add-hook 'nrepl-mode-hook 'subword-mode)

;; Auto completion for NREPL
(require 'ac-nrepl)
(eval-after-load "auto-complete"
  '(add-to-list 'ac-modes 'nrepl-mode))

(add-hook 'nrepl-mode-hook 'ac-nrepl-setup)
(add-hook 'clojure-nrepl-mode-hook 'ac-nrepl-setup)

(define-key clojure-mode-map (kbd "C-<ret>") 'nrepl-eval-expression-at-point)

;(global-set-key (kbd "C-<ret>") 'nrepl-eval-expression-at-point)

;;;;;;;;;;;;;;;;;;;;;
;;; auto-complete ;;; 
;;;;;;;;;;;;;;;;;;;;;

(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories "~/.emacs.d/ac-dict")
(setq ac-delay 0.0)
(setq ac-use-quick-help t)
(setq ac-quick-help-delay 0.05)
(setq ac-use-fuzzy 1)
(setq ac-auto-start 1)
(setq ac-auto-show-menu 1)
(ac-config-default)

(define-key ac-mode-map (kbd "C-SPC") 'auto-complete)
3

There are 3 answers

0
Leon Grapenthin On BEST ANSWER

After installing a fresh nrepl and ac-nrepl, here is what I put into the .emacs

(package-initialize)
(require 'auto-complete)
(global-auto-complete-mode)
(add-hook 'nrepl-mode-hook 'ac-nrepl-setup)
(add-hook 'nrepl-interaction-mode-hook 'ac-nrepl-setup)
(add-hook 'clojure-nrepl-mode-hook 'ac-nrepl-setup)

When I now nrepl-jack-in into a test-project and open a file of it, I have tab-completion on every namespace currently loaded in the project. It seems like you forgot the 'ac-nrepl-setup on the 'nrepl-interaction-mode-hook.

0
Alex Ott On

auto-complete should be able to complete from all open clojure files, but right now I can't say that it's possible to complete from the project & libraries. Potentially this could be done via Semantic (from CEDET), but right now there is no parser for clojure code there.

I've started work on Leiningen support in EDE (CEDET's project-related part) - some code exists in CEDET repo, or you can look to my CEDET repo at https://github.com/alexott/cedet/tree/devel (I plan to merge it back to CEDET some time later)

0
ghchoi On
(require 'auto-complete-config)
(require 'clojure-mode)
(require 'cider-mode)
(require 'ac-cider)

(ac-config-default)
;(add-hook 'cider-repl-mode-hook 'ac-cider-setup)                                                                                                                                             
(add-hook 'cider-mode-hook 'ac-cider-setup)
(eval-after-load "auto-complete"
  '(add-to-list 'ac-modes 'cider-repl-mode))

(add-hook 'clojure-mode-hook 'paredit-mode)
;(add-hook 'clojurescript-mode-hook 'paredit-mode)                                                                                                                                            
(add-hook 'clojure-mode-hook 'rainbow-delimiters-mode)
(setq cider-repl-pop-to-buffer-on-connect nil)

(require 'highlight-parentheses)
(add-hook 'clojure-mode-hook
          (lambda ()
            (highlight-parentheses-mode t)))

(defun set-auto-complete-as-completion-at-point-function ()
  (setq completion-at-point-functions '(auto-complete)))
(add-hook 'auto-complete-mode-hook 'set-auto-complete-as-completion-at-point-function)
;(add-hook 'cider-repl-mode-hook 'set-auto-complete-as-completion-at-point-function)                                                                                                          
(add-hook 'cider-mode-hook 'set-auto-complete-as-completion-at-point-function)
(eval-after-load "cider"
  '(define-key cider-mode-map (kbd "C-c C-d") 'ac-cider-popup-doc))

This is my init.el file. My package list contains: clojure-mode cider paredit auto-complete auto-indent-mode highlight-parentheses ac-cider popup

You may need at least ac-cider, cider, clojure-mode, auto-complete, popup for auto-completion feature!

**Once you install and set init.el file, Make a Clojure project using Lein. (It does not work with just .clj file, only work with Leiningen project.) Add

:plugins [[cider/cider-nrepl "0.8.2"]]

to project.clj file then open a source file and run:**

M-x cider-jack-in

Then you must be able to use auto-completion using ac-cider.