From 3fc9d9ef7b2dd6249b272a25f753dd6ebe412417 Mon Sep 17 00:00:00 2001 From: Dmitry Gutov Date: Wed, 26 Mar 2014 01:00:40 +0200 Subject: [PATCH] Minor tweaks * Rename --explicit-action to --manual-action. * Add tests for '("foo" . t), remove some duplicating tests. * Tweak the defcustom properties. * Simplify `company--good-prefix-p' a bit more; handle t cdr better. --- company-tests.el | 21 +++++++++++---------- company.el | 40 ++++++++++++++++++---------------------- 2 files changed, 29 insertions(+), 32 deletions(-) diff --git a/company-tests.el b/company-tests.el index e5fd2f70e..6b7ce887a 100644 --- a/company-tests.el +++ b/company-tests.el @@ -46,13 +46,15 @@ (ert-deftest company-good-prefix () (let ((company-minimum-prefix-length 5) - company--explicit-action - (company-selection-changed t)) ;never enough + company-abort-manual-when-too-short + company--manual-action ;idle begin + (company-selection-changed t)) ;has no effect (should (eq t (company--good-prefix-p "!@#$%"))) (should (eq nil (company--good-prefix-p "abcd"))) (should (eq nil (company--good-prefix-p 'stop))) (should (eq t (company--good-prefix-p '("foo" . 5)))) - (should (eq nil (company--good-prefix-p '("foo" . 4)))))) + (should (eq nil (company--good-prefix-p '("foo" . 4)))) + (should (eq t (company--good-prefix-p '("foo" . t)))))) (ert-deftest company--manual-prefix-set-and-unset () (with-temp-buffer @@ -72,21 +74,20 @@ (ert-deftest company-abort-manual-when-too-short () (let ((company-minimum-prefix-length 5) (company-abort-manual-when-too-short t) - (company-selection-changed t)) ;never enough - (let ((company--explicit-action nil)) ;idle begin + (company-selection-changed t)) ;has not effect + (let ((company--manual-action nil)) ;idle begin (should (eq t (company--good-prefix-p "!@#$%"))) - (should (eq nil (company--good-prefix-p "abcd"))) - (should (eq nil (company--good-prefix-p 'stop))) (should (eq t (company--good-prefix-p '("foo" . 5)))) - (should (eq nil (company--good-prefix-p '("foo" . 4))))) - (let ((company--explicit-action t) + (should (eq t (company--good-prefix-p '("foo" . t))))) + (let ((company--manual-action t) (company--manual-prefix "abc")) ;manual begin from this prefix (should (eq t (company--good-prefix-p "!@#$"))) (should (eq nil (company--good-prefix-p "ab"))) (should (eq nil (company--good-prefix-p 'stop))) (should (eq t (company--good-prefix-p '("foo" . 4)))) (should (eq t (company--good-prefix-p "abcd"))) - (should (eq t (company--good-prefix-p "abcd")))))) + (should (eq t (company--good-prefix-p "abc"))) + (should (eq t (company--good-prefix-p '("bar" . t))))))) (ert-deftest company-multi-backend-with-lambdas () (let ((company-backend diff --git a/company.el b/company.el index 7f327d4b2..0ff3e85d6 100644 --- a/company.el +++ b/company.el @@ -449,9 +449,7 @@ back-end, consider using the `post-completion' command instead." "If enabled, cancel a manually started completion when the prefix gets shorter than both `company-minimum-prefix-length' and the length of the prefix it was started from." - :group 'company - :type '(choice (const :tag "Off" nil) - (const :tag "On" t))) + :type 'boolean) (defcustom company-require-match 'company-explicit-action-p "If enabled, disallow non-matching input. @@ -855,9 +853,9 @@ means that `company-mode' is always turned on except in `message-mode' buffers." (defvar company-selection-changed nil) (make-variable-buffer-local 'company-selection-changed) -(defvar company--explicit-action nil - "Non-nil, if explicit completion took place.") -(make-variable-buffer-local 'company--explicit-action) +(defvar company--manual-action nil + "Non-nil, if manual completion took place.") +(make-variable-buffer-local 'company--manual-action) (defvar company--manual-prefix nil) (make-variable-buffer-local 'company--manual-prefix) @@ -903,7 +901,7 @@ can retrieve meta-data for them." (defun company-explicit-action-p () "Return whether explicit completion action was taken by the user." - (or company--explicit-action + (or company--manual-action company-selection-changed)) (defun company-reformat (candidate) @@ -1124,12 +1122,12 @@ Keywords and function definition names are ignored." (defun company-manual-begin () (interactive) (company-assert-enabled) - (setq company--explicit-action t) + (setq company--manual-action t) (unwind-protect (let ((company-minimum-prefix-length 0)) (company-auto-begin)) (unless company-candidates - (setq company--explicit-action nil)))) + (setq company--manual-action nil)))) (defun company-other-backend (&optional backward) (interactive (list current-prefix-arg)) @@ -1199,16 +1197,14 @@ Keywords and function definition names are ignored." (defun company--good-prefix-p (prefix) (and (stringp (or (car-safe prefix) prefix)) ;excludes 'stop - (or (and company--manual-prefix - ;; changed selection not enough for valid prefix - (not (and company-abort-manual-when-too-short - ;; must not be less than minimum or initial length - (< (or (cdr-safe prefix) (length prefix)) - (min company-minimum-prefix-length - (length company--manual-prefix)))))) - (or (eq (cdr-safe prefix) t) - (>= (or (cdr-safe prefix) (length prefix)) - company-minimum-prefix-length))))) + (or (eq (cdr-safe prefix) t) + (let ((len (or (cdr-safe prefix) (length prefix)))) + (if company--manual-prefix + (or (not company-abort-manual-when-too-short) + ;; Must not be less than minimum or initial length. + (>= len (min company-minimum-prefix-length + (length company--manual-prefix)))) + (>= len company-minimum-prefix-length)))))) (defun company--continue () (when (company-call-backend 'no-cache company-prefix) @@ -1255,10 +1251,10 @@ Keywords and function definition names are ignored." c (company-calculate-candidates prefix)) ;; t means complete/unique. We don't start, so no hooks. (if (not (consp c)) - (when company--explicit-action + (when company--manual-action (message "No completion found")) (setq company-prefix prefix) - (when company--explicit-action + (when company--manual-action (setq company--manual-prefix prefix)) (when (symbolp backend) (setq company-lighter (concat " " (symbol-name backend)))) @@ -1309,7 +1305,7 @@ Keywords and function definition names are ignored." company-common nil company-selection 0 company-selection-changed nil - company--explicit-action nil + company--manual-action nil company--manual-prefix nil company-lighter company-default-lighter company--point-max nil -- 2.39.2