Skip to content

Conversation

Jack251970
Copy link
Member

@Jack251970 Jack251970 commented Mar 8, 2025

Fix Index Issue for Format String

If format string skips certain values, these missing data will not be generated. We should analyze format string first to get the max index.

Add Culture Info When Need to Format

When format string needs to be formatted (like {1:D}), we should pass culture info so that it can parse string correctly.

Test

The string resources are:
image

The generated codes are:
Screenshot 2025-03-08 182826
Screenshot 2025-03-08 182835

Copy link
Contributor

coderabbitai bot commented Mar 8, 2025

📝 Walkthrough

Walkthrough

The update enhances the LocalizeSourceGenerator class by refactoring the parsing and generation of localizable strings from XAML files. A new GetParameters method extracts format parameters, replacing the previous approach. The ParseCommentAndUpdateParameters method is modified to update parameter details from XML comments, while the GenerateLocalizationMethod is adjusted for conditional formatting. Additionally, the LocalizableStringParam class now includes mutable properties and a new Format property.

Changes

File(s) Summary
Flow.Launcher.Localization…LocalizeSourceGenerator.cs - Added: GetParameters method to extract parameters from format strings.
- Updated: ParseComment to ParseCommentAndUpdateParameters to include parameter list.
- Modified: GenerateLocalizationMethod for conditional formatting.
- Updated: LocalizableStringParam properties (Name, Type mutable) and added new Format property.

Possibly related PRs

Suggested labels

enhancement

Suggested reviewers

  • Yusyuriv

Poem

I'm a little rabbit, hopping in the code,
Parsing strings where format parameters explode,
With comments and methods refined so fine,
I dig through XAML like a burrow divine,
Here's a joyful hop for changes that brightly shine! 🐇


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ce8ff62 and 671fc75.

