]> code.delx.au - gnu-emacs-elpa/commitdiff
company-template-c-like-templatify: use "argN" as actual field text
authorDmitry Gutov <dgutov@yandex.ru>
Sun, 14 Apr 2013 02:45:59 +0000 (06:45 +0400)
committerDmitry Gutov <dgutov@yandex.ru>
Sun, 14 Apr 2013 02:45:59 +0000 (06:45 +0400)
* company-template-add-field: Change the third argument type back; add an
  optional fourth argument; move point to the end of the field.

* company-template-c-like-templatify, company-clang-objc-templatify: Adjust for
  the above. Use `point-marker'.

* company-template-field-at: Extract from `company-template-forward-field'.

NEWS.md
company-clang.el
company-template.el
company-tests.el

diff --git a/NEWS.md b/NEWS.md
index cec4fa2a14bb32608f6935d40badeb78391e32a9..91064a45e54faa3d1e3ec60686c0bc2c474bfd5d 100644 (file)
--- a/NEWS.md
+++ b/NEWS.md
@@ -4,8 +4,9 @@
 
 * `company-auto-complete` is disabled by default.
 * `company-auto-complete-chars` default value includes fewer syntax classes.
+* In expanded function calls, arguments skipped by the user default to "argN".
 * `company-eclim` and `company-clang` do not strip argument types from fields.
-* `company-clang` expand function calls into templates for all three modes now.
+* `company-clang` expands function calls for all three modes now.
 * `company-clang` supports `c++-mode` by default.
 
 ## 2013-04-01 (0.6.7)
index 60e29c1694c15a96812c47bbfadf3375a55ce37f..954b9be5c6589b516d69a1f59bc72380c65cac42 100644 (file)
@@ -208,22 +208,18 @@ Prefix files (-include ...) can be selected with
       0)))
 
 (defun company-clang-objc-templatify (selector)
-  (let* ((end (point))
+  (let* ((end (point-marker))
          (beg (- (point) (length selector)))
          (templ (company-template-declare-template beg end))
          (cnt 0))
     (save-excursion
       (goto-char beg)
-      (while (search-forward ":" end t)
-        (let* ((name (format "arg%d" cnt))
-               (len (length name)))
-          (incf end len)
-          (let ((pt (point)))
-            (insert name)
-            (company-template-add-field templ pt (point)))
-          (when (< (point) end)
-            (insert " ")
-            (incf end))
+      (catch 'stop
+        (while (search-forward ":" end t)
+          (company-template-add-field templ (point) (format "arg%d" cnt))
+          (if (< (point) end)
+              (insert " ")
+            (throw 'stop t))
           (incf cnt))))
     (company-template-move-to-first templ)))
 
index fd4447fb17e1d8443e3daddf2797b9ddc5a42282..6ffd648f37f298605e3fe1dc857508dd49837922 100644 (file)
            (setq minimum pos)))
     (push-mark)
     (goto-char minimum)
-    (let ((field (loop for ovl in (overlays-at start)
-                       when (overlay-get ovl 'company-template-parent)
-                       return ovl)))
-      (company-template-remove-field field))))
+    (company-template-remove-field (company-template-field-at start))))
+
+(defun company-template-field-at (&optional point)
+  (loop for ovl in (overlays-at (or point (point)))
+        when (overlay-get ovl 'company-template-parent)
+        return ovl))
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
         (delq templ company-template--buffer-templates))
   (delete-overlay templ))
 
