[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] 28/30: company-template-c-like-templatify: Support generics
From: |
Dmitry Gutov |
Subject: |
[elpa] 28/30: company-template-c-like-templatify: Support generics |
Date: |
Tue, 14 Oct 2014 22:22:56 +0000 |
dgutov pushed a commit to branch master
in repository elpa.
commit c6e9fbad312fc62876d40032a84a94439a055d05
Author: Dmitry Gutov <address@hidden>
Date: Mon Oct 13 00:41:36 2014 +0400
company-template-c-like-templatify: Support generics
Closes #201
---
NEWS.md | 2 +
company-clang.el | 5 ++-
company-template.el | 59 +++++++++++++++++++++++++++++++++++++-------------
company-tests.el | 13 +++++++++++
4 files changed, 61 insertions(+), 18 deletions(-)
diff --git a/NEWS.md b/NEWS.md
index f563e78..51cae3d 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -2,6 +2,8 @@
## Next
+* `company-clang` and `company-template-c-like-templatify` support templated
+ functions and arguments.
* `company-dabbrev` ignores "uninteresting" buffers by default. Depends on the
new user option, `company-dabbrev-ignore-buffers`.
* `company-files` checks directory's last modification time.
diff --git a/company-clang.el b/company-clang.el
index 77a544f..90757a8 100644
--- a/company-clang.el
+++ b/company-clang.el
@@ -110,7 +110,7 @@ or automatically through a custom
`company-clang-prefix-guesser'."
;; TODO: Handle Pattern (syntactic hints would be neat).
;; Do we ever see OVERLOAD (or OVERRIDE)?
(defconst company-clang--completion-pattern
- "^COMPLETION: \\_<\\(%s[a-zA-Z0-9_:]*\\)\\(?: : \\(.*\\)$\\)?$")
+ "^COMPLETION: \\_<\\(%s[a-zA-Z0-9_:<>]*\\)\\(?: : \\(.*\\)$\\)?$")
(defconst company-clang--error-buffer-name "*clang-error*")
@@ -320,7 +320,8 @@ passed via standard input."
(insert anno)
(if (string-match "\\`:[^:]" anno)
(company-clang-objc-templatify anno)
- (company-template-c-like-templatify anno)))))))
+ (company-template-c-like-templatify
+ (concat arg anno))))))))
(provide 'company-clang)
;;; company-clang.el ends here
diff --git a/company-template.el b/company-template.el
index 576b246..bc1055c 100644
--- a/company-template.el
+++ b/company-template.el
@@ -1,6 +1,6 @@
;;; company-template.el
-;; Copyright (C) 2009, 2010, 2013 Free Software Foundation, Inc.
+;; Copyright (C) 2009, 2010, 2014 Free Software Foundation, Inc.
;; Author: Nikolaj Schumacher
@@ -149,22 +149,49 @@ Leave point at the end of the field."
(defun company-template-c-like-templatify (call)
(let* ((end (point-marker))
(beg (- (point) (length call)))
- (cnt 0))
- (when (re-search-backward ")" beg t)
- (delete-region (match-end 0) end))
- (goto-char beg)
- (when (search-forward "(" end 'move)
- (if (eq (char-after) ?\))
+ (cnt 0)
+ (templ (company-template-declare-template beg end))
+ paren-open paren-close)
+ (with-syntax-table (make-char-table 'syntax-table nil)
+ (modify-syntax-entry ?\( "(")
+ (modify-syntax-entry ?\) ")")
+ (modify-syntax-entry ?< "(")
+ (modify-syntax-entry ?> ")")
+ (when (search-backward ")" beg t)
+ (setq paren-close (point-marker))
+ (forward-char 1)
+ (delete-region (point) end)
+ (backward-sexp)
+ (forward-char 1)
+ (setq paren-open (point-marker)))
+ (when (search-backward ">" beg t)
+ (let ((angle-close (point-marker)))
(forward-char 1)
- (let ((templ (company-template-declare-template beg end)))
- (while (re-search-forward (concat " *\\([^,)]*\\)[,)]") end t)
- (let ((sig (match-string 1)))
- (delete-region (match-beginning 1) (match-end 1))
- (save-excursion
- (company-template-add-field templ (match-beginning 1)
- (format "arg%d" cnt) sig))
- (cl-incf cnt)))
- (company-template-move-to-first templ))))))
+ (backward-sexp)
+ (forward-char)
+ (setq cnt (company-template--c-like-args templ angle-close
+ cnt))))
+ (when paren-open
+ (goto-char paren-open)
+ (company-template--c-like-args templ paren-close cnt)))
+ (if (overlay-get templ 'company-template-fields)
+ (company-template-move-to-first templ)
+ (company-template-remove-template templ)
+ (goto-char end))))
+
+(defun company-template--c-like-args (templ end counter)
+ (let ((last-pos (point)))
+ (while (re-search-forward "\\([^,]+\\),?" end 'move)
+ (when (zerop (car (parse-partial-sexp last-pos (point))))
+ (let ((sig (buffer-substring-no-properties last-pos (match-end 1))))
+ (save-excursion
+ (company-template-add-field templ last-pos
+ (format "arg%d" counter) sig)
+ (delete-region (point) (+ (point) (length sig))))
+ (skip-chars-forward " ")
+ (setq last-pos (point))
+ (cl-incf counter)))))
+ counter)
(provide 'company-template)
;;; company-template.el ends here
diff --git a/company-tests.el b/company-tests.el
index 00a6227..0663c71 100644
--- a/company-tests.el
+++ b/company-tests.el
@@ -920,6 +920,19 @@ foo2"))
(should (equal "foo(arg0, arg1)" (buffer-string)))
(should (looking-at "arg0")))))
+(ert-deftest company-template-c-like-templatify-generics ()
+ (with-temp-buffer
+ (let ((text "foo<TKey, TValue>(int i, Dict<TKey, TValue>, long l)"))
+ (insert text)
+ (company-template-c-like-templatify text)
+ (should (equal "foo<arg0, arg1>(arg2, arg3, arg4)" (buffer-string)))
+ (should (looking-at "arg0"))
+ (should (equal "TKey" (overlay-get (company-template-field-at)
'display)))
+ (search-forward "arg3")
+ (forward-char -1)
+ (should (equal "Dict<TKey, TValue>"
+ (overlay-get (company-template-field-at) 'display))))))
+
;;; Clang
(ert-deftest company-clang-objc-templatify ()
- [elpa] 20/30: Handle non-printable and multiple-width chars in completions, (continued)
- [elpa] 20/30: Handle non-printable and multiple-width chars in completions, Dmitry Gutov, 2014/10/14
- [elpa] 18/30: Use `condition-case-unless-debug' more, Dmitry Gutov, 2014/10/14
- [elpa] 22/30: company-dabbrev--search: Ignore "uninteresting" buffers, Dmitry Gutov, 2014/10/14
- [elpa] 23/30: Make new tests and interactive-only, Dmitry Gutov, 2014/10/14
- [elpa] 24/30: company-fill-propertize: Use `string-width' on `company-common', Dmitry Gutov, 2014/10/14
- [elpa] 25/30: Handle nil company-common, in order not to bind it in tests, Dmitry Gutov, 2014/10/14
- [elpa] 12/30: Update NEWS; do the forgotten version bump, Dmitry Gutov, 2014/10/14
- [elpa] 14/30: company-files.el: Use the "private function" naming convention, Dmitry Gutov, 2014/10/14
- [elpa] 26/30: Call `bbdb-search' inside `eval' block, Dmitry Gutov, 2014/10/14
- [elpa] 27/30: Extract regexp for buffers to ignore into a new var, Dmitry Gutov, 2014/10/14
- [elpa] 28/30: company-template-c-like-templatify: Support generics,
Dmitry Gutov <=
- [elpa] 29/30: Bump the version, Dmitry Gutov, 2014/10/14
- [elpa] 30/30: Merge commit '60d4c09c982a1c562a70cd6aa705f47ab3badcfb' from company, Dmitry Gutov, 2014/10/14