-
-
Notifications
You must be signed in to change notification settings - Fork 252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
External hotkey commands: tagging, linting and checking sync #1498
Open
Niloth-p
wants to merge
4
commits into
zulip:main
Choose a base branch
from
Niloth-p:category/1498-lint-and-sync-readline/pr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
84f5014
keys: Add a common suffix for all urwid_readline hotkeys.
Niloth-p 514db27
lint/regex: keys: Add linting for usage of external commands.
Niloth-p fa92511
run: Check sync of readline shortcuts with urwid_readline's keymap.
Niloth-p 0495e29
keys/lint/regexes: Add suffix & linting to general terminal commands.
Niloth-p File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,28 +11,92 @@ from zulipterminal.config.keys import ( | |
KEY_BINDINGS, | ||
display_keys_for_command, | ||
) | ||
from zulipterminal.config.regexes import ( | ||
REGEX_READLINE_COMMANDS, | ||
REGEX_TERMINAL_COMMANDS, | ||
) | ||
|
||
|
||
KEYS_FILE = ( | ||
Path(__file__).resolve().parent.parent / "zulipterminal" / "config" / "keys.py" | ||
) | ||
# absolute path to zulip-terminal | ||
ROOT_DIRECTORY = Path(__file__).resolve().parent.parent | ||
|
||
# absolute path to zulip-terminal/zulipterminal to be passed as parameter | ||
ZULIPTERMINAL = ROOT_DIRECTORY / "zulipterminal" | ||
|
||
KEYS_FILE = ZULIPTERMINAL / "config" / "keys.py" | ||
KEYS_FILE_NAME = KEYS_FILE.name | ||
OUTPUT_FILE = Path(__file__).resolve().parent.parent / "docs" / "hotkeys.md" | ||
OUTPUT_FILE = ROOT_DIRECTORY / "docs" / "hotkeys.md" | ||
OUTPUT_FILE_NAME = OUTPUT_FILE.name | ||
SCRIPT_NAME = PurePath(__file__).name | ||
HELP_TEXT_STYLE = re.compile(r"^[a-zA-Z /()',&@#:_-]*$") | ||
|
||
# Exclude keys from duplicate keys checking | ||
KEYS_TO_EXCLUDE = ["q", "e", "m", "r"] | ||
|
||
# Exclude files from being checked for external command usage | ||
EXCLUDED_FILES = [ | ||
KEYS_FILE, | ||
ZULIPTERMINAL / "config" / "regexes.py", | ||
ZULIPTERMINAL / "cli" / "run.py", | ||
] | ||
|
||
|
||
def main(fix: bool) -> None: | ||
lint_all_external_commands() | ||
if fix: | ||
generate_hotkeys_file() | ||
else: | ||
lint_hotkeys_file() | ||
|
||
|
||
def lint_all_external_commands() -> None: | ||
lint_external_commands_by_type( | ||
regex_pattern=REGEX_READLINE_COMMANDS, | ||
command_type="Urwid Readline", | ||
suffix="READLINE_SUFFIX", | ||
) | ||
lint_external_commands_by_type( | ||
regex_pattern=REGEX_TERMINAL_COMMANDS, | ||
command_type="General terminal", | ||
suffix="GENERAL_TERMINAL_SUFFIX", | ||
) | ||
print("All external commands have been linted successfully.") | ||
|
||
|
||
def lint_external_commands_by_type( | ||
regex_pattern: str, command_type: str, suffix: str | ||
) -> None: | ||
""" | ||
Lint src directory for the usage of external commands | ||
in the codebase by checking for their regex | ||
""" | ||
error_flag = 0 | ||
for file_path in ZULIPTERMINAL.rglob("*.py"): | ||
if file_path in EXCLUDED_FILES: | ||
continue | ||
with file_path.open() as f: | ||
contents = f.read() | ||
regex_matches = re.finditer(regex_pattern, contents) | ||
suffix_matches = re.finditer(suffix, contents) | ||
count_matches = sum(1 for _ in regex_matches) + sum( | ||
1 for _ in suffix_matches | ||
) | ||
if count_matches > 0: | ||
print( | ||
f"{file_path.name} contains {count_matches} mentions of {command_type} commands." | ||
) | ||
error_flag = 1 | ||
if error_flag == 1: | ||
print( | ||
f"{command_type} commands are not intended for direct use or modification." | ||
) | ||
print( | ||
f"Please refer to {KEYS_FILE_NAME} for identifying the {command_type} commands." | ||
) | ||
print("Rerun this command after removing the usage of external commands.") | ||
sys.exit(error_flag) | ||
Comment on lines
+74
to
+97
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we adjust this approach to include the line number at which it was found along with the file name? |
||
|
||
|
||
def lint_hotkeys_file() -> None: | ||
""" | ||
Lint KEYS_FILE for key description, then compare if in sync with | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the motivation behind this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd have the same question :) (just reposting this, because it's now my turn to review)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, partially answering it myself. The commit message gives it away: The change seems to aim to
but I'm not sure why. Is it because we can not rely on the existence of readline shortcuts due to upstream changes? That seems reasonable to me. In general, it could be helpful to add the motivation for the goal to the commit message :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forgot to get back regarding this, @zormit, my apologies.
Sashank and I had talked about this, so I hadn't added an answer here for his initial question.
Yes, you're spot on.
Thank you for the pointer. You've worded it pretty well, I did not think of it in those terms.