Skip to content

refactor: optimize key blocking checks and streamline profile loading logic#8

Open
seekey13 wants to merge 2 commits into
mainfrom
ponytail
Open

refactor: optimize key blocking checks and streamline profile loading logic#8
seekey13 wants to merge 2 commits into
mainfrom
ponytail

Conversation

@seekey13

@seekey13 seekey13 commented Jun 20, 2026

Copy link
Copy Markdown
Owner

Summary by CodeRabbit

  • Refactor
    • Streamlined keybinding validation and blocked-key checking logic
    • Removed deprecated UI control functions (hide, toggle, load_bindings)
    • Modified keyboard profile unload behavior to unbind all tracked keys
    • Simplified key-lookup implementation and removed related utility APIs

Copilot AI review requested due to automatic review settings June 20, 2026 10:37
@coderabbitai

coderabbitai Bot commented Jun 20, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Across five modules, this PR removes precomputed/consolidated lookup tables (M.all_blocked, vk_codes.name_to_code) and several exported functions (get_block_reason, get_all_keys, get_vk_code, keyboard_ui.hide/toggle/load_bindings, generate_profile_name, generate_binding_suffix), replaces them with direct lookups or inline calls, and changes unload_profile to unbind all tracked keys without blacklist filtering.

Changes

API Cleanup and Lookup Simplification

Layer / File(s) Summary
Simplify blocked-key and vk_code lookup tables
blocked_keybinds.lua, vk_codes.lua
M.is_key_blocked now reads M.blocked directly; M.is_combination_blocked indexes M.blocked[base_key] directly. M.all_blocked, populate_all_blocked, and M.get_block_reason are removed. vk_codes drops name_to_code, get_vk_code, and get_all_keys, keeping only get_key_name and is_known_key.
Remove exported functions from keyboard_ui and ui_functions
keyboard_ui.lua, ui_functions.lua
keyboard_ui removes hide, toggle, and load_bindings. ui_functions removes ensure_directory_exists, generate_profile_name, and generate_binding_suffix; adds get_scripts_path; replaces ensure_directory_exists with an inline pcall-wrapped /mkdir; and uses error_msg directly in validate_key_binding instead of calling get_block_reason.
Inline keyboard_ui calls and fix unload blacklist behavior
jobbinds.lua
Removes update_keyboard_ui helper and inlines keyboard_ui.set_current_profile / keyboard_ui.load_profile at all call sites including the job-change path. unload_profile now unbinds every tracked key unconditionally; load_and_track_profile uses the precomputed profile_path variable.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • seekey13/JobBinds#4: Directly related — this PR removes get_block_reason, all_blocked, and keyboard UI exports that were introduced or relied upon by the code introduced in that PR.

Poem

🐇 Hop, hop, the table's gone,
No more all_blocked to lean upon!
get_block_reason? Swept away,
Direct lookups win the day.
The warren's tidy, clean, and bright —
Less code, same magic, pure delight! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main changes: optimization of key blocking checks (blocked_keybinds.lua refactoring) and streamlined profile loading logic (jobbinds.lua and keyboard_ui.lua simplifications).
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch ponytail

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the JobBinds addon by removing unused helper APIs and simplifying key-blocking/profile-loading logic, reducing redundant work and tightening the flow between profile execution and UI refresh.

Changes:

  • Removed unused helper functions and reverse-lookup tables from vk_codes.lua, ui_functions.lua, and keyboard_ui.lua.
  • Simplified blocked-key checks by removing the consolidated all_blocked table and relying directly on M.blocked.
  • Streamlined profile loading/UI update paths in jobbinds.lua and simplified macro directory creation in ui_functions.lua.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
