1414import com .intellij .codeInsight .MetaAnnotationUtil ;
1515import com .intellij .openapi .command .WriteCommandAction ;
1616import com .intellij .openapi .command .undo .UndoUtil ;
17+ import com .intellij .openapi .editor .Editor ;
18+ import com .intellij .openapi .editor .ScrollType ;
19+ import com .intellij .openapi .fileEditor .FileEditor ;
20+ import com .intellij .openapi .fileEditor .FileEditorManager ;
21+ import com .intellij .openapi .fileEditor .TextEditor ;
1722import com .intellij .openapi .module .LanguageLevelUtil ;
1823import com .intellij .openapi .module .Module ;
1924import com .intellij .openapi .module .ModuleUtilCore ;
2025import com .intellij .openapi .project .Project ;
2126import com .intellij .openapi .util .Pair ;
27+ import com .intellij .openapi .util .TextRange ;
28+ import com .intellij .openapi .util .text .Strings ;
2229import com .intellij .pom .java .LanguageLevel ;
2330import com .intellij .psi .JavaPsiFacade ;
2431import com .intellij .psi .PsiAnnotation ;
@@ -62,8 +69,9 @@ private MapstructAnnotationUtils() {
6269 * @param mappingAnnotation the {@link org.mapstruct.Mapping} annotation
6370 */
6471 public static void addMappingAnnotation (@ NotNull Project project ,
65- @ NotNull PsiMethod mappingMethod ,
66- @ NotNull PsiAnnotation mappingAnnotation ) {
72+ @ NotNull PsiMethod mappingMethod ,
73+ @ NotNull PsiAnnotation mappingAnnotation ,
74+ boolean moveCaretToEmptySourceAttribute ) {
6775 Pair <PsiAnnotation , Optional <PsiAnnotation >> mappingsPair = findOrCreateMappingsAnnotation (
6876 project ,
6977 mappingMethod
@@ -81,7 +89,13 @@ public static void addMappingAnnotation(@NotNull Project project,
8189 if ( containerAnnotation != null && containerAnnotation .isPhysical () ) {
8290 runWriteCommandAction (
8391 project ,
84- () -> containerAnnotation .replace ( newAnnotation ),
92+ () -> {
93+ PsiElement replaced = containerAnnotation .replace ( newAnnotation );
94+
95+ if ( moveCaretToEmptySourceAttribute ) {
96+ moveCaretToEmptySourceAttribute ( project , replaced );
97+ }
98+ },
8599 containingFile
86100 );
87101 }
@@ -108,13 +122,36 @@ public static void addMappingAnnotation(@NotNull Project project,
108122 mappingMethod .getModifierList ()
109123 );
110124 JavaCodeStyleManager .getInstance ( project ).shortenClassReferences ( inserted );
125+
126+ if ( moveCaretToEmptySourceAttribute ) {
127+ moveCaretToEmptySourceAttribute ( project , inserted );
128+ }
129+
111130 }, containingFile );
112131
113132 UndoUtil .markPsiFileForUndo ( containingFile );
114133 }
115134 }
116135 }
117136
137+ private static void moveCaretToEmptySourceAttribute (@ NotNull Project project , PsiElement element ) {
138+
139+ FileEditor selectedEditor = FileEditorManager .getInstance ( project ).getSelectedEditor ();
140+ if ( selectedEditor instanceof TextEditor ) {
141+ Editor editor = ( (TextEditor ) selectedEditor ).getEditor ();
142+
143+ TextRange textRange = element .getTextRange ();
144+ String textOfElement = String .valueOf ( editor .getDocument ()
145+ .getCharsSequence ()
146+ .subSequence ( textRange .getStartOffset (), textRange .getEndOffset () ) );
147+ int indexOfEmptySourceAttribute = Strings .indexOf ( textOfElement , "\" \" " ) + 1 ;
148+
149+ editor .getCaretModel ().moveToOffset ( textRange .getStartOffset () + indexOfEmptySourceAttribute );
150+ editor .getScrollingModel ().scrollToCaret ( ScrollType .MAKE_VISIBLE );
151+ }
152+
153+ }
154+
118155 /**
119156 * This methods looks for the {@link org.mapstruct.Mappings} annotation on the given {@code mappingMethod},
120157 * if the annotation was not found and the {@link org.mapstruct.Mapping} repeatable annotation cannot be used
0 commit comments