-(defun company-template-add-field (templ beg end)
-  "Add a field to template TEMPL, from BEG to END."
+(defun company-template-add-field (templ pos text &optional display)
+  "Add new field to template TEMPL at POS, inserting TEXT.
+When DISPLAY is non-nil, set the respective property on the overlay.
+Leave point at the end of the field."
   (assert templ)
-  (save-excursion
-    (when (> end (overlay-end templ))
-      (move-overlay templ (overlay-start templ) end))
-    (let ((ov (make-overlay beg end))
-          (siblings (overlay-get templ 'company-template-fields)))
-      ;; (overlay-put ov 'evaporate t)
-      (overlay-put ov 'intangible t)
-      (overlay-put ov 'face 'company-template-field)
-      (overlay-put ov 'company-template-parent templ)
-      (overlay-put ov 'insert-in-front-hooks '(company-template-insert-hook))
-      (push ov siblings)
-      (overlay-put templ 'company-template-fields siblings))))
+  (goto-char pos)
+  (insert text)
+  (when (> (point) (overlay-end templ))
+    (move-overlay templ (overlay-start templ) (point)))
+  (let ((ov (make-overlay pos (+ pos (length text))))
+        (siblings (overlay-get templ 'company-template-fields)))
+    ;; (overlay-put ov 'evaporate t)
+    (overlay-put ov 'intangible t)
+    (overlay-put ov 'face 'company-template-field)
+    (when display
+      (overlay-put ov 'display display))
+    (overlay-put ov 'company-template-parent templ)
+    (overlay-put ov 'insert-in-front-hooks '(company-template-insert-hook))
+    (push ov siblings)
+    (overlay-put templ 'company-template-fields siblings)))
 
 (defun company-template-remove-field (ovl &optional clear)
   (when (overlayp ovl)
 ;; common ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
 (defun company-template-c-like-templatify (call)
-  (let* ((end (point))
-         (beg (- (point) (length call))))
+  (let* ((end (point-marker))
+         (beg (- (point) (length call)))
+         (cnt 0))
     (goto-char beg)
     (when (search-forward "(" end 'move)
       (if (eq (char-after) ?\))
           (forward-char 1)
         (let ((templ (company-template-declare-template beg end)))
           (while (re-search-forward (concat " *\\([^,)]*\\)[,)]") end t)
-            (company-template-add-field templ
-                                        (match-beginning 1)
-                                        (match-end 1)))
+            (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))
+              (incf cnt)))
           (company-template-move-to-first templ))))))
 
 (provide 'company-template)
index 5aa4fd8bb0ea7f76b921dda9f8b6f49a5ce66d8c..309d8f1f8b4832a64fb8f562318488dd927f0544 100644 (file)
@@ -29,6 +29,8 @@
 (require 'company)
 (require 'company-keywords)
 
+;;; Core
+
 (ert-deftest company-sorted-keywords ()
   "Test that keywords in `company-keywords-alist' are in alphabetical order."
   (dolist (pair company-keywords-alist)
         (company-call 'open-line 1)
         (should (eq 2 (overlay-start company-pseudo-tooltip-overlay)))))))
 
+;;; Template
+
 (ert-deftest company-template-removed-after-the-last-jump ()
   (with-temp-buffer
-    (insert "{ foo foo }")
+    (insert "{ }")
     (goto-char 2)
     (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
       (save-excursion
         (dotimes (i 2)
-          (search-forward "foo")
-          (company-template-add-field tpl (match-beginning 0) (match-end 0))))
+          (insert " ")
+          (company-template-add-field tpl (point) "foo")))
       (company-call 'template-forward-field)
       (should (= 3 (point)))
       (company-call 'template-forward-field)
 
 (ert-deftest company-template-removed-after-input-and-jump ()
   (with-temp-buffer
-    (insert "{ bar }")
+    (insert "{ }")
     (goto-char 2)
     (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
-      (company-template-add-field tpl 3 6)
+      (save-excursion
+        (insert " ")
+        (company-template-add-field tpl (point) "bar"))
       (company-call 'template-move-to-first tpl)
       (should (= 3 (point)))
       (dolist (c (string-to-list "tee"))
     (let ((this-command command))
       (run-hooks 'post-command-hook))))
 
+(ert-deftest company-template-c-like-templatify ()
+  (with-temp-buffer
+    (let ((text "foo(int a, short b)"))
+      (insert text)
+      (company-template-c-like-templatify text)
+      (should (equal "foo(arg0, arg1)" (buffer-string)))
+      (should (looking-at "arg0"))
+      (should (equal "int a"
+                     (overlay-get (company-template-field-at) 'display))))))
+
+;;; Elisp
+
 (defmacro company-elisp-with-buffer (contents &rest body)
   (declare (indent 0))
   `(with-temp-buffer
   (company-elisp-with-buffer
     "(defun foob ()|)"
     (should (equal "" (company-elisp 'prefix)))))
+
+;;; Clang
+
+(ert-deftest company-clang-objc-templatify ()
+  (with-temp-buffer
+    (let ((text "createBookWithTitle:andAuthor:"))
+      (insert text)
+      (company-clang-objc-templatify text)
+      (should (equal "createBookWithTitle:arg0 andAuthor:arg1" (buffer-string)))
+      (should (looking-at "arg0"))
+      (should (null (overlay-get (company-template-field-at) 'display))))))