|
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | | - |
17 | 16 | package consulo.csharp.impl.ide.refactoring.move; |
18 | 17 |
|
19 | 18 | import consulo.annotation.access.RequiredReadAction; |
|
36 | 35 | import consulo.language.psi.PsiFileSystemItem; |
37 | 36 | import consulo.language.psi.scope.GlobalSearchScope; |
38 | 37 | import consulo.language.util.IncorrectOperationException; |
| 38 | +import consulo.localize.LocalizeValue; |
39 | 39 | import consulo.logging.Logger; |
40 | 40 | import consulo.project.Project; |
41 | 41 | import consulo.ui.annotation.RequiredUIAccess; |
|
45 | 45 | import consulo.util.lang.Couple; |
46 | 46 |
|
47 | 47 | import jakarta.annotation.Nonnull; |
| 48 | + |
48 | 49 | import java.util.*; |
49 | 50 | import java.util.function.Function; |
50 | 51 |
|
51 | 52 | /** |
52 | 53 | * @author VISTALL |
53 | 54 | * @since 2019-07-20 |
54 | 55 | */ |
55 | | -public class CSharpMoveClassesUtil |
56 | | -{ |
57 | | - private static final Logger LOG = Logger.getInstance(CSharpMoveClassesUtil.class); |
58 | | - |
59 | | - @RequiredUIAccess |
60 | | - public static void doMove( |
61 | | - final Project project, |
62 | | - final PsiElement[] elements, |
63 | | - final PsiElement[] targetElement, |
64 | | - final MoveCallback moveCallback |
65 | | - ) |
66 | | - { |
67 | | - doMove( |
68 | | - project, |
69 | | - elements, |
70 | | - targetElement, |
71 | | - moveCallback, |
72 | | - oldElements -> { |
73 | | - PsiElement[] newElements = new PsiElement[oldElements.length]; |
74 | | - |
75 | | - for (int i = 0; i < oldElements.length; i++) |
76 | | - { |
77 | | - PsiElement oldElement = oldElements[i]; |
78 | | - if (oldElement instanceof CSharpTypeDeclaration) |
79 | | - { |
80 | | - newElements[i] = oldElement.getContainingFile(); |
81 | | - } |
82 | | - else |
83 | | - { |
84 | | - newElements[i] = oldElement; |
85 | | - } |
86 | | - } |
87 | | - |
88 | | - return newElements; |
89 | | - } |
90 | | - ); |
91 | | - } |
92 | | - |
93 | | - @RequiredUIAccess |
94 | | - public static void doMove( |
95 | | - final Project project, |
96 | | - final PsiElement[] elements, |
97 | | - final PsiElement[] targetElement, |
98 | | - final MoveCallback moveCallback, |
99 | | - final Function<PsiElement[], PsiElement[]> adjustElements |
100 | | - ) |
101 | | - { |
102 | | - final PsiDirectory targetDirectory = MoveFilesOrDirectoriesUtil.resolveToDirectory(project, targetElement[0]); |
103 | | - if (targetElement[0] != null && targetDirectory == null) |
104 | | - { |
105 | | - return; |
106 | | - } |
107 | | - |
108 | | - final PsiElement[] newElements = adjustElements != null ? adjustElements.apply(elements) : elements; |
109 | | - |
110 | | - final PsiDirectory initialTargetDirectory = MoveFilesOrDirectoriesUtil.getInitialTargetDirectory(targetDirectory, elements); |
111 | | - |
112 | | - final MoveFilesOrDirectoriesDialog.Callback doRun = moveDialog -> CommandProcessor.getInstance().executeCommand( |
113 | | - project, |
114 | | - () -> { |
115 | | - final PsiDirectory targetDirectory1 = moveDialog != null ? moveDialog.getTargetDirectory() : initialTargetDirectory; |
116 | | - if (targetDirectory1 == null) |
117 | | - { |
118 | | - LOG.error("ItemPresentationImpl is null! The target directory, it is null!"); |
119 | | - return; |
120 | | - } |
121 | | - |
122 | | - Collection<PsiElement> toCheck = ContainerUtil.newArrayList((PsiElement) targetDirectory1); |
123 | | - for (PsiElement e : newElements) |
124 | | - { |
125 | | - toCheck.add(e instanceof PsiFileSystemItem && e.getParent() != null ? e.getParent() : e); |
126 | | - } |
127 | | - if (!CommonRefactoringUtil.checkReadOnlyStatus(project, toCheck, false)) |
128 | | - { |
129 | | - return; |
130 | | - } |
131 | | - |
132 | | - targetElement[0] = targetDirectory1; |
133 | | - |
134 | | - try |
135 | | - { |
136 | | - final int[] choice = elements.length > 1 || elements[0] instanceof PsiDirectory ? new int[]{-1} : null; |
137 | | - final List<PsiElement> els = new ArrayList<>(); |
138 | | - for (final PsiElement psiElement : newElements) |
139 | | - { |
140 | | - if (psiElement instanceof PsiFile) |
141 | | - { |
142 | | - final PsiFile file = (PsiFile) psiElement; |
143 | | - |
144 | | - if (CommonRefactoringUtil.checkFileExist(targetDirectory1, choice, file, file.getName(), "Move")) |
145 | | - { |
146 | | - continue; |
147 | | - } |
148 | | - } |
149 | | - MoveFilesOrDirectoriesUtil.checkMove(psiElement, targetDirectory1); |
150 | | - els.add(psiElement); |
151 | | - } |
152 | | - final Runnable callback = () -> { |
153 | | - if (moveDialog != null) |
154 | | - { |
155 | | - moveDialog.close(DialogWrapper.CANCEL_EXIT_CODE); |
156 | | - } |
157 | | - }; |
158 | | - if (els.isEmpty()) |
159 | | - { |
160 | | - callback.run(); |
161 | | - return; |
162 | | - } |
163 | | - new CSharpClassesMoveProcessor(project, |
164 | | - els.toArray(new PsiElement[els.size()]), |
165 | | - targetDirectory1, |
166 | | - RefactoringSettings.getInstance().MOVE_SEARCH_FOR_REFERENCES_FOR_FILE, |
167 | | - false, |
168 | | - false, |
169 | | - moveCallback, |
170 | | - callback |
171 | | - ).run(); |
172 | | - } |
173 | | - catch (IncorrectOperationException e) |
174 | | - { |
175 | | - CommonRefactoringUtil.showErrorMessage(RefactoringLocalize.errorTitle().get(), e.getMessage(), "refactoring.moveFile", project); |
176 | | - } |
177 | | - }, |
178 | | - MoveHandler.REFACTORING_NAME.get(), |
179 | | - null |
180 | | - ); |
181 | | - |
182 | | - final MoveFilesOrDirectoriesDialog moveDialog = new MoveFilesOrDirectoriesDialog(project, doRun); |
183 | | - moveDialog.setData(newElements, initialTargetDirectory, "refactoring.moveFile"); |
184 | | - moveDialog.show(); |
185 | | - } |
186 | | - |
187 | | - |
188 | | - /** |
189 | | - * Return couple of elements. |
190 | | - * |
191 | | - * If first element is type - second will be same element |
192 | | - * |
193 | | - * if first element is namespace declaration - second will namespace as element (global namespace object not c# file declaration) |
194 | | - */ |
195 | | - @Nonnull |
196 | | - public static Set<Couple<DotNetNamedElement>> findTypesAndNamespaces(@Nonnull PsiElement element) |
197 | | - { |
198 | | - Set<Couple<DotNetNamedElement>> result = new LinkedHashSet<>(); |
199 | | - element.accept(new CSharpRecursiveElementVisitor() |
200 | | - { |
201 | | - @Override |
202 | | - public void visitTypeDeclaration(CSharpTypeDeclaration declaration) |
203 | | - { |
204 | | - super.visitTypeDeclaration(declaration); |
205 | | - |
206 | | - result.add(Couple.of(declaration, declaration)); |
207 | | - } |
208 | | - |
209 | | - @Override |
210 | | - @RequiredReadAction |
211 | | - public void visitNamespaceDeclaration(CSharpNamespaceDeclaration declaration) |
212 | | - { |
213 | | - super.visitNamespaceDeclaration(declaration); |
214 | | - |
215 | | - DotNetPsiSearcher searcher = DotNetPsiSearcher.getInstance(declaration.getProject()); |
216 | | - DotNetNamespaceAsElement namespace = |
217 | | - searcher.findNamespace(declaration.getPresentableQName(), GlobalSearchScope.projectScope(declaration.getProject())); |
218 | | - |
219 | | - result.add(Couple.<DotNetNamedElement>of(declaration, namespace)); |
220 | | - } |
221 | | - }); |
222 | | - return result; |
223 | | - } |
| 56 | +public class CSharpMoveClassesUtil { |
| 57 | + private static final Logger LOG = Logger.getInstance(CSharpMoveClassesUtil.class); |
| 58 | + |
| 59 | + @RequiredUIAccess |
| 60 | + public static void doMove(Project project, PsiElement[] elements, PsiElement[] targetElement, MoveCallback moveCallback) { |
| 61 | + doMove( |
| 62 | + project, |
| 63 | + elements, |
| 64 | + targetElement, |
| 65 | + moveCallback, |
| 66 | + oldElements -> { |
| 67 | + PsiElement[] newElements = new PsiElement[oldElements.length]; |
| 68 | + |
| 69 | + for (int i = 0; i < oldElements.length; i++) { |
| 70 | + PsiElement oldElement = oldElements[i]; |
| 71 | + if (oldElement instanceof CSharpTypeDeclaration) { |
| 72 | + newElements[i] = oldElement.getContainingFile(); |
| 73 | + } |
| 74 | + else { |
| 75 | + newElements[i] = oldElement; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return newElements; |
| 80 | + } |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + @RequiredUIAccess |
| 85 | + public static void doMove( |
| 86 | + Project project, |
| 87 | + PsiElement[] elements, |
| 88 | + PsiElement[] targetElement, |
| 89 | + MoveCallback moveCallback, |
| 90 | + Function<PsiElement[], PsiElement[]> adjustElements |
| 91 | + ) { |
| 92 | + PsiDirectory targetDirectory = MoveFilesOrDirectoriesUtil.resolveToDirectory(project, targetElement[0]); |
| 93 | + if (targetElement[0] != null && targetDirectory == null) { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + PsiElement[] newElements = adjustElements != null ? adjustElements.apply(elements) : elements; |
| 98 | + |
| 99 | + PsiDirectory initialTargetDirectory = MoveFilesOrDirectoriesUtil.getInitialTargetDirectory(targetDirectory, elements); |
| 100 | + |
| 101 | + @RequiredUIAccess |
| 102 | + MoveFilesOrDirectoriesDialog.Callback doRun = moveDialog -> { |
| 103 | + CommandProcessor.getInstance().newCommand() |
| 104 | + .project(project) |
| 105 | + .name(MoveHandler.REFACTORING_NAME) |
| 106 | + .run(() -> { |
| 107 | + PsiDirectory targetDirectory1 = moveDialog != null ? moveDialog.getTargetDirectory() : initialTargetDirectory; |
| 108 | + if (targetDirectory1 == null) { |
| 109 | + LOG.error("ItemPresentationImpl is null! The target directory, it is null!"); |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + Collection<PsiElement> toCheck = ContainerUtil.newArrayList((PsiElement) targetDirectory1); |
| 114 | + for (PsiElement e : newElements) { |
| 115 | + toCheck.add(e instanceof PsiFileSystemItem && e.getParent() != null ? e.getParent() : e); |
| 116 | + } |
| 117 | + if (!CommonRefactoringUtil.checkReadOnlyStatus(project, toCheck, false)) { |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + targetElement[0] = targetDirectory1; |
| 122 | + |
| 123 | + try { |
| 124 | + int[] choice = elements.length > 1 || elements[0] instanceof PsiDirectory ? new int[]{-1} : null; |
| 125 | + List<PsiElement> els = new ArrayList<>(); |
| 126 | + for (PsiElement psiElement : newElements) { |
| 127 | + if (psiElement instanceof PsiFile file) { |
| 128 | + if (CommonRefactoringUtil.checkFileExist( |
| 129 | + targetDirectory1, |
| 130 | + choice, |
| 131 | + file, |
| 132 | + file.getName(), |
| 133 | + RefactoringLocalize.commandNameMove() |
| 134 | + )) { |
| 135 | + continue; |
| 136 | + } |
| 137 | + } |
| 138 | + MoveFilesOrDirectoriesUtil.checkMove(psiElement, targetDirectory1); |
| 139 | + els.add(psiElement); |
| 140 | + } |
| 141 | + Runnable callback = () -> { |
| 142 | + if (moveDialog != null) { |
| 143 | + moveDialog.close(DialogWrapper.CANCEL_EXIT_CODE); |
| 144 | + } |
| 145 | + }; |
| 146 | + if (els.isEmpty()) { |
| 147 | + callback.run(); |
| 148 | + return; |
| 149 | + } |
| 150 | + new CSharpClassesMoveProcessor( |
| 151 | + project, |
| 152 | + els.toArray(new PsiElement[els.size()]), |
| 153 | + targetDirectory1, |
| 154 | + RefactoringSettings.getInstance().MOVE_SEARCH_FOR_REFERENCES_FOR_FILE, |
| 155 | + false, |
| 156 | + false, |
| 157 | + moveCallback, |
| 158 | + callback |
| 159 | + ).run(); |
| 160 | + } |
| 161 | + catch (IncorrectOperationException e) { |
| 162 | + CommonRefactoringUtil.showErrorMessage( |
| 163 | + RefactoringLocalize.errorTitle(), |
| 164 | + LocalizeValue.ofNullable(e.getMessage()), |
| 165 | + "refactoring.moveFile", |
| 166 | + project |
| 167 | + ); |
| 168 | + } |
| 169 | + }); |
| 170 | + }; |
| 171 | + |
| 172 | + MoveFilesOrDirectoriesDialog moveDialog = new MoveFilesOrDirectoriesDialog(project, doRun); |
| 173 | + moveDialog.setData(newElements, initialTargetDirectory, "refactoring.moveFile"); |
| 174 | + moveDialog.show(); |
| 175 | + } |
| 176 | + |
| 177 | + |
| 178 | + /** |
| 179 | + * Return couple of elements. |
| 180 | + * <p> |
| 181 | + * If first element is type - second will be same element |
| 182 | + * <p> |
| 183 | + * if first element is namespace declaration - second will namespace as element (global namespace object not c# file declaration) |
| 184 | + */ |
| 185 | + @Nonnull |
| 186 | + public static Set<Couple<DotNetNamedElement>> findTypesAndNamespaces(@Nonnull PsiElement element) { |
| 187 | + Set<Couple<DotNetNamedElement>> result = new LinkedHashSet<>(); |
| 188 | + element.accept(new CSharpRecursiveElementVisitor() { |
| 189 | + @Override |
| 190 | + public void visitTypeDeclaration(CSharpTypeDeclaration declaration) { |
| 191 | + super.visitTypeDeclaration(declaration); |
| 192 | + |
| 193 | + result.add(Couple.of(declaration, declaration)); |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + @RequiredReadAction |
| 198 | + public void visitNamespaceDeclaration(CSharpNamespaceDeclaration declaration) { |
| 199 | + super.visitNamespaceDeclaration(declaration); |
| 200 | + |
| 201 | + DotNetPsiSearcher searcher = DotNetPsiSearcher.getInstance(declaration.getProject()); |
| 202 | + DotNetNamespaceAsElement namespace = |
| 203 | + searcher.findNamespace(declaration.getPresentableQName(), GlobalSearchScope.projectScope(declaration.getProject())); |
| 204 | + |
| 205 | + result.add(Couple.<DotNetNamedElement>of(declaration, namespace)); |
| 206 | + } |
| 207 | + }); |
| 208 | + return result; |
| 209 | + } |
224 | 210 | } |
0 commit comments