-
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.
Add inspection for checking if dataset exist
- Loading branch information
Oliver
committed
May 26, 2022
1 parent
cbe4222
commit 88e9f06
Showing
62 changed files
with
448 additions
and
285 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
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
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
142 changes: 142 additions & 0 deletions
142
src/main/kotlin/com/pestphp/pest/datasets/DataSetCompletionProvider.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,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() | ||
} | ||
} |
Oops, something went wrong.