Skip to content

Refactor columns() to use extract_parenthesized and simplify parsing logic #90

@sourcery-ai

Description

@sourcery-ai

The current implementation of the columns() function involves manual parsing logic, including custom buffering and parenthesis balancing, as well as several private helper functions (skip_to_lparen, process_token, handle_rparen, handle_comma, add_column_if_non_empty). This approach increases code complexity and maintenance overhead.

It is suggested to simplify columns() by leveraging the existing extract_parenthesized utility function (presumably in parse_utils.rs or syntax_utils.rs). This function can extract the content between the first matching pair of parentheses. Once extracted, the string can be split on commas, trimmed, and filtered for non-empty values, resulting in a much simpler and more maintainable implementation.

Proposed refactor:

use crate::parse_utils::extract_parenthesized;

pub fn columns(&self) -> Vec<String> {
    // grab everything between the first '(' and matching ')'
    let inside = extract_parenthesized(
        &self.syntax,
        SyntaxKind::T_LPAREN,
        SyntaxKind::T_RPAREN,
    ).unwrap_or_default();

    // split on commas and trim
    inside
        .split(',')
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .map(String::from)
        .collect()
}

Action items:

  • Refactor columns() to use extract_parenthesized and simple string splitting as shown above.
  • Remove the now-unnecessary private helper functions listed above.
  • Ensure all existing tests pass and add new tests if needed to cover edge cases.

This change will reduce code complexity and improve maintainability without altering the function's behavior.


I created this issue for @leynos from #89 (comment).

Tips and commands

Getting Help

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions