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

fix: not complete str functions when inside literal str #923

Merged
merged 2 commits into from
Nov 28, 2023

Conversation

He1pa
Copy link
Contributor

@He1pa He1pa commented Nov 28, 2023

1. Does this PR affect any open issues?(Y/N) and add issue references (e.g. "fix #123", "re #123".):

2. What is the scope of this PR (e.g. component or file name):

lsp/completion.rs

3. Provide a description of the PR(e.g. more details, effects, motivations or doc link):

not complete str functions when inside literal str

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Other

4. Are there any breaking changes?(Y/N) and describe the breaking changes(e.g. more details, motivations or doc link):

  • N
  • Y

5. Are there test cases for these changes?(Y/N) select and add more details, references or doc links:

  • Unit test
  • Integration test
  • Benchmark (add benchmark stats below)
  • Manual test (add detailed scripts or steps below)
  • Other

Copy link
Contributor

@Peefy Peefy left a comment

Choose a reason for hiding this comment

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

LGTM

@He1pa He1pa force-pushed the fix_lit_str_complete branch from c050175 to 5190392 Compare November 28, 2023 03:48
@coveralls
Copy link
Collaborator

Pull Request Test Coverage Report for Build 7013945867

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 87.82%

Totals Coverage Status
Change from base Build 7004959303: 0.0%
Covered Lines: 2300
Relevant Lines: 2619

💛 - Coveralls

@Peefy
Copy link
Contributor

Peefy commented Nov 28, 2023

@CodiumAI-Agent /review

@CodiumAI-Agent
Copy link

PR Analysis

  • 🎯 Main theme: This PR aims to fix the issue of string functions being auto-completed inside string literals in the Rust language.
  • 📝 PR summary: The PR addresses a bug in the language server protocol (LSP) for the Rust language where string functions were being auto-completed inside string literals. The changes ensure that string functions are only auto-completed at the end of string literals and not inside them. The PR also includes relevant tests to validate this behavior.
  • 📌 Type of PR: Bug fix
  • 🧪 Relevant tests added: Yes
  • ⏱️ Estimated effort to review [1-5]: 2, because the PR is relatively small and the changes are straightforward. The added tests also make it easier to understand the intended behavior.
  • 🔒 Security concerns: No security concerns found

