feat: add submit hook #203

Open
jingyi-zhao-01 wants to merge 26 commits from jingyi-zhao-01/master into master
jingyi-zhao-01 commented 2025-09-17 02:35:04 -05:00 (Migrated from github.com)

below is a sample on_submit hook with working demo

hooks = {
           ["on_submit"] = {
                 function(question, buffer, status_msg)
                   local ts = os.date("%y-%m-%d-%H-%M")
                   local safe_status = (status_msg or "unknown"):gsub("%s+", "_")



                   local submissions_dir =  "/home/jingyi/.local/share/nvim/leetcode/submissions"
                   vim.fn.mkdir(submissions_dir, "p")
           
				   local file = string.format(
					                       "%s/%s-%s-%s.txt",
					                       submissions_dir,
					                       question.q.title_slug,
					                       safe_status,
					                       ts
					                     )           
                   -- write buffer contents
  				   vim.fn.writefile(vim.split(buffer or "", "\n"), file)
           
                   print("Saved submission to", file)
                 end,
               }
          },

https://github.com/user-attachments/assets/40af4ce4-c398-495b-908d-9d58b8fd8c1f

below is a sample on_submit hook with working demo ``` hooks = { ["on_submit"] = { function(question, buffer, status_msg) local ts = os.date("%y-%m-%d-%H-%M") local safe_status = (status_msg or "unknown"):gsub("%s+", "_") local submissions_dir = "/home/jingyi/.local/share/nvim/leetcode/submissions" vim.fn.mkdir(submissions_dir, "p") local file = string.format( "%s/%s-%s-%s.txt", submissions_dir, question.q.title_slug, safe_status, ts ) -- write buffer contents vim.fn.writefile(vim.split(buffer or "", "\n"), file) print("Saved submission to", file) end, } }, ``` https://github.com/user-attachments/assets/40af4ce4-c398-495b-908d-9d58b8fd8c1f
kawre (Migrated from github.com) reviewed 2025-09-17 02:35:04 -05:00
copilot-pull-request-reviewer[bot] (Migrated from github.com) reviewed 2025-09-17 02:35:29 -05:00
copilot-pull-request-reviewer[bot] (Migrated from github.com) left a comment

Pull Request Overview

This PR adds a new on_submit hook to the LeetCode plugin that triggers when a solution is submitted. The hook provides access to the question, buffer contents, and submission status message.

  • Adds on_submit hook definition to the configuration template
  • Implements hook execution in the runner when a submission is detected
  • Includes utility module import for hook execution

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
lua/leetcode/config/template.lua Adds the on_submit hook type definition with proper typing
lua/leetcode/runner/init.lua Imports utils module and executes the on_submit hook when submission is detected

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

## Pull Request Overview This PR adds a new `on_submit` hook to the LeetCode plugin that triggers when a solution is submitted. The hook provides access to the question, buffer contents, and submission status message. - Adds `on_submit` hook definition to the configuration template - Implements hook execution in the runner when a submission is detected - Includes utility module import for hook execution ### Reviewed Changes Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment. | File | Description | | ---- | ----------- | | lua/leetcode/config/template.lua | Adds the `on_submit` hook type definition with proper typing | | lua/leetcode/runner/init.lua | Imports utils module and executes the `on_submit` hook when submission is detected | --- <sub>**Tip:** Customize your code reviews with copilot-instructions.md. <a href="/kawre/leetcode.nvim/new/master/.github?filename=copilot-instructions.md" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Create the file</a> or <a href="https://docs.github.com/en/copilot/customizing-copilot/adding-repository-custom-instructions-for-github-copilot" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">learn how to get started</a>.</sub>
@ -55,0 +62,4 @@
end
utils.exec_hooks(event, question, body.typed_code, item)
end
copilot-pull-request-reviewer[bot] (Migrated from github.com) commented 2025-09-17 02:35:28 -05:00

The hook parameters don't match the type definition. The hook expects (question, buffer, status_msg) but body.typed_code is passed as the buffer parameter. Consider renaming the parameter to match the actual data being passed or ensure the parameter names are consistent with the type definition.

                local buffer = body.typed_code
                utils.exec_hooks("on_submit", question, buffer, item.status_msg)
The hook parameters don't match the type definition. The hook expects `(question, buffer, status_msg)` but `body.typed_code` is passed as the buffer parameter. Consider renaming the parameter to match the actual data being passed or ensure the parameter names are consistent with the type definition. ```suggestion local buffer = body.typed_code utils.exec_hooks("on_submit", question, buffer, item.status_msg) ```
kawre commented 2025-09-20 09:54:48 -05:00 (Migrated from github.com)

The hook should include information about whether the submission was successful. You can do it by adding a success parameter sourced from item._.success

The hook should include information about whether the submission was successful. You can do it by adding a `success` parameter sourced from `item._.success`
kawre (Migrated from github.com) reviewed 2025-09-20 10:08:57 -05:00
@ -109,0 +118,4 @@
enabled = true, ---@type boolean
height = 10, ---@type lc.size
limit = 10, ---@type integer
},
kawre (Migrated from github.com) commented 2025-09-20 10:08:57 -05:00

You should rename it to submit to be on par with other hooks

You should rename it to `submit` to be on par with other hooks
jingyi-zhao-01 commented 2025-09-23 01:24:56 -05:00 (Migrated from github.com)

added ._.success into the hook parameter, it's now up to the hook implementation to how to differentiate successful submission and failure submission in case one wants to thoroughly review his submission quality

added ._.success into the hook parameter, it's now up to the hook implementation to how to differentiate successful submission and failure submission in case one wants to thoroughly review his submission quality
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin jingyi-zhao-01/master:jingyi-zhao-01/master
git switch jingyi-zhao-01/master

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch master
git merge --no-ff jingyi-zhao-01/master
git switch jingyi-zhao-01/master
git rebase master
git switch master
git merge --ff-only jingyi-zhao-01/master
git switch jingyi-zhao-01/master
git rebase master
git switch master
git merge --no-ff jingyi-zhao-01/master
git switch master
git merge --squash jingyi-zhao-01/master
git switch master
git merge --ff-only jingyi-zhao-01/master
git switch master
git merge jingyi-zhao-01/master
git push origin master
Sign in to join this conversation.
No description provided.