diff --git a/src/main/java/intellij_awk/psi/AwkStringMixin.java b/src/main/java/intellij_awk/psi/AwkStringMixin.java index beed79d..0b0d4f2 100644 --- a/src/main/java/intellij_awk/psi/AwkStringMixin.java +++ b/src/main/java/intellij_awk/psi/AwkStringMixin.java @@ -9,20 +9,37 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.regex.Pattern; + +import static com.intellij.openapi.util.text.StringUtil.unquoteString; + public abstract class AwkStringMixin extends AwkNamedElementImpl { public AwkStringMixin(@NotNull ASTNode node) { super(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() { + String str = getText(); + return canBeFunctionName(str) ? unquoteString(str) : null; + } + public PsiElement setName(String newName) { return replaceNameNode(AwkElementFactory.createFunctionCallName(getProject(), newName)); } @Override public PsiReference getReference() { -// return new AwkReferenceFunction(this, getNameTextRange()); - return new AwkReferenceFunction(this, TextRange.from(1, getTextLength()-2)); -// return null; + return getName() != null + ? new AwkReferenceFunction(this, TextRange.from(1, getTextLength() - 2)) + : null; } @Override diff --git a/src/test/java/intellij_awk/psi/AwkStringMixinTests.java b/src/test/java/intellij_awk/psi/AwkStringMixinTests.java new file mode 100644 index 0000000..6e230be --- /dev/null +++ b/src/test/java/intellij_awk/psi/AwkStringMixinTests.java @@ -0,0 +1,23 @@ +package intellij_awk.psi; + +import junit.framework.TestCase; +import org.junit.Assert; + +public class AwkStringMixinTests extends TestCase { + public void testTrue() { + Assert.assertTrue(AwkStringMixin.canBeFunctionName("\"aaa\"")); + Assert.assertTrue(AwkStringMixin.canBeFunctionName("\"_aaa01\"")); + Assert.assertTrue(AwkStringMixin.canBeFunctionName("\"Xxx123\"")); + Assert.assertTrue(AwkStringMixin.canBeFunctionName("\"_\"")); + Assert.assertTrue(AwkStringMixin.canBeFunctionName("\"X\"")); + } + public void testFalse() { + Assert.assertFalse(AwkStringMixin.canBeFunctionName(" \"aaa\" ")); + Assert.assertFalse(AwkStringMixin.canBeFunctionName("\"111\"")); + Assert.assertFalse(AwkStringMixin.canBeFunctionName("\"1aaa\"")); + Assert.assertFalse(AwkStringMixin.canBeFunctionName("\"_aa a01\"")); + Assert.assertFalse(AwkStringMixin.canBeFunctionName("\"Xxx'123\"")); + Assert.assertFalse(AwkStringMixin.canBeFunctionName("\"-\"")); + Assert.assertFalse(AwkStringMixin.canBeFunctionName("\"aaaa\\\"bbb\"")); + } +}