diff --git a/src/main/java/intellij_awk/psi/AwkStringMixin.java b/src/main/java/intellij_awk/psi/AwkStringMixin.java index 0e643c0..180b671 100644 --- a/src/main/java/intellij_awk/psi/AwkStringMixin.java +++ b/src/main/java/intellij_awk/psi/AwkStringMixin.java @@ -21,27 +21,43 @@ public AwkStringMixin(@NotNull ASTNode node) { private static final Pattern stringThatCanBeFunctionName = Pattern.compile("\"[A-Za-z_][A-Za-z0-9_]*\""); - static boolean canBeFunctionName(String s) { - return stringThatCanBeFunctionName.matcher(s).matches(); - } - @Override public String getName() { + return getPossibleFunctionName(); + } + + @Nullable + private String getPossibleFunctionName() { String str = getText(); return canBeFunctionName(str) ? unquoteString(str) : null; } + static boolean canBeFunctionName(String s) { + return stringThatCanBeFunctionName.matcher(s).matches(); + } + public PsiElement setName(String newName) { return replaceNameNode(AwkElementFactory.createFunctionCallName(getProject(), newName)); } @Override public PsiReference getReference() { - return getName() != null && isRhsOfAssignment() + return getPossibleFunctionName() != null && canBeFunctionReference() ? new AwkReferenceFunction(this, TextRange.from(1, getTextLength() - 2)) : null; } + private boolean canBeFunctionReference() { + return isRhsOfAssignment() || isUserFunctionArgument(); + } + + /** `f("fname")` but not `substr("fname")` (built-in) */ + private boolean isUserFunctionArgument() { + AwkGawkFuncCallList callList = AwkUtil.findParent(this, AwkGawkFuncCallList.class); + return callList != null && callList.getParent() instanceof AwkFunctionCallUser; + } + + /** `a = "fname"` */ private boolean isRhsOfAssignment() { return AwkUtil.isType(AwkUtil.getPrevNotWhitespace(getParent()), AwkTypes.ASSIGN); }