Elffuss BraidHub

kikocis/company-english-helper public

Make prefix-match-candidates as first candidates, then suffix-match-candidates and fuzz-match-candidates.
Changesetaa289f8aed7ac5032894468e2f10eaf55b32f440bf1a34c94fbe528cad37305d
AuthorAndy Stewart <lazycat.manatee@gmail.com> · 2018-08-28 23:15 +08:00
CommitterAndy Stewart <lazycat.manatee@gmail.com> · 2018-08-28 23:15 +08:00
Parentsaf4f8814e395

Browse the files at this changeset

3 files changed

modified company-english-helper-data.el +0 −0

Not shown: the file is larger than the hub shows, or this page holds as much as it shows. View the file.

modified company-english-helper.el +51 −25
@@ -6,8 +6,8 @@
66 ;; Maintainer: Andy Stewart <lazycat.manatee@gmail.com>
77 ;; Copyright (C) 2018, Andy Stewart, all rights reserved.
88 ;; Created: 2018-07-06 23:22:22
9-;; Version: 0.8
10-;; Last-Updated: 2018-08-28 21:58:45
9+;; Version: 1.0
10+;; Last-Updated: 2018-08-28 22:17:36
1111 ;; By: Andy Stewart
1212 ;; URL: http://www.emacswiki.org/emacs/download/company-english-helper.el
1313 ;; Keywords:
@@ -68,6 +68,7 @@
6868 ;;
6969 ;; 2018/08/27
7070 ;; * Add fuzz search algorithm.
71+;; * Make prefix-match-candidates as first candidates, then suffix-match-candidates and fuzz-match-candidates.
7172 ;;
7273 ;; 2018/08/10
7374 ;; * Require `cl' avoid error "Symbol's function definition is void: remove-if-not".
@@ -103,15 +104,18 @@
103104
104105 ;;; Code:
105106
106-(defvar company-en-words-candidate-max-width 30
107+(defvar company-english-helper-candidate-max-width 30
107108 "The max width of candidates.
108109 Default is 30, it will occur candidate is not alignment if this value too small.")
109110
110-(defvar company-en-words-active-p nil
111- "The status of company-en-words plugins.
111+(defvar company-english-helper-active-p nil
112+ "The status of company-english-helper plugins.
112113 Default is disable.")
113114
114-(defun en-words-annotation (s)
115+(defvar company-english-helper-match-group-size 10
116+ "The max size of match group.")
117+
118+(defun english-helper-annotation (s)
115119 (let* ((translation (get-text-property 0 :initials s))
116120 (translation-format-string (replace-regexp-in-string "\\cc" "" translation))
117121 (max-translation-length (+ 1 (apply 'max (mapcar 'length company-candidates))))
@@ -119,42 +123,64 @@
119123 (translation-length (length translation))
120124 (translation-format-length (length translation-format-string))
121125 (blank-length (max 0 (- max-translation-length candidate-length)))
122- (dot-length (max 0 (- company-en-words-candidate-max-width (- translation-length translation-format-length)))))
126+ (dot-length (max 0 (- company-english-helper-candidate-max-width (- translation-length translation-format-length)))))
123127 (format "%s" (concat (make-string blank-length ?\ )
124128 translation
125129 (make-string dot-length ?\.)))))
126130
127-(defun company-en-words (command &optional arg &rest ignored)
131+(defun company-english-helper-search (command &optional arg &rest ignored)
128132 (interactive (list 'interactive))
129133 (cl-case command
130- (interactive (company-begin-backend 'company-en-words))
134+ (interactive (company-begin-backend 'company-english-helper-search))
131135 (prefix (company-grab-symbol))
132136 (candidates
133- (remove-if-not
134- (lambda (c) (or (string-prefix-p (downcase arg) c)
135- (string-suffix-p (downcase arg) c)
136- (and (string-match-p "-" (downcase arg))
137- (and (string-prefix-p (car (split-string (downcase arg) "-")) c)
138- (string-suffix-p (cadr (split-string (downcase arg) "-")) c)
137+ (progn
138+ (setq prefix-match-candidates
139+ (remove-if-not
140+ (lambda (c) (string-prefix-p (downcase arg) c))
141+ english-helper-completions))
142+
143+ (setq suffix-match-candidates
144+ (remove-if-not
145+ (lambda (c) (string-suffix-p (downcase arg) c))
146+ english-helper-completions))
147+
148+ (setq fuzz-match-candidates
149+ (remove-if-not
150+ (lambda (c)
151+ (and (string-match-p "-" (downcase arg))
152+ (let* ((split-list (split-string (downcase arg) "-"))
153+ (left-string (car split-list))
154+ (right-string (cadr split-list)))
155+ (and (string-prefix-p left-string c)
156+ (string-match-p right-string (string-remove-prefix left-string c))
139157 ))))
140- en-words-completions))
141- (annotation (en-words-annotation arg))
158+ english-helper-completions))
159+
160+ ;; Make prefix-match-candidates as first candidates, then suffix-match-candidates and fuzz-match-candidates.
161+ (delete-dups (append
162+ (subseq prefix-match-candidates 0 (min company-english-helper-match-group-size (length prefix-match-candidates)))
163+ (subseq suffix-match-candidates 0 (min company-english-helper-match-group-size (length suffix-match-candidates)))
164+ (subseq fuzz-match-candidates 0 (min company-english-helper-match-group-size (length fuzz-match-candidates)))
165+ ))
166+ ))
167+ (annotation (english-helper-annotation arg))
142168 (sorted t)
143169 ))
144170
145171 (defun toggle-company-english-helper ()
146172 "Toggle company english helper."
147173 (interactive)
148- (if company-en-words-active-p
174+ (if company-english-helper-active-p
149175 (progn
150- (setq company-backends (remove 'company-en-words company-backends))
151- ;; I need remove `company-en-words' with `company-yasnippet',
152- ;; it's not enough just remove `company-en-words' from `company-backends'
153- (setq company-backends (remove '(company-en-words :with company-yasnippet) company-backends))
154- (setq company-en-words-active-p nil)
176+ (setq company-backends (remove 'company-english-helper-search company-backends))
177+ ;; I need remove `company-english-helper-search' with `company-yasnippet',
178+ ;; it's not enough just remove `company-english-helper-search' from `company-backends'
179+ (setq company-backends (remove '(company-english-helper-search :with company-yasnippet) company-backends))
180+ (setq company-english-helper-active-p nil)
155181 (message "English helper has disable."))
156- (add-to-list 'company-backends 'company-en-words)
157- (setq company-en-words-active-p t)
182+ (add-to-list 'company-backends 'company-english-helper-search)
183+ (setq company-english-helper-active-p t)
158184 (message "English helper has enable.")))
159185
160186 (provide 'company-english-helper)
modified stardict.py +1 −1
@@ -14,7 +14,7 @@
1414 f.write(";;\n")
1515 f.write(";; This file is generate by stardict.py, please don't edit this file.\n")
1616 f.write(";;\n\n")
17- f.write("(defconst en-words-completions\n '(\n")
17+ f.write("(defconst english-helper-completions\n '(\n")
1818
1919 index = 0
2020 for word in start_dictionary.keys():