Skip to content

Commit

Permalink
Add inspection for checking if dataset exist
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver committed May 26, 2022
1 parent cbe4222 commit 88e9f06
Show file tree
Hide file tree
Showing 62 changed files with 448 additions and 285 deletions.
16 changes: 11 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
# PEST IntelliJ Changelog

## [Unreleased]
### Added
- Added inspection for checking if dataset exists

### Fixed
- Fixed dataset autocompletion triggering on all strings
- Fixed dataset goto triggering on all strings

## [1.6.0]
### Added
- Added converting multiple `expect` to `and` calls instead
- Added dataset completion
- Added dataset goto
### Added
- Added converting multiple `expect` to `and` calls instead
- Added dataset completion
- Added dataset goto

### Fixed
### Fixed
- Fixed automatic case changing on multicased string

## [1.5.0]
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

pluginGroup = com.pestphp
pluginName = PEST PHP
pluginVersion = 1.6.0
pluginVersion = 1.6.1

# See https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
# for insight into build numbers and IntelliJ Platform versions.
Expand Down
3 changes: 1 addition & 2 deletions src/main/kotlin/com/pestphp/pest/PestIconProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ class PestIconProvider : IconProvider() {
return runReadAction { findIconFromPsiFile(element) }
}

private fun findIconFromPsiFile(file: PsiFile): Icon?
{
private fun findIconFromPsiFile(file: PsiFile): Icon? {
if (file.isPestTestFile()) {
return PestIcons.FILE
}
Expand Down
1 change: 0 additions & 1 deletion src/main/kotlin/com/pestphp/pest/PestIcons.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.intellij.icons.AllIcons
import com.intellij.ide.ui.LafManager
import com.intellij.ide.ui.laf.darcula.DarculaLookAndFeelInfo
import com.intellij.openapi.util.IconLoader
import com.jetbrains.php.PhpIcons

object PestIcons {
val LOGO = IconLoader.getIcon("/logo@${getThemeString()}.svg", PestIcons.javaClass)
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/pestphp/pest/PestNamingUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.jetbrains.php.lang.psi.elements.StringLiteralExpression
import com.jetbrains.php.lang.psi.elements.impl.FunctionReferenceImpl
import com.jetbrains.php.run.remote.PhpRemoteInterpreterManager
import com.jetbrains.php.util.pathmapper.PhpPathMapper
import java.util.Locale
import java.util.*

fun FunctionReferenceImpl.getPestTestName(): String? {
val testName = (getParameter(0) as? StringLiteralExpression)?.contents
Expand Down
7 changes: 1 addition & 6 deletions src/main/kotlin/com/pestphp/pest/PestSettings.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package com.pestphp.pest

import com.intellij.openapi.components.PersistentStateComponent
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.ServiceManager
import com.intellij.openapi.components.State
import com.intellij.openapi.components.Storage
import com.intellij.openapi.components.service
import com.intellij.openapi.components.*
import com.intellij.openapi.project.Project
import com.intellij.util.xmlb.XmlSerializerUtil
import com.pestphp.pest.parser.PestConfigurationFile
Expand Down
9 changes: 1 addition & 8 deletions src/main/kotlin/com/pestphp/pest/PestTestFileUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,8 @@ import com.intellij.psi.util.CachedValue
import com.intellij.psi.util.CachedValueProvider
import com.intellij.psi.util.CachedValuesManager
import com.intellij.psi.util.PsiTreeUtil
import com.jetbrains.php.lang.psi.elements.AssignmentExpression
import com.jetbrains.php.lang.psi.elements.ClassConstantReference
import com.jetbrains.php.lang.psi.elements.ClassReference
import com.jetbrains.php.lang.psi.elements.FieldReference
import com.jetbrains.php.lang.psi.elements.*
import com.jetbrains.php.lang.psi.elements.Function
import com.jetbrains.php.lang.psi.elements.FunctionReference
import com.jetbrains.php.lang.psi.elements.ParameterList
import com.jetbrains.php.lang.psi.elements.Statement
import com.jetbrains.php.lang.psi.elements.Variable
import com.jetbrains.php.lang.psi.elements.impl.FunctionReferenceImpl
import com.jetbrains.php.lang.psi.resolve.types.PhpType

Expand Down
1 change: 0 additions & 1 deletion src/main/kotlin/com/pestphp/pest/PestUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.jetbrains.php.lang.psi.PhpFile
import com.jetbrains.php.lang.psi.elements.PhpNamespace
import com.jetbrains.php.lang.psi.elements.PhpPsiElement
import com.jetbrains.php.lang.psi.elements.Statement
import com.jetbrains.php.lang.psi.elements.impl.FunctionReferenceImpl
import com.jetbrains.php.phpunit.PhpUnitUtil
import com.jetbrains.php.testFramework.PhpTestFrameworkSettingsManager

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ThisFieldsCompletionProvider : CompletionProvider<CompletionParameters>(),
if (!variable.isThisVariableInPest { it.isAnyPestFunction() }) return

return (fieldReference.containingFile).getAllBeforeThisAssignments()
.filter { it.variable?.name !== null }
.filter { it.variable?.name !== null }
.forEach {
result.addElement(
LookupElementBuilder.create(it.variable!!.name!!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class PestRunConfigurationHandler : PhpTestRunConfigurationHandler {
var rootPath: String? = null

companion object {
@JvmField val instance = PestRunConfigurationHandler()
@JvmField
val instance = PestRunConfigurationHandler()
}

override fun getConfigFileOption(): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ import com.intellij.util.Function
import com.jetbrains.php.lang.PhpFileType
import com.jetbrains.php.testFramework.run.PhpDefaultTestRunnerSettingsValidator
import com.jetbrains.php.testFramework.run.PhpTestConfigurationProducer
import com.pestphp.pest.getPestTestName
import com.pestphp.pest.isPestConfigurationFile
import com.pestphp.pest.isPestEnabled
import com.pestphp.pest.isPestTestFile
import com.pestphp.pest.isPestTestReference
import com.pestphp.pest.*

class PestRunConfigurationProducer : PhpTestConfigurationProducer<PestRunConfiguration>(
VALIDATOR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.intellij.execution.configurations.RunConfigurationBase
import com.intellij.execution.configurations.coverage.CoverageEnabledConfiguration
import com.jetbrains.php.phpunit.coverage.PhpUnitCoverageEngine
import com.pestphp.pest.configuration.PestRunConfiguration
import java.util.Date
import java.util.*

class PestCoverageEngine : PhpUnitCoverageEngine() {
override fun isApplicableTo(conf: RunConfigurationBase<*>): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ package com.pestphp.pest.customExpectations


import com.intellij.openapi.vfs.VirtualFile
import com.intellij.util.indexing.DataIndexer
import com.intellij.util.indexing.DefaultFileTypeSpecificInputFilter
import com.intellij.util.indexing.FileBasedIndex
import com.intellij.util.indexing.FileBasedIndexExtension
import com.intellij.util.indexing.FileContent
import com.intellij.util.indexing.ID
import com.intellij.util.indexing.*
import com.intellij.util.io.DataExternalizer
import com.intellij.util.io.EnumeratorStringDescriptor
import com.intellij.util.io.KeyDescriptor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.intellij.psi.PsiFile
import com.jetbrains.php.lang.psi.PhpFile
import com.pestphp.pest.customExpectations.generators.Method

class CustomExpectationListener(private val project: Project): CustomExpectationNotifier {
class CustomExpectationListener(private val project: Project) : CustomExpectationNotifier {
override fun changedExpectation(file: PsiFile, customExpectations: List<Method>) {
val expectationFileService = project.service<ExpectationFileService>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package com.pestphp.pest.customExpectations
import com.intellij.psi.PsiFile
import com.intellij.util.messages.Topic
import com.pestphp.pest.customExpectations.generators.Method
import java.util.EventListener
import java.util.*

interface CustomExpectationNotifier: EventListener {
interface CustomExpectationNotifier : EventListener {
companion object {
@Topic.ProjectLevel
val TOPIC = Topic.create("Custom expectation", CustomExpectationNotifier::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.jetbrains.php.lang.psi.PhpFile
/**
* Adds all methods to the expectation file service which has been indexed already.
*/
class CustomExpectationStartupActivity: StartupActivity {
class CustomExpectationStartupActivity : StartupActivity {
override fun runActivity(project: Project) {
val fileBasedIndex = FileBasedIndex.getInstance()
val expectationFileService = project.service<ExpectationFileService>()
Expand Down Expand Up @@ -45,7 +45,7 @@ class CustomExpectationStartupActivity: StartupActivity {
it.value
)
}
expectationFileService.generateFile { }
expectationFileService.generateFile { }
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.intellij.psi.PsiManager
import com.intellij.util.SlowOperations
import com.jetbrains.php.composer.lib.ComposerLibraryManager
import com.jetbrains.php.lang.psi.PhpFile
import com.jetbrains.rd.util.first
import com.pestphp.pest.customExpectations.generators.ExpectationGenerator
import com.pestphp.pest.customExpectations.generators.Method
import com.pestphp.pest.realPath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class MethodDataExternalizer : DataExternalizer<Method> {
returnType = PhpType.builder()
.add(
Regex("returnType='(.*?)'")
.find(it)!!
.groupValues[1]
.find(it)!!
.groupValues[1]
).build(),
defaultValue = Regex("defaultValue='(.*)'")
.find(it)!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,8 @@ import com.intellij.psi.PsiFile
import com.intellij.psi.util.PsiTreeUtil
import com.jetbrains.php.PhpIndex
import com.jetbrains.php.lang.psi.PhpFile
import com.jetbrains.php.lang.psi.elements.*
import com.jetbrains.php.lang.psi.elements.Function
import com.jetbrains.php.lang.psi.elements.MethodReference
import com.jetbrains.php.lang.psi.elements.ParameterList
import com.jetbrains.php.lang.psi.elements.PhpExpression
import com.jetbrains.php.lang.psi.elements.PhpNamespace
import com.jetbrains.php.lang.psi.elements.Statement
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression
import com.jetbrains.php.lang.psi.elements.Variable
import com.jetbrains.php.lang.psi.elements.impl.FunctionReferenceImpl
import com.jetbrains.php.lang.psi.elements.impl.MethodReferenceImpl
import com.jetbrains.php.lang.psi.resolve.types.PhpType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MethodDataExternalizer : DataExternalizer<Method> {

override fun save(out: DataOutput, value: Method) {
EnumeratorStringDescriptor.INSTANCE.save(out, value.name)
PhpTypeDataExternalizer.INSTANCE.save(out,value.returnType)
PhpTypeDataExternalizer.INSTANCE.save(out, value.returnType)
ListDataExternalizer(ParameterDataExternalizer.INSTANCE).save(
out,
value.parameters
Expand Down
142 changes: 142 additions & 0 deletions src/main/kotlin/com/pestphp/pest/datasets/DataSetCompletionProvider.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package com.pestphp.pest.datasets

import com.intellij.codeInsight.completion.CompletionParameters
import com.intellij.codeInsight.completion.CompletionProvider
import com.intellij.codeInsight.completion.CompletionResultSet
import com.intellij.codeInsight.lookup.LookupElementBuilder
import com.intellij.codeInsight.navigation.actions.GotoDeclarationHandler
import com.intellij.openapi.editor.Editor
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiManager
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.util.elementType
import com.intellij.util.ProcessingContext
import com.intellij.util.indexing.FileBasedIndex
import com.jetbrains.php.lang.lexer.PhpTokenTypes
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression
import com.jetbrains.php.lang.psi.elements.impl.FunctionReferenceImpl
import com.pestphp.pest.getRootPhpPsiElements
import com.pestphp.pest.isPestTestReference

class DataSetCompletionProvider : CompletionProvider<CompletionParameters>(), GotoDeclarationHandler {
override fun addCompletions(
parameters: CompletionParameters,
context: ProcessingContext,
result: CompletionResultSet
) {
val isPestTest = parameters.position
// String literal (dataset name)
.parent
// Parameter list (with call parameters)
.parent
// Method reference (pest test call)
.parent
// Check if method reference is a pest test
.isPestTestReference()
if (!isPestTest) {
return
}

val fileBasedIndex = FileBasedIndex.getInstance()

// Get all shared datasets
val sharedDatasets = fileBasedIndex.getAllKeys(DatasetIndex.key, parameters.originalFile.project)
.map {
fileBasedIndex.getValues(
DatasetIndex.key,
it,
GlobalSearchScope.projectScope(parameters.originalFile.project)
)
}
.flatten()
.flatten()

// Get all datasets in the same file
val localDatasets = parameters.originalFile
.getRootPhpPsiElements()
.filter { it.isPestDataset() }
.filterIsInstance<FunctionReferenceImpl>()
.mapNotNull { it.getPestDatasetName() }

listOf(
*sharedDatasets.toTypedArray(),
*localDatasets.toTypedArray(),
).forEach {
result.addElement(
LookupElementBuilder.create(it)
)
}
}

override fun getGotoDeclarationTargets(
sourceElement: PsiElement?,
offset: Int,
editor: Editor
): Array<PsiElement> {
if (sourceElement?.elementType !in listOf(
PhpTokenTypes.STRING_LITERAL_SINGLE_QUOTE,
PhpTokenTypes.STRING_LITERAL
)
) {
return PsiElement.EMPTY_ARRAY
}

val parent = sourceElement?.parent
if (parent !is StringLiteralExpression) {
return PsiElement.EMPTY_ARRAY
}

val isPestTest = parent
// Parameter list (with call parameters)
.parent
// Method reference (pest test call)
.parent
// Check if method reference is a pest test
.isPestTestReference()
if (!isPestTest) {
return PsiElement.EMPTY_ARRAY
}

val fileBasedIndex = FileBasedIndex.getInstance()
val datasetName = parent.contents

val foundDatasets = mutableListOf<PsiElement>()

fileBasedIndex.getAllKeys(
DatasetIndex.key,
parent.project
).forEach { key ->
fileBasedIndex.processValues(
DatasetIndex.key,
key,
null,
{ file, datasets ->
if (datasetName !in datasets) {
return@processValues true
}

// Add all shared datasets which matches
PsiManager.getInstance(parent.project).findFile(file)!!
.getRootPhpPsiElements()
.filter { it.isPestDataset() }
.filterIsInstance<FunctionReferenceImpl>()
.filter { it.getPestDatasetName() == datasetName }
.forEach { foundDatasets.add(it) }

true
},
GlobalSearchScope.projectScope(parent.project)
)
}

// Add all local datasets which matches
parent.containingFile
.getRootPhpPsiElements()
.filter { it.isPestDataset() }
.filterIsInstance<FunctionReferenceImpl>()
.filter { it.getPestDatasetName() == datasetName }
.forEach { foundDatasets.add(it) }

return foundDatasets.toTypedArray()
}
}
Loading

0 comments on commit 88e9f06

Please sign in to comment.