-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #215 from xonixx/func_reference_prevent_unused_fun…
…c_inspection Func reference prevent unused func inspection
- Loading branch information
Showing
26 changed files
with
230 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package intellij_awk.psi; | ||
|
||
import static com.intellij.openapi.util.text.StringUtil.unquoteString; | ||
|
||
import com.intellij.lang.ASTNode; | ||
import com.intellij.openapi.util.TextRange; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiReference; | ||
import intellij_awk.AwkReferenceFunction; | ||
import intellij_awk.AwkUtil; | ||
import java.util.regex.Pattern; | ||
import javax.swing.*; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
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_]*\""); | ||
|
||
@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.createString(getProject(), newName)); | ||
} | ||
|
||
@Override | ||
public PsiReference getReference() { | ||
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); | ||
} | ||
|
||
@Override | ||
public @Nullable Icon getIcon(int flags) { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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\"")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
BEGIN { | ||
PROCINFO["sorted_in"] = "compare" | ||
} | ||
|
||
function <caret>compare() {} |
5 changes: 5 additions & 0 deletions
5
src/test/testData/inspection/funcRefToPreventUnusedFunc1_1.awk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
BEGIN { | ||
PROCINFO["sorted_in"] = "compare1" | ||
} | ||
|
||
function <caret>compare() {} |
4 changes: 4 additions & 0 deletions
4
src/test/testData/inspection/funcRefToPreventUnusedFunc1_1After.awk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
BEGIN { | ||
PROCINFO["sorted_in"] = "compare1" | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
BEGIN { | ||
F = "fname" | ||
@F() | ||
} | ||
|
||
function <caret>fname() {} |
6 changes: 6 additions & 0 deletions
6
src/test/testData/inspection/funcRefToPreventUnusedFunc2_1.awk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
BEGIN { | ||
F = "aaa fname bbb" | ||
@F() | ||
} | ||
|
||
function <caret>fname() {} |
5 changes: 5 additions & 0 deletions
5
src/test/testData/inspection/funcRefToPreventUnusedFunc2_1After.awk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
BEGIN { | ||
F = "aaa fname bbb" | ||
@F() | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
BEGIN { | ||
print a = "fname" | ||
} | ||
|
||
function <caret>fname() {} |
5 changes: 5 additions & 0 deletions
5
src/test/testData/inspection/funcRefToPreventUnusedFunc3_1.awk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
BEGIN { | ||
print "fname" | ||
} | ||
|
||
function <caret>fname() {} |
4 changes: 4 additions & 0 deletions
4
src/test/testData/inspection/funcRefToPreventUnusedFunc3_1After.awk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
BEGIN { | ||
print "fname" | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
BEGIN { | ||
b("fname") | ||
} | ||
|
||
function b(f) { @f() } | ||
|
||
function <caret>fname() {} |
5 changes: 5 additions & 0 deletions
5
src/test/testData/inspection/funcRefToPreventUnusedFunc4_1.awk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
BEGIN { | ||
length("fname") | ||
} | ||
|
||
function <caret>fname() {} |
5 changes: 5 additions & 0 deletions
5
src/test/testData/inspection/funcRefToPreventUnusedFunc4_1After.awk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
BEGIN { | ||
length("fname") | ||
} | ||
|
||
<caret> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
BEGIN { | ||
a = "fname" | ||
b("fname") | ||
substr("fname") | ||
print "fname" | ||
} | ||
function b() {} | ||
function fname<caret>() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
BEGIN { | ||
a = "newName" | ||
b("newName") | ||
substr("fname") | ||
print "fname" | ||
} | ||
function b() {} | ||
function newName<caret>() {} |