📒 Files selected for processing (1)
  • Flow.Launcher.Localization.SourceGenerators/Localize/LocalizeSourceGenerator.cs (5 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build
🔇 Additional comments (6)
Flow.Launcher.Localization.SourceGenerators/Localize/LocalizeSourceGenerator.cs (6)

156-158: Good refactoring of parameter extraction logic

The code now extracts format parameters using a dedicated method and updates them with comment information. This is a cleaner approach than the previous implementation.


165-305: Well-implemented format string analysis

This new method effectively addresses the index issue mentioned in the PR by thoroughly parsing format strings and identifying all parameters. The approach:

  1. Correctly handles different format patterns (simple indices, alignment, and format specifiers)
  2. Properly processes escaped characters like {{ and }}
  3. Tracks the maximum index to ensure no parameters are skipped
  4. Returns a complete list of parameters with their indices and format specifiers

The implementation is robust and should resolve the format string index skipping issue.


307-349: Improved parameter handling with XML comments

The updated method now accepts a list of parameters to update rather than creating them from scratch, making it more flexible. The null checks and empty list handling improve robustness.


644-648: Culture information correctly applied to formatted strings

The implementation now conditionally applies CultureInfo.CurrentCulture when format specifiers are present, which addresses the second objective of the PR. This ensures culture-specific formatting is properly handled when needed.

Also applies to: 654-657


687-690: Appropriate modifications to parameter properties

Converting the properties to mutable and adding the Format property supports the new parameter extraction and update approach. This change is necessary to support the new functionality.


700-700: Smart detection of format specifiers

The Format property elegantly determines if culture information should be applied by checking if any parameter has a format specifier. This is a clean approach that ensures culture information is only added when needed.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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>, please review it.
    • Generate unit testing code 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 testing code 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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.

Actionable comments posted: 0

🧹 Nitpick comments (5)
Flow.Launcher.Localization.SourceGenerators/Localize/LocalizeSourceGenerator.cs (5)

165-285: Consider edge cases in custom .NET format parsing.
This method correctly handles escaped braces and basic format items. However, more advanced format specifiers (e.g., alignment like "{0,5}" or nested braces) aren't currently handled here. If that's within scope, consider extending the logic to handle additional format scenarios.


612-614: Minor clarity suggestion for parameter gathering.
Storing ls.Params.ToList() is fine, but you could consider using var parameters = ls.Params as IReadOnlyList<LocalizableStringParam> ?? ls.Params.ToList(); if you frequently need a list. It’s a small readability improvement.


624-626: Avoid nested ternary for readability.
Using an if statement or a local boolean variable to differentiate the presence of a custom format might make the code more readable than a nested ternary expression.


632-634: Consolidate similar logic for formatting.
As in lines 624-626, consider refactoring this nested ternary into a clearer conditional block. This will consistently improve maintainability and readability.


677-678: Clarify property name if usage is ambiguous.
Currently named Format, this property returns whether any parameter includes a format specifier. Consider a name like HasCustomFormat or RequiresCultureFormatting if you want to convey intent more clearly.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f8b14b4 and e1cfe81.

📒 Files selected for processing (1)
  • Flow.Launcher.Localization.SourceGenerators/Localize/LocalizeSourceGenerator.cs (5 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build
🔇 Additional comments (3)
Flow.Launcher.Localization.SourceGenerators/Localize/LocalizeSourceGenerator.cs (3)

156-158: Looks good – parameters are now parsed and updated.
Splitting the parameter extraction and comment-based enrichment steps into two separate calls improves maintainability and clarity.


287-329: Validate XML comment attributes before usage.
Accessing attributes like p.Attribute("index").Value can trigger exceptions if the XML comment is malformed or missing these attributes. Consider adding checks or graceful handling to avoid potential NullReferenceExceptions and improve resilience.


665-668: Adjustable parameter properties enhance flexibility.
Changing these fields to auto-properties with setters is a sensible move that allows for post-processing or updates to parameter definitions.

@Jack251970 Jack251970 added the bug Something isn't working label Mar 8, 2025
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.

Actionable comments posted: 0

🧹 Nitpick comments (1)
Flow.Launcher.Localization.SourceGenerators/Localize/LocalizeSourceGenerator.cs (1)

165-285: Robust implementation for format string parameter extraction.

This new method thoroughly handles all format string scenarios including:

  • Escaped braces ({{ and }})
  • Format specifiers (after :)
  • Proper index parsing and validation
  • Tracking of the maximum index to ensure no parameters are skipped

The implementation correctly addresses the index issue mentioned in the PR objectives by ensuring all parameters from index 0 to maxIndex are included, even if some indices are missing in the format string.

One minor optimization suggestion:

- var parameters = new Dictionary<int, string>();
+ var parameters = new Dictionary<int, string>(capacity: format.Length / 10 + 1);

Using a capacity hint can reduce allocations when dealing with longer format strings with many parameters.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e1cfe81 and ce8ff62.

📒 Files selected for processing (1)
  • Flow.Launcher.Localization.SourceGenerators/Localize/LocalizeSourceGenerator.cs (5 hunks)
🔇 Additional comments (6)
Flow.Launcher.Localization.SourceGenerators/Localize/LocalizeSourceGenerator.cs (6)

156-158: Good refactoring to improve parameter handling.

The change from direct parameter parsing to using a dedicated GetParameters method creates a cleaner separation of concerns. This is essential for the PR's goal of fixing the index issue for format strings.


293-324: Well-structured parameter update logic.

The method now correctly checks for empty parameters and properly updates the provided parameter list, supporting the PR objectives. The parameter handling is comprehensive and handles XML parsing errors gracefully.


625-628: Critical culture-aware formatting implementation.

This conditional formatting approach addresses the second objective of the PR by adding culture information when formatting strings. Using CultureInfo.CurrentCulture ensures numbers, dates, and other format-sensitive data will be displayed correctly according to the user's culture settings.

The conditional check on !ls.Format is elegant and avoids unnecessary overhead when no specific formatting is needed.


634-637: Consistent cultural formatting for plugin translations.

The same culture-aware formatting approach is correctly applied to plugin translations, ensuring consistent behavior across the application.


667-670: Property mutability enables better parameter handling.

Changing these properties from get-only to mutable ({ get; set; }) is necessary to support the updated parameter parsing and updating approach. The addition of the Format property is essential for tracking formatting specifications.


679-680: Smart format detection with LINQ.

This property efficiently determines if any parameter requires special formatting by checking if any parameter has a non-empty Format value. This drives the conditional formatting logic in GenerateLocalizationMethod.

@Jack251970 Jack251970 requested a review from Yusyuriv March 8, 2025 10:39
@Yusyuriv
Copy link
Member

Yusyuriv commented Mar 9, 2025

With these translations:

<!-- <param name="count" type="int" index="0" /> -->
<system:String x:Key="about_activate_times">You have activated Flow Launcher {0:D} times</system:String>
<!-- <param name="count" type="int" index="0" /> -->
<system:String x:Key="about_activate_times2">You have activated Flow Launcher {0,35} times</system:String>
<!-- <param name="count" type="int" index="0" /> -->
<system:String x:Key="about_activate_times3">You have activated Flow Launcher {0,35:D} times</system:String>

It seems to generate this code for me:

/// <code>
/// You have activated Flow Launcher {0:D} times
/// </code>
public static string about_activate_times(int count) => string.Format(System.Globalization.CultureInfo.CurrentCulture, Class1.Context.API.GetTranslation("about_activate_times"), count);

/// <code>
/// You have activated Flow Launcher {0,35} times
/// </code>
public static string about_activate_times2(int count) => string.Format(Class1.Context.API.GetTranslation("about_activate_times2"), count);

/// <code>
/// You have activated Flow Launcher {0,35:D} times
/// </code>
public static string about_activate_times3(int count) => string.Format(Class1.Context.API.GetTranslation("about_activate_times3"), count);

I'm not sure about the second example, but I think the third one also should include culture?

@Jack251970
Copy link
Member Author

Well, let me check. iirc it can work for your third example.

@Jack251970
Copy link
Member Author

It seems that the previous version cannot fully support alignment string format like "{0,20:D}".

@Jack251970
Copy link
Member Author

For second example, I do not think we need to add culture info since alighment should be the same for all cultures.

@Jack251970 Jack251970 merged commit f8496ab into main Mar 9, 2025
2 checks passed
@Jack251970 Jack251970 deleted the format_string branch March 9, 2025 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants