junichi11 commented on a change in pull request #2704: URL: https://github.com/apache/netbeans/pull/2704#discussion_r562607211
########## File path: php/php.editor/src/org/netbeans/modules/php/editor/verification/IncorrectNamedArgumentsHintError.java ########## @@ -0,0 +1,195 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.modules.php.editor.verification; + +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.netbeans.modules.csl.api.Hint; +import org.netbeans.modules.csl.api.HintFix; +import org.netbeans.modules.csl.api.OffsetRange; +import org.netbeans.modules.csl.spi.support.CancelSupport; +import org.netbeans.modules.php.editor.model.FileScope; +import org.netbeans.modules.php.editor.parser.PHPParseResult; +import org.netbeans.modules.php.editor.parser.astnodes.ASTNode; +import org.netbeans.modules.php.editor.parser.astnodes.AttributeDeclaration; +import org.netbeans.modules.php.editor.parser.astnodes.Expression; +import org.netbeans.modules.php.editor.parser.astnodes.FunctionInvocation; +import org.netbeans.modules.php.editor.parser.astnodes.NamedArgument; +import org.netbeans.modules.php.editor.parser.astnodes.Variadic; +import org.netbeans.modules.php.editor.parser.astnodes.visitors.DefaultVisitor; +import org.openide.filesystems.FileObject; +import org.openide.util.NbBundle; + +/** + * Handle incorrect Named Arguments. + */ +public class IncorrectNamedArgumentsHintError extends HintErrorRule { + + private FileObject fileObject; + + @Override + @NbBundle.Messages("IncorrectNamedArgumentsHintError.displayName=Incorrect Named Arguments") + public String getDisplayName() { + return Bundle.IncorrectConstructorPropertyPromotionHintError_displayName(); + } + + @Override + @NbBundle.Messages({ + "# {0} - name", + "IncorrectNamedArguments.desc.duplicate.name=Duplicate name: \"{0}\" already exists.", + "IncorrectNamedArguments.desc.combine.named.argument.and.argument.unpacking=Can't combine named arguments(name: arg) and argument unpacking(...)", + "IncorrectNamedArguments.desc.positional.arguments.after.named.argument=Can't use positional argument after named argument.", + }) + public void invoke(PHPRuleContext context, List<Hint> hints) { + PHPParseResult phpParseResult = (PHPParseResult) context.parserResult; + if (phpParseResult.getProgram() == null) { + return; + } + FileScope fileScope = context.fileScope; + fileObject = phpParseResult.getSnapshot().getSource().getFileObject(); + if (fileScope != null && fileObject != null) { + if (CancelSupport.getDefault().isCancelled()) { + return; + } + CheckVisitor checkVisitor = new CheckVisitor(); + phpParseResult.getProgram().accept(checkVisitor); + for (NamedArgument argument : checkVisitor.getDuplicateNames()) { + if (CancelSupport.getDefault().isCancelled()) { + return; + } + addHint(argument, Bundle.IncorrectNamedArguments_desc_duplicate_name(argument.getParameterName().getName()), hints); + } + for (Map.Entry<NamedArgument, Variadic> entry : checkVisitor.getCombinedNamedArgumentsWithArgumentUnpacking().entrySet()) { + if (CancelSupport.getDefault().isCancelled()) { + return; + } + addHint(entry.getKey(), Bundle.IncorrectNamedArguments_desc_combine_named_argument_and_argument_unpacking(), hints); + addHint(entry.getValue(), Bundle.IncorrectNamedArguments_desc_combine_named_argument_and_argument_unpacking(), hints); + } + for (Expression argument : checkVisitor.getArgumentsAfterNamedArgument()) { + if (CancelSupport.getDefault().isCancelled()) { + return; + } + addHint(argument, Bundle.IncorrectNamedArguments_desc_positional_arguments_after_named_argument(), hints); + } + } + } + + private void addHint(ASTNode node, String description, List<Hint> hints) { + addHint(node, description, hints, Collections.emptyList()); + } + + private void addHint(ASTNode node, String description, List<Hint> hints, List<HintFix> fixes) { + hints.add(new Hint( + this, + description, + fileObject, + new OffsetRange(node.getStartOffset(), node.getEndOffset()), + fixes, + 500 + )); + } + + //~ Inner classes + private static final class CheckVisitor extends DefaultVisitor { Review comment: Will fix. Thanks. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists
