Skip to content

Commit

Permalink
login with custom token (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
amirbilu authored Nov 5, 2023
1 parent 6ec8cdc commit ead5197
Show file tree
Hide file tree
Showing 8 changed files with 553 additions and 418 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ EOF
## Activate Tabnine Pro

`:TabnineHub` - to open Tabnine Hub and log in to your account
`:TabnineLoginWithAuthToken` - to log in using auth token (for headless environments, where no browser is available)

Sometimes Tabnine may fail to open the browser on Tabnine Hub, in this case use `:TabnineHubUrl` to get Tabnine Hub URL

Expand All @@ -151,6 +152,7 @@ Sometimes Tabnine may fail to open the browser on Tabnine Hub, in this case use
- `:TabnineEnable` - to enable Tabnine
- `:TabnineToggle` - to toggle enable/disable
- `:TabnineChat` - to launch Tabnine chat
- `:TabnineLoginWithAuthToken` - to log in using auth token (for headless environments, where no browser is available)

## `<Tab>` and `nvim-cmp`

Expand Down
922 changes: 515 additions & 407 deletions chat/index.html

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions doc/tabnine.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Print Tabnine status

Login and activate Tabnine Pro
:TabnineLogin
:TabnineLoginWithAuthToken - login for headleass environments

Logout
:TabnineLogout
Expand Down
2 changes: 1 addition & 1 deletion lua/tabnine/auto_commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function M.setup()
})
end

api.nvim_create_autocmd({"VimEnter","ColorScheme"}, {
api.nvim_create_autocmd({ "VimEnter", "ColorScheme" }, {
pattern = "*",
callback = function()
api.nvim_set_hl(0, consts.tabnine_hl_group, {
Expand Down
5 changes: 4 additions & 1 deletion lua/tabnine/chat/binary.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ function ChatBinary:start()
if handler then
handler(message.data, function(payload)
if payload then
self:post_message({ id = message.id, payload = payload })
self:post_message({
id = message.id,
payload = payload,
})
end
end)
end
Expand Down
17 changes: 11 additions & 6 deletions lua/tabnine/chat/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ local function register_events()
end)

chat_binary:register_event("get_basic_context", function(_, answer)
tabnine_binary:request({ FileMetadata = { path = api.nvim_buf_get_option(0, "filetype") } }, function(metadata)
tabnine_binary:request({
FileMetadata = { path = api.nvim_buf_get_option(0, "filetype") },
}, function(metadata)
answer({
fileUri = api.nvim_buf_get_name(0),
language = api.nvim_buf_get_option(0, "filetype"),
Expand All @@ -106,15 +108,16 @@ local function register_events()
selectedCodeUsages = {},
}
elseif contextType == "Diagnostics" then
return { type = "Diagnostics", diagnosticsText = get_diagnostics_text() }
return {
type = "Diagnostics",
diagnosticsText = get_diagnostics_text(),
}
elseif contextType == "Workspace" then
return {} -- not implemented
end
end, contextTypesSet)

answer({
enrichingContextData = enrichingContextData,
})
answer({ enrichingContextData = enrichingContextData })
end)

chat_binary:register_event("get_editor_context", function(_, answer)
Expand Down Expand Up @@ -143,7 +146,9 @@ local function register_events()
end)

chat_binary:register_event("send_event", function(event)
tabnine_binary:request({ Event = { name = event.eventName, properties = event.properties } }, function() end)
tabnine_binary:request({
Event = { name = event.eventName, properties = event.properties },
}, function() end)
end)
end

Expand Down
4 changes: 1 addition & 3 deletions lua/tabnine/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,7 @@ end

function M.prefetch()
-- TODO add active_text_editor_changed event
tabnine_binary:request({
Prefetch = { filename = api.nvim_buf_get_name(0) },
}, function() end)
tabnine_binary:request({ Prefetch = { filename = api.nvim_buf_get_name(0) } }, function() end)
end

return M
18 changes: 18 additions & 0 deletions lua/tabnine/user_commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ function M.setup()
end, {})
end

api.nvim_create_user_command("TabnineLoginWithAuthToken", function()
tabnine_binary:request({ LoginWithCustomTokenUrl = { dummy = true } }, function(url)
vim.ui.input({
prompt = string.format("Get your token from: %s\nPaste it here: ", url),
}, function(custom_token)
tabnine_binary:request({
LoginWithCustomToken = { custom_token = custom_token },
}, function(response)
if response.is_success then
vim.notify("Logged in successfully")
else
vim.notify("Sign in failed", vim.log.levels.WARN)
end
end)
end)
end)
end, {})

api.nvim_create_user_command("TabnineLogin", function()
tabnine_binary:request({ Login = { dummy = true } }, function() end)
end, {})
Expand Down

0 comments on commit ead5197

Please sign in to comment.