]> code.delx.au - gnu-emacs-elpa/commitdiff
company-modify-line: Handle an empty first line with line-prefix
authorDmitry Gutov <dgutov@yandex.ru>
Sun, 26 May 2013 02:03:06 +0000 (06:03 +0400)
committerDmitry Gutov <dgutov@yandex.ru>
Sun, 26 May 2013 02:03:06 +0000 (06:03 +0400)
* Fixes #24 follow-up.
* Cleaner code, less display magic.

company-tests.el
company.el

index 604e8c29f316bd31da5cf3a85a5ada75d406de51..9f837cbae99a1aa52ea21006273582acf494cec4 100644 (file)
 
 (ert-deftest company-modify-line-with-line-prefix ()
   (let ((str (propertize "foobar" 'line-prefix "-*-")))
-    (should (string= (company-modify-line str "zz" 4)
-                     "fzzbar"))
-    (should (string= (company-modify-line str "zzxx" 0)
-                     "zzxxoobar"))
-    (should (string= (company-modify-line str "zzxx" 0)
-                     "zzxxoobar"))
-    (should (string= (company-modify-line str "zz" 10)
-                     "foobar zz"))))
+    (should (equal-including-properties
+             (company-modify-line str "zz" 4)
+             "-*-fzzbar"))
+    (should (equal-including-properties
+             (company-modify-line str "zzxx" 1)
+             "-zzxxobar"))
+    (should (equal-including-properties
+             (company-modify-line str "xx" 0)
+             "xx-foobar"))
+    (should (equal-including-properties
+             (company-modify-line str "zz" 10)
+             "-*-foobar zz"))))
 
 ;;; Template
 
index f4b91c309a48bef99f8c05e57e2118403e6aa127..1a98036ea28e7bf0b37e4cee88314acceb510893 100644 (file)
@@ -1671,16 +1671,13 @@ Example:
     (nreverse lines)))
 
 (defun company-modify-line (old new offset)
-  (let ((prefix (get-text-property 0 'line-prefix old))
-        before)
-    (when prefix
-      (if (<= offset (length prefix))
-        (setq before (substring prefix 0 offset)))
-      (decf offset (length prefix)))
-    (concat (or before (company-safe-substring old 0 offset))
+  (let ((prefix (get-text-property 0 'line-prefix old)))
+    (when prefix ; Keep the original value unmodified, for no special reason.
+      (setq old (concat prefix old))
+      (remove-text-properties 0 (length old) '(line-prefix) old))
+    (concat (company-safe-substring old 0 offset)
             new
-            (company-safe-substring old
-                                    (max (+ offset (length new)) 0)))))
+            (company-safe-substring old (+ offset (length new))))))
 
 (defsubst company--length-limit (lst limit)
   (if (nthcdr limit lst)
@@ -1850,6 +1847,8 @@ Returns a negative number if the tooltip should be displayed above point."
     (overlay-put company-pseudo-tooltip-overlay 'invisible t)
     ;; Beat outline's folding overlays, at least.
     (overlay-put company-pseudo-tooltip-overlay 'priority 1)
+    ;; No (extra) prefix for the first line.
+    (overlay-put company-pseudo-tooltip-overlay 'line-prefix "")
     (overlay-put company-pseudo-tooltip-overlay 'before-string
                  (overlay-get company-pseudo-tooltip-overlay 'company-before))
     (overlay-put company-pseudo-tooltip-overlay 'window (selected-window))))