sdedic commented on a change in pull request #3736: URL: https://github.com/apache/netbeans/pull/3736#discussion_r829741824
########## File path: groovy/groovy.editor/src/org/netbeans/modules/groovy/editor/completion/SpockMethodParamCompletion.java ########## @@ -0,0 +1,149 @@ +/* + * 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.groovy.editor.completion; + +import java.util.ListIterator; +import java.util.Map; +import org.codehaus.groovy.ast.ASTNode; +import org.codehaus.groovy.ast.ClassHelper; +import org.codehaus.groovy.ast.ClassNode; +import org.codehaus.groovy.ast.MethodNode; +import org.codehaus.groovy.ast.Variable; +import org.codehaus.groovy.ast.Parameter; +import org.codehaus.groovy.ast.expr.Expression; +import org.netbeans.modules.csl.api.CompletionProposal; +import org.netbeans.modules.groovy.editor.api.completion.CaretLocation; +import org.netbeans.modules.groovy.editor.api.completion.CompletionItem; +import org.netbeans.modules.groovy.editor.api.completion.util.CompletionContext; + +/** + * + * @author Petr Pisl + */ +public class SpockMethodParamCompletion extends BaseCompletion { + + private static class SpockParameter implements Variable { + + private final String name; + + public SpockParameter(String name) { + this.name = name; + } + + @Override + public ClassNode getType() { + return ClassHelper.DYNAMIC_TYPE; + } + + @Override + public ClassNode getOriginType() { + return ClassHelper.GROOVY_OBJECT_TYPE; + } + + @Override + public String getName() { + return name; + } + + @Override + public Expression getInitialExpression() { + return null; + } + + @Override + public boolean hasInitialExpression() { + return false; + } + + @Override + public boolean isInStaticContext() { + return false; + } + + @Override + public boolean isDynamicTyped() { + return true; + } + + @Override + public boolean isClosureSharedVariable() { + return false; + } + + @Override + public void setClosureSharedVariable(boolean bln) { + + } + + @Override + public int getModifiers() { + return 0; + } + + } + + @Override + public boolean complete(Map<Object, CompletionProposal> proposals, CompletionContext request, int anchor) { + boolean updated = false; + if ((request.location == CaretLocation.INSIDE_METHOD + || request.location == CaretLocation.INSIDE_CLOSURE) + && SpockUtils.isInSpecificationClass(request)) { + + ListIterator<ASTNode> leafToRoot = request.path.leafToRoot(); + + MethodNode methodNode = null; + while(leafToRoot.hasNext()) { + ASTNode next = leafToRoot.next(); + if (next instanceof MethodNode) { + methodNode = (MethodNode) next; + break; + } + } + String name = null; + if (methodNode != null) { + name = methodNode.getName(); + } + + if (name != null && name.contains("#")) { //NOI18N + String[] parts = name.split("#"); //NOI18N + + for (int i = 1; i < parts.length; i++) { + String part = parts[i].trim(); + if (!part.isEmpty()) { Review comment: Since the `name` is already matched by a regex in split() ... wouldn't it be faster to iterate using `Pattern.compile("#\\p{Alpha}[\\p{Alnum}$_]*)[. ]?*).matcher(name)` ? ########## File path: groovy/groovy.editor/src/org/netbeans/modules/groovy/editor/completion/SpockMethodParamCompletion.java ########## @@ -0,0 +1,149 @@ +/* + * 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.groovy.editor.completion; + +import java.util.ListIterator; +import java.util.Map; +import org.codehaus.groovy.ast.ASTNode; +import org.codehaus.groovy.ast.ClassHelper; +import org.codehaus.groovy.ast.ClassNode; +import org.codehaus.groovy.ast.MethodNode; +import org.codehaus.groovy.ast.Variable; +import org.codehaus.groovy.ast.Parameter; +import org.codehaus.groovy.ast.expr.Expression; +import org.netbeans.modules.csl.api.CompletionProposal; +import org.netbeans.modules.groovy.editor.api.completion.CaretLocation; +import org.netbeans.modules.groovy.editor.api.completion.CompletionItem; +import org.netbeans.modules.groovy.editor.api.completion.util.CompletionContext; + +/** + * + * @author Petr Pisl + */ +public class SpockMethodParamCompletion extends BaseCompletion { + + private static class SpockParameter implements Variable { + + private final String name; + + public SpockParameter(String name) { + this.name = name; + } + + @Override + public ClassNode getType() { + return ClassHelper.DYNAMIC_TYPE; + } + + @Override + public ClassNode getOriginType() { + return ClassHelper.GROOVY_OBJECT_TYPE; + } + + @Override + public String getName() { + return name; + } + + @Override + public Expression getInitialExpression() { + return null; + } + + @Override + public boolean hasInitialExpression() { + return false; + } + + @Override + public boolean isInStaticContext() { + return false; + } + + @Override + public boolean isDynamicTyped() { + return true; + } + + @Override + public boolean isClosureSharedVariable() { + return false; + } + + @Override + public void setClosureSharedVariable(boolean bln) { + + } + + @Override + public int getModifiers() { + return 0; + } + + } + + @Override + public boolean complete(Map<Object, CompletionProposal> proposals, CompletionContext request, int anchor) { + boolean updated = false; + if ((request.location == CaretLocation.INSIDE_METHOD + || request.location == CaretLocation.INSIDE_CLOSURE) + && SpockUtils.isInSpecificationClass(request)) { + + ListIterator<ASTNode> leafToRoot = request.path.leafToRoot(); + + MethodNode methodNode = null; + while(leafToRoot.hasNext()) { + ASTNode next = leafToRoot.next(); + if (next instanceof MethodNode) { + methodNode = (MethodNode) next; + break; + } + } + String name = null; + if (methodNode != null) { + name = methodNode.getName(); + } + + if (name != null && name.contains("#")) { //NOI18N Review comment: Q: it seems that syntax ``` def "#name should have length #length"() { ``` is supported, but the syntax ``` @Unroll("#name should have length #length") def "name length"() { ``` is not ? I can't see a check for `@Unroll` annotation either ########## File path: groovy/groovy.editor/src/org/netbeans/modules/groovy/editor/completion/SpockUtils.java ########## @@ -0,0 +1,73 @@ +/* + * 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.groovy.editor.completion; + + +import java.util.HashSet; +import java.util.Set; +import org.codehaus.groovy.ast.ClassNode; +import org.netbeans.api.lexer.Token; +import org.netbeans.api.lexer.TokenSequence; +import org.netbeans.modules.groovy.editor.api.completion.util.CompletionContext; +import org.netbeans.modules.groovy.editor.api.lexer.GroovyTokenId; +import org.netbeans.modules.groovy.editor.api.lexer.LexUtilities; + +/** + * + * @author Petr Pisl + */ +public class SpockUtils { + + public static boolean isInSpecificationClass(CompletionContext context) { + + Set<String> visited = new HashSet<>(); + String name; + ClassNode classNode = context.declaringClass; + while (classNode != null && !visited.contains(name = classNode.getName())) { + if ("spock.lang.Specification".equals(name)) { + return true; + } + visited.add(name); + classNode = classNode.getSuperClass(); + } + return false; + } + + static boolean isFirstStatement(final CompletionContext request) { + TokenSequence<GroovyTokenId> ts = LexUtilities.getGroovyTokenSequence(request.doc, 1); + + if (ts != null) { Review comment: Same method in `KeywordCompletion`, consider reusing this utility there. ########## File path: groovy/groovy.editor/src/org/netbeans/modules/groovy/editor/completion/SpockUtils.java ########## @@ -0,0 +1,73 @@ +/* + * 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.groovy.editor.completion; + + +import java.util.HashSet; +import java.util.Set; +import org.codehaus.groovy.ast.ClassNode; +import org.netbeans.api.lexer.Token; +import org.netbeans.api.lexer.TokenSequence; +import org.netbeans.modules.groovy.editor.api.completion.util.CompletionContext; +import org.netbeans.modules.groovy.editor.api.lexer.GroovyTokenId; +import org.netbeans.modules.groovy.editor.api.lexer.LexUtilities; + +/** + * + * @author Petr Pisl + */ +public class SpockUtils { + + public static boolean isInSpecificationClass(CompletionContext context) { + + Set<String> visited = new HashSet<>(); + String name; + ClassNode classNode = context.declaringClass; Review comment: Idea: If there was a `GroovyParserResut` available from the `CompletionContext`, one could do ``` context.declaringClass.isDerivedFrom( parserResult.resolveClassName("spock.lang.Specification") ) ``` -- 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. To unsubscribe, e-mail: [email protected] 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
