generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 1
add Extract To Parameter code actions #32
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
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
9bee294
add Extract To Parameter code actions
fae6b4c
use existing intrinsic function list
988fe9f
fix import order
b05a611
defensive null/undefined handling
211e197
lint
a743ec4
Addressed comments
ad2e3f4
Merge branch 'main' into feature/extract-to-parameter-code-actions
atennak1 354ed94
Undo SytaxTree regex changes
b284264
Merge branch 'main' into feature/extract-to-parameter-code-actions
atennak1 5f0e8a8
Merge branch 'main' into feature/extract-to-parameter-code-actions
bf2fce4
Merge remote-tracking branch 'origin/main' into feature/extract-to-pa…
993a470
adapt detected indendation support
5e84710
handle null doc
6dc89d4
adapt detected indendation support
0d20d24
Merge branch 'main' into feature/extract-to-parameter-code-actions
atennak1 da4c7be
Fix JSON indentation and new parameter section placement
f6920d6
address ExtractToParameter.yaml.test.ts todos
210a1c4
Use preferred method for detected indentation
4a36bd6
Merge remote-tracking branch 'origin/main' into feature/extract-to-pa…
19ddee0
Merge remote-tracking branch 'origin/main' into feature/extract-to-pa…
59ae06a
remove unused components from CodeActionService
4711da4
lint
82a7df6
Merge branch 'main' into feature/extract-to-parameter-code-actions
atennak1 06956e4
Merge branch 'main' into feature/extract-to-parameter-code-actions
atennak1 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
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
115 changes: 115 additions & 0 deletions
115
src/services/extractToParameter/AllOccurrencesFinder.ts
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,115 @@ | ||
| import { SyntaxNode } from 'tree-sitter'; | ||
| import { Range } from 'vscode-languageserver'; | ||
| import { TopLevelSection } from '../../context/ContextType'; | ||
| import { SyntaxTreeManager } from '../../context/syntaxtree/SyntaxTreeManager'; | ||
| import { LoggerFactory } from '../../telemetry/LoggerFactory'; | ||
| import { LiteralValueInfo, LiteralValueType } from './ExtractToParameterTypes'; | ||
| import { LiteralValueDetector } from './LiteralValueDetector'; | ||
|
|
||
| /** | ||
| * Finds all occurrences of a literal value within a CloudFormation template. | ||
| * Used for the "Extract All Occurrences to Parameter" refactoring action. | ||
| * Only finds occurrences within Resources and Outputs sections as only they | ||
| * can reference parameters. | ||
| */ | ||
| export class AllOccurrencesFinder { | ||
| private readonly log = LoggerFactory.getLogger(AllOccurrencesFinder); | ||
| private readonly literalDetector: LiteralValueDetector; | ||
| private readonly syntaxTreeManager: SyntaxTreeManager; | ||
|
|
||
| constructor(syntaxTreeManager: SyntaxTreeManager) { | ||
| this.literalDetector = new LiteralValueDetector(); | ||
| this.syntaxTreeManager = syntaxTreeManager; | ||
| } | ||
|
|
||
| /** | ||
| * Finds all occurrences of the same literal value in the template. | ||
| * Returns ranges for all matching literals that can be safely replaced. | ||
| * Only searches within Resources and Outputs sections as only they | ||
| * can reference parameters. | ||
| */ | ||
| findAllOccurrences( | ||
| documentUri: string, | ||
| targetValue: string | number | boolean | unknown[], | ||
| targetType: LiteralValueType, | ||
| ): Range[] { | ||
| const occurrences: Range[] = []; | ||
|
|
||
| const syntaxTree = this.syntaxTreeManager.getSyntaxTree(documentUri); | ||
| if (!syntaxTree) { | ||
| return occurrences; | ||
| } | ||
|
|
||
| const sections = syntaxTree.findTopLevelSections([TopLevelSection.Resources, TopLevelSection.Outputs]); | ||
|
|
||
| for (const sectionNode of sections.values()) { | ||
| this.traverseForMatches(sectionNode, targetValue, targetType, occurrences); | ||
| } | ||
|
|
||
| return occurrences; | ||
| } | ||
|
|
||
| private traverseForMatches( | ||
| node: SyntaxNode, | ||
| targetValue: string | number | boolean | unknown[], | ||
| targetType: LiteralValueType, | ||
| occurrences: Range[], | ||
| ): void { | ||
| const literalInfo = this.literalDetector.detectLiteralValue(node); | ||
|
|
||
| if (literalInfo && this.isMatchingLiteral(literalInfo, targetValue, targetType) && !literalInfo.isReference) { | ||
| occurrences.push(literalInfo.range); | ||
| return; // Don't traverse children to avoid duplicates | ||
| } | ||
|
|
||
| for (const child of node.children) { | ||
| this.traverseForMatches(child, targetValue, targetType, occurrences); | ||
| } | ||
| } | ||
|
|
||
| private isMatchingLiteral( | ||
| literalInfo: LiteralValueInfo, | ||
| targetValue: string | number | boolean | unknown[], | ||
| targetType: LiteralValueType, | ||
| ): boolean { | ||
| if (literalInfo.type !== targetType) { | ||
| return false; | ||
| } | ||
|
|
||
| switch (targetType) { | ||
| case LiteralValueType.STRING: { | ||
| return literalInfo.value === targetValue; | ||
| } | ||
|
|
||
| case LiteralValueType.NUMBER: { | ||
| return literalInfo.value === targetValue; | ||
| } | ||
|
|
||
| case LiteralValueType.BOOLEAN: { | ||
| return literalInfo.value === targetValue; | ||
| } | ||
|
|
||
| case LiteralValueType.ARRAY: { | ||
| return this.arraysEqual(literalInfo.value as unknown[], targetValue as unknown[]); | ||
| } | ||
|
|
||
| default: { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private arraysEqual(arr1: unknown[], arr2: unknown[]): boolean { | ||
| if (arr1.length !== arr2.length) { | ||
| return false; | ||
| } | ||
|
|
||
| for (const [i, element] of arr1.entries()) { | ||
| if (element !== arr2[i]) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
Oops, something went wrong.
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.