branch: externals/pyim commit c579cc8a55bf4020472e109b5d2b0ac65d01c003 Author: Feng Shu <tuma...@163.com> Commit: Feng Shu <tuma...@163.com>
添加 pyim-dhashcache-update-shortcode2word-1 * .gitignore: Ignore temp dcache dir. * tests/pyim-tests.el (pyim-tests-pyim-dhashcache-update-shortcode2word): New test. * pyim-dhashcache.el (pyim-dhashcache-update-shortcode2word-1): New function. (pyim-dhashcache-update-shortcode2word): use pyim-dhashcache-update-shortcode2word-1. --- .gitignore | 2 ++ pyim-dhashcache.el | 56 ++++++++++++++++++++++++++++------------------------- tests/pyim-tests.el | 31 +++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index 25e1d2b..4149ef5 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,5 @@ deps/ *.elc /pyim-autoloads.el /pyim-pkg.el +/pyim-dcache-* +/tests/pyim-dcache-* \ No newline at end of file diff --git a/pyim-dhashcache.el b/pyim-dhashcache.el index e02763a..fe90ebf 100644 --- a/pyim-dhashcache.el +++ b/pyim-dhashcache.el @@ -139,35 +139,39 @@ (require 'pyim-dhashcache) (pyim-dcache-set-variable 'pyim-dhashcache-code2word) (pyim-dcache-set-variable 'pyim-dhashcache-iword2count) - (setq pyim-dhashcache-shortcode2word - (make-hash-table :test #'equal)) - (maphash - (lambda (key value) - (dolist (x (pyim-dhashcache-get-shortcode key)) - (puthash x - (mapcar - (lambda (word) - ;; 这个地方的代码用于实现五笔 code 自动提示功能, - ;; 比如输入 'aa' 后得到选词框: - ;; ---------------------- - ;; | 1. 莁aa 2.匶wv ... | - ;; ---------------------- - (if (get-text-property 0 :comment word) - word - (propertize word :comment (substring key (length x))))) - (delete-dups `(,@value ,@(gethash x pyim-dhashcache-shortcode2word)))) - pyim-dhashcache-shortcode2word))) - pyim-dhashcache-code2word) - (maphash - (lambda (key value) - (puthash key (pyim-dhashcache-sort-words value) - pyim-dhashcache-shortcode2word)) - pyim-dhashcache-shortcode2word) - (pyim-dcache-save-variable 'pyim-dhashcache-shortcode2word) - nil) + (pyim-dhashcache-update-shortcode2word-1)) (lambda (_) (pyim-dcache-set-variable 'pyim-dhashcache-shortcode2word t))))) +(defun pyim-dhashcache-update-shortcode2word-1 () + "`pyim-dhashcache-update-shortcode2word' 的内部函数" + (setq pyim-dhashcache-shortcode2word + (make-hash-table :test #'equal)) + (maphash + (lambda (key value) + (dolist (x (pyim-dhashcache-get-shortcode key)) + (puthash x + (mapcar + (lambda (word) + ;; 这个地方的代码用于实现五笔 code 自动提示功能, + ;; 比如输入 'aa' 后得到选词框: + ;; ---------------------- + ;; | 1. 莁aa 2.匶wv ... | + ;; ---------------------- + (if (get-text-property 0 :comment word) + word + (propertize word :comment (substring key (length x))))) + (delete-dups `(,@(gethash x pyim-dhashcache-shortcode2word) ,@value))) + pyim-dhashcache-shortcode2word))) + pyim-dhashcache-code2word) + (maphash + (lambda (key value) + (puthash key (pyim-dhashcache-sort-words value) + pyim-dhashcache-shortcode2word)) + pyim-dhashcache-shortcode2word) + (pyim-dcache-save-variable 'pyim-dhashcache-shortcode2word) + nil) + (defun pyim-dhashcache-get-path (variable) "获取保存 VARIABLE 取值的文件的路径." (when (symbolp variable) diff --git a/tests/pyim-tests.el b/tests/pyim-tests.el index 332eac7..8741f01 100644 --- a/tests/pyim-tests.el +++ b/tests/pyim-tests.el @@ -639,6 +639,37 @@ zuo-zuo-you-mang 作作有芒") (should (equal (gethash "啊" output2) '("a"))) (should (equal (gethash "啊啊" output2) nil)))) +(ert-deftest pyim-tests-pyim-dhashcache-update-shortcode2word () + (let ((pyim-dcache-directory (file-name-as-directory (make-temp-name "pyim-dcache-"))) + (pyim-dhashcache-code2word (make-hash-table :test #'equal)) + (pyim-dhashcache-iword2count (make-hash-table :test #'equal)) + (pyim-dhashcache-shortcode2word (make-hash-table :test #'equal)) + output) + + (puthash "wubi/a" '("戈") pyim-dhashcache-code2word) + (puthash "wubi/aa" '("式" "藏") pyim-dhashcache-code2word) + (puthash "wubi/aaa" '("工") pyim-dhashcache-code2word) + (puthash "wubi/aaaa" '("工" "㠭") pyim-dhashcache-code2word) + (puthash "wubi/aaab" '("㐂") pyim-dhashcache-code2word) + (puthash "wubi/aaae" '("𧝣") pyim-dhashcache-code2word) + + (pyim-dhashcache-update-shortcode2word-1) + + (with-temp-buffer + (insert-file-contents (concat pyim-dcache-directory "pyim-dhashcache-shortcode2word")) + (setq output (read (current-buffer)))) + + (should (equal (gethash "wubi/aa" output) + '(#("工" 0 1 (:comment "a")) + #("㠭" 0 1 (:comment "aa")) + #("㐂" 0 1 (:comment "ab")) + #("𧝣" 0 1 (:comment "ae"))))) + (should (equal (gethash "wubi/aaa" output) + '(#("工" 0 1 (:comment "a")) + #("㠭" 0 1 (:comment "a")) + #("㐂" 0 1 (:comment "b")) + #("𧝣" 0 1 (:comment "e"))))))) + ;; ** pyim-dregcache 相关单元测试 (ert-deftest pyim-tests-pyim-general () (let ((pyim-dcache-backend 'pyim-dregcache))