generated from JetBrains/intellij-platform-plugin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d8f788
commit cac519e
Showing
21 changed files
with
289 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
github: olivernybroe |
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
10 changes: 0 additions & 10 deletions
10
src/main/kotlin/com/github/olivernybroe/collectionsintellij/services/MyApplicationService.kt
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
src/main/kotlin/com/github/olivernybroe/collectionsintellij/services/MyProjectService.kt
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...vernybroe/collectionsintellij/MyBundle.kt → ...n/kotlin/dev/nybroe/collector/MyBundle.kt
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
25 changes: 25 additions & 0 deletions
25
src/main/kotlin/dev/nybroe/collector/inspections/ForeachToCollectionInspection.kt
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,25 @@ | ||
package dev.nybroe.collector.inspections | ||
|
||
import com.intellij.codeInspection.ProblemHighlightType | ||
import com.intellij.codeInspection.ProblemsHolder | ||
import com.intellij.psi.PsiElementVisitor | ||
import com.jetbrains.php.lang.inspections.PhpInspection | ||
import com.jetbrains.php.lang.psi.elements.ForeachStatement | ||
import com.jetbrains.php.lang.psi.visitors.PhpElementVisitor | ||
import dev.nybroe.collector.MyBundle | ||
import dev.nybroe.collector.quickFixes.ForeachToCollectionQuickFix | ||
|
||
class ForeachToCollectionInspection : PhpInspection() { | ||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { | ||
return object : PhpElementVisitor() { | ||
override fun visitPhpForeach(foreach: ForeachStatement) { | ||
holder.registerProblem( | ||
foreach, | ||
MyBundle.message("foreachToCollectionDescription"), | ||
ProblemHighlightType.INFORMATION, | ||
ForeachToCollectionQuickFix() | ||
) | ||
} | ||
} | ||
} | ||
} |
5 changes: 2 additions & 3 deletions
5
...lij/listeners/MyProjectManagerListener.kt → ...tor/listeners/MyProjectManagerListener.kt
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
69 changes: 69 additions & 0 deletions
69
src/main/kotlin/dev/nybroe/collector/quickFixes/ForeachToCollectionQuickFix.kt
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 dev.nybroe.collector.quickFixes | ||
|
||
import com.intellij.codeInspection.LocalQuickFix | ||
import com.intellij.codeInspection.ProblemDescriptor | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.util.PsiTreeUtil | ||
import com.jetbrains.php.lang.inspections.codeStyle.PhpLoopCanBeConvertedToArrayMapInspection | ||
import com.jetbrains.php.lang.psi.PhpPsiElementFactory | ||
import com.jetbrains.php.lang.psi.elements.ForeachStatement | ||
import com.jetbrains.php.lang.psi.elements.ParameterList | ||
import com.jetbrains.php.lang.psi.elements.PhpNamedElement | ||
import com.jetbrains.php.lang.psi.elements.Variable | ||
|
||
class ForeachToCollectionQuickFix : LocalQuickFix { | ||
companion object { | ||
const val QUICK_FIX_NAME = "Refactor foreach to collection" | ||
} | ||
|
||
override fun getFamilyName(): String { | ||
return QUICK_FIX_NAME | ||
} | ||
|
||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) { | ||
val foreach = descriptor.psiElement | ||
|
||
if (foreach !is ForeachStatement) { | ||
return | ||
} | ||
|
||
val statement = PhpLoopCanBeConvertedToArrayMapInspection.unwrapStatement(foreach.statement)!! | ||
|
||
val arguments = createArguments(project, foreach) | ||
|
||
val useList = createUseListStatement( | ||
foreach, | ||
arrayOf( | ||
foreach.key?.name, | ||
foreach.value?.name, | ||
if (foreach.array is PhpNamedElement) (foreach.array as PhpNamedElement).name else null | ||
).asSequence().filterNotNull().toList() | ||
) ?: "" | ||
|
||
val psiCollectionCall = PhpPsiElementFactory.createStatement( | ||
project, | ||
"collect(${foreach.array!!.text})->each(function(${arguments.text}) $useList {${statement.text}});" | ||
) | ||
|
||
descriptor.psiElement.replace( | ||
psiCollectionCall | ||
) | ||
} | ||
|
||
private fun createArguments(project: Project, foreach: ForeachStatement): ParameterList { | ||
return PhpPsiElementFactory.createArgumentList( | ||
project, | ||
foreach.value!!.text | ||
+ if (foreach.key != null) ", ${foreach.key!!.text}" else "" | ||
) | ||
} | ||
|
||
private fun createUseListStatement(foreach: ForeachStatement, ignoredNames: List<String>): String? { | ||
return PsiTreeUtil.collectElementsOfType(foreach, Variable::class.java) | ||
.map { it.name } | ||
.filter { !ignoredNames.contains(it) } | ||
.ifEmpty { return null } | ||
.joinToString(separator = ", ") { "$$it" } | ||
.let { "use ($it)" } | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/kotlin/dev/nybroe/collector/services/MyProjectService.kt
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,10 @@ | ||
package dev.nybroe.collector.services | ||
|
||
import com.intellij.openapi.project.Project | ||
import dev.nybroe.collector.MyBundle | ||
|
||
class MyProjectService(project: Project) { | ||
|
||
init { | ||
} | ||
} |
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 |
---|---|---|
@@ -1,19 +1,29 @@ | ||
<idea-plugin> | ||
<id>com.github.olivernybroe.collectionsintellij</id> | ||
<name>collections-intellij</name> | ||
<vendor>olivernybroe</vendor> | ||
<id>dev.nybroe.collector</id> | ||
<name>Collector</name> | ||
<vendor email="[email protected]" url="https://nybroe.dev">Oliver Nybroe</vendor> | ||
|
||
<!-- Product and plugin compatibility requirements --> | ||
<!-- https://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html --> | ||
<depends>com.intellij.modules.platform</depends> | ||
<depends>com.jetbrains.php</depends> | ||
|
||
<extensions defaultExtensionNs="com.intellij"> | ||
<applicationService serviceImplementation="com.github.olivernybroe.collectionsintellij.services.MyApplicationService"/> | ||
<projectService serviceImplementation="com.github.olivernybroe.collectionsintellij.services.MyProjectService"/> | ||
<projectService serviceImplementation="dev.nybroe.collector.services.MyProjectService"/> | ||
<localInspection | ||
language="PHP" | ||
groupPath="PHP" | ||
groupKey="collectionGroupName" | ||
shortName="ForeachToCollectionInspection" | ||
enabledByDefault="true" | ||
bundle="messages.MyBundle" | ||
key="foreachToCollectionDisplayName" | ||
implementationClass="dev.nybroe.collector.inspections.ForeachToCollectionInspection" | ||
/> | ||
</extensions> | ||
|
||
<applicationListeners> | ||
<listener class="com.github.olivernybroe.collectionsintellij.listeners.MyProjectManagerListener" | ||
<listener class="dev.nybroe.collector.listeners.MyProjectManagerListener" | ||
topic="com.intellij.openapi.project.ProjectManagerListener"/> | ||
</applicationListeners> | ||
</idea-plugin> |
6 changes: 6 additions & 0 deletions
6
src/main/resources/inspectionDescriptions/ForeachToCollectionInspection.html
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 @@ | ||
|
||
<html lang="en"> | ||
<body> | ||
'foreach' used instead of 'Collection' | ||
</body> | ||
</html> |
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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
name=My Plugin | ||
applicationService=Application service | ||
name=Collector | ||
projectService=Project service: {0} | ||
|
||
collectionGroupName=Collections | ||
|
||
foreachToCollectionDisplayName='foreach' used instead of 'Collection' | ||
foreachToCollectionDescription='foreach' used instead of 'Collection' |
46 changes: 46 additions & 0 deletions
46
src/test/kotlin/dev/nybroe/collector/inspections/ForeachStatementInspectionTest.kt
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,46 @@ | ||
package dev.nybroe.collector.inspections | ||
|
||
import com.intellij.testFramework.fixtures.BasePlatformTestCase | ||
import dev.nybroe.collector.quickFixes.ForeachToCollectionQuickFix | ||
import junit.framework.TestCase | ||
|
||
|
||
internal class ForeachStatementInspectionTest : BasePlatformTestCase() { | ||
override fun getTestDataPath(): String { | ||
return "src/test/resources/inspections/ForeachStatementInspection" | ||
} | ||
|
||
/** | ||
* Given the name of a test file, runs comparing references inspection quick fix and tests | ||
* the results against a reference outcome file. File name pattern 'foo.java' and 'foo.after.java' | ||
* are matching before and after files in the testData directory. | ||
* | ||
* @param testName The name of the test file before comparing references inspection. | ||
*/ | ||
private fun doTest(testName: String) { | ||
// Initialize the test based on the testData file | ||
myFixture.configureByFile("$testName.php") | ||
// Initialize the inspection and get a list of highlighted | ||
myFixture.enableInspections(ForeachToCollectionInspection()) | ||
val highlightInfos = myFixture.doHighlighting() | ||
TestCase.assertFalse(highlightInfos.isEmpty()) | ||
// Get the quick fix action for comparing references inspection and apply it to the file | ||
val action = myFixture.findSingleIntention(ForeachToCollectionQuickFix.QUICK_FIX_NAME) | ||
TestCase.assertNotNull(action) | ||
myFixture.launchAction(action) | ||
// Verify the results | ||
myFixture.checkResultByFile("$testName.after.php") | ||
} | ||
|
||
fun testForeachValueOnly() { | ||
doTest("foreach-value-only") | ||
} | ||
|
||
fun testForeachKeyValue() { | ||
doTest("foreach-key-value") | ||
} | ||
|
||
fun testForeachOuterScopeVariable() { | ||
doTest("foreach-outer-scope-variable") | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/test/resources/inspections/ForeachStatementInspection/foreach-key-value.after.php
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,11 @@ | ||
<?php | ||
|
||
$array = [ | ||
'one' => 'monkey', | ||
'two' => 'cow', | ||
'tree' => 'snake', | ||
]; | ||
|
||
collect($array)->each(function ($animal, $num) { | ||
echo "{$num} = {$animal}"; | ||
}); |
11 changes: 11 additions & 0 deletions
11
src/test/resources/inspections/ForeachStatementInspection/foreach-key-value.php
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,11 @@ | ||
<?php | ||
|
||
$array = [ | ||
'one' => 'monkey', | ||
'two' => 'cow', | ||
'tree' => 'snake', | ||
]; | ||
|
||
<caret>foreach ($array as $num => $animal) { | ||
echo "{$num} = {$animal}"; | ||
} |
15 changes: 15 additions & 0 deletions
15
...t/resources/inspections/ForeachStatementInspection/foreach-outer-scope-variable.after.php
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,15 @@ | ||
<?php | ||
|
||
$array = [ | ||
'one', | ||
'two', | ||
'tree' | ||
]; | ||
|
||
$name = 'one'; | ||
|
||
collect($array)->each(function ($item) use ($name) { | ||
if ($item === $name) { | ||
echo "found it!"; | ||
} | ||
}); |
Oops, something went wrong.