-
Notifications
You must be signed in to change notification settings - Fork 15
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
Oliver
committed
May 26, 2022
1 parent
fdd8734
commit 8d012e1
Showing
21 changed files
with
463 additions
and
44 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
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/com/pestphp/pest/PhpTestFolderInputFilter.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,19 @@ | ||
package com.pestphp.pest | ||
|
||
import com.intellij.openapi.project.ProjectManager | ||
import com.intellij.openapi.roots.TestSourcesFilter | ||
import com.intellij.openapi.vfs.VirtualFile | ||
import com.intellij.util.indexing.DefaultFileTypeSpecificInputFilter | ||
import com.jetbrains.php.lang.PhpFileType | ||
|
||
open class PhpTestFolderInputFilter : DefaultFileTypeSpecificInputFilter(PhpFileType.INSTANCE) { | ||
override fun acceptInput(file: VirtualFile): Boolean { | ||
if (file.path.contains(""".*?test.*?/.*\..*""".toRegex())) { | ||
return true | ||
} | ||
|
||
return ProjectManager.getInstance().openProjects.any { | ||
TestSourcesFilter.isTestSources(file, it) | ||
} | ||
} | ||
} |
127 changes: 127 additions & 0 deletions
127
src/main/kotlin/com/pestphp/pest/datasets/DatasetCompletionContributor.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,127 @@ | ||
package com.pestphp.pest.datasets | ||
|
||
import com.intellij.codeInsight.completion.* | ||
import com.intellij.codeInsight.lookup.LookupElementBuilder | ||
import com.intellij.codeInsight.navigation.actions.GotoDeclarationHandler | ||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.patterns.PlatformPatterns | ||
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.FieldReference | ||
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression | ||
import com.jetbrains.php.lang.psi.elements.impl.FunctionReferenceImpl | ||
import com.pestphp.pest.getAllBeforeThisAssignments | ||
import com.pestphp.pest.getRootPhpPsiElements | ||
import com.pestphp.pest.isAnyPestFunction | ||
import com.pestphp.pest.isThisVariableInPest | ||
|
||
|
||
class DatasetCompletionContributor: CompletionContributor() { | ||
init { | ||
extend( | ||
CompletionType.BASIC, | ||
PlatformPatterns.psiElement().withParent( | ||
StringLiteralExpression::class.java | ||
), | ||
DataSetCompletionProvider() | ||
) | ||
} | ||
|
||
class DataSetCompletionProvider: CompletionProvider<CompletionParameters>(), GotoDeclarationHandler { | ||
override fun addCompletions( | ||
parameters: CompletionParameters, | ||
context: ProcessingContext, | ||
result: CompletionResultSet | ||
) { | ||
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 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() | ||
} | ||
} | ||
} |
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,71 @@ | ||
package com.pestphp.pest.datasets | ||
|
||
import com.intellij.openapi.vfs.VirtualFile | ||
import com.intellij.util.indexing.* | ||
import com.intellij.util.io.DataExternalizer | ||
import com.intellij.util.io.EnumeratorStringDescriptor | ||
import com.intellij.util.io.KeyDescriptor | ||
import com.jetbrains.php.lang.psi.PhpFile | ||
import com.jetbrains.php.lang.psi.elements.impl.FunctionReferenceImpl | ||
import com.pestphp.pest.PhpTestFolderInputFilter | ||
import com.pestphp.pest.customExpectations.externalizers.ListDataExternalizer | ||
import com.pestphp.pest.getRootPhpPsiElements | ||
import com.pestphp.pest.realPath | ||
|
||
class DatasetIndex : FileBasedIndexExtension<String, List<String>>() { | ||
companion object { | ||
val key = ID.create<String, List<String>>("php.pest.datasets") | ||
} | ||
|
||
override fun getName(): ID<String, List<String>> { | ||
return key | ||
} | ||
|
||
override fun getVersion(): Int { | ||
return 1 | ||
} | ||
|
||
override fun getIndexer(): DataIndexer<String, List<String>, FileContent> { | ||
return DataIndexer { inputData -> | ||
val file = inputData.psiFile | ||
|
||
if (file !is PhpFile) { | ||
return@DataIndexer mapOf() | ||
} | ||
|
||
val datasets = file | ||
.getRootPhpPsiElements() | ||
.filter { it.isPestDataset() } | ||
.filterIsInstance<FunctionReferenceImpl>() | ||
.mapNotNull { it.getPestDatasetName() } | ||
|
||
if(datasets.isEmpty()) { | ||
return@DataIndexer mapOf() | ||
} | ||
|
||
mapOf( | ||
file.realPath to datasets | ||
) | ||
} | ||
} | ||
|
||
override fun getKeyDescriptor(): KeyDescriptor<String> { | ||
return EnumeratorStringDescriptor.INSTANCE | ||
} | ||
|
||
override fun getValueExternalizer(): DataExternalizer<List<String>> { | ||
return ListDataExternalizer(EnumeratorStringDescriptor.INSTANCE) | ||
} | ||
|
||
override fun getInputFilter(): FileBasedIndex.InputFilter { | ||
return object : PhpTestFolderInputFilter() { | ||
override fun acceptInput(file: VirtualFile): Boolean { | ||
return super.acceptInput(file) && file.parent.path.endsWith("/Datasets") | ||
} | ||
} | ||
} | ||
|
||
override fun dependsOnFileContent(): Boolean { | ||
return true | ||
} | ||
} |
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,28 @@ | ||
package com.pestphp.pest.datasets | ||
|
||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiFile | ||
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression | ||
import com.jetbrains.php.lang.psi.elements.impl.FunctionReferenceImpl | ||
import com.pestphp.pest.getRootPhpPsiElements | ||
|
||
fun PsiFile.isPestDatasetFile(): Boolean { | ||
return this.getRootPhpPsiElements() | ||
.any(PsiElement::isPestDataset) | ||
} | ||
|
||
fun PsiElement?.isPestDataset(): Boolean { | ||
return when (this) { | ||
null -> false | ||
is FunctionReferenceImpl -> this.isPestDatasetFunction() | ||
else -> false | ||
} | ||
} | ||
|
||
fun FunctionReferenceImpl.isPestDatasetFunction(): Boolean { | ||
return this.canonicalText in setOf("dataset") | ||
} | ||
|
||
fun FunctionReferenceImpl.getPestDatasetName(): String? { | ||
return (getParameter(0) as? StringLiteralExpression)?.contents | ||
} |
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
Oops, something went wrong.