PR Feedback

  • 💡 General suggestions: The PR is well-structured and follows good practices like linking to the issue it resolves and providing a clear description. The changes are focused and include relevant tests. It would be beneficial to include a brief explanation of why the bug occurred and how the changes fix it in the PR description.

  • 🤖 Code feedback:
    • relevant file: kclvm/tools/src/LSP/src/completion.rs
      suggestion: Consider adding a comment explaining the conditional check if pre_pos == node.get_end_pos(). This would make it easier for other developers to understand why this check is necessary. [medium]
      relevant line: if pre_pos == node.get_end_pos() {

    • relevant file: kclvm/tools/src/LSP/src/completion.rs
      suggestion: The else clause returns Some(into_completion_items(&items).into()), but it's not clear where items is defined or what it contains. Ensure that items is properly defined and populated before this point. [important]
      relevant line: return Some(into_completion_items(&items).into());

How to use

Instructions

Tag me in a comment '@CodiumAI-Agent' and add one of the following commands:
/review: Request a review of your Pull Request.
/describe: Update the PR title and description based on the contents of the PR.
/improve [--extended]: Suggest code improvements. Extended mode provides a higher quality feedback.
/ask <QUESTION>: Ask a question about the PR.
/update_changelog: Update the changelog based on the PR's contents.
/add_docs: Generate docstring for new components introduced in the PR.
/generate_labels: Generate labels for the PR based on the PR's contents.
see the tools guide for more details.

To edit any configuration parameter from the configuration.toml, add --config_path=new_value.
For example: /review --pr_reviewer.extra_instructions="focus on the file: ..."
To list the possible configuration parameters, add a /config comment.

@Peefy
Copy link
Contributor

Peefy commented Nov 28, 2023

@CodiumAI-Agent /improve

@CodiumAI-Agent
Copy link

PR Code Suggestions

💡 Suggestion:

Consider refactoring the completion function to reduce its complexity and improve readability. The function is currently doing too much, which makes it hard to understand. You could extract the logic for handling different types of expressions into separate functions.

File: kclvm/tools/src/LSP/src/completion.rs (169-198)

Example code:

Existing code:

fn completion_dot(
    ...
    Stmt::Import(stmt) => return completion_import(&stmt, &pre_pos, prog_scope, program),
    _ => {
        // Todo: string lit has not been processed using the new semantic model and need to handle here.
        let (expr, _) = inner_most_expr_in_stmt(&stmt.node, &pre_pos, None);
        if let Some(node) = expr {
            if let Expr::StringLit(_) = node.node {
                if pre_pos == node.get_end_pos() {
                    return Some(
                        into_completion_items(
                            &STRING_MEMBER_FUNCTIONS
                                .iter()
                                .map(|(name, ty)| KCLCompletionItem {
                                    label: func_ty_complete_label(
                                        name,
                                        &ty.into_function_ty(),
                                    ),
                                    detail: Some(ty.ty_str()),
                                    documentation: ty.ty_doc(),
                                    kind: Some(KCLCompletionItemKind::Function),
                                })
                                .collect(),
                        )
                        .into(),
                    );
                } else {
                    return Some(into_completion_items(&items).into());
                }
            }
        }
    }
)

Improved code:

fn completion_dot(
    ...
    Stmt::Import(stmt) => return completion_import(&stmt, &pre_pos, prog_scope, program),
    _ => {
        return handle_other_statements(&stmt, &pre_pos);
    }
)

fn handle_other_statements(stmt: &Stmt, pre_pos: &KCLPos) -> Option<CompletionResponse> {
    // Todo: string lit has not been processed using the new semantic model and need to handle here.
    let (expr, _) = inner_most_expr_in_stmt(&stmt.node, pre_pos, None);
    if let Some(node) = expr {
        if let Expr::StringLit(_) = node.node {
            return handle_string_literal(node, pre_pos);
        }
    }
    None
}

fn handle_string_literal(node: &Expr, pre_pos: &KCLPos) -> Option<CompletionResponse> {
    if pre_pos == node.get_end_pos() {
        return Some(
            into_completion_items(
                &STRING_MEMBER_FUNCTIONS
                    .iter()
                    .map(|(name, ty)| KCLCompletionItem {
                        label: func_ty_complete_label(
                            name,
                            &ty.into_function_ty(),
                        ),
                        detail: Some(ty.ty_str()),
                        documentation: ty.ty_doc(),
                        kind: Some(KCLCompletionItemKind::Function),
                    })
                    .collect(),
            )
            .into(),
        );
    } else {
        return Some(into_completion_items(&items).into());
    }
}

Signed-off-by: he1pa <[email protected]>
@He1pa He1pa force-pushed the fix_lit_str_complete branch from 60acc52 to 228f7d6 Compare November 28, 2023 06:04
@coveralls
Copy link
Collaborator

coveralls commented Nov 28, 2023

Pull Request Test Coverage Report for Build 7014853009

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 87.787%

Totals Coverage Status
Change from base Build 7014765335: 0.0%
Covered Lines: 2293
Relevant Lines: 2612

💛 - Coveralls

@Peefy Peefy added the lsp label Nov 28, 2023
@Peefy Peefy added this to the v0.7.0 Release milestone Nov 28, 2023
@He1pa He1pa merged commit 3e34c93 into kcl-lang:main Nov 28, 2023
9 of 11 checks passed
@He1pa He1pa deleted the fix_lit_str_complete branch November 28, 2023 08:56
@github-actions github-actions bot locked and limited conversation to collaborators Nov 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants