emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] 20/30: Handle non-printable and multiple-width chars in completio


From: Dmitry Gutov
Subject: [elpa] 20/30: Handle non-printable and multiple-width chars in completions
Date: Tue, 14 Oct 2014 22:22:52 +0000

dgutov pushed a commit to branch master
in repository elpa.

commit a0e1a71fa288c0db6dc855fdc92ef53318fe7a39
Author: Dmitry Gutov <address@hidden>
Date:   Wed Oct 8 07:05:50 2014 +0400

    Handle non-printable and multiple-width chars in completions
    
    #199
---
 company-tests.el |   21 ++++++++++++++++++++-
 company.el       |   22 +++++++++++++++++++++-
 2 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/company-tests.el b/company-tests.el
index 6060aed..517ad41 100644
--- a/company-tests.el
+++ b/company-tests.el
@@ -557,8 +557,27 @@
       (should (eq 'company-tooltip-selection
                   (get-text-property (1- ww) 'face
                                      (car res))))
+      )))
+
+(ert-deftest company-create-lines-clears-out-non-printables ()
+  (let (company-show-numbers
+        (company-candidates (list
+                             (decode-coding-string "avalis\351e" 'utf-8)
+                             "avatar"))
+        (company-candidates-length 2)
+        (company-backend 'ignore))
+    (should (equal '(" avalis‗e    "
+                     " avatar      ")
+                   (company--create-lines 0 999)))))
 
-)))
+(ert-deftest company-create-lines-handles-multiple-width ()
+  (let (company-show-numbers
+        (company-candidates '("蛙蛙蛙蛙" "蛙abc"))
+        (company-candidates-length 2)
+        (company-backend 'ignore))
+    (should (equal '(" 蛙蛙蛙蛙 "
+                     " 蛙abc    ")
+                   (company--create-lines 0 999)))))
 
 (ert-deftest company-column-with-composition ()
   :tags '(interactive)
diff --git a/company.el b/company.el
index c7b7be7..0ae9b27 100644
--- a/company.el
+++ b/company.el
@@ -2093,6 +2093,7 @@ If SHOW-VERSION is non-nil, show the version in the echo 
area."
          (common (or (company-call-backend 'match value)
                      (length company-common)))
          (ann-ralign company-tooltip-align-annotations)
+         (value (company--clean-string value))
          (ann-truncate (< width
                           (+ (length value) (length annotation)
                              (if ann-ralign 1 0))))
@@ -2151,6 +2152,25 @@ If SHOW-VERSION is non-nil, show the version in the echo 
area."
                              line)))
     line))
 
+(defun company--clean-string (str)
+  (replace-regexp-in-string
+   "\\([^[:graph:] ]\\)\\|\\(\ufeff\\)\\|[[:multibyte:]]"
+   (lambda (match)
+     (cond
+      ((match-beginning 1)
+       ;; FIXME: Better char for 'non-printable'?
+       ;; We shouldn't get any of these, but sometimes we might.
+       "\u2017")
+      ((match-beginning 2)
+       ;; Zero-width non-breakable space.
+       "")
+      ((> (string-width match) 1)
+       (concat
+        (make-string (1- (string-width match)) ?\ufeff)
+        match))
+      (t match)))
+   str))
+
 ;;; replace
 
 (defun company-buffer-lines (beg end)
@@ -2278,7 +2298,7 @@ If SHOW-VERSION is non-nil, show the version in the echo 
area."
           ;; `lisp-completion-at-point' adds a space.
           (setq annotation (comment-string-strip annotation t nil)))
         (push (cons value annotation) items)
-        (setq width (max (+ (length value)
+        (setq width (max (+ (string-width value)
                             (if (and annotation 
company-tooltip-align-annotations)
                                 (1+ (length annotation))
                               (length annotation)))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]