Skip to content

Commit

Permalink
add tabnine-login command to login tabnine account #8
Browse files Browse the repository at this point in the history
  • Loading branch information
shuxiao9058 committed Nov 23, 2023
1 parent 9d66dd9 commit 856b881
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.org
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ After installing the TabNine package, you should finish the installation by exec

** TabNine configuration

The advanced features (e.g. advanced completions, TabNine Chat) require a =TabNine Pro= account. Open the TabNine Hub page with =tabnine-configuration= after installation. Then login with your account.
The advanced features (e.g. advanced completions, TabNine Chat) require a =TabNine Pro= account. Use =tabnine-login= command to login your TabNine account.

** TabNine Chat

Expand Down
48 changes: 48 additions & 0 deletions tabnine-core.el
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ access-token: access token for TabNine API
username: username in TabNine Hub
avatar-url: avatar url in TabNine Hub.")

(defvar tabnine--auth-token nil
"TabNine auth token, used for login.")

(defvar tabnine--access-token nil
"TabNine access token with plist.
Expand Down Expand Up @@ -338,6 +341,21 @@ REQUEST should be JSON-serializable object."
:version tabnine--api-version
:request
(list :Configuration '(:__ t))))
((eq method 'login-with-custom-token-url)
(list
:version tabnine--api-version
:request
(list :LoginWithCustomTokenUrl '(:__ t))))
((eq method 'login-with-custom-token)
(list
:version tabnine--api-version
:request
(list :LoginWithCustomToken `(:custom_token ,tabnine--auth-token))))
((eq method 'logout)
(list
:version tabnine--api-version
:request
(list :Logout '(:__ t))))
;; NotifyWorkspaceChanged
;; setState
;; StartupActions
Expand Down Expand Up @@ -555,6 +573,36 @@ REQUEST should be JSON-serializable object."
(interactive)
(tabnine--request 'configuration))

(defun tabnine--login-with-custom-token-url()
"Login with custom token URL."
(tabnine--request 'login-with-custom-token-url))

(defun tabnine-logout()
"Logout from TabNine."
(interactive)
(if (tabnine-state)
(tabnine--request 'logout)
(user-error "Already logout")))

(defun tabnine-login()
"Login to TabNine."
(interactive)
(when (tabnine-state)
(user-error "Already signed in as %s!" (plist-get tabnine--user :username)))
(let ((token-url (tabnine--login-with-custom-token-url))
(auth-token))
(if token-url
(progn
(read-from-minibuffer (format "Press ENTER to open TabNine Hub in your browser."))
(browse-url token-url)
(setq tabnine--auth-token (read-from-minibuffer (format "Paste auth token copied from page:")))
(when (> (length tabnine--auth-token) 10)
(tabnine--request 'login-with-custom-token))
(if (tabnine-state)
(message "Authenticated as TabNine user %s." (plist-get tabnine--user :username))
(user-error "Login failed!")))
(user-error "Failed to get the token url from TabNine process!"))))

;;
;; Other helper functions
;;
Expand Down
2 changes: 1 addition & 1 deletion tabnine.el
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
;; 2. Enable `tabnine-mode` in `prog-mode`(if you want to use `capf` only, skip this step).
;; (add-to-list 'prog-mode-hook #'tabnine-mode)
;; 3. Run M-x tabnine-install-binary to install the TabNine binary for your system.
;; 4. Run M-x tabnine-configuration to login your TabNine account.
;; 4. Run M-x tabnine-login to login your TabNine account.
;;
;; Usage:
;;
Expand Down

0 comments on commit 856b881

Please sign in to comment.