-
Notifications
You must be signed in to change notification settings - Fork 818
[bicep console] Use visitor pattern to handle multi-line inputs #18216
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/Bicep.Cli.UnitTests/Helpers/Repl/StructuralCompletenessHelperTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using Bicep.Cli.Helpers.Repl; | ||
| using FluentAssertions; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| namespace Bicep.Cli.UnitTests.Helpers.Repl; | ||
|
|
||
| [TestClass] | ||
| public class StructuralCompletenessHelperTests | ||
| { | ||
| [DataTestMethod] | ||
| [DataRow("42", true)] | ||
| [DataRow("'hello'", true)] | ||
| [DataRow("true", true)] | ||
| [DataRow("items([1, 2, 3])", true)] | ||
| [DataRow("var a = 'complete'", true)] | ||
| [DataRow("length(['a', 'b'])", true)] | ||
| [DataRow( | ||
| """ | ||
| ''' | ||
| Hello | ||
| World | ||
| ''' | ||
| """, true)] | ||
| [DataRow("items({", false)] | ||
| [DataRow("var a = items({", false)] | ||
| [DataRow("var b = length([", false)] | ||
| [DataRow("[", false)] | ||
| [DataRow("{", false)] | ||
| [DataRow("(length(", false)] | ||
| [DataRow("filter(map([1,2],", false)] | ||
| [DataRow( | ||
| """ | ||
| filter(map([1,2], | ||
| x => x + 1), | ||
| y => y % 2 == 0 | ||
| """, false)] | ||
| [DataRow( | ||
| """ | ||
| ''' | ||
| Hello | ||
| World | ||
| """, false)] | ||
| public void IsStructurallyComplete_CompleteExpressions_ReturnsExpectedResult(string text, bool expected) | ||
| { | ||
| StructuralCompletenessHelper.IsStructurallyComplete(text).Should().Be(expected); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/Bicep.Cli/Helpers/Repl/StructuralCompletenessHelper.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using Bicep.Core.Parsing; | ||
| using Bicep.Core.Syntax; | ||
|
|
||
| namespace Bicep.Cli.Helpers.Repl; | ||
|
|
||
| public static class StructuralCompletenessHelper | ||
| { | ||
| public static bool IsStructurallyComplete(string text) | ||
| { | ||
| var program = new ReplParser(text).Program(); | ||
| var completenessVisitor = new StructuralCompletenessVisitor(); | ||
| program.Accept(completenessVisitor); | ||
| return completenessVisitor.IsComplete; | ||
| } | ||
|
|
||
| private sealed class StructuralCompletenessVisitor : CstVisitor | ||
| { | ||
| public bool IsComplete { get; private set; } = true; | ||
|
|
||
| public override void VisitSkippedTriviaSyntax(SkippedTriviaSyntax syntax) | ||
| { | ||
| IsComplete = false; | ||
| // don't visit children - we already know it's incomplete | ||
| } | ||
|
|
||
| public override void VisitVariableDeclarationSyntax(VariableDeclarationSyntax syntax) | ||
| { | ||
| if (syntax.Value is SkippedTriviaSyntax) | ||
| { | ||
| IsComplete = false; | ||
| return; | ||
| } | ||
|
|
||
| base.VisitVariableDeclarationSyntax(syntax); | ||
| } | ||
|
|
||
| public override void VisitFunctionCallSyntax(FunctionCallSyntax syntax) | ||
| { | ||
| if (syntax.CloseParen is SkippedTriviaSyntax) | ||
| { | ||
| IsComplete = false; | ||
| return; | ||
| } | ||
|
|
||
| base.VisitFunctionCallSyntax(syntax); | ||
| } | ||
|
|
||
| public override void VisitParenthesizedExpressionSyntax(ParenthesizedExpressionSyntax syntax) | ||
| { | ||
| if (syntax.CloseParen is SkippedTriviaSyntax) | ||
| { | ||
| IsComplete = false; | ||
| return; | ||
| } | ||
|
|
||
| base.VisitParenthesizedExpressionSyntax(syntax); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One possible complexity with the visitor pattern is that you can get "stuck" inside a piece of deeply nested syntax - e.g. something like:
{ foo: { bar: [ 'abc' abc: 'def' } }Here I accidentally missed a
], and so my expression is not structurally complete. It's not very clear to me that I need to type a], and so it could feel like I'm "stuck" - e.g. I can't exit the statement whatever I press.Is that something to consider here, or do you have a mitigation for this already?