Commit b79c3cae authored by Yuanle Song's avatar Yuanle Song
Browse files

zero-pinyin-service.el GetCandidates add fetch-size param

parent f4a20631
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -186,7 +186,7 @@ if t, `zero-debug' will output debug msg in *zero-debug* buffer")
    (zero-panel-show-candidates
     (funcall zero-get-preedit-str-for-panel-func)
     (length candidates-on-page) candidates-on-page)
    (zero-debug "candidates: %s\n  " (s-join "\n  " candidates-on-page))
    (zero-debug "candidates:\n  %s\n  " (s-join "\n  " candidates-on-page))
    (destructuring-bind (x y) (zero-get-point-position)
      (zero-panel-move x y))))

+35 −8
Original line number Diff line number Diff line
@@ -36,17 +36,44 @@
;; public API
;;============

(defun zero-pinyin-service-get-candidates (preedit-str)
  "get candidates for pinyin in preedit-str synchronously"
  (zero-pinyin-service-call "GetCandidates" :string preedit-str))
(defun zero-pinyin-service-get-candidates (preedit-str fetch-size)
  "get candidates for pinyin in preedit-str synchronously.

(defun zero-pinyin-service-get-candidates-async (preedit-str get-candidates-complete)
  "get candidates for pinyin in preedit-str asynchronously"
preedit-str the preedit-str, should be pure pinyin string
fetch-size try to fetch this many candidates or more"
  (zero-pinyin-service-call "GetCandidates" :string preedit-str :uint32 fetch-size))

(defun zero-pinyin-service-get-candidates-async (preedit-str fetch-size get-candidates-complete)
  "get candidates for pinyin in preedit-str asynchronously.

preedit-str the preedit-str, should be pure pinyin string
fetch-size try to fetch this many candidates or more"
  (zero-pinyin-service-async-call
   "GetCandidates" get-candidates-complete :string preedit-str))
   "GetCandidates" get-candidates-complete :string preedit-str :uint32 fetch-size))

(defun zero-panel-quit ()
(defun zero-pinyin-service-quit ()
  "quit panel application"
  (zero-pinyin-service-async-call "Quit" nil))

(provide 'zero-panel)
;;================
;; some app test
;;================

(ert-deftest zero-pinyin-service-get-candidates ()
  (destructuring-bind (cs ls) (zero-pinyin-service-get-candidates "liyifeng" 1)
    (should (equal (first cs) "李易峰"))
    (should (= (first ls) 8)))
  (destructuring-bind (cs ls) (zero-pinyin-service-get-candidates "wenti" 1)
    (should (equal (first cs) "问题"))
    (should (= (first ls) 5)))
  (destructuring-bind (cs ls) (zero-pinyin-service-get-candidates "meiyou" 1)
    (should (equal (first cs) "没有"))
    (should (= (first ls) 6)))
  (destructuring-bind (cs ls) (zero-pinyin-service-get-candidates "shi" 1)
    (should (equal (first cs) "是"))
    (should (= (first ls) 3)))
  (destructuring-bind (cs ls) (zero-pinyin-service-get-candidates "de" 1)
    (should (equal (first cs) "的"))
    (should (= (first ls) 2))))

(provide 'zero-pinyin-service)
+6 −2
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@

(require 's)
(require 'zero-framework)
(require 'zero-pinyin-service)

;;===============================
;; basic data and emacs facility
@@ -19,6 +20,8 @@
  "accompany `zero-candidates', marks how many preedit-str chars are used for each candidate")
(defvar zero-pinyin-pending-str "")
(defvar zero-pinyin-pending-preedit-str "")
(defvar zero-pinyin-initial-fetch-size 20
  "how many candidates to fetch for the first call to GetCandidates")

;;=====================
;; key logic functions
@@ -40,7 +43,7 @@
(defun zero-pinyin-build-candidates (preedit-str)
  (if zero-pinyin--build-candidates-use-test-data
      (zero-pinyin-build-candidates-test preedit-str)
    (let ((result (zero-pinyin-service-get-candidates preedit-str)))
    (let ((result (zero-pinyin-service-get-candidates preedit-str zero-pinyin-initial-fetch-size)))
      (setq zero-pinyin-used-preedit-str-lengths (second result))
      (first result))))

@@ -49,6 +52,7 @@
  (zero-debug "building candidate list async\n")
  (zero-pinyin-service-get-candidates-async
   preedit-str
   zero-pinyin-initial-fetch-size
   (lambda (candidates matched_preedit_str_lengths)
     (setq zero-pinyin-used-preedit-str-lengths matched_preedit_str_lengths)
     ;; Note: with dynamic binding, this command result in (void-variable
@@ -73,7 +77,7 @@
  (should-not (zero-pinyin-can-start-sequence ?v)))

(defun zero-pinyin-pending-preedit-str-changed ()
  (zero-build-candidates-async zero-pinyin-pending-preedit-str))
  (zero-pinyin-build-candidates-async zero-pinyin-pending-preedit-str 'zero-build-candidates-complete))

(defun zero-pinyin-commit-nth-candidate (n)
  "commit nth candidate and return true if it exists, otherwise, return false"