feat(emacs/lean-company): add lean-company

This commit is contained in:
Soonho Kong 2014-09-04 15:46:41 -07:00
parent e7f6228001
commit cc89cd051a
2 changed files with 49 additions and 70 deletions

View file

@ -1,81 +1,61 @@
;; -*- lexical-binding: t; -*-
;; Copyright (c) 2014 Microsoft Corporation. All rights reserved.
;; Released under Apache 2.0 license as described in the file LICENSE.
;;
;; Author: Soonho Kong
;;
(require 'company) (require 'company)
(defun company-lean () (require 'company-etags)
(set (make-local-variable 'company-backends) '(company-lean)) (require 'dash)
(company-mode)) (require 'dash-functional)
(add-hook 'lean-mode-hook 'company-lean) (require 'lean-tags)
(require 'lean-server)
(setq company-tooltip-limit 20) ; bigger popup window (defun company-lean-hook ()
(setq company-idle-delay .3) ; decrease delay before autocompletion popup shows (set (make-local-variable 'company-backends) '(company-lean))
(setq company-echo-delay 0) ; remove annoying blinking (setq-local company-tooltip-limit 20) ; bigger popup window
(setq company-begin-commands '(self-insert-command)) ; start autocompletion only after typing (setq-local company-idle-delay .3) ; decrease delay before autocompletion popup shows
(setq-local company-echo-delay 0) ; remove annoying blinking
(setq-local company-begin-commands '(self-insert-command)) ; start autocompletion only after typing
(company-mode t))
(defun company-lean--prefix () (defun company-lean--prefix ()
"Returns the symbol to complete. Also, if point is on a dot, "Returns the symbol to complete. Also, if point is on a dot,
triggers a completion immediately." triggers a completion immediately."
(if company-lean-begin-after-member-access (let ((prefix (company-grab-symbol)))
(company-grab-symbol-cons "\\." 1) (when (or
(company-grab-symbol))) (> (length prefix) 3))
prefix)))
(defun company-go--invoke-autocomplete () (defun company-lean--make-candidate (arg)
(let ((temp-buffer (generate-new-buffer "*leancompany*"))) (propertize (car arg) 'type (cdr arg)))
(prog2
(call-process-region (point-min)
(point-max)
"leancompany"
nil
temp-buffer
nil
"-f=csv"
"autocomplete"
(or (buffer-file-name) "")
(concat "c" (int-to-string (- (point) 1))))
(with-current-buffer temp-buffer (buffer-string))
(kill-buffer temp-buffer))))
(defun company-lean--candidates () (defun company-lean--candidates (prefix)
(company-lean--get-candidates (split-string (company-lean--invoke-autocomplete) "\n" t))) (let ((line-number (line-number-at-pos)))
(lean-server-send-cmd-sync (lean-cmd-findp line-number prefix)
(lambda (candidates)
(-map 'company-lean--make-candidate candidates)))))
(defun company-lean--location (arg)
(lean-generate-tags)
(let ((tags-table-list (company-etags-buffer-table)))
(when (fboundp 'find-tag-noselect)
(save-excursion
(let ((buffer (find-tag-noselect arg)))
(cons buffer (with-current-buffer buffer (point))))))))
(defun company-lean--annotation (candidate)
(let ((type (get-text-property 0 'type candidate)))
(when type
(format " : %s" type))))
;;;###autoload ;;;###autoload
(defun company-lean (command &optional arg &rest ignored) (defun company-lean (command &optional arg &rest ignored)
(case command (case command
(prefix (and (derived-mode-p 'lean-mode) (prefix (company-lean--prefix))
(not (company-in-string-or-comment)) (candidates (company-lean--candidates arg))
(or (company-lean--prefix) 'stop))) (annotation (company-lean--annotation arg))
(candidates (company-lean--candidates))
(meta (get-text-property 0 'meta arg))
(annotation
(when company-lean-show-annotation
(get-text-property 0 'meta arg)))
(location (company-lean--location arg)) (location (company-lean--location arg))
(sorted t))) (sorted t)))
(provide 'company-lean) (provide 'lean-company)
(defun company-lean--doc-buffer (candidate)
(message "doc candidate = %S" candidate)
(company-doc-buffer (get-text-property 0 'doc candidate)))
(defun company-lean--annotation (candidate)
(message "annotation candidate = %S" candidate)
(let ((anno (get-text-property 0 'kind candidate)))
(when anno (concat " blabla" anno)))
" : annotation1")
(defun company-lean--candidates (prefix)
(cl-loop for x in '("foobar" "foobaz" "foobarbaz")
collect x
))
(defun company-my-backend (command &optional arg &rest ignored)
(interactive (list 'interactive))
(case command
(interactive (company-begin-backend 'company-my-backend))
(prefix (when (looking-back "foo\\>")
(match-string 0)))
(candidates (company-lean--candidates arg))
(annotation (company-lean--annotation arg))
))
(set (make-local-variable 'company-backends) '(company-my-backend))
(company-mode)

View file

@ -26,6 +26,7 @@
(require 'lean-option) (require 'lean-option)
(require 'lean-syntax) (require 'lean-syntax)
(require 'lean-mmm-lua) (require 'lean-mmm-lua)
(require 'lean-company)
(defun lean-compile-string (exe-name args file-name) (defun lean-compile-string (exe-name args file-name)
"Concatenate exe-name, args, and file-name" "Concatenate exe-name, args, and file-name"
@ -139,9 +140,7 @@ enabled and disabled respectively.")
(lean-eldoc-documentation-function)) (lean-eldoc-documentation-function))
;; company-mode ;; company-mode
(when lean-company-use (when lean-company-use
(require 'company) (company-lean-hook)))
(company-mode t)
(set (make-local-variable 'company-backends) '(company-etags))))
;; Automode List ;; Automode List
;;;###autoload ;;;###autoload