Quick Fixes to create new parameters or variables when there are undefined symbols in bicep files. #18654
+1,538
−17
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.
Description
Adds a quick fix for BCP057 ("The name does not exist in the current context") that offers to create a parameter or variable declaration for undefined symbols. This makes using snippets without included parameters / variables much easier. This was requested in and fixes #12536
When determining the type for a new parameter, the fix first checks usage context (e.g., bool if used in a condition, int if used in arithmetic), then tries resource-derived input types for complex properties, then user-defined type aliases, and finally falls back to TypeStringifier for the inferred type.
Variables try to assign some reasonable defaults when created, inferred from types. However, for objects if there are more than 5 properties, the variable defaults to an empty object to not make too many assumptions which the user might need.
New declarations are inserted after the last existing declaration of the same kind, or at the start of the referencing statement.
Type inference requires semantic model APIs (GetDeclaredType, GetDeclaredTypeAssignment, contextual analysis) that aren't available during diagnostic emission, so this was implemented as an LSP code action rather than attached directly to the diagnostic.
Example Usage
Please check the included tests for further use cases. Here's a video showing some in the IDE.
UndefinedTypesQuickFix.webm
NOTE: Parameters passed through module context pass the resourceInput values through, but defined user types are not explicitly imported.
For example a module:
Calling file's codefix produces
Checklist
Microsoft Reviewers: Open in CodeFlow