A Neovim plugin enabling you to solve LeetCode problems.
feat: sign in menu
18 files changed, +274 -165
+4-2lua/leetcode/api/command/init.lua
| @@ -51,8 +51,8 @@ end | ||
| 51 | 51 | |
| 52 | 52 | ---Merge configurations into default configurations and set it as user configurations. |
| 53 | 53 | --- |
| 54 | ----@return lc.UserStatus | nil | |
| 55 | -function cmd.authenticate() gql.auth.user_status() end | |
| 54 | +---@return lc.UserAuth | nil | |
| 55 | +function cmd.authenticate() gql.auth.user() end | |
| 56 | 56 | |
| 57 | 57 | ---Merge configurations into default configurations and set it as user configurations. |
| 58 | 58 | --- |
| @@ -84,6 +84,8 @@ function cmd.start() | ||
| 84 | 84 | local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false) |
| 85 | 85 | if #lines > 1 or (#lines == 1 and lines[1]:len() > 0) then return true end |
| 86 | 86 | |
| 87 | + gql.auth.user() | |
| 88 | + | |
| 87 | 89 | menu:init() |
| 88 | 90 | end |
| 89 | 91 |
+19-25lua/leetcode/api/graphql/auth.lua
| @@ -1,38 +1,32 @@ | ||
| 1 | 1 | local utils = require("leetcode.api.graphql.utils") |
| 2 | 2 | local config = require("leetcode.config") |
| 3 | +local log = require("leetcode.logger") | |
| 3 | 4 | |
| 4 | 5 | ---@class lc.AuthApi |
| 5 | 6 | local M = {} |
| 6 | 7 | |
| 7 | ----@class lc.UserStatus | |
| 8 | ----@field username string | |
| 9 | ----@field is_signed_in boolean | |
| 10 | ----@field is_premium boolean | |
| 11 | ----@field user_id integer | |
| 12 | - | |
| 13 | ----@return lc.UserStatus | nil | |
| 14 | -function M.user_status() | |
| 8 | +---@return config_auth | |
| 9 | +function M.user() | |
| 15 | 10 | local query = [[ |
| 16 | - query globalData { | |
| 17 | - userStatus { | |
| 18 | - userId | |
| 19 | - is_signed_in: isSignedIn | |
| 20 | - isMockUser | |
| 21 | - is_premium: isPremium | |
| 22 | - isVerified | |
| 23 | - username | |
| 24 | - activeSessionId | |
| 25 | - } | |
| 26 | - } | |
| 27 | - ]] | |
| 11 | + query globalData { | |
| 12 | + userStatus { | |
| 13 | + id: userId | |
| 14 | + is_signed_in: isSignedIn | |
| 15 | + is_premium: isPremium | |
| 16 | + name: username | |
| 28 | 17 | |
| 29 | - local ok, res = pcall(utils.query, query) | |
| 30 | - if not ok then return end | |
| 18 | + isVerified | |
| 19 | + activeSessionId | |
| 20 | + isMockUser | |
| 21 | + } | |
| 22 | + } | |
| 23 | + ]] | |
| 31 | 24 | |
| 32 | - local status = res["userStatus"] | |
| 33 | - config.authenticate(status) | |
| 25 | + local ok, res = pcall(utils.query, query) | |
| 26 | + if not ok then return {} end | |
| 34 | 27 | |
| 35 | - return res["userStatus"] | |
| 28 | + config.auth = res["userStatus"] | |
| 29 | + return config.auth | |
| 36 | 30 | end |
| 37 | 31 | |
| 38 | 32 | return M |
+2-2lua/leetcode/api/rest/utils.lua
| @@ -47,11 +47,11 @@ function utils._get(url, cb) | ||
| 47 | 47 | Job:new({ |
| 48 | 48 | command = "curl", |
| 49 | 49 | args = args, |
| 50 | - on_exit = function(self, val) | |
| 50 | + on_exit = vim.schedule_wrap(function(self, val) | |
| 51 | 51 | local result = table.concat(self:result(), "\n") |
| 52 | 52 | local decoded = vim.json.decode(result) |
| 53 | 53 | cb(decoded) |
| 54 | - end, | |
| 54 | + end), | |
| 55 | 55 | on_stdout = function(error, data, self) |
| 56 | 56 | -- log.info(data) |
| 57 | 57 | end, |
+14-19lua/leetcode/config/init.lua
| @@ -2,31 +2,31 @@ local template = require("leetcode.config.template") | ||
| 2 | 2 | |
| 3 | 3 | ---@class lc.Settings |
| 4 | 4 | local config = { |
| 5 | + default = template, ---@type lc.UserConfig Default User configuration | |
| 6 | + user = template, ---@type lc.UserConfig User configuration | |
| 7 | + | |
| 5 | 8 | name = "leetcode.nvim", |
| 9 | + domain = "https://leetcode.com", | |
| 6 | 10 | debug = true, |
| 7 | 11 | notify = false, |
| 8 | 12 | lang = "cpp", |
| 9 | - domain = "https://leetcode.com", | |
| 10 | 13 | } |
| 11 | 14 | |
| 12 | ----Default configurations. | |
| 13 | ---- | |
| 14 | ----@type lc.Config | |
| 15 | -config.default = template | |
| 16 | - | |
| 17 | ----User configurations. | |
| 18 | ---- | |
| 19 | ----@type lc.Config | |
| 20 | -config.user = config.default | |
| 21 | - | |
| 22 | ----@type lc.UserStatus|nil | |
| 15 | +---@class lc.UserAuth | |
| 16 | +---@field name string | |
| 17 | +---@field is_signed_in boolean | |
| 18 | +---@field is_premium boolean | |
| 19 | +---@field id integer | |
| 23 | 20 | config.auth = nil |
| 24 | 21 | |
| 22 | +config.default = template ---@type lc.UserConfig Default User configuration | |
| 23 | +config.user = template ---@type lc.UserConfig User configuration | |
| 24 | + | |
| 25 | 25 | ---Merge configurations into default configurations and set it as user configurations. |
| 26 | 26 | --- |
| 27 | ----@param cfg lc.Config Configurations to be merged. | |
| 27 | +---@param cfg lc.UserConfig Configurations to be merged. | |
| 28 | 28 | function config.apply(cfg) |
| 29 | - config.user = vim.tbl_deep_extend("force", template, cfg) | |
| 29 | + config.user = vim.tbl_deep_extend("force", config.default, cfg) | |
| 30 | 30 | |
| 31 | 31 | local ok, notify = pcall(require, "notify") |
| 32 | 32 | if ok then |
| @@ -45,9 +45,4 @@ function config.apply(cfg) | ||
| 45 | 45 | vim.api.nvim_set_hl(0, "LeetCodePreTag", { link = "@text" }) |
| 46 | 46 | end |
| 47 | 47 | |
| 48 | ----Merge configurations into default configurations and set it as user configurations. | |
| 49 | ---- | |
| 50 | ----@param status lc.UserStatus | nil | |
| 51 | -function config.authenticate(status) config.auth = status end | |
| 52 | - | |
| 53 | 48 | return config |
+1-1lua/leetcode/config/template.lua
| @@ -1,4 +1,4 @@ | ||
| 1 | ----@class lc.Config | |
| 1 | +---@class lc.UserConfig | |
| 2 | 2 | local M = { |
| 3 | 3 | ---@type string |
| 4 | 4 | domain = "com", |
+1-1lua/leetcode/init.lua
| @@ -6,7 +6,7 @@ local cmd = require("leetcode.api.command") | ||
| 6 | 6 | |
| 7 | 7 | local leetcode = {} |
| 8 | 8 | |
| 9 | ----@param cfg? lc.Config | |
| 9 | +---@param cfg? lc.UserConfig | |
| 10 | 10 | function leetcode.setup(cfg) |
| 11 | 11 | config.apply(cfg or {}) |
| 12 | 12 |
+1-1lua/leetcode/runner/init.lua
| @@ -28,7 +28,7 @@ function runner:run() | ||
| 28 | 28 | rest.interpreter.interpret_solution( |
| 29 | 29 | question.q.title_slug, |
| 30 | 30 | body, |
| 31 | - vim.schedule_wrap(function(item) self:callback(item) end) | |
| 31 | + function(item) self:callback(item) end | |
| 32 | 32 | ) |
| 33 | 33 | end |
| 34 | 34 |