-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added PythonFunctionCallHeuristic (#148)
* Added PythonMethodHeuristic Try in PythonMethodHeuristic Logic Fixed PythonMethodHeuristic * Lint fix * Fixes after review * Fixes after review * Fixes after review * Fixes after review * Fixes after review * Fixes after review
- Loading branch information
Showing
13 changed files
with
193 additions
and
19 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
2 changes: 1 addition & 1 deletion
2
core/src/main/kotlin/org/virtuslab/bazelsteward/core/library/LibraryId.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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
package org.virtuslab.bazelsteward.core.library | ||
|
||
abstract class LibraryId { | ||
abstract fun associatedStrings(): List<String> | ||
abstract fun associatedStrings(): List<List<String>> | ||
abstract val name: String | ||
final override fun toString(): String = name | ||
} |
47 changes: 47 additions & 0 deletions
47
...rc/main/kotlin/org/virtuslab/bazelsteward/core/replacement/PythonFunctionCallHeuristic.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,47 @@ | ||
package org.virtuslab.bazelsteward.core.replacement | ||
|
||
import org.virtuslab.bazelsteward.core.common.FileChange | ||
import org.virtuslab.bazelsteward.core.common.TextFile | ||
import org.virtuslab.bazelsteward.core.common.UpdateSuggestion | ||
|
||
object PythonFunctionCallHeuristic : VersionReplacementHeuristic { | ||
override val name: String = "python-function-call" | ||
|
||
override fun apply(files: List<TextFile>, updateSuggestion: UpdateSuggestion): LibraryUpdate? { | ||
val functionCalls = getFunctionCalls(files) | ||
|
||
if (functionCalls.isNotEmpty()) { | ||
val associatedStringVariants = updateSuggestion.currentLibrary.id.associatedStrings() | ||
val functionCallsWithAssociatedStrings = functionCalls.filter { call -> | ||
associatedStringVariants.any { strings -> | ||
strings.all { call.matchedText.contains(it) } | ||
} | ||
} | ||
|
||
val currentVersion = updateSuggestion.currentLibrary.version.value | ||
val matchedVersion = functionCallsWithAssociatedStrings.firstNotNullOfOrNull { call -> | ||
Regex.fromLiteral(currentVersion).find(call.matchedText)?.let { call.subMatch(it) } | ||
} ?: return null | ||
|
||
return LibraryUpdate( | ||
updateSuggestion, | ||
listOf( | ||
FileChange( | ||
matchedVersion.origin, | ||
matchedVersion.offset, | ||
updateSuggestion.currentLibrary.version.value.length, | ||
updateSuggestion.suggestedVersion.value | ||
) | ||
) | ||
) | ||
} | ||
return null | ||
} | ||
|
||
private fun getFunctionCalls(files: List<TextFile>): List<MatchedText> { | ||
val pythonMethodRegex = Regex("""\w+\([\w\n\s".\-,=]+\)""") | ||
return files.flatMap { textFile -> | ||
pythonMethodRegex.findAll(textFile.content).map { MatchedText(it, textFile.path) } | ||
} | ||
} | ||
} |
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