vk_codes.lua Removes unused reverse lookup + helper APIs, keeping only the mapping and key-name helpers used by the UI.
ui_functions.lua Removes unused helpers, simplifies blocked-key validation to use the module’s returned message, and inlines mkdir with pcall.
keyboard_ui.lua Removes unused UI control helpers and minor comment cleanup; keeps profile loading via load_bindings_from_profile.
jobbinds.lua Simplifies UI update helper usage and avoids recomputing the profile path when reading tracked keys.
blocked_keybinds.lua Removes all_blocked consolidation and simplifies checks to directly reference M.blocked.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@ui_functions.lua`:
- Around line 274-277: The create_macro_file function has a race condition where
the `/mkdir` command is queued asynchronously with QueueCommand but the
subsequent file write via io.open happens immediately in the same execution
thread before the directory is created. To fix this, either implement polling
logic to wait for the directory to exist before calling io.open on the file
path, or refactor the function to defer the actual file write operation until
after the directory creation completes (potentially by using a callback or
moving the io.open call to be executed after confirming the scripts_path
directory exists).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d9cc2332-3dce-4010-b829-0e55e7d306d4

📥 Commits

Reviewing files that changed from the base of the PR and between b21718d and 95f4999.

📒 Files selected for processing (5)
  • blocked_keybinds.lua
  • jobbinds.lua
  • keyboard_ui.lua
  • ui_functions.lua
  • vk_codes.lua
💤 Files with no reviewable changes (2)
  • vk_codes.lua
  • keyboard_ui.lua

Comment thread ui_functions.lua
Comment on lines 274 to 277
function ui_functions.create_macro_file(macro_name, content, existing_command)
local scripts_path = ui_functions.get_scripts_path()
ensure_directory_exists(scripts_path)
pcall(function() AshitaCore:GetChatManager():QueueCommand(1, string.format('/mkdir "%s"', scripts_path)) end)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🌐 Web query:

In Ashita Lua API, does AshitaCore:GetChatManager():QueueCommand(delay, command)execute asynchronously relative to subsequent Lua statements? If/mkdir ""is queued andio.open("/file.txt", "w")is called immediately after, canio.open run before directory creation?

💡 Result:

In Ashita Lua (ADK) API, QueueCommand is not documented as “asynchronous relative to the next Lua statement.” Instead, it queues a chat/console command to be executed by Ashita’s command system at some later tick based on the provided delay argument. Example addon code shows QueueCommand being called on a later event (“timerpulse”/“render”) and that the delay parameter is used to control when the queued command is sent. Because the command is queued (not executed inline), if you queue a game command like /mkdir "" and then immediately call io.open("/file.txt","w") in the same Lua execution thread, directory creation performed by the queued in-game command may not have happened yet. That means io.open can run before the directory exists. In other words: - QueueCommand(delay, command) schedules the command; it should not be assumed to have completed before subsequent Lua lines run. - With a non-negative delay (e.g., 1), it will run on a subsequent update/tick rather than immediately, so a race is possible if you rely on filesystem effects from the queued command. Practically, to avoid the race you should (a) queue the /mkdir with a delay of -1 only if you know it is processed immediately by Ashita, or (b) wait/yield/poll for directory existence before calling io.open (e.g., schedule io.open in a later timer callback after the command has had time to execute). Evidence: Ashita addon documentation lists GetChatManager and IChatManager methods including QueueCommand/ParseCommand, and community code uses QueueCommand with explicit delay values in event callbacks rather than expecting immediate completion. For example, QueueCommand is called inside an on-render timer loop with delay=1, demonstrating that delay controls when the command is processed rather than executing within the same Lua statement flow [1]. Additional usage examples show QueueCommand being invoked during OnLoad/per-event setup with delay values (e.g., -1) without any mechanism that would make it synchronous with filesystem operations [2].

Citations:


Fix the /mkdir race condition before file creation.

At Line 276, /mkdir is queued with delay 1 via QueueCommand, but io.open(...) is called immediately after in the same Lua execution thread. QueueCommand schedules the command for a later game tick and does not execute synchronously—the directory may not exist when io.open runs, causing macro file creation to fail on first use. Either poll/yield for directory existence before calling io.open, or defer the file write to a callback after directory creation completes.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@ui_functions.lua` around lines 274 - 277, The create_macro_file function has
a race condition where the `/mkdir` command is queued asynchronously with
QueueCommand but the subsequent file write via io.open happens immediately in
the same execution thread before the directory is created. To fix this, either
implement polling logic to wait for the directory to exist before calling
io.open on the file path, or refactor the function to defer the actual file
write operation until after the directory creation completes (potentially by
using a callback or moving the io.open call to be executed after confirming the
scripts_path directory exists).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants