|
20 | 20 | import java.util.List;
|
21 | 21 | import java.util.Optional;
|
22 | 22 | import java.util.Set;
|
| 23 | +import java.util.stream.Collectors; |
23 | 24 | import java.util.stream.Stream;
|
24 | 25 |
|
25 | 26 | import org.eclipse.core.runtime.CoreException;
|
|
39 | 40 | import org.eclipse.jdt.core.dom.FieldDeclaration;
|
40 | 41 | import org.eclipse.jdt.core.dom.ITypeBinding;
|
41 | 42 | import org.eclipse.jdt.core.dom.ImportDeclaration;
|
42 |
| -import org.eclipse.jdt.core.dom.SimpleName; |
43 | 43 | import org.eclipse.jdt.core.dom.Statement;
|
44 | 44 | import org.eclipse.jdt.core.dom.TypeDeclaration;
|
45 |
| -import org.eclipse.jdt.core.dom.VariableDeclarationFragment; |
46 | 45 | import org.eclipse.jdt.core.manipulation.CoreASTProvider;
|
47 | 46 | import org.eclipse.jdt.core.manipulation.OrganizeImportsOperation;
|
48 | 47 | import org.eclipse.jdt.internal.corext.dom.ASTNodes;
|
@@ -109,14 +108,11 @@ public List<Either<Command, CodeAction>> getSourceActionCommands(CodeActionParam
|
109 | 108 | List<Either<Command, CodeAction>> $ = new ArrayList<>();
|
110 | 109 | ICompilationUnit cu = context.getCompilationUnit();
|
111 | 110 | IType type = getSelectionType(context);
|
112 |
| - ASTNode node = context.getCoveredNode(); |
113 |
| - if (node == null) { |
114 |
| - node = context.getCoveringNode(); |
115 |
| - } |
116 |
| - FieldDeclaration fieldDeclaration = getFieldDeclarationNode(node); |
117 |
| - boolean isInFieldDeclaration = fieldDeclaration != null; |
118 |
| - boolean isInTypeDeclaration = getTypeDeclarationNode(node) != null; |
119 |
| - boolean isInImportDeclaration = getImportDeclarationNode(node) != null; |
| 111 | + ArrayList<ASTNode> coveredNodes = QuickAssistProcessor.getFullyCoveredNodes(context, context.getCoveringNode()); |
| 112 | + ASTNode coveringNode = context.getCoveringNode(); |
| 113 | + boolean isInFieldDeclaration = CodeActionUtility.findASTNode(coveredNodes, coveringNode, FieldDeclaration.class) != null; |
| 114 | + boolean isInTypeDeclaration = CodeActionUtility.findASTNode(coveredNodes, coveringNode, TypeDeclaration.class) != null; |
| 115 | + boolean isInImportDeclaration = CodeActionUtility.findASTNode(coveredNodes, coveringNode, ImportDeclaration.class) != null; |
120 | 116 |
|
121 | 117 | // Generate Constructor QuickAssist
|
122 | 118 | if (isInFieldDeclaration || isInTypeDeclaration) {
|
@@ -165,9 +161,9 @@ public List<Either<Command, CodeAction>> getSourceActionCommands(CodeActionParam
|
165 | 161 | addSourceActionCommand($, params.getContext(), sourceOverrideMethods);
|
166 | 162 | }
|
167 | 163 |
|
168 |
| - String fieldName = getFieldName(fieldDeclaration, node); |
| 164 | + List<String> fieldNames = CodeActionUtility.getFieldNames(coveredNodes, coveringNode); |
169 | 165 | try {
|
170 |
| - addGenerateAccessorsSourceActionCommand(params, context, $, type, fieldName, isInTypeDeclaration); |
| 166 | + addGenerateAccessorsSourceActionCommand(params, context, $, type, fieldNames, isInTypeDeclaration); |
171 | 167 | } catch (JavaModelException e) {
|
172 | 168 | JavaLanguageServerPlugin.logException("Failed to generate Getter and Setter source action", e);
|
173 | 169 | }
|
@@ -233,50 +229,23 @@ public List<Either<Command, CodeAction>> getSourceActionCommands(CodeActionParam
|
233 | 229 | return $;
|
234 | 230 | }
|
235 | 231 |
|
236 |
| - public static String getFieldName(FieldDeclaration fieldDeclaration, ASTNode node) { |
237 |
| - if (fieldDeclaration == null || node == null) { |
238 |
| - return null; |
239 |
| - } |
240 |
| - if (node instanceof SimpleName) { |
241 |
| - // Focus on a field name |
242 |
| - ASTNode parent = node.getParent(); |
243 |
| - if (parent instanceof VariableDeclarationFragment) { |
244 |
| - // Ensure the field name is under a variable declaration |
245 |
| - return ((SimpleName) node).getIdentifier(); |
246 |
| - } |
247 |
| - } |
248 |
| - List<VariableDeclarationFragment> fragments = fieldDeclaration.fragments(); |
249 |
| - if (fragments != null && fragments.size() == 1) { |
250 |
| - // FieldDeclaration has only one field |
251 |
| - return fragments.get(0).getName().getIdentifier(); |
252 |
| - } |
253 |
| - return null; |
254 |
| - } |
255 |
| - |
256 |
| - public static String getFieldName(VariableDeclarationFragment fragment) { |
257 |
| - if (fragment == null) { |
258 |
| - return null; |
259 |
| - } |
260 |
| - return fragment.getName().getIdentifier(); |
261 |
| - } |
262 |
| - |
263 |
| - private void addGenerateAccessorsSourceActionCommand(CodeActionParams params, IInvocationContext context, List<Either<Command, CodeAction>> $, IType type, String fieldName, boolean isInTypeDeclaration) throws JavaModelException { |
| 232 | + private void addGenerateAccessorsSourceActionCommand(CodeActionParams params, IInvocationContext context, List<Either<Command, CodeAction>> $, IType type, List<String> fieldNames, boolean isInTypeDeclaration) throws JavaModelException { |
264 | 233 | AccessorField[] accessors = GenerateGetterSetterOperation.getUnimplementedAccessors(type, AccessorKind.BOTH);
|
265 | 234 | AccessorField[] getters = GenerateGetterSetterOperation.getUnimplementedAccessors(type, AccessorKind.GETTER);
|
266 | 235 | AccessorField[] setters = GenerateGetterSetterOperation.getUnimplementedAccessors(type, AccessorKind.SETTER);
|
267 | 236 |
|
268 |
| - if (fieldName != null) { |
269 |
| - Optional<AccessorField> accessorField = Arrays.stream(accessors).filter(accessor -> accessor.fieldName.equals(fieldName)).findFirst(); |
270 |
| - if (accessorField.isPresent() && accessorField.get().generateGetter && accessorField.get().generateSetter) { |
271 |
| - addSourceActionCommand($, params.getContext(), getGetterSetterAction(params, context, type, JavaCodeActionKind.QUICK_ASSIST, isInTypeDeclaration, new AccessorField[] { accessorField.get() }, AccessorKind.BOTH)); |
| 237 | + if (fieldNames.size() > 0) { |
| 238 | + List<AccessorField> accessorFields = Arrays.stream(accessors).filter(accessor -> fieldNames.contains(accessor.fieldName) && accessor.generateGetter && accessor.generateSetter).collect(Collectors.toList()); |
| 239 | + if (accessorFields.size() > 0) { |
| 240 | + addSourceActionCommand($, params.getContext(), getGetterSetterAction(params, context, type, JavaCodeActionKind.QUICK_ASSIST, isInTypeDeclaration, accessorFields.toArray(new AccessorField[0]), AccessorKind.BOTH)); |
272 | 241 | }
|
273 |
| - Optional<AccessorField> getterField = Arrays.stream(getters).filter(getter -> getter.fieldName.equals(fieldName)).findFirst(); |
274 |
| - if (getterField.isPresent()) { |
275 |
| - addSourceActionCommand($, params.getContext(), getGetterSetterAction(params, context, type, JavaCodeActionKind.QUICK_ASSIST, isInTypeDeclaration, new AccessorField[] { getterField.get() }, AccessorKind.GETTER)); |
| 242 | + List<AccessorField> getterFields = Arrays.stream(getters).filter(getter -> fieldNames.contains(getter.fieldName)).collect(Collectors.toList()); |
| 243 | + if (getterFields.size() > 0) { |
| 244 | + addSourceActionCommand($, params.getContext(), getGetterSetterAction(params, context, type, JavaCodeActionKind.QUICK_ASSIST, isInTypeDeclaration, getterFields.toArray(new AccessorField[0]), AccessorKind.GETTER)); |
276 | 245 | }
|
277 |
| - Optional<AccessorField> setterField = Arrays.stream(setters).filter(setter -> setter.fieldName.equals(fieldName)).findFirst(); |
278 |
| - if (setterField.isPresent()) { |
279 |
| - addSourceActionCommand($, params.getContext(), getGetterSetterAction(params, context, type, JavaCodeActionKind.QUICK_ASSIST, isInTypeDeclaration, new AccessorField[] { setterField.get() }, AccessorKind.SETTER)); |
| 246 | + List<AccessorField> setterFields = Arrays.stream(setters).filter(setter -> fieldNames.contains(setter.fieldName)).collect(Collectors.toList()); |
| 247 | + if (setterFields.size() > 0) { |
| 248 | + addSourceActionCommand($, params.getContext(), getGetterSetterAction(params, context, type, JavaCodeActionKind.QUICK_ASSIST, isInTypeDeclaration, setterFields.toArray(new AccessorField[0]), AccessorKind.SETTER)); |
280 | 249 | }
|
281 | 250 | }
|
282 | 251 |
|
@@ -381,17 +350,17 @@ private Optional<Either<Command, CodeAction>> getGetterSetterAction(CodeActionPa
|
381 | 350 | try {
|
382 | 351 | if (accessors == null || accessors.length == 0) {
|
383 | 352 | return Optional.empty();
|
384 |
| - } else if (accessors.length == 1 || !preferenceManager.getClientPreferences().isAdvancedGenerateAccessorsSupported()) { |
| 353 | + } else if (isQuickAssist || accessors.length == 1 || !preferenceManager.getClientPreferences().isAdvancedGenerateAccessorsSupported()) { |
385 | 354 | String actionMessage;
|
386 | 355 | switch (accessorKind) {
|
387 | 356 | case BOTH:
|
388 |
| - actionMessage = isQuickAssist ? Messages.format(ActionMessages.GenerateGetterSetterAction_templateLabel, accessors[0].fieldName) : ActionMessages.GenerateGetterSetterAction_label; |
| 357 | + actionMessage = isQuickAssist && accessors.length == 1 ? Messages.format(ActionMessages.GenerateGetterSetterAction_templateLabel, accessors[0].fieldName) : ActionMessages.GenerateGetterSetterAction_label; |
389 | 358 | break;
|
390 | 359 | case GETTER:
|
391 |
| - actionMessage = isQuickAssist ? Messages.format(ActionMessages.GenerateGetterAction_templateLabel, accessors[0].fieldName) : ActionMessages.GenerateGetterAction_label; |
| 360 | + actionMessage = isQuickAssist && accessors.length == 1 ? Messages.format(ActionMessages.GenerateGetterAction_templateLabel, accessors[0].fieldName) : ActionMessages.GenerateGetterAction_label; |
392 | 361 | break;
|
393 | 362 | case SETTER:
|
394 |
| - actionMessage = isQuickAssist ? Messages.format(ActionMessages.GenerateSetterAction_templateLabel, accessors[0].fieldName) : ActionMessages.GenerateSetterAction_label; |
| 363 | + actionMessage = isQuickAssist && accessors.length == 1 ? Messages.format(ActionMessages.GenerateSetterAction_templateLabel, accessors[0].fieldName) : ActionMessages.GenerateSetterAction_label; |
395 | 364 | break;
|
396 | 365 | default:
|
397 | 366 | return Optional.empty();
|
@@ -719,24 +688,4 @@ public static ASTNode getTypeDeclarationNode(ASTNode node) {
|
719 | 688 | }
|
720 | 689 | return node instanceof TypeDeclaration ? node : null;
|
721 | 690 | }
|
722 |
| - |
723 |
| - private static ASTNode getImportDeclarationNode(ASTNode node) { |
724 |
| - if (node == null) { |
725 |
| - return null; |
726 |
| - } |
727 |
| - while (node != null && !(node instanceof ImportDeclaration) && !(node instanceof Statement)) { |
728 |
| - node = node.getParent(); |
729 |
| - } |
730 |
| - return node instanceof ImportDeclaration ? node : null; |
731 |
| - } |
732 |
| - |
733 |
| - public static FieldDeclaration getFieldDeclarationNode(ASTNode node) { |
734 |
| - if (node == null) { |
735 |
| - return null; |
736 |
| - } |
737 |
| - while (node != null && !(node instanceof FieldDeclaration) && !(node instanceof Statement)) { |
738 |
| - node = node.getParent(); |
739 |
| - } |
740 |
| - return node instanceof FieldDeclaration ? (FieldDeclaration) node : null; |
741 |
| - } |
742 | 691 | }
|
0 commit comments