From d2587c44cefd96b52a27bbc296cb8156eb2ad773 Mon Sep 17 00:00:00 2001 From: Dmitry Gutov Date: Sun, 26 May 2013 06:03:06 +0400 Subject: [PATCH] company-modify-line: Handle an empty first line with line-prefix * Fixes #24 follow-up. * Cleaner code, less display magic. --- company-tests.el | 20 ++++++++++++-------- company.el | 17 ++++++++--------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/company-tests.el b/company-tests.el index 604e8c29f..9f837cbae 100644 --- a/company-tests.el +++ b/company-tests.el @@ -212,14 +212,18 @@ (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 diff --git a/company.el b/company.el index f4b91c309..1a98036ea 100644 --- a/company.el +++ b/company.el @@ -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)))) -- 2.39.2