diff --git a/tabnine-chat-curl.el b/tabnine-chat-curl.el index d826639..94f5959 100644 --- a/tabnine-chat-curl.el +++ b/tabnine-chat-curl.el @@ -60,13 +60,13 @@ INFO is a plist with the following keys: `(("Content-Type" . "application/json; charset=utf-8") ("Authorization" . ,(concat "Bearer " (tabnine--access-token))))) (request (tabnine-chat--make-request info)) - (url-request-data (tabnine-util--json-serialize request)) + (data-json (encode-coding-string (tabnine-util--json-serialize request) 'utf-8)) (url-str (format "%s/chat/v1/generate_chat_response_async" tabnine-api-server)) (curl-args (append (list "--location" "--silent" "--compressed" "--disable" (format "-X%s" "POST") (format "-m%s" 60) - (format "-d%s" url-request-data)) + (format "-d%s" data-json)) (when (and tabnine-network-proxy (stringp tabnine-network-proxy) (not (string-empty-p tabnine-network-proxy))) (list "--insecure" "--proxy" tabnine-network-proxy)) diff --git a/tabnine-chat.el b/tabnine-chat.el index 1dc263f..ea780d1 100644 --- a/tabnine-chat.el +++ b/tabnine-chat.el @@ -366,19 +366,17 @@ Should be messageContext's item or userContext' value." (defun tabnine-chat--code-around-by-line(line-count before) "TabNine Chat get code around BEFORE or after with LINE-COUNT." (save-excursion - (let* ((curr-line (line-number-at-pos)) - (lines (count-lines (point-min) (point-max))) - (point-min) + (let* ((point-min) (point-max)) (save-excursion (if before - (forward-line (max 0 (- curr-line line-count))) - (forward-line (min lines (+ curr-line 1)))) + (forward-line (- line-count)) + (forward-line 1)) (setq point-min (line-beginning-position))) (save-excursion (if before - (forward-line (max 0 (- curr-line 1))) - (forward-line (min lines (+ curr-line line-count)))) + (forward-line -1) + (forward-line line-count)) (setq point-max (line-end-position))) (decode-coding-string (buffer-substring-no-properties point-min point-max) diff --git a/tabnine-core.el b/tabnine-core.el index 8006796..145ae07 100644 --- a/tabnine-core.el +++ b/tabnine-core.el @@ -690,15 +690,15 @@ PROCESS is the process under watch, EVENT is the event occurred." "Filter duplicates and bad COMPLETIONS result." (when completions (when-let* ((completions (cl-remove-duplicates completions - :key (lambda (x) (plist-get x :new_prefix)) - :test #'s-equals-p)) - (completions (cl-remove-duplicates completions - :key (lambda (x) (let ((new_prefix (plist-get x :new_prefix)) - (new_posfix (plist-get x :new_posfix))) - (s-trim (concat new_prefix (or new_posfix ""))))) - :test #'s-equals-p)) - (completions (cl-remove-if #'tabnine--invalid-completion-p - completions))) + :key (lambda (x) (plist-get x :new_prefix)) + :test #'s-equals-p)) + (completions (cl-remove-duplicates completions + :key (lambda (x) (let ((new_prefix (plist-get x :new_prefix)) + (new_posfix (plist-get x :new_posfix))) + (s-trim (concat new_prefix (or new_posfix ""))))) + :test #'s-equals-p)) + (completions (cl-remove-if #'tabnine--invalid-completion-p + completions))) completions))) (defun tabnine--process-filter (_ output) @@ -721,11 +721,23 @@ PROCESS is the process under watch, OUTPUT is the output received." (when (s-present? str) (setq result (tabnine-util--read-json str)) (unless (equal result 'json-error) - (setq tabnine--response result) (when (and result (tabnine--valid-response-p result)) - (setq tabnine--completion-cache-result result) + (when-let* ((completions (tabnine--filter-completions (plist-get result :results)))) + (setq completions (cl-sort completions + (lambda(a b) + (let* ((get-detail-number-fn + (lambda(x) + (when-let* ((md (plist-get x :completion_metadata)) + (detail (plist-get md :detail))) + (string-to-number (s-trim detail))))) + (detail-a (funcall get-detail-number-fn a)) + (detail-b (funcall get-detail-number-fn b))) + (> detail-a detail-b))))) + (plist-put result :results completions)) + (setq tabnine--completion-cache-result result) (when (and tabnine-mode (equal (point) tabnine--trigger-point)) - (tabnine--show-completion-1 result))))) + (tabnine--show-completion-1 result))) + (setq tabnine--response result))) (setq ss (cdr ss))))) ;; @@ -800,8 +812,7 @@ PROCESS is the process under watch, OUTPUT is the output received." (let* ((cache tabnine--completion-cache) (correlation_id (plist-get result :correlation_id)) (old_prefix (plist-get cache :old_prefix)) - (completions (plist-get cache :results)) - (completions (tabnine--filter-completions completions))) + (completions (plist-get cache :results))) (cond ((seq-empty-p completions) (message "No completion is available.")) ((= (length completions) 1) @@ -961,7 +972,7 @@ Use TRANSFORM-FN to transform completion if provided." (defun tabnine--show-completion-1 (response) "Show completion result after first get RESPONSE." (when-let* ((result response) - (completions (tabnine--filter-completions (plist-get result :results))) + (completions (plist-get result :results)) (completion (if (seq-empty-p completions) nil (seq-elt completions 0))) (old_prefix (plist-get result :old_prefix)) (new_prefix (plist-get completion :new_prefix)) @@ -1244,19 +1255,10 @@ command that triggered `post-command-hook'." "Get candidates for RESPONSE." (when (tabnine--response-display-with-capf-p response) (let* ((result response) - (completions (tabnine--filter-completions (plist-get result :results))) - (candidates (tabnine--construct-candidates + (completions (plist-get result :results))) + (tabnine--construct-candidates completions - #'tabnine--construct-candidate-generic))) - (cl-sort candidates - (lambda(a b) - (let* ((get-candidate-detail-number-fn - (lambda(x) - (let ((detail (get-text-property 0 'detail x))) - (string-to-number (s-trim detail))))) - (detail-a (funcall get-candidate-detail-number-fn a)) - (detail-b (funcall get-candidate-detail-number-fn b))) - (> detail-a detail-b))))))) + #'tabnine--construct-candidate-generic)))) (defun tabnine--post-completion (candidate) "Replace old suffix with new suffix for CANDIDATE." diff --git a/tabnine-util.el b/tabnine-util.el index a8f8f35..5a42a16 100644 --- a/tabnine-util.el +++ b/tabnine-util.el @@ -168,12 +168,11 @@ Example of a UUID: 1df63142-a513-c850-31a3-535fc3520c3d." (if (progn (require 'json) (fboundp 'json-serialize)) - `(encode-coding-string - (json-serialize ,params - :null-object nil - :false-object :json-false) 'utf-8) + `(json-serialize ,params + :null-object nil + :false-object :json-false) `(let ((json-false :json-false)) - (encode-coding-string (json-encode ,params) 'utf-8)))) + (json-encode ,params)))) (defmacro tabnine-util--read-json (str) "Read JSON string STR. and return the decoded object."