fix: FieldsTo* resolves caret class instead of first class in file#727
Merged
Conversation
Right-clicking inside a data class in the editor (e.g. SalesReportVO declared after AnalyticsController in the same file) and selecting ToJson/ToJson5/ToProperties formatted the wrong class — the controller, not the data class under the caret. Root cause: FieldFormatAction.findPsiClass relied on CommonDataKeys. PSI_ELEMENT, which is populated by the Project View, not by the editor context-menu. With PSI_ELEMENT null, resolution fell through to PsiTreeUtil.findChildOfType(file, PsiClass), which always returns the first top-level class in the file regardless of caret position. Fix: - SelectedHelper.resolveSelection: add a caret-based resolution step (between the PSI_ELEMENT check and the PSI_FILE fallback) that resolves the PSI element at the caret offset and walks up to the nearest PsiMethod/PsiClass. This benefits every caller of SelectedHelper (export actions, field-format actions). - FieldFormatAction: drop the bespoke findPsiClass and use SelectedHelper.resolveSelection(e)?.psiClass(), matching the convention used by ChannelExportAction/ExportApiAction. Tests: add 5 cases to SelectedHelperTest covering caret-based resolution in a multi-class file (non-first class, first class, method body, EOF fallback, PSI_ELEMENT precedence).
Contributor
|
📦 Plugin has been packaged for this PR. You can download |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #727 +/- ##
=============================================
+ Coverage 63.989% 63.997% +0.007%
=============================================
Files 363 363
Lines 20880 20887 +7
Branches 5130 5133 +3
=============================================
+ Hits 13361 13367 +6
Misses 5645 5645
- Partials 1874 1875 +1
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 1 file with indirect coverage changes Continue to review full report in Codecov by Harness.
🚀 New features to boost your workflow:
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
Right-clicking inside a data class in the editor (e.g.
SalesReportVOdeclared afterAnalyticsControllerin the same file) and selecting ToJson / ToJson5 / ToProperties formatted the wrong class — the controller, not the data class under the caret.Root cause
FieldFormatAction.findPsiClassrelied onCommonDataKeys.PSI_ELEMENT, which is populated by the Project View, not by the editor context-menu. WithPSI_ELEMENTnull, resolution fell through to:which always returns the first top-level class in the file regardless of caret position. Any file with multiple top-level classes (controller + sibling VO/DTO classes, common in sample projects) hit this bug.
Fix
SelectedHelper.resolveSelection— added a caret-based resolution step (between thePSI_ELEMENTcheck and thePSI_FILEfallback) that resolves the PSI element at the caret offset and walks up to the nearestPsiMethod/PsiClass. Benefits every caller ofSelectedHelper(export actions, field-format actions).FieldFormatAction— dropped the bespokefindPsiClassand now usesSelectedHelper.resolveSelection(e)?.psiClass(), matching the convention used byChannelExportAction/ExportApiAction.Behavior
Right-clicking inside
SalesReportVO(or any VO/DTO class) and choosing ToJson now correctly formats that data class, notAnalyticsController.Tests
Added 5 cases to
SelectedHelperTestcovering caret-based resolution in a multi-class file:testResolveSelectionFromCaretInNonFirstClassSalesReportVO→ resolvesSalesReportVO, notAnalyticsController(the regression test for this bug)testResolveSelectionFromCaretInFirstClasstestResolveSelectionFromCaretInMethodResolvesContainingClasstestResolveSelectionFallsBackToFileWhenCaretResolvesNothingPSI_FILEbranch (preserves existing behaviour)testPsiElementTakesPrecedenceOverEditorCaretPSI_ELEMENTwins over caret (guards documented precedence)All 21
SelectedHelperTestcases pass.