mirror of
https://gitee.com/zhangsken/AndroidIDE.git
synced 2026-10-16 07:49:44 +08:00
feat: add API for inverted query predicates
This commit is contained in:
+49
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* This file is part of AndroidIDE.
|
||||
*
|
||||
* AndroidIDE is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* AndroidIDE is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.itsaky.androidide.editor.language.treesitter.predicates
|
||||
|
||||
import com.itsaky.androidide.treesitter.TSQuery
|
||||
import com.itsaky.androidide.treesitter.TSQueryMatch
|
||||
import io.github.rosemoe.sora.editor.ts.predicate.PredicateResult
|
||||
import io.github.rosemoe.sora.editor.ts.predicate.TsClientPredicateStep
|
||||
|
||||
/**
|
||||
* A [TreeSitterPredicate] which inverts the result of another predicate.
|
||||
*
|
||||
* @author Akash Yadav
|
||||
*/
|
||||
open class InvertingPredicate(override val name: String, private val predicate: TreeSitterPredicate) :
|
||||
TreeSitterPredicate() {
|
||||
|
||||
override fun canHandle(steps: List<TsClientPredicateStep>): Boolean {
|
||||
return predicate.canHandle(steps)
|
||||
}
|
||||
|
||||
override fun doPredicateInternal(
|
||||
tsQuery: TSQuery,
|
||||
text: CharSequence,
|
||||
match: TSQueryMatch,
|
||||
predicate: List<TsClientPredicateStep>
|
||||
): PredicateResult {
|
||||
return when (val result = this.predicate.doPredicateInternal(tsQuery, text, match, predicate)) {
|
||||
PredicateResult.ACCEPT -> PredicateResult.REJECT
|
||||
PredicateResult.REJECT -> PredicateResult.ACCEPT
|
||||
else -> result
|
||||
}
|
||||
}
|
||||
}
|
||||
+9
-15
@@ -33,25 +33,19 @@ import java.util.regex.PatternSyntaxException
|
||||
*
|
||||
* @author Akash Yadav
|
||||
*/
|
||||
open class MatchPredicate protected constructor() : TreeSitterPredicate() {
|
||||
object MatchPredicate : TreeSitterPredicate() {
|
||||
|
||||
override val name: String
|
||||
get() = "match"
|
||||
|
||||
companion object {
|
||||
|
||||
@JvmField
|
||||
val INSTANCE = MatchPredicate()
|
||||
|
||||
@JvmField
|
||||
val PARAMETERS =
|
||||
arrayOf(
|
||||
TSQueryPredicateStep.Type.String,
|
||||
TSQueryPredicateStep.Type.Capture,
|
||||
TSQueryPredicateStep.Type.String,
|
||||
TSQueryPredicateStep.Type.Done
|
||||
)
|
||||
}
|
||||
@JvmField
|
||||
val PARAMETERS =
|
||||
arrayOf(
|
||||
TSQueryPredicateStep.Type.String,
|
||||
TSQueryPredicateStep.Type.Capture,
|
||||
TSQueryPredicateStep.Type.String,
|
||||
TSQueryPredicateStep.Type.Done
|
||||
)
|
||||
|
||||
private val cache = ConcurrentHashMap<String, Regex>()
|
||||
|
||||
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* This file is part of AndroidIDE.
|
||||
*
|
||||
* AndroidIDE is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* AndroidIDE is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.itsaky.androidide.editor.language.treesitter.predicates
|
||||
|
||||
import io.github.rosemoe.sora.editor.ts.predicate.TsPredicate
|
||||
|
||||
/**
|
||||
* [TsPredicate] implementation for '#not-eq?' query predicates.
|
||||
*
|
||||
* Syntax : `"#not-eq?" @capture @capture | "string" Done`
|
||||
*
|
||||
* Checks if the contents of the first capture is NOT equal to the given string or contents of the
|
||||
* second capture.
|
||||
*
|
||||
* @author Akash Yadav
|
||||
*/
|
||||
object NotEqualPredicate : InvertingPredicate("not-eq", EqualPredicate)
|
||||
+1
-24
@@ -17,10 +17,6 @@
|
||||
|
||||
package com.itsaky.androidide.editor.language.treesitter.predicates
|
||||
|
||||
import com.itsaky.androidide.treesitter.TSQuery
|
||||
import com.itsaky.androidide.treesitter.TSQueryMatch
|
||||
import io.github.rosemoe.sora.editor.ts.predicate.PredicateResult
|
||||
import io.github.rosemoe.sora.editor.ts.predicate.TsClientPredicateStep
|
||||
import io.github.rosemoe.sora.editor.ts.predicate.TsPredicate
|
||||
|
||||
/**
|
||||
@@ -28,23 +24,4 @@ import io.github.rosemoe.sora.editor.ts.predicate.TsPredicate
|
||||
*
|
||||
* @author Akash Yadav
|
||||
*/
|
||||
object NotMatchPredicate : MatchPredicate() {
|
||||
|
||||
override val name: String
|
||||
get() = "not-match"
|
||||
|
||||
override fun doPredicateInternal(
|
||||
tsQuery: TSQuery,
|
||||
text: CharSequence,
|
||||
match: TSQueryMatch,
|
||||
predicate: List<TsClientPredicateStep>
|
||||
): PredicateResult {
|
||||
|
||||
// Invert the result of the match predicate
|
||||
return when (val result = super.doPredicateInternal(tsQuery, text, match, predicate)) {
|
||||
PredicateResult.ACCEPT -> PredicateResult.REJECT
|
||||
PredicateResult.REJECT -> PredicateResult.ACCEPT
|
||||
else -> result
|
||||
}
|
||||
}
|
||||
}
|
||||
object NotMatchPredicate : InvertingPredicate("not-match", MatchPredicate)
|
||||
|
||||
+1
-1
@@ -51,7 +51,7 @@ abstract class TreeSitterPredicate : TsPredicate {
|
||||
* @param predicate The predicate steps.
|
||||
* @return The result of the predicate check.
|
||||
*/
|
||||
abstract fun doPredicateInternal(
|
||||
internal abstract fun doPredicateInternal(
|
||||
tsQuery: TSQuery,
|
||||
text: CharSequence,
|
||||
match: TSQueryMatch,
|
||||
|
||||
@@ -22,6 +22,7 @@ import com.itsaky.androidide.editor.language.treesitter.TreeSitterLanguageSpec
|
||||
import com.itsaky.androidide.editor.language.treesitter.predicates.AnyOfPredicate
|
||||
import com.itsaky.androidide.editor.language.treesitter.predicates.EqualPredicate
|
||||
import com.itsaky.androidide.editor.language.treesitter.predicates.MatchPredicate
|
||||
import com.itsaky.androidide.editor.language.treesitter.predicates.NotEqualPredicate
|
||||
import com.itsaky.androidide.editor.language.treesitter.predicates.NotMatchPredicate
|
||||
import com.itsaky.androidide.treesitter.TSLanguage
|
||||
import com.itsaky.androidide.utils.ILogger
|
||||
@@ -55,7 +56,7 @@ object LanguageSpecProvider {
|
||||
bracketsScmSource = readScheme(context, type, "brackets"),
|
||||
localsCaptureSpec = localsCaptureSpec,
|
||||
predicates =
|
||||
listOf(MatchPredicate.INSTANCE, NotMatchPredicate, EqualPredicate, AnyOfPredicate)
|
||||
listOf(MatchPredicate, NotMatchPredicate, EqualPredicate, NotEqualPredicate, AnyOfPredicate)
|
||||
)
|
||||
return TreeSitterLanguageSpec(
|
||||
spec = editorLangSpec,
|
||||
|
||||
Reference in New Issue
Block a user