Skip to content
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

LSP default message for diagnostics #970

Merged
merged 2 commits into from
Mar 7, 2024

Conversation

StevenLove
Copy link
Member

@StevenLove StevenLove commented Mar 7, 2024

Fix for #969
If a published diagnostic's message property would be the empty string, replace it with a default string.
The default string is the rule's id property.

Summary by CodeRabbit

  • New Features
    • Improved message handling in configurations by introducing a fallback to default messages when no custom message is provided.

… Otherwise an empty message prevents any diagnostic from appearing
Copy link
Contributor

coderabbitai bot commented Mar 7, 2024

Walkthrough

The recent update introduces a new method to the SerializableRuleConfig structure for retrieving a default message, enhancing the existing get_message method to provide a fallback option. This improvement ensures that even when a custom message is not specified, a predefined message will be used, thereby refining the configuration's robustness and user experience.

Changes

File Path Change Summary
.../config/src/rule_config.rs
.../lsp/src/lib.rs
Added get_default_message method to SerializableRuleConfig and get_non_empty_message function in lib.rs. Updated get_message and convert_match_to_diagnostic to handle empty messages with defaults.

🐇✨
In the realm of code, where rules are king,
A rabbit hopped in, a new song to sing.
"No message lost, no whisper in vain,
For defaults now fill the empty lane."
With a hop and a skip, it set things right,
In the glow of the code, a beacon of light. 🌟
🐇✨

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 745ccca and 812ac08.
Files selected for processing (1)
  • crates/config/src/rule_config.rs (1 hunks)
Additional comments: 2
crates/config/src/rule_config.rs (2)
  • 70-72: The implementation of get_default_message method is straightforward and effectively returns the rule's id as the default message. Ensure that the id field is always populated with meaningful identifiers for this default mechanism to be useful.
  • 75-81: The modification to get_message to use a default message when the original message is empty is a good enhancement. However, consider reviewing the error handling strategy for UTF-8 conversion to ensure it aligns with the overall error handling approach of the application. Additionally, assess the performance implications of calling generate_replacement in this context.

fn get_message(&self, node_match: &NodeMatch<StrDoc<L>>) -> String {
let bytes = self.message.generate_replacement(node_match);
// Note: The LSP client in vscode won't show any diagnostics at all if it receives one with an empty message
Copy link
Member

Choose a reason for hiding this comment

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

hmm, this is not 100% correct since the empty message will be used in sg scan CLI.
Can we move this to LSP?

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay I've submitted a change where it's only the diagnostic that has an empty message replaced with a default message. The message field is also used to generate the codeaction title, but vscode is happy to display a codeaction with an empty title, so I'm happy to leave the codeaction unchanged.

Copy link

codecov bot commented Mar 7, 2024

Codecov Report

Attention: Patch coverage is 50.00000% with 3 lines in your changes are missing coverage. Please review.

Project coverage is 71.85%. Comparing base (745ccca) to head (812ac08).
Report is 1 commits behind head on main.

❗ Current head 812ac08 differs from pull request most recent head 3d8ed01. Consider uploading reports for the commit 3d8ed01 to get more accurate results

Files Patch % Lines
crates/config/src/rule_config.rs 50.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #970      +/-   ##
==========================================
- Coverage   71.88%   71.85%   -0.04%     
==========================================
  Files          72       72              
  Lines        4706     4711       +5     
==========================================
+ Hits         3383     3385       +2     
- Misses       1323     1326       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 812ac08 and 3d8ed01.
Files selected for processing (1)
  • crates/lsp/src/lib.rs (2 hunks)
Additional comments: 2
crates/lsp/src/lib.rs (2)
  • 190-196: The implementation of get_non_empty_message effectively ensures that diagnostic messages are never empty, which aligns with the PR's objectives. However, directly using the rule's id as a fallback message might not always provide clear context to the user. Consider adding a prefix or a more descriptive fallback message format, such as "Rule violation: " + rule.id.to_string(), to enhance clarity.
  • 214-214: The integration of get_non_empty_message within convert_match_to_diagnostic is correctly implemented and aligns with the PR's objectives to ensure meaningful diagnostic messages. It's also worth reviewing the error handling and logging mechanisms within the diagnostic generation process to ensure that any issues are appropriately reported and handled.

Copy link
Member

@HerringtonDarkholme HerringtonDarkholme left a comment

Choose a reason for hiding this comment

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

thanks!

@HerringtonDarkholme HerringtonDarkholme merged commit 7de1ac2 into ast-grep:main Mar 7, 2024
2 checks passed
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.

None yet

2 participants