KEMBAR78
Add `QuoteCompletionText` method to CompletionHelpers class by ArmaanMcleod · Pull Request #25180 · PowerShell/PowerShell · GitHub
Skip to content

Conversation

@ArmaanMcleod
Copy link
Contributor

@ArmaanMcleod ArmaanMcleod commented Mar 15, 2025

PR Summary

Add QuoteCompletionText method to CompletionHelpers class.

This is to ensure quoted completions also get escaping done before surrounding quotes are added. Current code in GetMatchingResults just surrounds completion with quotes without doing any escaping.

I've replaced this code as well as many single quote escaping calls in CompletionCompleters.

PR Context

PR Checklist

@ArmaanMcleod ArmaanMcleod requested a review from iSazonov March 15, 2025 14:47
@ArmaanMcleod
Copy link
Contributor Author

Thanks for feedback @iSazonov. I have made method more flexible with different escaping parameters, a lot of code in CompletionCompleters can use new method. Mostly the simple blocks where single quote escaping was happening before quoting. I've also added XUnit tests for the QuoteCompletionText method.

@iSazonov
Copy link
Collaborator

a lot of code in CompletionCompleters can use new method.

Can we use it in GetMatchingResults() method for other completers?

@ArmaanMcleod
Copy link
Contributor Author

a lot of code in CompletionCompleters can use new method.

Can we use it in GetMatchingResults() method for other completers?

@iSazonov It is used in GetMatchingResults:

string completionText = QuoteCompletionText(value, quote);

@ArmaanMcleod
Copy link
Contributor Author

Probably best to also wait on #25184 so existing implementation of GetMatchingResults covered by test.

@ArmaanMcleod ArmaanMcleod requested a review from iSazonov March 16, 2025 08:22
@ArmaanMcleod
Copy link
Contributor Author

I'll fix the tests.

@ArmaanMcleod
Copy link
Contributor Author

@MartinGC94 If you have time, it would be great to have a review. I have started writing a new method QuoteCompletionText to consolidate escaping + quoting into a single method.

@iSazonov
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@MartinGC94
Copy link
Contributor

@ArmaanMcleod At a glance it looks fine to me for the intended purpose of removing all the duplicate code and escaping single quotes.

@iSazonov
Copy link
Collaborator

@ArmaanMcleod Please look CI fails.

@ArmaanMcleod ArmaanMcleod requested a review from iSazonov March 19, 2025 08:54
@ArmaanMcleod
Copy link
Contributor Author

Thanks @iSazonov for review and suggestions. I have added back in dynamic escaping for double quotes and updated tests.

@ArmaanMcleod ArmaanMcleod requested a review from iSazonov March 19, 2025 11:06
[InlineData("", "'", "''")]
[InlineData("", "\"", "\"\"")]
[InlineData("'", "'", "''''")]
[InlineData("", "", "''")]
Copy link
Collaborator

@iSazonov iSazonov Mar 19, 2025

Choose a reason for hiding this comment

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

I still think we should cover CompletionRequiresQuotes by tests too. There we use Parser and additionally checks for backtick and $.
So string for tests could be like "a b", "$a", "a" Maybe you could think more tests - I see StringTokenandTokenFlags.Keyword` in the method. The test strings could be "a$a" and "while" accordingly with variants with backtick and $.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@iSazonov Added some tests here for CompletionRequiresQuotes: fced8d7

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks!

The QuoteCompletionText is more interesting for us. Maybe add 2-3 key tests here?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Up!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@iSazonov Ok I have added more tests and reorganized them in 9bff47b.

@ArmaanMcleod ArmaanMcleod requested a review from iSazonov March 19, 2025 12:20
Co-authored-by: Ilya <darpa@yandex.ru>
[InlineData("", "'", "''")]
[InlineData("", "\"", "\"\"")]
[InlineData("'", "'", "''''")]
[InlineData("", "", "''")]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks!

The QuoteCompletionText is more interesting for us. Maybe add 2-3 key tests here?

ArmaanMcleod and others added 2 commits March 20, 2025 06:42
@ArmaanMcleod ArmaanMcleod requested a review from iSazonov March 22, 2025 01:51
[InlineData("word with space", "'", "'word with space'")]
[InlineData("word with space", "\"", "\"word with space\"")]
[InlineData("word\"with\"quotes", "'", "'word\"with\"quotes'")]
[InlineData("word'with'quotes", "\"", "\"word'with'quotes\"")]
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd add tests for "while", "$variable" and "key$word".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@iSazonov I believe we have tests for those already?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or do we want the same test from CompletionRequiresQuotes in QuoteCompletionText?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It looks like duplicate but it protects from regression if we will improve CompletionRequiresQuotes and change its tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep makes sense, I have added tests in 5cb39e0

@iSazonov iSazonov self-assigned this Mar 22, 2025
@iSazonov

This comment was marked as outdated.

@azure-pipelines

This comment was marked as outdated.

@iSazonov iSazonov merged commit 94d0361 into PowerShell:master Mar 22, 2025
36 of 39 checks passed
@microsoft-github-policy-service
Copy link
Contributor

microsoft-github-policy-service bot commented Mar 22, 2025

📣 Hey @ArmaanMcleod, how did we do? We would love to hear your feedback with the link below! 🗣️

🔗 https://aka.ms/PSRepoFeedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-CodeCleanup Indicates that a PR should be marked as a Code Cleanup change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants