From f0c619d88574245a9593fa54737a0a8d119e99a1 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Tue, 14 Nov 2023 20:29:05 +0100 Subject: [PATCH 01/81] add test for a file class code getting api --- .../api/models/IdeApiModelsTest.kt | 27 +++++------ .../api/models/fileRelated/FileClassesTest.kt | 47 +++++++++++++++++-- 2 files changed, 56 insertions(+), 18 deletions(-) diff --git a/ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/IdeApiModelsTest.kt b/ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/IdeApiModelsTest.kt index fb8d2b6..0dd153e 100644 --- a/ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/IdeApiModelsTest.kt +++ b/ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/IdeApiModelsTest.kt @@ -3,6 +3,8 @@ package org.jetbrains.research.ideFormerPlugin.api.models import com.intellij.testFramework.TestDataPath import com.intellij.testFramework.fixtures.BasePlatformTestCase import org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.FileText +import org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileClasses.FileClasses +import org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileFunctions.FileFunctions import org.jetbrains.research.ideFormerPlugin.api.models.fileSystemRelated.ChangeDirectory import org.jetbrains.research.ideFormerPlugin.api.models.fileSystemRelated.ListDirectoryContents import org.jetbrains.research.ideFormerPlugin.api.models.utils.chooseFileClassesApiForFile @@ -18,19 +20,6 @@ abstract class IdeApiModelsTest : BasePlatformTestCase() { configureTestProject() } - /* - Test project structure: - - testProject - |-- dir1 - |-- subdir - | |-- someKtFile1.kt - |-- someKtFile2.kt - |-- dir2 - |-- subdir - |-- subsubdir - |-- someTextFile.txt - */ private fun configureTestProject() { myFixture.copyFileToProject("dir1/subdir/someKtFile1.kt") myFixture.copyFileToProject("dir1/someKtFile2.kt") @@ -81,7 +70,7 @@ abstract class IdeApiModelsTest : BasePlatformTestCase() { ideStateKeeper: IdeStateKeeper, fileName: String, expectedClassesNames: Set - ) { + ): FileClasses { val fileClasses = chooseFileClassesApiForFile(fileName, ideStateKeeper.currentProjectDirectory) fileClasses.also { it.execute() @@ -90,8 +79,18 @@ abstract class IdeApiModelsTest : BasePlatformTestCase() { it.getClassesNames()?.toSet() ?: error("File classes names list is null") ) } + return fileClasses } + protected fun checkFileClassCodeGetting( + fileClasses: FileClasses, + className: String, + expectedClassCode: String + ) { + fileClasses.getClassCode(className)?.also { + assertEquals(expectedClassCode, it) + } + } protected fun checkFileTextExecution( ideStateKeeper: IdeStateKeeper, diff --git a/ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/FileClassesTest.kt b/ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/FileClassesTest.kt index 91d26ef..60cf43a 100644 --- a/ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/FileClassesTest.kt +++ b/ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/FileClassesTest.kt @@ -13,22 +13,61 @@ class FileClassesTest : IdeApiModelsTest() { "dir1" ) - checkFileClassesExecution( + // TODO: create parametrized tests + + val ktFileClasses = checkFileClassesExecution( ideStateKeeper, "someKtFile2.kt", setOf("SimpleClass", "ComplexClass") ) - checkFileClassesExecution( + checkFileClassCodeGetting( + ktFileClasses, + "SimpleClass", + """ + class SimpleClass(private val num: Int = 0) { + fun decreaseNum(): Int = return ++num + + fun printSomePhrase(phrase: String = "some phrase") = println(phrase) + } + """.trimIndent() + ) + + val pyFileClasses = checkFileClassesExecution( ideStateKeeper, "pyFile.py", setOf("Dog") ) - checkFileClassesExecution( + checkFileClassCodeGetting( + pyFileClasses, + "Dog", + """ + class Dog: + def __init__(self, name, age): + self.name = name + self.age = age + + def bark(self): + print("bark bark!") + """.trimIndent() + ) + + val javaFileClasses = checkFileClassesExecution( ideStateKeeper, "SomeJavaClass.java", setOf("SomeJavaClass") ) + checkFileClassCodeGetting( + javaFileClasses, + "SomeJavaClass", + """ + class SomeJavaClass() { + private Integer a = 1; - // TODO: add tests for getClassCode method + public void setA(Integer a) { + this.a = a; + } + } + """.trimIndent() + ) } } \ No newline at end of file From c9a5f37c055d94a6fe89aecc5b744e03b47ca066 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Tue, 14 Nov 2023 21:14:29 +0100 Subject: [PATCH 02/81] add test for a file function code api --- .../api/models/IdeApiModelsTest.kt | 18 ++++++- .../models/fileRelated/FileFunctionsTest.kt | 53 ++++++++++++++++--- 2 files changed, 62 insertions(+), 9 deletions(-) diff --git a/ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/IdeApiModelsTest.kt b/ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/IdeApiModelsTest.kt index 0dd153e..6f41825 100644 --- a/ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/IdeApiModelsTest.kt +++ b/ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/IdeApiModelsTest.kt @@ -51,11 +51,11 @@ abstract class IdeApiModelsTest : BasePlatformTestCase() { assertEquals(expectedProjectDirectoryName, ideStateKeeper.currentProjectDirectory.name) } - protected fun checkFileMethodsExecution( + protected fun checkFileFunctionsExecution( ideStateKeeper: IdeStateKeeper, fileName: String, expectedKtMethodsNames: Set - ) { + ): FileFunctions { val fileFunctions = chooseFileFunctionsApiForFile(fileName, ideStateKeeper.currentProjectDirectory) fileFunctions.also { it.execute() @@ -64,6 +64,20 @@ abstract class IdeApiModelsTest : BasePlatformTestCase() { it.getFunctionsNames()?.toSet() ?: error("File functions names list is null") ) } + return fileFunctions + } + + /** The absence of the necessary indentation on the 1st line before the start of the function is ok, + bc the function text obtained from PSI begins exactly with the first non-space letter of the function code + **/ + protected fun checkFileFunctionCodeGetting( + fileFunctions: FileFunctions, + functionName: String, + expectedMethodCode: String + ) { + fileFunctions.getFunctionCode(functionName)?.also { + assertEquals(expectedMethodCode, it) + } } protected fun checkFileClassesExecution( diff --git a/ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/FileFunctionsTest.kt b/ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/FileFunctionsTest.kt index 4814a97..610693d 100644 --- a/ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/FileFunctionsTest.kt +++ b/ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/FileFunctionsTest.kt @@ -9,27 +9,66 @@ class FileFunctionsTest : IdeApiModelsTest() { checkChangeDirectoryExecution(ideStateKeeper, "dir1") - checkFileMethodsExecution( + // TODO: create parametrized tests + + val ktFileMethods = checkFileFunctionsExecution( ideStateKeeper, "someKtFile2.kt", setOf("decreaseNum", "printSomePhrase", "delegatePrinting", "SimpleClass", "ComplexClass") ) - checkFileMethodsExecution( + checkFileFunctionCodeGetting( + ktFileMethods, + "printSomePhrase", + """ + fun printSomePhrase(phrase: String = "some phrase") = println(phrase) + """.trimIndent() + ) + + + val javaFileMethods = checkFileFunctionsExecution( ideStateKeeper, "SomeJavaClass.java", setOf("setA") ) - checkFileMethodsExecution( + checkFileFunctionCodeGetting( + javaFileMethods, + "setA", + """ + public void setA(Integer a) { + this.a = a; + } + """.trimIndent() + ) + + val pyFileMethods = checkFileFunctionsExecution( ideStateKeeper, "pyFile.py", setOf("a_b", "__init__", "bark") ) + // function outside the class + checkFileFunctionCodeGetting( + pyFileMethods, + "a_b", + """ + def a_b(a: int, b: int) -> int: + return a + b + """.trimIndent() + ) + // function inside the class + checkFileFunctionCodeGetting( + pyFileMethods, + "__init__", + """ + def __init__(self, name, age): + self.name = name + self.age = age + """.trimIndent() + ) - checkChangeDirectoryExecution(ideStateKeeper, "subdir") - - checkFileMethodsExecution( + // check file functions for a file with the complex structured path + checkFileFunctionsExecution( ideStateKeeper, - "someKtFile1.kt", + "subdir/someKtFile1.kt", setOf("main", "increaseNum") ) } From a3526926cd9773e3847067eba33d8260f0663517 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Tue, 14 Nov 2023 22:24:23 +0100 Subject: [PATCH 03/81] add method for processing any server request parameter --- .../ideFormerPlugin/server/RoutingUtils.kt | 21 +++++++++++++++---- .../requests/fileRelated/getFileClasses.kt | 15 ++++++------- .../requests/fileRelated/getFileFunctions.kt | 11 ++++------ .../requests/fileRelated/getFileText.kt | 7 ++----- 4 files changed, 29 insertions(+), 25 deletions(-) diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/RoutingUtils.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/RoutingUtils.kt index ba1993c..3027238 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/RoutingUtils.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/RoutingUtils.kt @@ -19,16 +19,29 @@ suspend fun ApplicationCall.respondJson(responseObject: Any, status: HttpStatusC ) } -suspend fun ApplicationCall.processFileNameParameter(logger: Logger): String? = - this.parameters["fileName"] ?: run { - logger.error("File name was not provided") +suspend fun ApplicationCall.processRequestParameter( + parameterName: String, + errorMessage: String, + logger: Logger +): String? = + this.parameters[parameterName] ?: run { + logger.error(errorMessage) this.respondJson( - IdeServerConstants.MISSING_FILENAME, + errorMessage, HttpStatusCode.BadRequest ) null } +const val FILENAME_REQUEST_PARAMETER = "fileName" + +suspend fun ApplicationCall.processFileNameParameter(logger: Logger): String? = + this.processRequestParameter( + FILENAME_REQUEST_PARAMETER, + IdeServerConstants.MISSING_FILENAME, + logger + ) + suspend fun PipelineContext.executeAndRespondError( ideApiMethod: IdeApiMethod, logger: Logger diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileClasses.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileClasses.kt index e9671a3..6a502fa 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileClasses.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileClasses.kt @@ -4,24 +4,21 @@ import io.ktor.http.* import io.ktor.server.application.* import io.ktor.server.routing.* import org.jetbrains.research.ideFormerPlugin.api.models.utils.chooseFileClassesApiForFile -import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants -import org.jetbrains.research.ideFormerPlugin.server.executeAndRespondError -import org.jetbrains.research.ideFormerPlugin.server.processFileNameParameter -import org.jetbrains.research.ideFormerPlugin.server.respondJson +import org.jetbrains.research.ideFormerPlugin.server.* import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper import org.slf4j.Logger fun Routing.getFileClasses(logger: Logger, ideStateKeeper: IdeStateKeeper) { - get("/file-classes/{fileName?}") { - val fileName = call.processFileNameParameter(logger) - ?: return@get - logger.info("Server GET file classes request for file '$fileName' is called") + get("/file-classes/{$FILENAME_REQUEST_PARAMETER?}{className?}") { + val fileName = call.processFileNameParameter(logger) ?: return@get + logger.info("Server GET file classes request for the file '$fileName' is called") val fileClasses = try { chooseFileClassesApiForFile(fileName, ideStateKeeper.currentProjectDirectory) } catch (e: Exception) { logger.error(e.message) return@get call.respondJson( + // TODO: mb this is redundant bc there is the thrown error has its own message IdeServerConstants.INCORRECT_REQUESTED_FILE_EXTENSION, HttpStatusCode.BadRequest ) @@ -32,6 +29,6 @@ fun Routing.getFileClasses(logger: Logger, ideStateKeeper: IdeStateKeeper) { } call.respondJson(fileClasses.getClassesNames()!!) - logger.info("Server GET file classes request for file '$fileName' is processed") + logger.info("Server GET file classes request for the file '$fileName' is processed") } } \ No newline at end of file diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileFunctions.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileFunctions.kt index 260f613..ebf33a9 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileFunctions.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileFunctions.kt @@ -4,17 +4,13 @@ import io.ktor.http.* import io.ktor.server.application.* import io.ktor.server.routing.* import org.jetbrains.research.ideFormerPlugin.api.models.utils.chooseFileFunctionsApiForFile -import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants -import org.jetbrains.research.ideFormerPlugin.server.executeAndRespondError -import org.jetbrains.research.ideFormerPlugin.server.processFileNameParameter -import org.jetbrains.research.ideFormerPlugin.server.respondJson +import org.jetbrains.research.ideFormerPlugin.server.* import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper import org.slf4j.Logger fun Routing.getFileFunctions(logger: Logger, ideStateKeeper: IdeStateKeeper) { - get("/file-functions/{fileName?}") { - val fileName = call.processFileNameParameter(logger) - ?: return@get + get("/file-functions/{$FILENAME_REQUEST_PARAMETER?}") { + val fileName = call.processFileNameParameter(logger) ?: return@get logger.info("Server GET file methods request for file '$fileName' is called") val fileFunctions = try { @@ -22,6 +18,7 @@ fun Routing.getFileFunctions(logger: Logger, ideStateKeeper: IdeStateKeeper) { } catch (e: Exception) { logger.error(e.message) return@get call.respondJson( + // TODO: mb this is redundant bc there is the thrown error has its own message IdeServerConstants.INCORRECT_REQUESTED_FILE_EXTENSION, HttpStatusCode.BadRequest ) diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileText.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileText.kt index 1c01b69..6f0fdad 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileText.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileText.kt @@ -3,16 +3,13 @@ package org.jetbrains.research.ideFormerPlugin.server.requests.fileRelated import io.ktor.server.application.* import io.ktor.server.routing.* import org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.FileText -import org.jetbrains.research.ideFormerPlugin.server.executeAndRespondError -import org.jetbrains.research.ideFormerPlugin.server.processFileNameParameter -import org.jetbrains.research.ideFormerPlugin.server.respondJson +import org.jetbrains.research.ideFormerPlugin.server.* import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper import org.slf4j.Logger fun Routing.getFileText(logger: Logger, ideStateKeeper: IdeStateKeeper) { get("/file-text/{fileName?}") { - val fileName = call.processFileNameParameter(logger) - ?: return@get + val fileName = call.processFileNameParameter(logger) ?: return@get logger.info("Server GET file text request for file '$fileName' is called") val fileText = FileText(ideStateKeeper.currentProjectDirectory, fileName) From fee029547b23bbfbd389f9d57eed74f57c8dee05 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Tue, 14 Nov 2023 23:38:52 +0100 Subject: [PATCH 04/81] add file class code api server request, add error throwing to the class code getters --- .../fileRelated/fileClasses/FileClasses.kt | 2 +- .../fileClasses/JavaFileClasses.kt | 5 ++++- .../fileRelated/fileClasses/KtFileClasses.kt | 4 +++- .../fileRelated/fileClasses/PyFileClasses.kt | 4 +++- .../api/models/utils/ApiUtils.kt | 2 ++ .../ideFormerPlugin/server/Routing.kt | 3 +++ .../ideFormerPlugin/server/RoutingUtils.kt | 4 +--- .../requests/fileRelated/getFileClasses.kt | 21 +++++++++++++++---- 8 files changed, 34 insertions(+), 11 deletions(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/FileClasses.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/FileClasses.kt index 92f3e39..f18c31b 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/FileClasses.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/FileClasses.kt @@ -4,5 +4,5 @@ import org.jetbrains.research.ideFormerPlugin.api.models.IdeApiMethod interface FileClasses : IdeApiMethod { fun getClassesNames(): List? - fun getClassCode(className: String): String? + fun getClassCode(className: String): String } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/JavaFileClasses.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/JavaFileClasses.kt index 4b49d20..6641796 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/JavaFileClasses.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/JavaFileClasses.kt @@ -2,6 +2,7 @@ package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileClasse import com.intellij.psi.PsiClass import com.intellij.psi.PsiDirectory +import org.jetbrains.research.ideFormerPlugin.api.models.utils.NO_SUCH_FILE_CLASS import org.jetbrains.research.ideFormerPlugin.api.models.utils.getFilePsiElementsOfType class JavaFileClasses( @@ -17,7 +18,9 @@ class JavaFileClasses( override fun getClassesNames(): List? = javaClasses?.mapNotNull { it.name } - override fun getClassCode(className: String): String? = javaClasses + // TODO: to think about the case when javaClasses is not initialized yet + override fun getClassCode(className: String): String = javaClasses ?.firstOrNull { it.name == className } ?.text + ?: error(NO_SUCH_FILE_CLASS) } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/KtFileClasses.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/KtFileClasses.kt index 07be647..281a87d 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/KtFileClasses.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/KtFileClasses.kt @@ -2,6 +2,7 @@ package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileClasse import com.intellij.psi.PsiDirectory import org.jetbrains.kotlin.psi.KtClass +import org.jetbrains.research.ideFormerPlugin.api.models.utils.NO_SUCH_FILE_CLASS import org.jetbrains.research.ideFormerPlugin.api.models.utils.getFilePsiElementsOfType class KtFileClasses( @@ -16,7 +17,8 @@ class KtFileClasses( override fun getClassesNames(): List? = ktClasses?.mapNotNull { it.name } - override fun getClassCode(className: String): String? = ktClasses + override fun getClassCode(className: String): String = ktClasses ?.firstOrNull { it.name == className } ?.text + ?: error(NO_SUCH_FILE_CLASS) } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/PyFileClasses.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/PyFileClasses.kt index 6411bca..8c981eb 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/PyFileClasses.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/PyFileClasses.kt @@ -2,6 +2,7 @@ package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileClasse import com.intellij.psi.PsiDirectory import com.jetbrains.python.psi.PyClass +import org.jetbrains.research.ideFormerPlugin.api.models.utils.NO_SUCH_FILE_CLASS import org.jetbrains.research.ideFormerPlugin.api.models.utils.getFilePsiElementsOfType class PyFileClasses( @@ -16,7 +17,8 @@ class PyFileClasses( override fun getClassesNames(): List? = pyClasses?.mapNotNull { it.name } - override fun getClassCode(className: String): String? = pyClasses + override fun getClassCode(className: String): String = pyClasses ?.firstOrNull { it.name == className } ?.text + ?: error(NO_SUCH_FILE_CLASS) } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/ApiUtils.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/ApiUtils.kt index 63704b4..39c00b7 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/ApiUtils.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/ApiUtils.kt @@ -6,7 +6,9 @@ import org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileClasses import org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileFunctions.* const val DEFAULT_DIRECTORY_NAME = "." + const val UNSUPPORTED_FILE_EXTENSION_ERROR = "Unsupported file extension" +const val NO_SUCH_FILE_CLASS = "No such class in a file" enum class FileExtensions(val extension: String) { KT("kt"), diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt index 94dc0a3..d60132f 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt @@ -40,4 +40,7 @@ object IdeServerConstants { const val API_EXECUTION_UNKNOWN_ERROR = "Unknown error while api method execution" const val INCORRECT_REQUESTED_FILE_EXTENSION = "Incorrect requested file extension: only .py, .kt, .java file can be processed" + + const val FILENAME_REQUEST_PARAMETER = "fileName" + const val CLASSNAME_REQUEST_PARAMETER = "className" } \ No newline at end of file diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/RoutingUtils.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/RoutingUtils.kt index 3027238..8691f71 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/RoutingUtils.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/RoutingUtils.kt @@ -33,11 +33,9 @@ suspend fun ApplicationCall.processRequestParameter( null } -const val FILENAME_REQUEST_PARAMETER = "fileName" - suspend fun ApplicationCall.processFileNameParameter(logger: Logger): String? = this.processRequestParameter( - FILENAME_REQUEST_PARAMETER, + IdeServerConstants.FILENAME_REQUEST_PARAMETER, IdeServerConstants.MISSING_FILENAME, logger ) diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileClasses.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileClasses.kt index 6a502fa..9669a63 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileClasses.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileClasses.kt @@ -5,11 +5,13 @@ import io.ktor.server.application.* import io.ktor.server.routing.* import org.jetbrains.research.ideFormerPlugin.api.models.utils.chooseFileClassesApiForFile import org.jetbrains.research.ideFormerPlugin.server.* +import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.CLASSNAME_REQUEST_PARAMETER +import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.FILENAME_REQUEST_PARAMETER import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper import org.slf4j.Logger fun Routing.getFileClasses(logger: Logger, ideStateKeeper: IdeStateKeeper) { - get("/file-classes/{$FILENAME_REQUEST_PARAMETER?}{className?}") { + get("/file-classes/{$FILENAME_REQUEST_PARAMETER?}{$CLASSNAME_REQUEST_PARAMETER?}") { val fileName = call.processFileNameParameter(logger) ?: return@get logger.info("Server GET file classes request for the file '$fileName' is called") @@ -28,7 +30,18 @@ fun Routing.getFileClasses(logger: Logger, ideStateKeeper: IdeStateKeeper) { return@get } - call.respondJson(fileClasses.getClassesNames()!!) - logger.info("Server GET file classes request for the file '$fileName' is processed") + val className = call.parameters[CLASSNAME_REQUEST_PARAMETER] + if (className == null) { + call.respondJson(fileClasses.getClassesNames()!!) + logger.info("Server GET file classes request for the file '$fileName' is processed") + } else { + val classCode = try { + fileClasses.getClassCode(className) + } catch (e: Exception) { + e.message!! + } + call.respondJson(classCode) + logger.info("Server GET file class code request for the file '$fileName' and the class '$className' is processed") + } } -} \ No newline at end of file +} From eaa49305f7aa87a71f9c4ac308a2282709da2717 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Tue, 14 Nov 2023 23:48:41 +0100 Subject: [PATCH 05/81] add error throwing to the function code getters --- .../api/models/fileRelated/fileFunctions/FileFunctions.kt | 2 +- .../models/fileRelated/fileFunctions/JavaFileFunctions.kt | 5 ++++- .../api/models/fileRelated/fileFunctions/KtFileFunctions.kt | 5 ++++- .../api/models/fileRelated/fileFunctions/PyFileFunctions.kt | 5 ++++- .../research/ideFormerPlugin/api/models/utils/ApiUtils.kt | 1 + 5 files changed, 14 insertions(+), 4 deletions(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/FileFunctions.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/FileFunctions.kt index ad40fbc..8d5e71b 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/FileFunctions.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/FileFunctions.kt @@ -4,5 +4,5 @@ import org.jetbrains.research.ideFormerPlugin.api.models.IdeApiMethod interface FileFunctions : IdeApiMethod { fun getFunctionsNames(): List? - fun getFunctionCode(functionName: String): String? + fun getFunctionCode(functionName: String): String } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/JavaFileFunctions.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/JavaFileFunctions.kt index 9dd9c23..d8b3d99 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/JavaFileFunctions.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/JavaFileFunctions.kt @@ -2,6 +2,7 @@ package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileFuncti import com.intellij.psi.PsiDirectory import com.intellij.psi.PsiMethod +import org.jetbrains.research.ideFormerPlugin.api.models.utils.NO_SUCH_FILE_FUNCTION import org.jetbrains.research.ideFormerPlugin.api.models.utils.getFilePsiElementsOfType class JavaFileFunctions( @@ -15,7 +16,9 @@ class JavaFileFunctions( } override fun getFunctionsNames(): List? = javaFunctions?.map { it.name } - override fun getFunctionCode(functionName: String): String? = javaFunctions + + override fun getFunctionCode(functionName: String): String = javaFunctions ?.firstOrNull { it.name == functionName } ?.text + ?: error(NO_SUCH_FILE_FUNCTION) } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/KtFileFunctions.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/KtFileFunctions.kt index 78858f9..5f223ff 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/KtFileFunctions.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/KtFileFunctions.kt @@ -2,6 +2,7 @@ package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileFuncti import com.intellij.psi.PsiDirectory import org.jetbrains.kotlin.psi.KtFunction +import org.jetbrains.research.ideFormerPlugin.api.models.utils.NO_SUCH_FILE_FUNCTION import org.jetbrains.research.ideFormerPlugin.api.models.utils.getFilePsiElementsOfType class KtFileFunctions( @@ -15,7 +16,9 @@ class KtFileFunctions( } override fun getFunctionsNames(): List? = ktFunctions?.mapNotNull { it.name } - override fun getFunctionCode(functionName: String): String? = ktFunctions + + override fun getFunctionCode(functionName: String): String = ktFunctions ?.firstOrNull { it.name == functionName } ?.text + ?: error(NO_SUCH_FILE_FUNCTION) } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/PyFileFunctions.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/PyFileFunctions.kt index 779eeb3..94c8779 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/PyFileFunctions.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/PyFileFunctions.kt @@ -2,6 +2,7 @@ package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileFuncti import com.intellij.psi.PsiDirectory import com.jetbrains.python.psi.PyFunction +import org.jetbrains.research.ideFormerPlugin.api.models.utils.NO_SUCH_FILE_FUNCTION import org.jetbrains.research.ideFormerPlugin.api.models.utils.getFilePsiElementsOfType class PyFileFunctions( @@ -15,7 +16,9 @@ class PyFileFunctions( } override fun getFunctionsNames(): List? = pyFunctions?.mapNotNull { it.name } - override fun getFunctionCode(functionName: String): String? = pyFunctions + + override fun getFunctionCode(functionName: String): String = pyFunctions ?.firstOrNull { it.name == functionName } ?.text + ?: error(NO_SUCH_FILE_FUNCTION) } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/ApiUtils.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/ApiUtils.kt index 39c00b7..15afe19 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/ApiUtils.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/ApiUtils.kt @@ -9,6 +9,7 @@ const val DEFAULT_DIRECTORY_NAME = "." const val UNSUPPORTED_FILE_EXTENSION_ERROR = "Unsupported file extension" const val NO_SUCH_FILE_CLASS = "No such class in a file" +const val NO_SUCH_FILE_FUNCTION = "No such function in a file" enum class FileExtensions(val extension: String) { KT("kt"), From 5dcbd70edaf62823a6678e6222f42d9f54a194eb Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Tue, 14 Nov 2023 23:57:03 +0100 Subject: [PATCH 06/81] add file function getting server request --- .../ideFormerPlugin/server/Routing.kt | 2 ++ .../requests/fileRelated/getFileFunctions.kt | 24 +++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt index d60132f..c5f2cb8 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt @@ -41,6 +41,8 @@ object IdeServerConstants { const val INCORRECT_REQUESTED_FILE_EXTENSION = "Incorrect requested file extension: only .py, .kt, .java file can be processed" + // TODO: renaming const val FILENAME_REQUEST_PARAMETER = "fileName" const val CLASSNAME_REQUEST_PARAMETER = "className" + const val FUNCTION_NAME_REQUEST_PARAMETER = "functionName" } \ No newline at end of file diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileFunctions.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileFunctions.kt index ebf33a9..0fd8db3 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileFunctions.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileFunctions.kt @@ -5,13 +5,15 @@ import io.ktor.server.application.* import io.ktor.server.routing.* import org.jetbrains.research.ideFormerPlugin.api.models.utils.chooseFileFunctionsApiForFile import org.jetbrains.research.ideFormerPlugin.server.* +import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.FILENAME_REQUEST_PARAMETER +import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.FUNCTION_NAME_REQUEST_PARAMETER import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper import org.slf4j.Logger fun Routing.getFileFunctions(logger: Logger, ideStateKeeper: IdeStateKeeper) { - get("/file-functions/{$FILENAME_REQUEST_PARAMETER?}") { + get("/file-functions/{$FILENAME_REQUEST_PARAMETER?}{$FUNCTION_NAME_REQUEST_PARAMETER?}") { val fileName = call.processFileNameParameter(logger) ?: return@get - logger.info("Server GET file methods request for file '$fileName' is called") + logger.info("Server GET file functions request for file '$fileName' is called") val fileFunctions = try { chooseFileFunctionsApiForFile(fileName, ideStateKeeper.currentProjectDirectory) @@ -28,7 +30,21 @@ fun Routing.getFileFunctions(logger: Logger, ideStateKeeper: IdeStateKeeper) { return@get } - call.respondJson(fileFunctions.getFunctionsNames()!!) - logger.info("Server GET file methods request for file '$fileName' is processed") + when (val functionName = call.parameters[FUNCTION_NAME_REQUEST_PARAMETER]) { + null -> { + call.respondJson(fileFunctions.getFunctionsNames()!!) + logger.info("Server GET file functions request for file '$fileName' is processed") + } + else -> { + val functionCode = try { + fileFunctions.getFunctionCode(functionName) + } catch (e: Exception) { + e.message!! + } + + call.respondJson(functionCode) + logger.info("Server GET file function code request for file '$fileName' and function '$functionName' is processed") + } + } } } \ No newline at end of file From 9ecae0bad2d55f6152b51a785e1f24581186831e Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Tue, 14 Nov 2023 23:58:59 +0100 Subject: [PATCH 07/81] rename requests params constants --- .../jetbrains/research/ideFormerPlugin/server/Routing.kt | 6 +++--- .../research/ideFormerPlugin/server/RoutingUtils.kt | 2 +- .../server/requests/fileRelated/getFileClasses.kt | 8 ++++---- .../server/requests/fileRelated/getFileFunctions.kt | 8 ++++---- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt index c5f2cb8..8270c7f 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt @@ -42,7 +42,7 @@ object IdeServerConstants { "Incorrect requested file extension: only .py, .kt, .java file can be processed" // TODO: renaming - const val FILENAME_REQUEST_PARAMETER = "fileName" - const val CLASSNAME_REQUEST_PARAMETER = "className" - const val FUNCTION_NAME_REQUEST_PARAMETER = "functionName" + const val FILE_NAME_REQUEST_PARAM = "fileName" + const val CLASS_NAME_REQUEST_PARAM = "className" + const val FUNCTION_NAME_REQUEST_PARAM = "functionName" } \ No newline at end of file diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/RoutingUtils.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/RoutingUtils.kt index 8691f71..4737c7e 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/RoutingUtils.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/RoutingUtils.kt @@ -35,7 +35,7 @@ suspend fun ApplicationCall.processRequestParameter( suspend fun ApplicationCall.processFileNameParameter(logger: Logger): String? = this.processRequestParameter( - IdeServerConstants.FILENAME_REQUEST_PARAMETER, + IdeServerConstants.FILE_NAME_REQUEST_PARAM, IdeServerConstants.MISSING_FILENAME, logger ) diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileClasses.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileClasses.kt index 9669a63..6f996b8 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileClasses.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileClasses.kt @@ -5,13 +5,13 @@ import io.ktor.server.application.* import io.ktor.server.routing.* import org.jetbrains.research.ideFormerPlugin.api.models.utils.chooseFileClassesApiForFile import org.jetbrains.research.ideFormerPlugin.server.* -import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.CLASSNAME_REQUEST_PARAMETER -import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.FILENAME_REQUEST_PARAMETER +import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.CLASS_NAME_REQUEST_PARAM +import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.FILE_NAME_REQUEST_PARAM import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper import org.slf4j.Logger fun Routing.getFileClasses(logger: Logger, ideStateKeeper: IdeStateKeeper) { - get("/file-classes/{$FILENAME_REQUEST_PARAMETER?}{$CLASSNAME_REQUEST_PARAMETER?}") { + get("/file-classes/{$FILE_NAME_REQUEST_PARAM?}{$CLASS_NAME_REQUEST_PARAM?}") { val fileName = call.processFileNameParameter(logger) ?: return@get logger.info("Server GET file classes request for the file '$fileName' is called") @@ -30,7 +30,7 @@ fun Routing.getFileClasses(logger: Logger, ideStateKeeper: IdeStateKeeper) { return@get } - val className = call.parameters[CLASSNAME_REQUEST_PARAMETER] + val className = call.parameters[CLASS_NAME_REQUEST_PARAM] if (className == null) { call.respondJson(fileClasses.getClassesNames()!!) logger.info("Server GET file classes request for the file '$fileName' is processed") diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileFunctions.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileFunctions.kt index 0fd8db3..a927a7f 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileFunctions.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileFunctions.kt @@ -5,13 +5,13 @@ import io.ktor.server.application.* import io.ktor.server.routing.* import org.jetbrains.research.ideFormerPlugin.api.models.utils.chooseFileFunctionsApiForFile import org.jetbrains.research.ideFormerPlugin.server.* -import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.FILENAME_REQUEST_PARAMETER -import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.FUNCTION_NAME_REQUEST_PARAMETER +import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.FILE_NAME_REQUEST_PARAM +import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.FUNCTION_NAME_REQUEST_PARAM import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper import org.slf4j.Logger fun Routing.getFileFunctions(logger: Logger, ideStateKeeper: IdeStateKeeper) { - get("/file-functions/{$FILENAME_REQUEST_PARAMETER?}{$FUNCTION_NAME_REQUEST_PARAMETER?}") { + get("/file-functions/{$FILE_NAME_REQUEST_PARAM?}{$FUNCTION_NAME_REQUEST_PARAM?}") { val fileName = call.processFileNameParameter(logger) ?: return@get logger.info("Server GET file functions request for file '$fileName' is called") @@ -30,7 +30,7 @@ fun Routing.getFileFunctions(logger: Logger, ideStateKeeper: IdeStateKeeper) { return@get } - when (val functionName = call.parameters[FUNCTION_NAME_REQUEST_PARAMETER]) { + when (val functionName = call.parameters[FUNCTION_NAME_REQUEST_PARAM]) { null -> { call.respondJson(fileFunctions.getFunctionsNames()!!) logger.info("Server GET file functions request for file '$fileName' is processed") From 284d346784e3025fc5a4a48ad4269e59d177f822 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Wed, 15 Nov 2023 10:37:23 +0100 Subject: [PATCH 08/81] add json descriptions of the class/ function code getting apis --- .../server/ideApiDescriptions.json | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json index ffebde0..936649e 100644 --- a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json +++ b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json @@ -69,6 +69,27 @@ ], "optional_parameters": [] }, + { + "name": "file_function_code", + "url": "/file-functions", + "description": "Gets a list of names of the requested file functions.", + "method": "GET", + "required_parameters": [ + { + "name": "fileName", + "type": "STRING", + "regex": "^[\\w,\\s-]+\\.[A-Za-z]{2, 3, 4, 5}$", + "description": "The name of the file in which the requested function code is located." + }, + { + "name": "functionName", + "type": "STRING", + "regex": "[A-Za-z]*", + "description": "The name of the function whose code should be returned." + } + ], + "optional_parameters": [] + }, { "name": "file_classes", "url": "/file-classes", @@ -84,6 +105,27 @@ ], "optional_parameters": [] }, + { + "name": "file_class_code", + "url": "/file-classes", + "description": "Gets a list of names of the requested file classes.", + "method": "GET", + "required_parameters": [ + { + "name": "fileName", + "type": "STRING", + "regex": "^[\\w,\\s-]+\\.[A-Za-z]{2, 3, 4, 5}$", + "description": "The name of the file in which the requested function code is located." + }, + { + "name": "className", + "type": "STRING", + "regex": "[A-Za-z]*", + "description": "The name of the class whose code should be returned." + } + ], + "optional_parameters": [] + }, { "name": "reverse_last_api_methods", "url": "/reverse-api-methods", From 98192876a8dc4f3895acf7ac6e59fa90b682b5dd Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Wed, 15 Nov 2023 10:38:09 +0100 Subject: [PATCH 09/81] some class/ function code getting requests refactoring --- .../requests/fileRelated/getFileClasses.kt | 26 +++++++++++-------- .../requests/fileRelated/getFileFunctions.kt | 1 + 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileClasses.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileClasses.kt index 6f996b8..2af4390 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileClasses.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileClasses.kt @@ -30,18 +30,22 @@ fun Routing.getFileClasses(logger: Logger, ideStateKeeper: IdeStateKeeper) { return@get } - val className = call.parameters[CLASS_NAME_REQUEST_PARAM] - if (className == null) { - call.respondJson(fileClasses.getClassesNames()!!) - logger.info("Server GET file classes request for the file '$fileName' is processed") - } else { - val classCode = try { - fileClasses.getClassCode(className) - } catch (e: Exception) { - e.message!! + when(val className = call.parameters[CLASS_NAME_REQUEST_PARAM]) { + null -> { + call.respondJson(fileClasses.getClassesNames()!!) + logger.info("Server GET file classes request for the file '$fileName' is processed") + } + + else -> { + val classCode = try { + fileClasses.getClassCode(className) + } catch (e: Exception) { + e.message!! + } + + call.respondJson(classCode) + logger.info("Server GET file class code request for the file '$fileName' and the class '$className' is processed") } - call.respondJson(classCode) - logger.info("Server GET file class code request for the file '$fileName' and the class '$className' is processed") } } } diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileFunctions.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileFunctions.kt index a927a7f..dd012be 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileFunctions.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileFunctions.kt @@ -35,6 +35,7 @@ fun Routing.getFileFunctions(logger: Logger, ideStateKeeper: IdeStateKeeper) { call.respondJson(fileFunctions.getFunctionsNames()!!) logger.info("Server GET file functions request for file '$fileName' is processed") } + else -> { val functionCode = try { fileFunctions.getFunctionCode(functionName) From 4fb85d3f6c26e43e5dfaee0002f99814332c27a3 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Thu, 16 Nov 2023 21:58:24 +0100 Subject: [PATCH 10/81] add Git4Idea to platform plugins list in gradle.properties --- ide-former-plugin/gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ide-former-plugin/gradle.properties b/ide-former-plugin/gradle.properties index 4e48a8e..3a9b0af 100644 --- a/ide-former-plugin/gradle.properties +++ b/ide-former-plugin/gradle.properties @@ -18,7 +18,7 @@ platformVersion = 2023.2.4 # Plugin Dependencies -> https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html # To find actual PythonCore version go to https://plugins.jetbrains.com/plugin/631-python/versions and check, which version is matched with platformVersion # Example: platformPlugins = com.intellij.java, com.jetbrains.php:203.4449.22 -platformPlugins = com.intellij.java, org.jetbrains.kotlin, PythonCore:232.10203.2 +platformPlugins = com.intellij.java, org.jetbrains.kotlin, PythonCore:232.10203.2, Git4Idea # Gradle Releases -> https://github.com/gradle/gradle/releases gradleVersion = 8.3 From f6a3a4c63b6930cdbc18f91e4b8fc0cd96731000 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Thu, 16 Nov 2023 21:58:56 +0100 Subject: [PATCH 11/81] add base version of the GitStatus api --- .../api/models/gitRelated/GitStatus.kt | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitStatus.kt diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitStatus.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitStatus.kt new file mode 100644 index 0000000..cc14fa5 --- /dev/null +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitStatus.kt @@ -0,0 +1,32 @@ +package org.jetbrains.research.ideFormerPlugin.api.models.gitRelated + +import com.intellij.openapi.actionSystem.DataContext +import com.intellij.openapi.project.Project +import git4idea.GitVcs +import git4idea.actions.GitPull +import git4idea.branch.GitBranchUtil +import git4idea.commands.* +import org.jetbrains.research.ideFormerPlugin.api.models.IdeApiMethod + +class GitStatus( + private val project: Project +) : IdeApiMethod { + private var status: String? = null + + override fun execute() { + val gitVcs = GitVcs.getInstance(project) + + val gitRoots = GitPull.getGitRoots(project, gitVcs) + if (gitRoots.isNullOrEmpty()) return + + val selectedRepo = GitBranchUtil.guessRepositoryForOperation(project, DataContext.EMPTY_CONTEXT) + val defaultRoot = selectedRepo?.root ?: gitRoots[0] + + val git = Git.getInstance() + val statusCommandResult: GitCommandResult = git.runCommand(GitLineHandler(project, defaultRoot, GitCommand.STATUS)) + status = statusCommandResult.outputAsJoinedString + } + + // TODO + fun getStatus(): String = status ?: "" +} \ No newline at end of file From 27d71b07f76dfde9434799773bc540d6cf66026f Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Thu, 16 Nov 2023 22:00:53 +0100 Subject: [PATCH 12/81] add getGitStatus server request --- .../ideFormerPlugin/server/Routing.kt | 14 +++++++++--- .../requests/gitRelated/getGitStatus.kt | 22 +++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitStatus.kt diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt index 8270c7f..68d22e9 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt @@ -9,6 +9,7 @@ import org.jetbrains.research.ideFormerPlugin.server.requests.fileRelated.getFil import org.jetbrains.research.ideFormerPlugin.server.requests.fileSystemRelated.getChangeDirectory import org.jetbrains.research.ideFormerPlugin.server.requests.fileSystemRelated.getListDirectoryContents import org.jetbrains.research.ideFormerPlugin.server.requests.fileSystemRelated.getProjectModules +import org.jetbrains.research.ideFormerPlugin.server.requests.gitRelated.getGitStatus import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper import org.slf4j.Logger @@ -18,14 +19,21 @@ fun Application.configureRouting(ideStateKeeper: IdeStateKeeper, logger: Logger) routing { getMainPage(logger) getIdeApiList(logger) - getProjectModules(logger, ideStateKeeper) + getReverseApiMethods(logger, ideStateKeeper) + postFinalAnswer(logger, ideStateKeeper) + + // file related getFileText(logger, ideStateKeeper) getFileFunctions(logger, ideStateKeeper) getFileClasses(logger, ideStateKeeper) + + // file system related + getProjectModules(logger, ideStateKeeper) getListDirectoryContents(logger, ideStateKeeper) getChangeDirectory(logger, ideStateKeeper) - getReverseApiMethods(logger, ideStateKeeper) - postFinalAnswer(logger, ideStateKeeper) + + // git related + getGitStatus(logger, ideStateKeeper) } } diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitStatus.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitStatus.kt new file mode 100644 index 0000000..3ceef0f --- /dev/null +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitStatus.kt @@ -0,0 +1,22 @@ +package org.jetbrains.research.ideFormerPlugin.server.requests.gitRelated + +import io.ktor.server.application.* +import io.ktor.server.routing.* +import org.jetbrains.research.ideFormerPlugin.api.models.gitRelated.GitStatus +import org.jetbrains.research.ideFormerPlugin.server.* +import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper +import org.slf4j.Logger + +fun Routing.getGitStatus(logger: Logger, ideStateKeeper: IdeStateKeeper) { + get("/git-status") { + logger.info("Server GET git status is called") + + val gitStatus = GitStatus(ideStateKeeper.userProject) + if (!executeAndRespondError(gitStatus, logger)) { + return@get + } + + call.respondJson(gitStatus.getStatus()) + logger.info("Server GET git status is processed") + } +} \ No newline at end of file From 0a0314e53ad4947014d2c582e057e4cac9f258ad Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Thu, 16 Nov 2023 23:13:21 +0100 Subject: [PATCH 13/81] move git repository and root getting from git status to ide state keeper --- .../api/models/gitRelated/GitStatus.kt | 20 ++++++------------- .../stateKeeper/IdeStateKeeper.kt | 19 ++++++++++++++++++ .../requests/gitRelated/getGitStatus.kt | 2 +- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitStatus.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitStatus.kt index cc14fa5..2f3293b 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitStatus.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitStatus.kt @@ -1,29 +1,21 @@ package org.jetbrains.research.ideFormerPlugin.api.models.gitRelated -import com.intellij.openapi.actionSystem.DataContext import com.intellij.openapi.project.Project -import git4idea.GitVcs -import git4idea.actions.GitPull -import git4idea.branch.GitBranchUtil +import com.intellij.openapi.vfs.VirtualFile import git4idea.commands.* import org.jetbrains.research.ideFormerPlugin.api.models.IdeApiMethod class GitStatus( - private val project: Project + private val project: Project, + private val projectGitRoot: VirtualFile ) : IdeApiMethod { private var status: String? = null override fun execute() { - val gitVcs = GitVcs.getInstance(project) - - val gitRoots = GitPull.getGitRoots(project, gitVcs) - if (gitRoots.isNullOrEmpty()) return - - val selectedRepo = GitBranchUtil.guessRepositoryForOperation(project, DataContext.EMPTY_CONTEXT) - val defaultRoot = selectedRepo?.root ?: gitRoots[0] - val git = Git.getInstance() - val statusCommandResult: GitCommandResult = git.runCommand(GitLineHandler(project, defaultRoot, GitCommand.STATUS)) + val statusCommandResult: GitCommandResult = git.runCommand( + GitLineHandler(project, projectGitRoot, GitCommand.STATUS) + ) status = statusCommandResult.outputAsJoinedString } diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/stateKeeper/IdeStateKeeper.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/stateKeeper/IdeStateKeeper.kt index 9a08146..e18b933 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/stateKeeper/IdeStateKeeper.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/stateKeeper/IdeStateKeeper.kt @@ -1,10 +1,16 @@ package org.jetbrains.research.ideFormerPlugin.stateKeeper +import com.intellij.openapi.actionSystem.DataContext import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.project.Project import com.intellij.openapi.project.guessProjectDir +import com.intellij.openapi.vfs.VirtualFile import com.intellij.psi.PsiDirectory import com.intellij.psi.PsiManager +import git4idea.GitVcs +import git4idea.actions.GitPull +import git4idea.branch.GitBranchUtil +import git4idea.repo.GitRepository import org.jetbrains.research.ideFormerPlugin.api.models.ReversibleApiMethod import java.util.* @@ -12,6 +18,19 @@ class IdeStateKeeper(val userProject: Project) { private val apiMethodStack: Stack = Stack() var currentProjectDirectory: PsiDirectory = findProjectBaseDirectory() + private val projectGitRepo: GitRepository + val projectGitRoot: VirtualFile + + init { + val gitVcs = GitVcs.getInstance(userProject) + val gitRoots = GitPull.getGitRoots(userProject, gitVcs) + if (gitRoots.isNullOrEmpty()) error("No git roots was found") + + projectGitRepo = GitBranchUtil.guessRepositoryForOperation(userProject, DataContext.EMPTY_CONTEXT) + ?: error("No git repository was found for user project") + projectGitRoot = projectGitRepo.root + } + // TODO: to think about null project path processing private fun findProjectBaseDirectory() = userProject.guessProjectDir()?.let { projectBaseDir -> ApplicationManager.getApplication().runReadAction { diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitStatus.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitStatus.kt index 3ceef0f..c17e760 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitStatus.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitStatus.kt @@ -11,7 +11,7 @@ fun Routing.getGitStatus(logger: Logger, ideStateKeeper: IdeStateKeeper) { get("/git-status") { logger.info("Server GET git status is called") - val gitStatus = GitStatus(ideStateKeeper.userProject) + val gitStatus = GitStatus(ideStateKeeper.userProject, ideStateKeeper.projectGitRoot) if (!executeAndRespondError(gitStatus, logger)) { return@get } From a2f70f253e3e37371208e845c0e3c8e015e5a5c0 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 17 Nov 2023 09:57:15 +0100 Subject: [PATCH 14/81] add uncalled execute() error to file related apis --- .../api/models/fileRelated/FileText.kt | 6 +++++- .../fileRelated/fileClasses/JavaFileClasses.kt | 16 +++++++++------- .../fileRelated/fileClasses/KtFileClasses.kt | 15 +++++++++------ .../fileRelated/fileClasses/PyFileClasses.kt | 15 +++++++++------ .../fileFunctions/JavaFileFunctions.kt | 15 +++++++++------ .../fileRelated/fileFunctions/KtFileFunctions.kt | 15 +++++++++------ .../fileRelated/fileFunctions/PyFileFunctions.kt | 15 +++++++++------ .../ideFormerPlugin/api/models/utils/ApiUtils.kt | 2 ++ 8 files changed, 61 insertions(+), 38 deletions(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/FileText.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/FileText.kt index 4257614..693f88f 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/FileText.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/FileText.kt @@ -2,6 +2,7 @@ package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated import com.intellij.psi.PsiDirectory import org.jetbrains.research.ideFormerPlugin.api.models.IdeApiMethod +import org.jetbrains.research.ideFormerPlugin.api.models.utils.UNCALLED_EXECUTE_BEFORE_RESULT_GETTING import org.jetbrains.research.ideFormerPlugin.api.models.utils.findFileRecursively class FileText( @@ -15,5 +16,8 @@ class FileText( fileText = psiFile.text } - fun getFileText(): String? = fileText + fun getFileText(): String { + if (fileText == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) + return fileText!! + } } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/JavaFileClasses.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/JavaFileClasses.kt index 6641796..d1eedba 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/JavaFileClasses.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/JavaFileClasses.kt @@ -2,8 +2,7 @@ package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileClasse import com.intellij.psi.PsiClass import com.intellij.psi.PsiDirectory -import org.jetbrains.research.ideFormerPlugin.api.models.utils.NO_SUCH_FILE_CLASS -import org.jetbrains.research.ideFormerPlugin.api.models.utils.getFilePsiElementsOfType +import org.jetbrains.research.ideFormerPlugin.api.models.utils.* class JavaFileClasses( private val projectDirectory: PsiDirectory, @@ -18,9 +17,12 @@ class JavaFileClasses( override fun getClassesNames(): List? = javaClasses?.mapNotNull { it.name } - // TODO: to think about the case when javaClasses is not initialized yet - override fun getClassCode(className: String): String = javaClasses - ?.firstOrNull { it.name == className } - ?.text - ?: error(NO_SUCH_FILE_CLASS) + override fun getClassCode(className: String): String { + if (javaClasses == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) + + return javaClasses!! + .firstOrNull { it.name == className } + ?.text + ?: error(NO_SUCH_FILE_CLASS) + } } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/KtFileClasses.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/KtFileClasses.kt index 281a87d..c0028be 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/KtFileClasses.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/KtFileClasses.kt @@ -2,8 +2,7 @@ package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileClasse import com.intellij.psi.PsiDirectory import org.jetbrains.kotlin.psi.KtClass -import org.jetbrains.research.ideFormerPlugin.api.models.utils.NO_SUCH_FILE_CLASS -import org.jetbrains.research.ideFormerPlugin.api.models.utils.getFilePsiElementsOfType +import org.jetbrains.research.ideFormerPlugin.api.models.utils.* class KtFileClasses( private val projectDirectory: PsiDirectory, @@ -17,8 +16,12 @@ class KtFileClasses( override fun getClassesNames(): List? = ktClasses?.mapNotNull { it.name } - override fun getClassCode(className: String): String = ktClasses - ?.firstOrNull { it.name == className } - ?.text - ?: error(NO_SUCH_FILE_CLASS) + override fun getClassCode(className: String): String { + if (ktClasses == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) + + return ktClasses!! + .firstOrNull { it.name == className } + ?.text + ?: error(NO_SUCH_FILE_CLASS) + } } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/PyFileClasses.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/PyFileClasses.kt index 8c981eb..1fe34b2 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/PyFileClasses.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/PyFileClasses.kt @@ -2,8 +2,7 @@ package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileClasse import com.intellij.psi.PsiDirectory import com.jetbrains.python.psi.PyClass -import org.jetbrains.research.ideFormerPlugin.api.models.utils.NO_SUCH_FILE_CLASS -import org.jetbrains.research.ideFormerPlugin.api.models.utils.getFilePsiElementsOfType +import org.jetbrains.research.ideFormerPlugin.api.models.utils.* class PyFileClasses( private val projectDirectory: PsiDirectory, @@ -17,8 +16,12 @@ class PyFileClasses( override fun getClassesNames(): List? = pyClasses?.mapNotNull { it.name } - override fun getClassCode(className: String): String = pyClasses - ?.firstOrNull { it.name == className } - ?.text - ?: error(NO_SUCH_FILE_CLASS) + override fun getClassCode(className: String): String { + if (pyClasses == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) + + return pyClasses!! + .firstOrNull { it.name == className } + ?.text + ?: error(NO_SUCH_FILE_CLASS) + } } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/JavaFileFunctions.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/JavaFileFunctions.kt index d8b3d99..d8253c2 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/JavaFileFunctions.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/JavaFileFunctions.kt @@ -2,8 +2,7 @@ package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileFuncti import com.intellij.psi.PsiDirectory import com.intellij.psi.PsiMethod -import org.jetbrains.research.ideFormerPlugin.api.models.utils.NO_SUCH_FILE_FUNCTION -import org.jetbrains.research.ideFormerPlugin.api.models.utils.getFilePsiElementsOfType +import org.jetbrains.research.ideFormerPlugin.api.models.utils.* class JavaFileFunctions( private val projectDirectory: PsiDirectory, @@ -17,8 +16,12 @@ class JavaFileFunctions( override fun getFunctionsNames(): List? = javaFunctions?.map { it.name } - override fun getFunctionCode(functionName: String): String = javaFunctions - ?.firstOrNull { it.name == functionName } - ?.text - ?: error(NO_SUCH_FILE_FUNCTION) + override fun getFunctionCode(functionName: String): String { + if (javaFunctions == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) + + return javaFunctions!! + .firstOrNull { it.name == functionName } + ?.text + ?: error(NO_SUCH_FILE_FUNCTION) + } } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/KtFileFunctions.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/KtFileFunctions.kt index 5f223ff..43e4d30 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/KtFileFunctions.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/KtFileFunctions.kt @@ -2,8 +2,7 @@ package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileFuncti import com.intellij.psi.PsiDirectory import org.jetbrains.kotlin.psi.KtFunction -import org.jetbrains.research.ideFormerPlugin.api.models.utils.NO_SUCH_FILE_FUNCTION -import org.jetbrains.research.ideFormerPlugin.api.models.utils.getFilePsiElementsOfType +import org.jetbrains.research.ideFormerPlugin.api.models.utils.* class KtFileFunctions( private val projectDirectory: PsiDirectory, @@ -17,8 +16,12 @@ class KtFileFunctions( override fun getFunctionsNames(): List? = ktFunctions?.mapNotNull { it.name } - override fun getFunctionCode(functionName: String): String = ktFunctions - ?.firstOrNull { it.name == functionName } - ?.text - ?: error(NO_SUCH_FILE_FUNCTION) + override fun getFunctionCode(functionName: String): String { + if (ktFunctions == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) + + return ktFunctions!! + .firstOrNull { it.name == functionName } + ?.text + ?: error(NO_SUCH_FILE_FUNCTION) + } } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/PyFileFunctions.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/PyFileFunctions.kt index 94c8779..6c6582b 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/PyFileFunctions.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/PyFileFunctions.kt @@ -2,8 +2,7 @@ package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileFuncti import com.intellij.psi.PsiDirectory import com.jetbrains.python.psi.PyFunction -import org.jetbrains.research.ideFormerPlugin.api.models.utils.NO_SUCH_FILE_FUNCTION -import org.jetbrains.research.ideFormerPlugin.api.models.utils.getFilePsiElementsOfType +import org.jetbrains.research.ideFormerPlugin.api.models.utils.* class PyFileFunctions( private val projectDirectory: PsiDirectory, @@ -17,8 +16,12 @@ class PyFileFunctions( override fun getFunctionsNames(): List? = pyFunctions?.mapNotNull { it.name } - override fun getFunctionCode(functionName: String): String = pyFunctions - ?.firstOrNull { it.name == functionName } - ?.text - ?: error(NO_SUCH_FILE_FUNCTION) + override fun getFunctionCode(functionName: String): String { + if (pyFunctions == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) + + return pyFunctions!! + .firstOrNull { it.name == functionName } + ?.text + ?: error(NO_SUCH_FILE_FUNCTION) + } } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/ApiUtils.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/ApiUtils.kt index 15afe19..2554626 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/ApiUtils.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/ApiUtils.kt @@ -7,6 +7,8 @@ import org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileFunctio const val DEFAULT_DIRECTORY_NAME = "." +const val UNCALLED_EXECUTE_BEFORE_RESULT_GETTING = + "Execute() must be called before getting the result. The property storing the result has not been initialized yet" const val UNSUPPORTED_FILE_EXTENSION_ERROR = "Unsupported file extension" const val NO_SUCH_FILE_CLASS = "No such class in a file" const val NO_SUCH_FILE_FUNCTION = "No such function in a file" From b18bc5e4810b4bdad901cebff6f9d6b2473587f4 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 17 Nov 2023 10:04:37 +0100 Subject: [PATCH 15/81] add uncalled execute() error to file system related apis --- .../api/models/fileSystemRelated/ChangeDirectory.kt | 7 ++++--- .../api/models/fileSystemRelated/ListDirectoryContents.kt | 8 +++++--- .../api/models/fileSystemRelated/ProjectModules.kt | 6 +++++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ChangeDirectory.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ChangeDirectory.kt index 84390db..e852428 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ChangeDirectory.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ChangeDirectory.kt @@ -1,9 +1,8 @@ package org.jetbrains.research.ideFormerPlugin.api.models.fileSystemRelated import com.intellij.psi.PsiDirectory -import org.jetbrains.research.ideFormerPlugin.api.models.utils.DEFAULT_DIRECTORY_NAME -import org.jetbrains.research.ideFormerPlugin.api.models.utils.findSubdirectoryRecursively import org.jetbrains.research.ideFormerPlugin.api.models.ReversibleApiMethod +import org.jetbrains.research.ideFormerPlugin.api.models.utils.* import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper class ChangeDirectory( @@ -20,7 +19,9 @@ class ChangeDirectory( } override fun reverse() { - prevDir?.let { + if (prevDir == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) + + prevDir!!.let { ideStateKeeper.currentProjectDirectory = it prevDir = null } diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ListDirectoryContents.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ListDirectoryContents.kt index 9042738..e245f5e 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ListDirectoryContents.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ListDirectoryContents.kt @@ -2,9 +2,8 @@ package org.jetbrains.research.ideFormerPlugin.api.models.fileSystemRelated import com.intellij.psi.PsiDirectory import com.intellij.psi.PsiFileSystemItem -import org.jetbrains.research.ideFormerPlugin.api.models.utils.DEFAULT_DIRECTORY_NAME -import org.jetbrains.research.ideFormerPlugin.api.models.utils.findSubdirectoryRecursively import org.jetbrains.research.ideFormerPlugin.api.models.IdeApiMethod +import org.jetbrains.research.ideFormerPlugin.api.models.utils.* class ListDirectoryContents( private val currentProjectDirectory: PsiDirectory, @@ -23,5 +22,8 @@ class ListDirectoryContents( searchDirectoryItems = searchDirectory.fileSystemItems() } - fun getSearchDirectoryItemsNames() = searchDirectoryItems?.map { it.name } + fun getSearchDirectoryItemsNames(): List { + if (searchDirectoryItems == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) + return searchDirectoryItems!!.map { it.name } + } } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ProjectModules.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ProjectModules.kt index f26e1ab..2bf4a90 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ProjectModules.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ProjectModules.kt @@ -4,6 +4,7 @@ import com.intellij.openapi.module.Module import com.intellij.openapi.module.ModuleManager import com.intellij.openapi.project.Project import org.jetbrains.research.ideFormerPlugin.api.models.IdeApiMethod +import org.jetbrains.research.ideFormerPlugin.api.models.utils.UNCALLED_EXECUTE_BEFORE_RESULT_GETTING class ProjectModules(private val project: Project) : IdeApiMethod { private var projectModules: List? = null @@ -13,5 +14,8 @@ class ProjectModules(private val project: Project) : IdeApiMethod { projectModules = project.modules() } - fun getProjectModulesNames(): List? = projectModules?.map { it.name }?.toList() + fun getProjectModulesNames(): List { + if (projectModules == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) + return projectModules!!.map { it.name }.toList() + } } From e7b2c66fe2322c7aceca78692279f3d8784fe21f Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 17 Nov 2023 10:05:52 +0100 Subject: [PATCH 16/81] add uncalled execute() error to git status api --- .../ideFormerPlugin/api/models/gitRelated/GitStatus.kt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitStatus.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitStatus.kt index 2f3293b..d5dd78f 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitStatus.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitStatus.kt @@ -4,6 +4,7 @@ import com.intellij.openapi.project.Project import com.intellij.openapi.vfs.VirtualFile import git4idea.commands.* import org.jetbrains.research.ideFormerPlugin.api.models.IdeApiMethod +import org.jetbrains.research.ideFormerPlugin.api.models.utils.UNCALLED_EXECUTE_BEFORE_RESULT_GETTING class GitStatus( private val project: Project, @@ -19,6 +20,8 @@ class GitStatus( status = statusCommandResult.outputAsJoinedString } - // TODO - fun getStatus(): String = status ?: "" + fun getStatus(): String { + if (status == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) + return status!! + } } \ No newline at end of file From 9005936ed18b44737123e0309ea28a7380af343f Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 17 Nov 2023 10:13:03 +0100 Subject: [PATCH 17/81] add uncalled execute() error to other git related apis methods --- .../api/models/fileRelated/fileClasses/FileClasses.kt | 2 +- .../api/models/fileRelated/fileClasses/JavaFileClasses.kt | 7 ++++--- .../api/models/fileRelated/fileClasses/KtFileClasses.kt | 5 ++++- .../api/models/fileRelated/fileClasses/PyFileClasses.kt | 5 ++++- .../api/models/fileRelated/fileFunctions/FileFunctions.kt | 2 +- .../models/fileRelated/fileFunctions/JavaFileFunctions.kt | 6 ++++-- .../models/fileRelated/fileFunctions/KtFileFunctions.kt | 6 ++++-- .../models/fileRelated/fileFunctions/PyFileFunctions.kt | 6 ++++-- 8 files changed, 26 insertions(+), 13 deletions(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/FileClasses.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/FileClasses.kt index f18c31b..543f845 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/FileClasses.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/FileClasses.kt @@ -3,6 +3,6 @@ package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileClasse import org.jetbrains.research.ideFormerPlugin.api.models.IdeApiMethod interface FileClasses : IdeApiMethod { - fun getClassesNames(): List? + fun getClassesNames(): List fun getClassCode(className: String): String } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/JavaFileClasses.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/JavaFileClasses.kt index d1eedba..49d54d2 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/JavaFileClasses.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/JavaFileClasses.kt @@ -14,12 +14,13 @@ class JavaFileClasses( javaClasses = getFilePsiElementsOfType(projectDirectory, javaFileName) } - override fun getClassesNames(): List? = - javaClasses?.mapNotNull { it.name } + override fun getClassesNames(): List { + if (javaClasses == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) + return javaClasses!!.mapNotNull { it.name } + } override fun getClassCode(className: String): String { if (javaClasses == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) - return javaClasses!! .firstOrNull { it.name == className } ?.text diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/KtFileClasses.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/KtFileClasses.kt index c0028be..35b0f24 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/KtFileClasses.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/KtFileClasses.kt @@ -14,7 +14,10 @@ class KtFileClasses( ktClasses = getFilePsiElementsOfType(projectDirectory, ktFileName) } - override fun getClassesNames(): List? = ktClasses?.mapNotNull { it.name } + override fun getClassesNames(): List { + if (ktClasses == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) + return ktClasses!!.mapNotNull { it.name } + } override fun getClassCode(className: String): String { if (ktClasses == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/PyFileClasses.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/PyFileClasses.kt index 1fe34b2..3c0223a 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/PyFileClasses.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/PyFileClasses.kt @@ -14,7 +14,10 @@ class PyFileClasses( pyClasses = getFilePsiElementsOfType(projectDirectory, pyFileName) } - override fun getClassesNames(): List? = pyClasses?.mapNotNull { it.name } + override fun getClassesNames(): List { + if (pyClasses == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) + return pyClasses!!.mapNotNull { it.name } + } override fun getClassCode(className: String): String { if (pyClasses == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/FileFunctions.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/FileFunctions.kt index 8d5e71b..eed7b04 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/FileFunctions.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/FileFunctions.kt @@ -3,6 +3,6 @@ package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileFuncti import org.jetbrains.research.ideFormerPlugin.api.models.IdeApiMethod interface FileFunctions : IdeApiMethod { - fun getFunctionsNames(): List? + fun getFunctionsNames(): List fun getFunctionCode(functionName: String): String } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/JavaFileFunctions.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/JavaFileFunctions.kt index d8253c2..da48764 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/JavaFileFunctions.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/JavaFileFunctions.kt @@ -14,11 +14,13 @@ class JavaFileFunctions( javaFunctions = getFilePsiElementsOfType(projectDirectory, javaFileName) } - override fun getFunctionsNames(): List? = javaFunctions?.map { it.name } + override fun getFunctionsNames(): List { + if (javaFunctions == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) + return javaFunctions!!.map { it.name } + } override fun getFunctionCode(functionName: String): String { if (javaFunctions == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) - return javaFunctions!! .firstOrNull { it.name == functionName } ?.text diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/KtFileFunctions.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/KtFileFunctions.kt index 43e4d30..3206213 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/KtFileFunctions.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/KtFileFunctions.kt @@ -14,11 +14,13 @@ class KtFileFunctions( ktFunctions = getFilePsiElementsOfType(projectDirectory, ktFileName) } - override fun getFunctionsNames(): List? = ktFunctions?.mapNotNull { it.name } + override fun getFunctionsNames(): List { + if (ktFunctions == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) + return ktFunctions!!.mapNotNull { it.name } + } override fun getFunctionCode(functionName: String): String { if (ktFunctions == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) - return ktFunctions!! .firstOrNull { it.name == functionName } ?.text diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/PyFileFunctions.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/PyFileFunctions.kt index 6c6582b..bcc2870 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/PyFileFunctions.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/PyFileFunctions.kt @@ -14,11 +14,13 @@ class PyFileFunctions( pyFunctions = getFilePsiElementsOfType(projectDirectory, pyFileName) } - override fun getFunctionsNames(): List? = pyFunctions?.mapNotNull { it.name } + override fun getFunctionsNames(): List { + if (pyFunctions == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) + return pyFunctions!!.mapNotNull { it.name } + } override fun getFunctionCode(functionName: String): String { if (pyFunctions == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) - return pyFunctions!! .firstOrNull { it.name == functionName } ?.text From 5ccc91f802ab61b9688144020d4adcd9af7878d7 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 17 Nov 2023 10:24:41 +0100 Subject: [PATCH 18/81] replace error throwing with null returning for class/ func code getting apis --- .../api/models/fileRelated/fileClasses/FileClasses.kt | 2 +- .../api/models/fileRelated/fileClasses/JavaFileClasses.kt | 3 +-- .../api/models/fileRelated/fileClasses/KtFileClasses.kt | 7 +++---- .../api/models/fileRelated/fileClasses/PyFileClasses.kt | 7 +++---- .../api/models/fileRelated/fileFunctions/FileFunctions.kt | 2 +- .../models/fileRelated/fileFunctions/JavaFileFunctions.kt | 6 +++--- .../models/fileRelated/fileFunctions/KtFileFunctions.kt | 6 +++--- .../models/fileRelated/fileFunctions/PyFileFunctions.kt | 6 +++--- 8 files changed, 18 insertions(+), 21 deletions(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/FileClasses.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/FileClasses.kt index 543f845..9f8e11c 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/FileClasses.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/FileClasses.kt @@ -4,5 +4,5 @@ import org.jetbrains.research.ideFormerPlugin.api.models.IdeApiMethod interface FileClasses : IdeApiMethod { fun getClassesNames(): List - fun getClassCode(className: String): String + fun getClassCode(className: String): String? } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/JavaFileClasses.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/JavaFileClasses.kt index 49d54d2..b458b25 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/JavaFileClasses.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/JavaFileClasses.kt @@ -19,11 +19,10 @@ class JavaFileClasses( return javaClasses!!.mapNotNull { it.name } } - override fun getClassCode(className: String): String { + override fun getClassCode(className: String): String? { if (javaClasses == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) return javaClasses!! .firstOrNull { it.name == className } ?.text - ?: error(NO_SUCH_FILE_CLASS) } } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/KtFileClasses.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/KtFileClasses.kt index 35b0f24..e2ab792 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/KtFileClasses.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/KtFileClasses.kt @@ -2,7 +2,8 @@ package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileClasse import com.intellij.psi.PsiDirectory import org.jetbrains.kotlin.psi.KtClass -import org.jetbrains.research.ideFormerPlugin.api.models.utils.* +import org.jetbrains.research.ideFormerPlugin.api.models.utils.UNCALLED_EXECUTE_BEFORE_RESULT_GETTING +import org.jetbrains.research.ideFormerPlugin.api.models.utils.getFilePsiElementsOfType class KtFileClasses( private val projectDirectory: PsiDirectory, @@ -19,12 +20,10 @@ class KtFileClasses( return ktClasses!!.mapNotNull { it.name } } - override fun getClassCode(className: String): String { + override fun getClassCode(className: String): String? { if (ktClasses == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) - return ktClasses!! .firstOrNull { it.name == className } ?.text - ?: error(NO_SUCH_FILE_CLASS) } } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/PyFileClasses.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/PyFileClasses.kt index 3c0223a..1eff846 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/PyFileClasses.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileClasses/PyFileClasses.kt @@ -2,7 +2,8 @@ package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileClasse import com.intellij.psi.PsiDirectory import com.jetbrains.python.psi.PyClass -import org.jetbrains.research.ideFormerPlugin.api.models.utils.* +import org.jetbrains.research.ideFormerPlugin.api.models.utils.UNCALLED_EXECUTE_BEFORE_RESULT_GETTING +import org.jetbrains.research.ideFormerPlugin.api.models.utils.getFilePsiElementsOfType class PyFileClasses( private val projectDirectory: PsiDirectory, @@ -19,12 +20,10 @@ class PyFileClasses( return pyClasses!!.mapNotNull { it.name } } - override fun getClassCode(className: String): String { + override fun getClassCode(className: String): String? { if (pyClasses == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) - return pyClasses!! .firstOrNull { it.name == className } ?.text - ?: error(NO_SUCH_FILE_CLASS) } } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/FileFunctions.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/FileFunctions.kt index eed7b04..1c75883 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/FileFunctions.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/FileFunctions.kt @@ -4,5 +4,5 @@ import org.jetbrains.research.ideFormerPlugin.api.models.IdeApiMethod interface FileFunctions : IdeApiMethod { fun getFunctionsNames(): List - fun getFunctionCode(functionName: String): String + fun getFunctionCode(functionName: String): String? } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/JavaFileFunctions.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/JavaFileFunctions.kt index da48764..358a2ba 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/JavaFileFunctions.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/JavaFileFunctions.kt @@ -2,7 +2,8 @@ package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileFuncti import com.intellij.psi.PsiDirectory import com.intellij.psi.PsiMethod -import org.jetbrains.research.ideFormerPlugin.api.models.utils.* +import org.jetbrains.research.ideFormerPlugin.api.models.utils.UNCALLED_EXECUTE_BEFORE_RESULT_GETTING +import org.jetbrains.research.ideFormerPlugin.api.models.utils.getFilePsiElementsOfType class JavaFileFunctions( private val projectDirectory: PsiDirectory, @@ -19,11 +20,10 @@ class JavaFileFunctions( return javaFunctions!!.map { it.name } } - override fun getFunctionCode(functionName: String): String { + override fun getFunctionCode(functionName: String): String? { if (javaFunctions == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) return javaFunctions!! .firstOrNull { it.name == functionName } ?.text - ?: error(NO_SUCH_FILE_FUNCTION) } } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/KtFileFunctions.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/KtFileFunctions.kt index 3206213..3edd789 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/KtFileFunctions.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/KtFileFunctions.kt @@ -2,7 +2,8 @@ package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileFuncti import com.intellij.psi.PsiDirectory import org.jetbrains.kotlin.psi.KtFunction -import org.jetbrains.research.ideFormerPlugin.api.models.utils.* +import org.jetbrains.research.ideFormerPlugin.api.models.utils.UNCALLED_EXECUTE_BEFORE_RESULT_GETTING +import org.jetbrains.research.ideFormerPlugin.api.models.utils.getFilePsiElementsOfType class KtFileFunctions( private val projectDirectory: PsiDirectory, @@ -19,11 +20,10 @@ class KtFileFunctions( return ktFunctions!!.mapNotNull { it.name } } - override fun getFunctionCode(functionName: String): String { + override fun getFunctionCode(functionName: String): String? { if (ktFunctions == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) return ktFunctions!! .firstOrNull { it.name == functionName } ?.text - ?: error(NO_SUCH_FILE_FUNCTION) } } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/PyFileFunctions.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/PyFileFunctions.kt index bcc2870..f896081 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/PyFileFunctions.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileFunctions/PyFileFunctions.kt @@ -2,7 +2,8 @@ package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileFuncti import com.intellij.psi.PsiDirectory import com.jetbrains.python.psi.PyFunction -import org.jetbrains.research.ideFormerPlugin.api.models.utils.* +import org.jetbrains.research.ideFormerPlugin.api.models.utils.UNCALLED_EXECUTE_BEFORE_RESULT_GETTING +import org.jetbrains.research.ideFormerPlugin.api.models.utils.getFilePsiElementsOfType class PyFileFunctions( private val projectDirectory: PsiDirectory, @@ -19,11 +20,10 @@ class PyFileFunctions( return pyFunctions!!.mapNotNull { it.name } } - override fun getFunctionCode(functionName: String): String { + override fun getFunctionCode(functionName: String): String? { if (pyFunctions == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) return pyFunctions!! .firstOrNull { it.name == functionName } ?.text - ?: error(NO_SUCH_FILE_FUNCTION) } } \ No newline at end of file From 5ce6e11d03fe8777f1671e009f62cad18e66e5f8 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 17 Nov 2023 10:36:41 +0100 Subject: [PATCH 19/81] update server requests due to apis execution result getters updates --- .../api/models/utils/ApiUtils.kt | 4 +--- .../ideFormerPlugin/server/Routing.kt | 4 ++-- .../requests/fileRelated/getFileClasses.kt | 19 ++++++------------- .../requests/fileRelated/getFileFunctions.kt | 17 +++++------------ .../requests/fileRelated/getFileText.kt | 6 ++---- .../fileSystemRelated/getChangeDirectory.kt | 4 +--- .../getListDirectoryContents.kt | 6 ++---- .../fileSystemRelated/getProjectModules.kt | 6 ++---- 8 files changed, 21 insertions(+), 45 deletions(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/ApiUtils.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/ApiUtils.kt index 2554626..3d4e5f6 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/ApiUtils.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/ApiUtils.kt @@ -9,9 +9,7 @@ const val DEFAULT_DIRECTORY_NAME = "." const val UNCALLED_EXECUTE_BEFORE_RESULT_GETTING = "Execute() must be called before getting the result. The property storing the result has not been initialized yet" -const val UNSUPPORTED_FILE_EXTENSION_ERROR = "Unsupported file extension" -const val NO_SUCH_FILE_CLASS = "No such class in a file" -const val NO_SUCH_FILE_FUNCTION = "No such function in a file" +const val UNSUPPORTED_FILE_EXTENSION_ERROR = "Unsupported file extension: only .py, .kt, .java file can be processed" enum class FileExtensions(val extension: String) { KT("kt"), diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt index 68d22e9..ec95e12 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt @@ -46,8 +46,8 @@ object IdeServerConstants { const val NEGATIVE_API_METHODS_CNT = "apiMethodsCount parameter should be a positive integer" const val NOT_A_NUMBER_API_METHODS_CNT = "apiMethodsCount parameter should be a number" const val API_EXECUTION_UNKNOWN_ERROR = "Unknown error while api method execution" - const val INCORRECT_REQUESTED_FILE_EXTENSION = - "Incorrect requested file extension: only .py, .kt, .java file can be processed" + const val NO_SUCH_FILE_CLASS = "No such class in a file" + const val NO_SUCH_FILE_FUNCTION = "No such function in a file" // TODO: renaming const val FILE_NAME_REQUEST_PARAM = "fileName" diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileClasses.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileClasses.kt index 2af4390..4302359 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileClasses.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileClasses.kt @@ -7,6 +7,7 @@ import org.jetbrains.research.ideFormerPlugin.api.models.utils.chooseFileClasses import org.jetbrains.research.ideFormerPlugin.server.* import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.CLASS_NAME_REQUEST_PARAM import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.FILE_NAME_REQUEST_PARAM +import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.NO_SUCH_FILE_CLASS import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper import org.slf4j.Logger @@ -20,29 +21,21 @@ fun Routing.getFileClasses(logger: Logger, ideStateKeeper: IdeStateKeeper) { } catch (e: Exception) { logger.error(e.message) return@get call.respondJson( - // TODO: mb this is redundant bc there is the thrown error has its own message - IdeServerConstants.INCORRECT_REQUESTED_FILE_EXTENSION, + e.message!!, HttpStatusCode.BadRequest ) } - if (!executeAndRespondError(fileClasses, logger)) { - return@get - } + if (!executeAndRespondError(fileClasses, logger)) return@get - when(val className = call.parameters[CLASS_NAME_REQUEST_PARAM]) { + when (val className = call.parameters[CLASS_NAME_REQUEST_PARAM]) { null -> { - call.respondJson(fileClasses.getClassesNames()!!) + call.respondJson(fileClasses.getClassesNames()) logger.info("Server GET file classes request for the file '$fileName' is processed") } else -> { - val classCode = try { - fileClasses.getClassCode(className) - } catch (e: Exception) { - e.message!! - } - + val classCode = fileClasses.getClassCode(className) ?: NO_SUCH_FILE_CLASS call.respondJson(classCode) logger.info("Server GET file class code request for the file '$fileName' and the class '$className' is processed") } diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileFunctions.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileFunctions.kt index dd012be..a07c018 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileFunctions.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileFunctions.kt @@ -7,6 +7,7 @@ import org.jetbrains.research.ideFormerPlugin.api.models.utils.chooseFileFunctio import org.jetbrains.research.ideFormerPlugin.server.* import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.FILE_NAME_REQUEST_PARAM import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.FUNCTION_NAME_REQUEST_PARAM +import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.NO_SUCH_FILE_FUNCTION import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper import org.slf4j.Logger @@ -20,29 +21,21 @@ fun Routing.getFileFunctions(logger: Logger, ideStateKeeper: IdeStateKeeper) { } catch (e: Exception) { logger.error(e.message) return@get call.respondJson( - // TODO: mb this is redundant bc there is the thrown error has its own message - IdeServerConstants.INCORRECT_REQUESTED_FILE_EXTENSION, + e.message!!, HttpStatusCode.BadRequest ) } - if (!executeAndRespondError(fileFunctions, logger)) { - return@get - } + if (!executeAndRespondError(fileFunctions, logger)) return@get when (val functionName = call.parameters[FUNCTION_NAME_REQUEST_PARAM]) { null -> { - call.respondJson(fileFunctions.getFunctionsNames()!!) + call.respondJson(fileFunctions.getFunctionsNames()) logger.info("Server GET file functions request for file '$fileName' is processed") } else -> { - val functionCode = try { - fileFunctions.getFunctionCode(functionName) - } catch (e: Exception) { - e.message!! - } - + val functionCode = fileFunctions.getFunctionCode(functionName) ?: NO_SUCH_FILE_FUNCTION call.respondJson(functionCode) logger.info("Server GET file function code request for file '$fileName' and function '$functionName' is processed") } diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileText.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileText.kt index 6f0fdad..62b2638 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileText.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileText.kt @@ -13,11 +13,9 @@ fun Routing.getFileText(logger: Logger, ideStateKeeper: IdeStateKeeper) { logger.info("Server GET file text request for file '$fileName' is called") val fileText = FileText(ideStateKeeper.currentProjectDirectory, fileName) - if (!executeAndRespondError(fileText, logger)) { - return@get - } + if (!executeAndRespondError(fileText, logger)) return@get - call.respondJson(fileText.getFileText()!!) + call.respondJson(fileText.getFileText()) logger.info("Server GET file text request for file '$fileName' is processed") } } \ No newline at end of file diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getChangeDirectory.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getChangeDirectory.kt index d933aee..27de091 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getChangeDirectory.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getChangeDirectory.kt @@ -19,9 +19,7 @@ fun Routing.getChangeDirectory(logger: Logger, ideStateKeeper: IdeStateKeeper) { ChangeDirectory(ideStateKeeper, it) } ?: ChangeDirectory(ideStateKeeper) - if (!executeAndRespondError(changeDirectory, logger)) { - return@get - } + if (!executeAndRespondError(changeDirectory, logger)) return@get ideStateKeeper.saveReversibleApiMethod(changeDirectory) logger.info("Change directory api method was saved on the api calls stack") diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getListDirectoryContents.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getListDirectoryContents.kt index 55b9923..c4e7ba7 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getListDirectoryContents.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getListDirectoryContents.kt @@ -17,11 +17,9 @@ fun Routing.getListDirectoryContents(logger: Logger, ideStateKeeper: IdeStateKee ListDirectoryContents(ideStateKeeper.currentProjectDirectory, it) } ?: ListDirectoryContents(ideStateKeeper.currentProjectDirectory) - if (!executeAndRespondError(listDirectoryContents, logger)) { - return@get - } + if (!executeAndRespondError(listDirectoryContents, logger)) return@get - call.respondJson(listDirectoryContents.getSearchDirectoryItemsNames()!!) + call.respondJson(listDirectoryContents.getSearchDirectoryItemsNames()) logger.info("Server GET ls request for dir '$dirName' is processed") } } \ No newline at end of file diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getProjectModules.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getProjectModules.kt index fb2825c..0a86ae5 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getProjectModules.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getProjectModules.kt @@ -13,11 +13,9 @@ fun Routing.getProjectModules(logger: Logger, ideStateKeeper: IdeStateKeeper) { logger.info("Server GET project modules request is called") val projectModules = ProjectModules(ideStateKeeper.userProject) - if (!executeAndRespondError(projectModules, logger)) { - return@get - } + if (!executeAndRespondError(projectModules, logger)) return@get - call.respondJson(projectModules.getProjectModulesNames()!!) + call.respondJson(projectModules.getProjectModulesNames()) logger.info("Server GET project modules request is processed") } } \ No newline at end of file From 82cc20414d04759f9166e23d3e4d497648b50e0a Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 17 Nov 2023 10:44:04 +0100 Subject: [PATCH 20/81] update constants in the ide state keeper --- .../ideFormerPlugin/api/models/utils/ApiUtils.kt | 2 ++ .../ideFormerPlugin/stateKeeper/IdeStateKeeper.kt | 14 +++++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/ApiUtils.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/ApiUtils.kt index 3d4e5f6..d176958 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/ApiUtils.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/ApiUtils.kt @@ -10,6 +10,8 @@ const val DEFAULT_DIRECTORY_NAME = "." const val UNCALLED_EXECUTE_BEFORE_RESULT_GETTING = "Execute() must be called before getting the result. The property storing the result has not been initialized yet" const val UNSUPPORTED_FILE_EXTENSION_ERROR = "Unsupported file extension: only .py, .kt, .java file can be processed" +const val NO_GIT_ROOTS = "No git roots was found" +const val NO_GIT_REPO_FOR_PROJECT = "No git repository was found for the project" enum class FileExtensions(val extension: String) { KT("kt"), diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/stateKeeper/IdeStateKeeper.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/stateKeeper/IdeStateKeeper.kt index e18b933..291faa9 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/stateKeeper/IdeStateKeeper.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/stateKeeper/IdeStateKeeper.kt @@ -12,6 +12,8 @@ import git4idea.actions.GitPull import git4idea.branch.GitBranchUtil import git4idea.repo.GitRepository import org.jetbrains.research.ideFormerPlugin.api.models.ReversibleApiMethod +import org.jetbrains.research.ideFormerPlugin.api.models.utils.NO_GIT_REPO_FOR_PROJECT +import org.jetbrains.research.ideFormerPlugin.api.models.utils.NO_GIT_ROOTS import java.util.* class IdeStateKeeper(val userProject: Project) { @@ -21,13 +23,17 @@ class IdeStateKeeper(val userProject: Project) { private val projectGitRepo: GitRepository val projectGitRoot: VirtualFile + companion object { + const val DEFAULT_API_METHODS_COUNT_TO_REVERSE = 1 + } + init { val gitVcs = GitVcs.getInstance(userProject) val gitRoots = GitPull.getGitRoots(userProject, gitVcs) - if (gitRoots.isNullOrEmpty()) error("No git roots was found") + if (gitRoots.isNullOrEmpty()) error(NO_GIT_ROOTS) projectGitRepo = GitBranchUtil.guessRepositoryForOperation(userProject, DataContext.EMPTY_CONTEXT) - ?: error("No git repository was found for user project") + ?: error(NO_GIT_REPO_FOR_PROJECT) projectGitRoot = projectGitRepo.root } @@ -50,6 +56,4 @@ class IdeStateKeeper(val userProject: Project) { } return apiMethodsCount } -} - -const val DEFAULT_API_METHODS_COUNT_TO_REVERSE = 1 \ No newline at end of file +} \ No newline at end of file From 82dd4b4ade1c166d5a09daa56af5c2b16b0c3c73 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 17 Nov 2023 10:59:50 +0100 Subject: [PATCH 21/81] add git status json description --- .../{exampleDesc.json => apiDescriptionExample.json} | 0 .../ideFormerPlugin/server/ideApiDescriptions.json | 8 ++++++++ 2 files changed, 8 insertions(+) rename ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/{exampleDesc.json => apiDescriptionExample.json} (100%) diff --git a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/exampleDesc.json b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/apiDescriptionExample.json similarity index 100% rename from ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/exampleDesc.json rename to ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/apiDescriptionExample.json diff --git a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json index 936649e..681f1c3 100644 --- a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json +++ b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json @@ -126,6 +126,14 @@ ], "optional_parameters": [] }, + { + "name": "git_status", + "url": "/git-status", + "description": "Displays the state of the working directory and the staging area. It lets see which changes have been staged, which haven't, and which files aren't being tracked by Git. Status output does not show any information regarding the committed project history.", + "method": "GET", + "required_parameters": [], + "optional_parameters": [] + }, { "name": "reverse_last_api_methods", "url": "/reverse-api-methods", From a0cd4220ea1d5c7200e7612c4a0a04a8166053a8 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 17 Nov 2023 13:36:07 +0100 Subject: [PATCH 22/81] add git add api --- .../api/models/gitRelated/GitAdd.kt | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitAdd.kt diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitAdd.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitAdd.kt new file mode 100644 index 0000000..409e03e --- /dev/null +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitAdd.kt @@ -0,0 +1,27 @@ +package org.jetbrains.research.ideFormerPlugin.api.models.gitRelated + +import com.intellij.openapi.project.Project +import com.intellij.openapi.vfs.VirtualFile +import git4idea.commands.* +import org.jetbrains.research.ideFormerPlugin.api.models.ReversibleApiMethod + +class GitAdd( + private val project: Project, + private val projectGitRoot: VirtualFile, + private val fileNames: List +) : ReversibleApiMethod { + + override fun execute() { + val git = Git.getInstance() + + val addCommandHandler = GitLineHandler(project, projectGitRoot, GitCommand.ADD) + addCommandHandler.addParameters(fileNames) + + val addCommandResult: GitCommandResult = git.runCommand(addCommandHandler) + if (!addCommandResult.success()) error("Error while git add execution") + } + + override fun reverse() { + TODO("Not yet implemented") + } +} \ No newline at end of file From 692f57e55a7ff089c73123f4c88bd449b21c6c9a Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 17 Nov 2023 13:37:30 +0100 Subject: [PATCH 23/81] add git add server request --- .../ideFormerPlugin/server/Routing.kt | 2 ++ .../server/requests/gitRelated/getGitAdd.kt | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitAdd.kt diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt index ec95e12..2df0035 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt @@ -9,6 +9,7 @@ import org.jetbrains.research.ideFormerPlugin.server.requests.fileRelated.getFil import org.jetbrains.research.ideFormerPlugin.server.requests.fileSystemRelated.getChangeDirectory import org.jetbrains.research.ideFormerPlugin.server.requests.fileSystemRelated.getListDirectoryContents import org.jetbrains.research.ideFormerPlugin.server.requests.fileSystemRelated.getProjectModules +import org.jetbrains.research.ideFormerPlugin.server.requests.gitRelated.getGitAdd import org.jetbrains.research.ideFormerPlugin.server.requests.gitRelated.getGitStatus import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper import org.slf4j.Logger @@ -34,6 +35,7 @@ fun Application.configureRouting(ideStateKeeper: IdeStateKeeper, logger: Logger) // git related getGitStatus(logger, ideStateKeeper) + getGitAdd(logger, ideStateKeeper) } } diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitAdd.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitAdd.kt new file mode 100644 index 0000000..8c07834 --- /dev/null +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitAdd.kt @@ -0,0 +1,22 @@ +package org.jetbrains.research.ideFormerPlugin.server.requests.gitRelated + +import io.ktor.server.application.* +import io.ktor.server.routing.* +import org.jetbrains.research.ideFormerPlugin.api.models.gitRelated.GitAdd +import org.jetbrains.research.ideFormerPlugin.server.executeAndRespondError +import org.jetbrains.research.ideFormerPlugin.server.respondJson +import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper +import org.slf4j.Logger + +fun Routing.getGitAdd(logger: Logger, ideStateKeeper: IdeStateKeeper) { + get("/git-add") { + // TODO: add file names list as a parameter + logger.info("Server GET git add is called") + + val gitStatus = GitAdd(ideStateKeeper.userProject, ideStateKeeper.projectGitRoot, listOf("models/cat.py")) + if (!executeAndRespondError(gitStatus, logger)) return@get + + call.respondJson("success") + logger.info("Server GET git add is processed") + } +} \ No newline at end of file From b0a09698e77ab9d38c7831ab869ebe3619cd6359 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 17 Nov 2023 13:37:45 +0100 Subject: [PATCH 24/81] small refactoring of the git status server request --- .../server/requests/gitRelated/getGitStatus.kt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitStatus.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitStatus.kt index c17e760..2a1e6bb 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitStatus.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitStatus.kt @@ -12,9 +12,7 @@ fun Routing.getGitStatus(logger: Logger, ideStateKeeper: IdeStateKeeper) { logger.info("Server GET git status is called") val gitStatus = GitStatus(ideStateKeeper.userProject, ideStateKeeper.projectGitRoot) - if (!executeAndRespondError(gitStatus, logger)) { - return@get - } + if (!executeAndRespondError(gitStatus, logger)) return@get call.respondJson(gitStatus.getStatus()) logger.info("Server GET git status is processed") From 74ddf34342be019be3f16f4a9bb21c7ba1c86080 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 17 Nov 2023 13:48:04 +0100 Subject: [PATCH 25/81] add fileName parameter to the git add server request --- .../server/requests/gitRelated/getGitAdd.kt | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitAdd.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitAdd.kt index 8c07834..9994ba6 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitAdd.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitAdd.kt @@ -3,20 +3,21 @@ package org.jetbrains.research.ideFormerPlugin.server.requests.gitRelated import io.ktor.server.application.* import io.ktor.server.routing.* import org.jetbrains.research.ideFormerPlugin.api.models.gitRelated.GitAdd -import org.jetbrains.research.ideFormerPlugin.server.executeAndRespondError -import org.jetbrains.research.ideFormerPlugin.server.respondJson +import org.jetbrains.research.ideFormerPlugin.server.* +import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.FILE_NAME_REQUEST_PARAM import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper import org.slf4j.Logger fun Routing.getGitAdd(logger: Logger, ideStateKeeper: IdeStateKeeper) { - get("/git-add") { - // TODO: add file names list as a parameter - logger.info("Server GET git add is called") + // TODO: add the ability to send a list of file names in the request + get("/git-add/{$FILE_NAME_REQUEST_PARAM?}") { + val fileName = call.processFileNameParameter(logger) ?: return@get + logger.info("Server GET git add for the file '$fileName' is called") - val gitStatus = GitAdd(ideStateKeeper.userProject, ideStateKeeper.projectGitRoot, listOf("models/cat.py")) - if (!executeAndRespondError(gitStatus, logger)) return@get + val gitAdd = GitAdd(ideStateKeeper.userProject, ideStateKeeper.projectGitRoot, listOf(fileName)) + if (!executeAndRespondError(gitAdd, logger)) return@get - call.respondJson("success") - logger.info("Server GET git add is processed") + call.respondJson("File $fileName was successfully added to Git") + logger.info("Server GET git add for the file '$fileName' is processed") } } \ No newline at end of file From 6bf453240a53eb2c360aaebb59b8fac757029f67 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 17 Nov 2023 14:46:31 +0100 Subject: [PATCH 26/81] add git command error message returning in git related apis --- .../ideFormerPlugin/api/models/gitRelated/GitAdd.kt | 2 +- .../ideFormerPlugin/api/models/gitRelated/GitStatus.kt | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitAdd.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitAdd.kt index 409e03e..c6bc27b 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitAdd.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitAdd.kt @@ -18,7 +18,7 @@ class GitAdd( addCommandHandler.addParameters(fileNames) val addCommandResult: GitCommandResult = git.runCommand(addCommandHandler) - if (!addCommandResult.success()) error("Error while git add execution") + if (!addCommandResult.success()) error(addCommandResult.errorOutputAsJoinedString) } override fun reverse() { diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitStatus.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitStatus.kt index d5dd78f..7e11f56 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitStatus.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitStatus.kt @@ -14,9 +14,11 @@ class GitStatus( override fun execute() { val git = Git.getInstance() - val statusCommandResult: GitCommandResult = git.runCommand( - GitLineHandler(project, projectGitRoot, GitCommand.STATUS) - ) + + val statusCommandHandler = GitLineHandler(project, projectGitRoot, GitCommand.STATUS) + val statusCommandResult: GitCommandResult = git.runCommand(statusCommandHandler) + + if (!statusCommandResult.success()) error(statusCommandResult.errorOutputAsJoinedString) status = statusCommandResult.outputAsJoinedString } From 37d4f75a8b1518e87d42c2e50ef433b3eea6fb3a Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 17 Nov 2023 15:24:54 +0100 Subject: [PATCH 27/81] add git commit api --- .../api/models/gitRelated/GitCommit.kt | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCommit.kt diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCommit.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCommit.kt new file mode 100644 index 0000000..aff35a0 --- /dev/null +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCommit.kt @@ -0,0 +1,27 @@ +package org.jetbrains.research.ideFormerPlugin.api.models.gitRelated + +import com.intellij.openapi.project.Project +import com.intellij.openapi.vfs.VirtualFile +import git4idea.commands.* +import org.jetbrains.research.ideFormerPlugin.api.models.ReversibleApiMethod + +class GitCommit( + private val project: Project, + private val projectGitRoot: VirtualFile, + private val commitMessage: String +) : ReversibleApiMethod { + + override fun execute() { + val git = Git.getInstance() + + val commitCommandHandler = GitLineHandler(project, projectGitRoot, GitCommand.COMMIT) + commitCommandHandler.addParameters("-m", commitMessage) + + val commitCommandResult: GitCommandResult = git.runCommand(commitCommandHandler) + if (!commitCommandResult.success()) error(commitCommandResult.errorOutputAsJoinedString) + } + + override fun reverse() { + TODO("Not yet implemented") + } +} \ No newline at end of file From 9f0d758375e14b13b3cd0a75f3705fb7034fab0a Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 17 Nov 2023 15:26:23 +0100 Subject: [PATCH 28/81] add git commit server request --- .../ideFormerPlugin/server/Routing.kt | 12 ++++++--- .../requests/gitRelated/getGitCommit.kt | 27 +++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitCommit.kt diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt index 2df0035..b9fbb93 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt @@ -9,8 +9,7 @@ import org.jetbrains.research.ideFormerPlugin.server.requests.fileRelated.getFil import org.jetbrains.research.ideFormerPlugin.server.requests.fileSystemRelated.getChangeDirectory import org.jetbrains.research.ideFormerPlugin.server.requests.fileSystemRelated.getListDirectoryContents import org.jetbrains.research.ideFormerPlugin.server.requests.fileSystemRelated.getProjectModules -import org.jetbrains.research.ideFormerPlugin.server.requests.gitRelated.getGitAdd -import org.jetbrains.research.ideFormerPlugin.server.requests.gitRelated.getGitStatus +import org.jetbrains.research.ideFormerPlugin.server.requests.gitRelated.* import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper import org.slf4j.Logger @@ -36,13 +35,14 @@ fun Application.configureRouting(ideStateKeeper: IdeStateKeeper, logger: Logger) // git related getGitStatus(logger, ideStateKeeper) getGitAdd(logger, ideStateKeeper) + getGitCommit(logger, ideStateKeeper) } } object IdeServerConstants { const val NO_API_AVAILABLE = "No IDE API available" const val ROOT_PAGE_TEXT = "IDE server" - const val MISSING_FILENAME = "Missing file name" + const val PROJECT_DIR_REMAINS_THE_SAME = "Project directory remains the same" const val PROJECT_DIR_WAS_CHANGED_TO = "Project directory was successfully changed to" const val NEGATIVE_API_METHODS_CNT = "apiMethodsCount parameter should be a positive integer" @@ -52,7 +52,13 @@ object IdeServerConstants { const val NO_SUCH_FILE_FUNCTION = "No such function in a file" // TODO: renaming + // requests parameters const val FILE_NAME_REQUEST_PARAM = "fileName" const val CLASS_NAME_REQUEST_PARAM = "className" const val FUNCTION_NAME_REQUEST_PARAM = "functionName" + const val COMMIT_MESSAGE_REQUEST_PARAM = "commitMessage" + + // request parameters errors + const val MISSING_FILENAME = "Missing file name" + const val MISSING_COMMIT_MESSAGE = "Missing commit message" } \ No newline at end of file diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitCommit.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitCommit.kt new file mode 100644 index 0000000..071a7b0 --- /dev/null +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitCommit.kt @@ -0,0 +1,27 @@ +package org.jetbrains.research.ideFormerPlugin.server.requests.gitRelated + +import io.ktor.server.application.* +import io.ktor.server.routing.* +import org.jetbrains.research.ideFormerPlugin.api.models.gitRelated.GitCommit +import org.jetbrains.research.ideFormerPlugin.server.* +import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.COMMIT_MESSAGE_REQUEST_PARAM +import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.MISSING_COMMIT_MESSAGE +import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper +import org.slf4j.Logger + +fun Routing.getGitCommit(logger: Logger, ideStateKeeper: IdeStateKeeper) { + get("/git-commit/{${COMMIT_MESSAGE_REQUEST_PARAM}?}") { + val commitMessage = call.processRequestParameter( + COMMIT_MESSAGE_REQUEST_PARAM, + MISSING_COMMIT_MESSAGE, + logger + ) ?: return@get + logger.info("Server GET git commit with the message '$commitMessage' is called") + + val gitCommit = GitCommit(ideStateKeeper.userProject, ideStateKeeper.projectGitRoot, commitMessage) + if (!executeAndRespondError(gitCommit, logger)) return@get + + call.respondJson("Git commit was processed successfully") + logger.info("Server GET git commit with the message '$commitMessage' is processed") + } +} \ No newline at end of file From 3c073e6dd85108453944efe869a548f05bd2f0e3 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 17 Nov 2023 15:32:28 +0100 Subject: [PATCH 29/81] add git add json description --- .../server/ideApiDescriptions.json | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json index 681f1c3..3a4035d 100644 --- a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json +++ b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json @@ -134,6 +134,21 @@ "required_parameters": [], "optional_parameters": [] }, + { + "name": "git_add", + "url": "/git-add", + "description": "Adds the file to the Git index to prepare it for the next commit.", + "method": "GET", + "required_parameters": [ + { + "name": "fileName", + "type": "STRING", + "regex": "^[\\w,\\s-]+\\.[A-Za-z]{2, 3, 4, 5}$", + "description": "The name of the file that should be added to Git index." + } + ], + "optional_parameters": [] + }, { "name": "reverse_last_api_methods", "url": "/reverse-api-methods", From fbdaaa76927b5d05f00d3fd220868c3312f65e44 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 17 Nov 2023 15:42:32 +0100 Subject: [PATCH 30/81] add git commit json description --- .../server/ideApiDescriptions.json | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json index 3a4035d..259d5b8 100644 --- a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json +++ b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json @@ -149,6 +149,21 @@ ], "optional_parameters": [] }, + { + "name": "git_commit", + "url": "/git-commit", + "description": "Creates a snapshot of the staged changes along a timeline of a Git projects' history.", + "method": "GET", + "required_parameters": [ + { + "name": "commitMessage", + "type": "STRING", + "regex": "[^\"\\{\\}]*?", + "description": "A short message that describes and summarizes the changes the were made in the commit and why they were made." + } + ], + "optional_parameters": [] + }, { "name": "reverse_last_api_methods", "url": "/reverse-api-methods", From b005a3b3386a1abc316590a3f33045f022b59f0b Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 17 Nov 2023 16:19:17 +0100 Subject: [PATCH 31/81] update process req parameter function --- .../jetbrains/research/ideFormerPlugin/server/Routing.kt | 4 +--- .../research/ideFormerPlugin/server/RoutingUtils.kt | 4 ++-- .../server/requests/gitRelated/getGitCommit.kt | 8 ++------ 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt index b9fbb93..0a0b8d4 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt @@ -58,7 +58,5 @@ object IdeServerConstants { const val FUNCTION_NAME_REQUEST_PARAM = "functionName" const val COMMIT_MESSAGE_REQUEST_PARAM = "commitMessage" - // request parameters errors - const val MISSING_FILENAME = "Missing file name" - const val MISSING_COMMIT_MESSAGE = "Missing commit message" + const val MISSING_REQUEST_PARAM = "Missing request parameter" } \ No newline at end of file diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/RoutingUtils.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/RoutingUtils.kt index 4737c7e..c16a134 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/RoutingUtils.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/RoutingUtils.kt @@ -6,6 +6,7 @@ import io.ktor.server.application.* import io.ktor.server.response.* import io.ktor.util.pipeline.* import org.jetbrains.research.ideFormerPlugin.api.models.IdeApiMethod +import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.MISSING_REQUEST_PARAM import org.slf4j.Logger object RoutingUtils { @@ -21,10 +22,10 @@ suspend fun ApplicationCall.respondJson(responseObject: Any, status: HttpStatusC suspend fun ApplicationCall.processRequestParameter( parameterName: String, - errorMessage: String, logger: Logger ): String? = this.parameters[parameterName] ?: run { + val errorMessage = "$MISSING_REQUEST_PARAM: $parameterName" logger.error(errorMessage) this.respondJson( errorMessage, @@ -36,7 +37,6 @@ suspend fun ApplicationCall.processRequestParameter( suspend fun ApplicationCall.processFileNameParameter(logger: Logger): String? = this.processRequestParameter( IdeServerConstants.FILE_NAME_REQUEST_PARAM, - IdeServerConstants.MISSING_FILENAME, logger ) diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitCommit.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitCommit.kt index 071a7b0..74b0303 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitCommit.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitCommit.kt @@ -5,17 +5,13 @@ import io.ktor.server.routing.* import org.jetbrains.research.ideFormerPlugin.api.models.gitRelated.GitCommit import org.jetbrains.research.ideFormerPlugin.server.* import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.COMMIT_MESSAGE_REQUEST_PARAM -import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.MISSING_COMMIT_MESSAGE import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper import org.slf4j.Logger fun Routing.getGitCommit(logger: Logger, ideStateKeeper: IdeStateKeeper) { get("/git-commit/{${COMMIT_MESSAGE_REQUEST_PARAM}?}") { - val commitMessage = call.processRequestParameter( - COMMIT_MESSAGE_REQUEST_PARAM, - MISSING_COMMIT_MESSAGE, - logger - ) ?: return@get + val commitMessage = call.processRequestParameter(COMMIT_MESSAGE_REQUEST_PARAM, logger) + ?: return@get logger.info("Server GET git commit with the message '$commitMessage' is called") val gitCommit = GitCommit(ideStateKeeper.userProject, ideStateKeeper.projectGitRoot, commitMessage) From 6b794b484457409dcf707e83b1da3f9ff9578f0b Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 17 Nov 2023 16:26:02 +0100 Subject: [PATCH 32/81] add git branch api --- .../api/models/gitRelated/GitBranch.kt | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitBranch.kt diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitBranch.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitBranch.kt new file mode 100644 index 0000000..7292b94 --- /dev/null +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitBranch.kt @@ -0,0 +1,27 @@ +package org.jetbrains.research.ideFormerPlugin.api.models.gitRelated + +import com.intellij.openapi.project.Project +import com.intellij.openapi.vfs.VirtualFile +import git4idea.commands.* +import org.jetbrains.research.ideFormerPlugin.api.models.ReversibleApiMethod + +class GitBranch( + private val project: Project, + private val projectGitRoot: VirtualFile, + private val branchName: String +) : ReversibleApiMethod { + + override fun execute() { + val git = Git.getInstance() + + val branchCommandHandler = GitLineHandler(project, projectGitRoot, GitCommand.BRANCH) + branchCommandHandler.addParameters(branchName) + + val branchCommandResult: GitCommandResult = git.runCommand(branchCommandHandler) + if (!branchCommandResult.success()) error(branchCommandResult.errorOutputAsJoinedString) + } + + override fun reverse() { + TODO("Not yet implemented") + } +} \ No newline at end of file From 2fd8b914c57e0486cd3f103225ba3bcd5991eee8 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 17 Nov 2023 16:26:47 +0100 Subject: [PATCH 33/81] add git branch server request --- .../ideFormerPlugin/server/Routing.kt | 2 ++ .../requests/gitRelated/getGitBranch.kt | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitBranch.kt diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt index 0a0b8d4..66689ab 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt @@ -36,6 +36,7 @@ fun Application.configureRouting(ideStateKeeper: IdeStateKeeper, logger: Logger) getGitStatus(logger, ideStateKeeper) getGitAdd(logger, ideStateKeeper) getGitCommit(logger, ideStateKeeper) + getGitBranch(logger, ideStateKeeper) } } @@ -57,6 +58,7 @@ object IdeServerConstants { const val CLASS_NAME_REQUEST_PARAM = "className" const val FUNCTION_NAME_REQUEST_PARAM = "functionName" const val COMMIT_MESSAGE_REQUEST_PARAM = "commitMessage" + const val BRANCH_NAME_REQUEST_PARAM = "branchName" const val MISSING_REQUEST_PARAM = "Missing request parameter" } \ No newline at end of file diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitBranch.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitBranch.kt new file mode 100644 index 0000000..257c3bd --- /dev/null +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitBranch.kt @@ -0,0 +1,23 @@ +package org.jetbrains.research.ideFormerPlugin.server.requests.gitRelated + +import io.ktor.server.application.* +import io.ktor.server.routing.* +import org.jetbrains.research.ideFormerPlugin.api.models.gitRelated.GitBranch +import org.jetbrains.research.ideFormerPlugin.server.* +import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.BRANCH_NAME_REQUEST_PARAM +import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper +import org.slf4j.Logger + +fun Routing.getGitBranch(logger: Logger, ideStateKeeper: IdeStateKeeper) { + get("/git-branch/{$BRANCH_NAME_REQUEST_PARAM?}") { + val branchName = call.processRequestParameter(BRANCH_NAME_REQUEST_PARAM, logger) + ?: return@get + logger.info("Server GET git branch $branchName is called") + + val gitBranch = GitBranch(ideStateKeeper.userProject, ideStateKeeper.projectGitRoot, branchName) + if (!executeAndRespondError(gitBranch, logger)) return@get + + call.respondJson("Git branch $branchName was processed successfully") + logger.info("Server GET git branch $branchName is processed") + } +} \ No newline at end of file From 6bc66ed7c378dc3aacc6a96cf2bb84f526983382 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 17 Nov 2023 16:30:45 +0100 Subject: [PATCH 34/81] add git branch api json description --- .../server/ideApiDescriptions.json | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json index 259d5b8..52ccdb3 100644 --- a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json +++ b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json @@ -164,6 +164,21 @@ ], "optional_parameters": [] }, + { + "name": "git_branch", + "url": "/git-branch", + "description": "Creates a new branch with a given name in a Git repository.", + "method": "GET", + "required_parameters": [ + { + "name": "branchName", + "type": "STRING", + "regex": "[A-Za-z]*", + "description": "The name of a new branch to be created." + } + ], + "optional_parameters": [] + }, { "name": "reverse_last_api_methods", "url": "/reverse-api-methods", From 1d7735fe0c9575d4c809a5def7b283913d643d17 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 17 Nov 2023 17:31:11 +0100 Subject: [PATCH 35/81] add git checkout api --- .../api/models/gitRelated/GitCheckout.kt | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCheckout.kt diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCheckout.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCheckout.kt new file mode 100644 index 0000000..9405186 --- /dev/null +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCheckout.kt @@ -0,0 +1,28 @@ +package org.jetbrains.research.ideFormerPlugin.api.models.gitRelated + +import com.intellij.openapi.project.Project +import com.intellij.openapi.vfs.VirtualFile +import git4idea.commands.* +import org.jetbrains.research.ideFormerPlugin.api.models.ReversibleApiMethod + +class GitCheckout( + private val project: Project, + private val projectGitRoot: VirtualFile, + private val branchName: String +) : ReversibleApiMethod { + // TODO: to add a current branch for reverse method + + override fun execute() { + val git = Git.getInstance() + + val checkoutCommandHandler = GitLineHandler(project, projectGitRoot, GitCommand.CHECKOUT) + checkoutCommandHandler.addParameters(branchName) + + val checkoutCommandResult: GitCommandResult = git.runCommand(checkoutCommandHandler) + if (!checkoutCommandResult.success()) error(checkoutCommandResult.errorOutputAsJoinedString) + } + + override fun reverse() { + TODO("Not yet implemented") + } +} \ No newline at end of file From 1cea4c66dd9b9211fcb48ed8c2cfe4edac032626 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 17 Nov 2023 17:31:21 +0100 Subject: [PATCH 36/81] add git checkout server request --- .../ideFormerPlugin/server/Routing.kt | 1 + .../requests/gitRelated/getGitCheckout.kt | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitCheckout.kt diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt index 66689ab..49bc46f 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt @@ -37,6 +37,7 @@ fun Application.configureRouting(ideStateKeeper: IdeStateKeeper, logger: Logger) getGitAdd(logger, ideStateKeeper) getGitCommit(logger, ideStateKeeper) getGitBranch(logger, ideStateKeeper) + getGitCheckout(logger, ideStateKeeper) } } diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitCheckout.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitCheckout.kt new file mode 100644 index 0000000..6c2be41 --- /dev/null +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitCheckout.kt @@ -0,0 +1,23 @@ +package org.jetbrains.research.ideFormerPlugin.server.requests.gitRelated + +import io.ktor.server.application.* +import io.ktor.server.routing.* +import org.jetbrains.research.ideFormerPlugin.api.models.gitRelated.GitCheckout +import org.jetbrains.research.ideFormerPlugin.server.* +import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.BRANCH_NAME_REQUEST_PARAM +import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper +import org.slf4j.Logger + +fun Routing.getGitCheckout(logger: Logger, ideStateKeeper: IdeStateKeeper) { + get("/git-checkout/{$BRANCH_NAME_REQUEST_PARAM?}") { + val branchName = call.processRequestParameter(BRANCH_NAME_REQUEST_PARAM, logger) + ?: return@get + logger.info("Server GET git checkout $branchName is called") + + val gitCheckout = GitCheckout(ideStateKeeper.userProject, ideStateKeeper.projectGitRoot, branchName) + if (!executeAndRespondError(gitCheckout, logger)) return@get + + call.respondJson("Git checkout $branchName was processed successfully") + logger.info("Server GET git checkout $branchName is processed") + } +} \ No newline at end of file From 0cd8457b9f91e0de1bf55f356301b4c796d943cc Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 17 Nov 2023 17:31:51 +0100 Subject: [PATCH 37/81] add some comments to git api reverse methods --- .../research/ideFormerPlugin/api/models/gitRelated/GitAdd.kt | 1 + .../research/ideFormerPlugin/api/models/gitRelated/GitBranch.kt | 1 + .../research/ideFormerPlugin/api/models/gitRelated/GitCommit.kt | 1 + 3 files changed, 3 insertions(+) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitAdd.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitAdd.kt index c6bc27b..d45113d 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitAdd.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitAdd.kt @@ -23,5 +23,6 @@ class GitAdd( override fun reverse() { TODO("Not yet implemented") + // delete file from the index } } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitBranch.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitBranch.kt index 7292b94..ff9b0a5 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitBranch.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitBranch.kt @@ -23,5 +23,6 @@ class GitBranch( override fun reverse() { TODO("Not yet implemented") + // delete a new branch } } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCommit.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCommit.kt index aff35a0..1e30680 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCommit.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCommit.kt @@ -23,5 +23,6 @@ class GitCommit( override fun reverse() { TODO("Not yet implemented") + // drop the new commit } } \ No newline at end of file From cf8abf50574ecfd9e9fce498a18e57e2cd140451 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 17 Nov 2023 17:37:28 +0100 Subject: [PATCH 38/81] add git checkout api json description --- .../server/ideApiDescriptions.json | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json index 52ccdb3..4638401 100644 --- a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json +++ b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json @@ -179,6 +179,21 @@ ], "optional_parameters": [] }, + { + "name": "git_checkout", + "url": "/git-checkout", + "description": "Switches Git repository to a given existing branch.", + "method": "GET", + "required_parameters": [ + { + "name": "branchName", + "type": "STRING", + "regex": "[A-Za-z]*", + "description": "The name of an existing git branch to switch to." + } + ], + "optional_parameters": [] + }, { "name": "reverse_last_api_methods", "url": "/reverse-api-methods", From 4fd088d593af72a374f10ca7e150d2edc7610a08 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Wed, 3 Jan 2024 15:43:28 +0100 Subject: [PATCH 39/81] create common function to execute git api method --- .../api/models/gitRelated/GitAdd.kt | 11 +++------- .../api/models/gitRelated/GitBranch.kt | 9 ++------ .../api/models/gitRelated/GitCheckout.kt | 9 ++------ .../api/models/gitRelated/GitCommit.kt | 9 ++------ .../api/models/gitRelated/GitStatus.kt | 9 ++------ .../api/models/utils/GitUtils.kt | 22 +++++++++++++++++++ 6 files changed, 33 insertions(+), 36 deletions(-) create mode 100644 ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/GitUtils.kt diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitAdd.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitAdd.kt index d45113d..192c656 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitAdd.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitAdd.kt @@ -4,6 +4,7 @@ import com.intellij.openapi.project.Project import com.intellij.openapi.vfs.VirtualFile import git4idea.commands.* import org.jetbrains.research.ideFormerPlugin.api.models.ReversibleApiMethod +import org.jetbrains.research.ideFormerPlugin.api.models.utils.executeGitCommand class GitAdd( private val project: Project, @@ -12,17 +13,11 @@ class GitAdd( ) : ReversibleApiMethod { override fun execute() { - val git = Git.getInstance() - - val addCommandHandler = GitLineHandler(project, projectGitRoot, GitCommand.ADD) - addCommandHandler.addParameters(fileNames) - - val addCommandResult: GitCommandResult = git.runCommand(addCommandHandler) - if (!addCommandResult.success()) error(addCommandResult.errorOutputAsJoinedString) + executeGitCommand(project, projectGitRoot, GitCommand.ADD, fileNames) } override fun reverse() { TODO("Not yet implemented") - // delete file from the index + // delete files from the index } } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitBranch.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitBranch.kt index ff9b0a5..634ca7e 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitBranch.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitBranch.kt @@ -4,6 +4,7 @@ import com.intellij.openapi.project.Project import com.intellij.openapi.vfs.VirtualFile import git4idea.commands.* import org.jetbrains.research.ideFormerPlugin.api.models.ReversibleApiMethod +import org.jetbrains.research.ideFormerPlugin.api.models.utils.executeGitCommand class GitBranch( private val project: Project, @@ -12,13 +13,7 @@ class GitBranch( ) : ReversibleApiMethod { override fun execute() { - val git = Git.getInstance() - - val branchCommandHandler = GitLineHandler(project, projectGitRoot, GitCommand.BRANCH) - branchCommandHandler.addParameters(branchName) - - val branchCommandResult: GitCommandResult = git.runCommand(branchCommandHandler) - if (!branchCommandResult.success()) error(branchCommandResult.errorOutputAsJoinedString) + executeGitCommand(project, projectGitRoot, GitCommand.BRANCH, listOf( branchName)) } override fun reverse() { diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCheckout.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCheckout.kt index 9405186..35937ad 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCheckout.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCheckout.kt @@ -4,6 +4,7 @@ import com.intellij.openapi.project.Project import com.intellij.openapi.vfs.VirtualFile import git4idea.commands.* import org.jetbrains.research.ideFormerPlugin.api.models.ReversibleApiMethod +import org.jetbrains.research.ideFormerPlugin.api.models.utils.executeGitCommand class GitCheckout( private val project: Project, @@ -13,13 +14,7 @@ class GitCheckout( // TODO: to add a current branch for reverse method override fun execute() { - val git = Git.getInstance() - - val checkoutCommandHandler = GitLineHandler(project, projectGitRoot, GitCommand.CHECKOUT) - checkoutCommandHandler.addParameters(branchName) - - val checkoutCommandResult: GitCommandResult = git.runCommand(checkoutCommandHandler) - if (!checkoutCommandResult.success()) error(checkoutCommandResult.errorOutputAsJoinedString) + executeGitCommand(project, projectGitRoot, GitCommand.CHECKOUT, listOf(branchName)) } override fun reverse() { diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCommit.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCommit.kt index 1e30680..422224f 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCommit.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCommit.kt @@ -4,6 +4,7 @@ import com.intellij.openapi.project.Project import com.intellij.openapi.vfs.VirtualFile import git4idea.commands.* import org.jetbrains.research.ideFormerPlugin.api.models.ReversibleApiMethod +import org.jetbrains.research.ideFormerPlugin.api.models.utils.executeGitCommand class GitCommit( private val project: Project, @@ -12,13 +13,7 @@ class GitCommit( ) : ReversibleApiMethod { override fun execute() { - val git = Git.getInstance() - - val commitCommandHandler = GitLineHandler(project, projectGitRoot, GitCommand.COMMIT) - commitCommandHandler.addParameters("-m", commitMessage) - - val commitCommandResult: GitCommandResult = git.runCommand(commitCommandHandler) - if (!commitCommandResult.success()) error(commitCommandResult.errorOutputAsJoinedString) + executeGitCommand(project, projectGitRoot, GitCommand.COMMIT, listOf("-m", commitMessage)) } override fun reverse() { diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitStatus.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitStatus.kt index 7e11f56..a5e8c11 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitStatus.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitStatus.kt @@ -5,6 +5,7 @@ import com.intellij.openapi.vfs.VirtualFile import git4idea.commands.* import org.jetbrains.research.ideFormerPlugin.api.models.IdeApiMethod import org.jetbrains.research.ideFormerPlugin.api.models.utils.UNCALLED_EXECUTE_BEFORE_RESULT_GETTING +import org.jetbrains.research.ideFormerPlugin.api.models.utils.executeGitCommand class GitStatus( private val project: Project, @@ -13,13 +14,7 @@ class GitStatus( private var status: String? = null override fun execute() { - val git = Git.getInstance() - - val statusCommandHandler = GitLineHandler(project, projectGitRoot, GitCommand.STATUS) - val statusCommandResult: GitCommandResult = git.runCommand(statusCommandHandler) - - if (!statusCommandResult.success()) error(statusCommandResult.errorOutputAsJoinedString) - status = statusCommandResult.outputAsJoinedString + status = executeGitCommand(project, projectGitRoot, GitCommand.STATUS, null) } fun getStatus(): String { diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/GitUtils.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/GitUtils.kt new file mode 100644 index 0000000..4a58cf1 --- /dev/null +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/GitUtils.kt @@ -0,0 +1,22 @@ +package org.jetbrains.research.ideFormerPlugin.api.models.utils + +import com.intellij.openapi.project.Project +import com.intellij.openapi.vfs.VirtualFile +import git4idea.commands.* + +fun executeGitCommand( + project: Project, + projectGitRoot: VirtualFile, + gitCommand: GitCommand, + gitCommandParameters: List? +) : String { + val git = Git.getInstance() + + val gitCommandHandler = GitLineHandler(project, projectGitRoot, gitCommand) + gitCommandParameters?.let { gitCommandHandler.addParameters(it) } + + val gitCommandResult: GitCommandResult = git.runCommand(gitCommandHandler) + if (!gitCommandResult.success()) error(gitCommandResult.errorOutputAsJoinedString) + + return gitCommandResult.outputAsJoinedString +} \ No newline at end of file From 4be157d455dd491814d1348c90ec7a458a763fe1 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Wed, 3 Jan 2024 16:55:11 +0100 Subject: [PATCH 40/81] implement reverse() for the git add --- .../research/ideFormerPlugin/api/models/gitRelated/GitAdd.kt | 3 ++- .../ideFormerPlugin/server/requests/gitRelated/getGitAdd.kt | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitAdd.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitAdd.kt index 192c656..0b29699 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitAdd.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitAdd.kt @@ -17,7 +17,8 @@ class GitAdd( } override fun reverse() { - TODO("Not yet implemented") // delete files from the index + val rmCommandParameters = listOf("--cached") + fileNames + executeGitCommand(project, projectGitRoot, GitCommand.RM, rmCommandParameters) } } \ No newline at end of file diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitAdd.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitAdd.kt index 9994ba6..4ece1d2 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitAdd.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitAdd.kt @@ -17,6 +17,9 @@ fun Routing.getGitAdd(logger: Logger, ideStateKeeper: IdeStateKeeper) { val gitAdd = GitAdd(ideStateKeeper.userProject, ideStateKeeper.projectGitRoot, listOf(fileName)) if (!executeAndRespondError(gitAdd, logger)) return@get + ideStateKeeper.saveReversibleApiMethod(gitAdd) + logger.info("Git add api method was saved on the api methods stack") + call.respondJson("File $fileName was successfully added to Git") logger.info("Server GET git add for the file '$fileName' is processed") } From 4c53a58a19660554595449ce1d5f2281dba2336b Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Wed, 3 Jan 2024 17:04:41 +0100 Subject: [PATCH 41/81] implement reverse() for the git branch --- .../ideFormerPlugin/api/models/gitRelated/GitBranch.kt | 3 ++- .../ideFormerPlugin/server/requests/gitRelated/getGitBranch.kt | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitBranch.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitBranch.kt index 634ca7e..ab6a608 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitBranch.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitBranch.kt @@ -17,7 +17,8 @@ class GitBranch( } override fun reverse() { - TODO("Not yet implemented") // delete a new branch + val deleteBranchCommandParameters = listOf("--delete", branchName) + executeGitCommand(project, projectGitRoot, GitCommand.BRANCH, deleteBranchCommandParameters) } } \ No newline at end of file diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitBranch.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitBranch.kt index 257c3bd..314369d 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitBranch.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitBranch.kt @@ -17,6 +17,9 @@ fun Routing.getGitBranch(logger: Logger, ideStateKeeper: IdeStateKeeper) { val gitBranch = GitBranch(ideStateKeeper.userProject, ideStateKeeper.projectGitRoot, branchName) if (!executeAndRespondError(gitBranch, logger)) return@get + ideStateKeeper.saveReversibleApiMethod(gitBranch) + logger.info("Git branch api method was saved on the api methods stack") + call.respondJson("Git branch $branchName was processed successfully") logger.info("Server GET git branch $branchName is processed") } From 302928f025270eb6d387fe364a1c3d30f549c51f Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Wed, 3 Jan 2024 18:39:44 +0100 Subject: [PATCH 42/81] implement reverse() for the git commit --- .../ideFormerPlugin/api/models/gitRelated/GitCommit.kt | 9 ++++++--- .../server/requests/gitRelated/getGitCommit.kt | 3 +++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCommit.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCommit.kt index 422224f..80df1a6 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCommit.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCommit.kt @@ -2,7 +2,7 @@ package org.jetbrains.research.ideFormerPlugin.api.models.gitRelated import com.intellij.openapi.project.Project import com.intellij.openapi.vfs.VirtualFile -import git4idea.commands.* +import git4idea.commands.GitCommand import org.jetbrains.research.ideFormerPlugin.api.models.ReversibleApiMethod import org.jetbrains.research.ideFormerPlugin.api.models.utils.executeGitCommand @@ -16,8 +16,11 @@ class GitCommit( executeGitCommand(project, projectGitRoot, GitCommand.COMMIT, listOf("-m", commitMessage)) } + /** + * Invariant: reverse is always called when the head of the branch is on that particular commit + **/ override fun reverse() { - TODO("Not yet implemented") - // drop the new commit + // drop the last commit + executeGitCommand(project, projectGitRoot, GitCommand.RESET, listOf("--soft", "HEAD~1")) } } \ No newline at end of file diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitCommit.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitCommit.kt index 74b0303..2baabc9 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitCommit.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitCommit.kt @@ -17,6 +17,9 @@ fun Routing.getGitCommit(logger: Logger, ideStateKeeper: IdeStateKeeper) { val gitCommit = GitCommit(ideStateKeeper.userProject, ideStateKeeper.projectGitRoot, commitMessage) if (!executeAndRespondError(gitCommit, logger)) return@get + ideStateKeeper.saveReversibleApiMethod(gitCommit) + logger.info("Git commit api method was saved on the api methods stack") + call.respondJson("Git commit was processed successfully") logger.info("Server GET git commit with the message '$commitMessage' is processed") } From 7e5544e108cab50f87c8057409d20be451eaf473 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Wed, 3 Jan 2024 18:51:54 +0100 Subject: [PATCH 43/81] implement reverse() for the git checkout --- .../api/models/gitRelated/GitCheckout.kt | 10 ++++++++-- .../server/requests/gitRelated/getGitCheckout.kt | 3 +++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCheckout.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCheckout.kt index 35937ad..711562f 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCheckout.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCheckout.kt @@ -11,13 +11,19 @@ class GitCheckout( private val projectGitRoot: VirtualFile, private val branchName: String ) : ReversibleApiMethod { - // TODO: to add a current branch for reverse method + private val previousBranchName: String = executeGitCommand( + project, + projectGitRoot, + GitCommand.REV_PARSE, + listOf("--abbrev-ref", "HEAD") + ) override fun execute() { executeGitCommand(project, projectGitRoot, GitCommand.CHECKOUT, listOf(branchName)) } override fun reverse() { - TODO("Not yet implemented") + // checkout to the previous branch + executeGitCommand(project, projectGitRoot, GitCommand.CHECKOUT, listOf(previousBranchName)) } } \ No newline at end of file diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitCheckout.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitCheckout.kt index 6c2be41..26a1699 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitCheckout.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitCheckout.kt @@ -17,6 +17,9 @@ fun Routing.getGitCheckout(logger: Logger, ideStateKeeper: IdeStateKeeper) { val gitCheckout = GitCheckout(ideStateKeeper.userProject, ideStateKeeper.projectGitRoot, branchName) if (!executeAndRespondError(gitCheckout, logger)) return@get + ideStateKeeper.saveReversibleApiMethod(gitCheckout) + logger.info("Git checkout api method was saved on the api methods stack") + call.respondJson("Git checkout $branchName was processed successfully") logger.info("Server GET git checkout $branchName is processed") } From e51c8244186383e474a5eb38a589862d7936bdff Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Wed, 3 Jan 2024 20:10:11 +0100 Subject: [PATCH 44/81] create base GitApiMethod class --- .../api/models/gitRelated/GitApiMethod.kt | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitApiMethod.kt diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitApiMethod.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitApiMethod.kt new file mode 100644 index 0000000..a59a500 --- /dev/null +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitApiMethod.kt @@ -0,0 +1,42 @@ +package org.jetbrains.research.ideFormerPlugin.api.models.gitRelated + +import com.intellij.openapi.project.Project +import com.intellij.openapi.vfs.VirtualFile +import git4idea.commands.GitCommand +import org.jetbrains.research.ideFormerPlugin.api.models.ReversibleApiMethod +import org.jetbrains.research.ideFormerPlugin.api.models.utils.executeGitCommand + +open class GitApiMethod( + private val project: Project, + private val projectGitRoot: VirtualFile +) : ReversibleApiMethod { + open var gitCommand: GitCommand? = null + open var gitCommandParameters: List? = null + + open var gitReverseCommand: GitCommand? = null + open var gitReverseCommandParameters: List? = null + + override fun execute() { + executeGitCommand( + project, + projectGitRoot, + gitCommand!!, + gitCommandParameters + ) + } + + /** + * Reverse -- calling the opposite git command, which cancels the action of the original command + * If the method is unmodifying (e.g. status), reverse() does nothing + **/ + override fun reverse() { + gitReverseCommand?.let { + executeGitCommand( + project, + projectGitRoot, + it, + gitReverseCommandParameters + ) + } + } +} \ No newline at end of file From bc2a845d35c08204dc0d7bd30ffa13d86f956f06 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Wed, 3 Jan 2024 20:13:37 +0100 Subject: [PATCH 45/81] update git add due to inheritance from GitApiMethod base class --- .../api/models/gitRelated/GitAdd.kt | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitAdd.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitAdd.kt index 0b29699..e9074d7 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitAdd.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitAdd.kt @@ -2,23 +2,16 @@ package org.jetbrains.research.ideFormerPlugin.api.models.gitRelated import com.intellij.openapi.project.Project import com.intellij.openapi.vfs.VirtualFile -import git4idea.commands.* -import org.jetbrains.research.ideFormerPlugin.api.models.ReversibleApiMethod -import org.jetbrains.research.ideFormerPlugin.api.models.utils.executeGitCommand +import git4idea.commands.GitCommand class GitAdd( - private val project: Project, - private val projectGitRoot: VirtualFile, - private val fileNames: List -) : ReversibleApiMethod { + project: Project, + projectGitRoot: VirtualFile, + fileNames: List +) : GitApiMethod(project, projectGitRoot) { + override var gitCommand: GitCommand? = GitCommand.ADD + override var gitCommandParameters: List? = fileNames - override fun execute() { - executeGitCommand(project, projectGitRoot, GitCommand.ADD, fileNames) - } - - override fun reverse() { - // delete files from the index - val rmCommandParameters = listOf("--cached") + fileNames - executeGitCommand(project, projectGitRoot, GitCommand.RM, rmCommandParameters) - } + override var gitReverseCommand: GitCommand? = GitCommand.RM + override var gitReverseCommandParameters: List? = listOf("--cached") + fileNames } \ No newline at end of file From fa682bd68cd9d1e2c63507a4a092d4287c8e44bc Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Wed, 3 Jan 2024 20:13:47 +0100 Subject: [PATCH 46/81] update git branch due to inheritance from GitApiMethod base class --- .../api/models/gitRelated/GitBranch.kt | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitBranch.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitBranch.kt index ab6a608..72834ca 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitBranch.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitBranch.kt @@ -2,23 +2,16 @@ package org.jetbrains.research.ideFormerPlugin.api.models.gitRelated import com.intellij.openapi.project.Project import com.intellij.openapi.vfs.VirtualFile -import git4idea.commands.* -import org.jetbrains.research.ideFormerPlugin.api.models.ReversibleApiMethod -import org.jetbrains.research.ideFormerPlugin.api.models.utils.executeGitCommand +import git4idea.commands.GitCommand class GitBranch( - private val project: Project, - private val projectGitRoot: VirtualFile, - private val branchName: String -) : ReversibleApiMethod { + project: Project, + projectGitRoot: VirtualFile, + branchName: String +) : GitApiMethod(project, projectGitRoot) { + override var gitCommand: GitCommand? = GitCommand.BRANCH + override var gitCommandParameters: List? = listOf(branchName) - override fun execute() { - executeGitCommand(project, projectGitRoot, GitCommand.BRANCH, listOf( branchName)) - } - - override fun reverse() { - // delete a new branch - val deleteBranchCommandParameters = listOf("--delete", branchName) - executeGitCommand(project, projectGitRoot, GitCommand.BRANCH, deleteBranchCommandParameters) - } + override var gitReverseCommand: GitCommand? = GitCommand.BRANCH + override var gitReverseCommandParameters: List? = listOf("--delete", branchName) } \ No newline at end of file From e71b5bc06ff7db1671d88c0867a13d3760a91dc5 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Wed, 3 Jan 2024 20:14:03 +0100 Subject: [PATCH 47/81] update git checkout due to inheritance from GitApiMethod base class --- .../api/models/gitRelated/GitCheckout.kt | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCheckout.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCheckout.kt index 711562f..181e101 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCheckout.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCheckout.kt @@ -2,28 +2,24 @@ package org.jetbrains.research.ideFormerPlugin.api.models.gitRelated import com.intellij.openapi.project.Project import com.intellij.openapi.vfs.VirtualFile -import git4idea.commands.* -import org.jetbrains.research.ideFormerPlugin.api.models.ReversibleApiMethod +import git4idea.commands.GitCommand import org.jetbrains.research.ideFormerPlugin.api.models.utils.executeGitCommand class GitCheckout( - private val project: Project, - private val projectGitRoot: VirtualFile, - private val branchName: String -) : ReversibleApiMethod { + project: Project, + projectGitRoot: VirtualFile, + branchName: String +) : GitApiMethod(project, projectGitRoot) { private val previousBranchName: String = executeGitCommand( project, projectGitRoot, GitCommand.REV_PARSE, listOf("--abbrev-ref", "HEAD") - ) + ) - override fun execute() { - executeGitCommand(project, projectGitRoot, GitCommand.CHECKOUT, listOf(branchName)) - } + override var gitCommand: GitCommand? = GitCommand.CHECKOUT + override var gitCommandParameters: List? = listOf(branchName) - override fun reverse() { - // checkout to the previous branch - executeGitCommand(project, projectGitRoot, GitCommand.CHECKOUT, listOf(previousBranchName)) - } + override var gitReverseCommand: GitCommand? = GitCommand.CHECKOUT + override var gitReverseCommandParameters: List? = listOf(previousBranchName) } \ No newline at end of file From 512e8348c47d157e4fca63a66ea9a7f44d07e0bb Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Wed, 3 Jan 2024 20:14:14 +0100 Subject: [PATCH 48/81] update git commit due to inheritance from GitApiMethod base class --- .../api/models/gitRelated/GitCommit.kt | 26 +++++++------------ 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCommit.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCommit.kt index 80df1a6..6ccafb6 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCommit.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitCommit.kt @@ -3,24 +3,16 @@ package org.jetbrains.research.ideFormerPlugin.api.models.gitRelated import com.intellij.openapi.project.Project import com.intellij.openapi.vfs.VirtualFile import git4idea.commands.GitCommand -import org.jetbrains.research.ideFormerPlugin.api.models.ReversibleApiMethod -import org.jetbrains.research.ideFormerPlugin.api.models.utils.executeGitCommand class GitCommit( - private val project: Project, - private val projectGitRoot: VirtualFile, - private val commitMessage: String -) : ReversibleApiMethod { + project: Project, + projectGitRoot: VirtualFile, + commitMessage: String +) : GitApiMethod(project, projectGitRoot) { + override var gitCommand: GitCommand? = GitCommand.COMMIT + override var gitCommandParameters: List? = listOf("-m", commitMessage) - override fun execute() { - executeGitCommand(project, projectGitRoot, GitCommand.COMMIT, listOf("-m", commitMessage)) - } - - /** - * Invariant: reverse is always called when the head of the branch is on that particular commit - **/ - override fun reverse() { - // drop the last commit - executeGitCommand(project, projectGitRoot, GitCommand.RESET, listOf("--soft", "HEAD~1")) - } + // Invariant: reverse is always called when the head of the branch is on that particular commit + override var gitReverseCommand: GitCommand? = GitCommand.RESET + override var gitReverseCommandParameters: List? = listOf("--soft", "HEAD~1") } \ No newline at end of file From 3997896a0b3e67f9f03e7254094f1c3590cf9fb1 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Wed, 3 Jan 2024 20:14:25 +0100 Subject: [PATCH 49/81] update git status due to inheritance from GitApiMethod base class --- .../ideFormerPlugin/api/models/gitRelated/GitStatus.kt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitStatus.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitStatus.kt index a5e8c11..6875465 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitStatus.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitStatus.kt @@ -2,19 +2,21 @@ package org.jetbrains.research.ideFormerPlugin.api.models.gitRelated import com.intellij.openapi.project.Project import com.intellij.openapi.vfs.VirtualFile -import git4idea.commands.* -import org.jetbrains.research.ideFormerPlugin.api.models.IdeApiMethod +import git4idea.commands.GitCommand import org.jetbrains.research.ideFormerPlugin.api.models.utils.UNCALLED_EXECUTE_BEFORE_RESULT_GETTING import org.jetbrains.research.ideFormerPlugin.api.models.utils.executeGitCommand class GitStatus( private val project: Project, private val projectGitRoot: VirtualFile -) : IdeApiMethod { +) : GitApiMethod(project, projectGitRoot) { + override var gitCommand: GitCommand? = GitCommand.STATUS + override var gitCommandParameters: List? = null + private var status: String? = null override fun execute() { - status = executeGitCommand(project, projectGitRoot, GitCommand.STATUS, null) + status = executeGitCommand(project, projectGitRoot, gitCommand!!, gitCommandParameters) } fun getStatus(): String { From abf83065b1fbcd84d451ed9831a83dca8f4ca969 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Wed, 3 Jan 2024 20:14:45 +0100 Subject: [PATCH 50/81] small log message fix --- .../server/requests/fileSystemRelated/getChangeDirectory.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getChangeDirectory.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getChangeDirectory.kt index 27de091..93def68 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getChangeDirectory.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getChangeDirectory.kt @@ -22,7 +22,7 @@ fun Routing.getChangeDirectory(logger: Logger, ideStateKeeper: IdeStateKeeper) { if (!executeAndRespondError(changeDirectory, logger)) return@get ideStateKeeper.saveReversibleApiMethod(changeDirectory) - logger.info("Change directory api method was saved on the api calls stack") + logger.info("Change directory api method was saved on the api methods stack") val response = targetDirName?.let { "$PROJECT_DIR_WAS_CHANGED_TO $targetDirName." From 29b8ea774882417331f870b41bf15009bd1fb94f Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Wed, 3 Jan 2024 20:35:01 +0100 Subject: [PATCH 51/81] add git command result saving, update git status --- .../api/models/gitRelated/GitApiMethod.kt | 19 +++++++++++++++++-- .../api/models/gitRelated/GitStatus.kt | 17 ++--------------- .../requests/gitRelated/getGitStatus.kt | 2 +- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitApiMethod.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitApiMethod.kt index a59a500..ee280c1 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitApiMethod.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitApiMethod.kt @@ -4,6 +4,7 @@ import com.intellij.openapi.project.Project import com.intellij.openapi.vfs.VirtualFile import git4idea.commands.GitCommand import org.jetbrains.research.ideFormerPlugin.api.models.ReversibleApiMethod +import org.jetbrains.research.ideFormerPlugin.api.models.utils.UNCALLED_EXECUTE_BEFORE_RESULT_GETTING import org.jetbrains.research.ideFormerPlugin.api.models.utils.executeGitCommand open class GitApiMethod( @@ -16,8 +17,22 @@ open class GitApiMethod( open var gitReverseCommand: GitCommand? = null open var gitReverseCommandParameters: List? = null + var gitCommandOutputString: String? = null + get() { + if (field == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) + return field!! + } + private set + + var gitReverseCommandOutputString: String? = null + get() { + if (field == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) + return field!! + } + private set + override fun execute() { - executeGitCommand( + gitCommandOutputString = executeGitCommand( project, projectGitRoot, gitCommand!!, @@ -31,7 +46,7 @@ open class GitApiMethod( **/ override fun reverse() { gitReverseCommand?.let { - executeGitCommand( + gitReverseCommandOutputString = executeGitCommand( project, projectGitRoot, it, diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitStatus.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitStatus.kt index 6875465..f01ac07 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitStatus.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitStatus.kt @@ -3,24 +3,11 @@ package org.jetbrains.research.ideFormerPlugin.api.models.gitRelated import com.intellij.openapi.project.Project import com.intellij.openapi.vfs.VirtualFile import git4idea.commands.GitCommand -import org.jetbrains.research.ideFormerPlugin.api.models.utils.UNCALLED_EXECUTE_BEFORE_RESULT_GETTING -import org.jetbrains.research.ideFormerPlugin.api.models.utils.executeGitCommand class GitStatus( - private val project: Project, - private val projectGitRoot: VirtualFile + project: Project, + projectGitRoot: VirtualFile ) : GitApiMethod(project, projectGitRoot) { override var gitCommand: GitCommand? = GitCommand.STATUS override var gitCommandParameters: List? = null - - private var status: String? = null - - override fun execute() { - status = executeGitCommand(project, projectGitRoot, gitCommand!!, gitCommandParameters) - } - - fun getStatus(): String { - if (status == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) - return status!! - } } \ No newline at end of file diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitStatus.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitStatus.kt index 2a1e6bb..8344441 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitStatus.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitStatus.kt @@ -14,7 +14,7 @@ fun Routing.getGitStatus(logger: Logger, ideStateKeeper: IdeStateKeeper) { val gitStatus = GitStatus(ideStateKeeper.userProject, ideStateKeeper.projectGitRoot) if (!executeAndRespondError(gitStatus, logger)) return@get - call.respondJson(gitStatus.getStatus()) + call.respondJson(gitStatus.gitCommandOutputString!!) logger.info("Server GET git status is processed") } } \ No newline at end of file From 32f97d7ba003e64c9da1c07ae90caa9da14f5968 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Thu, 4 Jan 2024 14:26:48 +0100 Subject: [PATCH 52/81] add git log --- .../api/models/gitRelated/GitLog.kt | 13 ++++++++++++ .../ideFormerPlugin/server/Routing.kt | 1 + .../server/requests/gitRelated/getGitLog.kt | 21 +++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitLog.kt create mode 100644 ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitLog.kt diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitLog.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitLog.kt new file mode 100644 index 0000000..a7be802 --- /dev/null +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitLog.kt @@ -0,0 +1,13 @@ +package org.jetbrains.research.ideFormerPlugin.api.models.gitRelated + +import com.intellij.openapi.project.Project +import com.intellij.openapi.vfs.VirtualFile +import git4idea.commands.GitCommand + +class GitLog( + project: Project, + projectGitRoot: VirtualFile +) : GitApiMethod(project, projectGitRoot) { + override var gitCommand: GitCommand? = GitCommand.LOG + override var gitCommandParameters: List? = null +} \ No newline at end of file diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt index 49bc46f..37699a0 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt @@ -34,6 +34,7 @@ fun Application.configureRouting(ideStateKeeper: IdeStateKeeper, logger: Logger) // git related getGitStatus(logger, ideStateKeeper) + getGitLog(logger, ideStateKeeper) getGitAdd(logger, ideStateKeeper) getGitCommit(logger, ideStateKeeper) getGitBranch(logger, ideStateKeeper) diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitLog.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitLog.kt new file mode 100644 index 0000000..235e8d1 --- /dev/null +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitLog.kt @@ -0,0 +1,21 @@ +package org.jetbrains.research.ideFormerPlugin.server.requests.gitRelated + +import io.ktor.server.application.* +import io.ktor.server.routing.* +import org.jetbrains.research.ideFormerPlugin.api.models.gitRelated.GitLog +import org.jetbrains.research.ideFormerPlugin.server.executeAndRespondError +import org.jetbrains.research.ideFormerPlugin.server.respondJson +import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper +import org.slf4j.Logger + +fun Routing.getGitLog(logger: Logger, ideStateKeeper: IdeStateKeeper) { + get("/git-log") { + logger.info("Server GET git log is called") + + val gitLog = GitLog(ideStateKeeper.userProject, ideStateKeeper.projectGitRoot) + if (!executeAndRespondError(gitLog, logger)) return@get + + call.respondJson(gitLog.gitCommandOutputString!!) + logger.info("Server GET git status is processed") + } +} \ No newline at end of file From 47d10af2dc8a86626fad90fa9c40049d90dba11d Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Thu, 4 Jan 2024 18:03:04 +0100 Subject: [PATCH 53/81] add git rm --- .../api/models/gitRelated/GitRm.kt | 17 ++++++++++++ .../ideFormerPlugin/server/Routing.kt | 1 + .../server/requests/gitRelated/getGitRm.kt | 26 +++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitRm.kt create mode 100644 ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitRm.kt diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitRm.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitRm.kt new file mode 100644 index 0000000..8234fb7 --- /dev/null +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitRm.kt @@ -0,0 +1,17 @@ +package org.jetbrains.research.ideFormerPlugin.api.models.gitRelated + +import com.intellij.openapi.project.Project +import com.intellij.openapi.vfs.VirtualFile +import git4idea.commands.GitCommand + +class GitRm( + project: Project, + projectGitRoot: VirtualFile, + fileNames: List +) : GitApiMethod(project, projectGitRoot) { + override var gitCommand: GitCommand? = GitCommand.RM + override var gitCommandParameters: List? = listOf("--cached") + fileNames + + override var gitReverseCommand: GitCommand? = GitCommand.ADD + override var gitReverseCommandParameters: List? = fileNames +} \ No newline at end of file diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt index 37699a0..1d6dc8d 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt @@ -36,6 +36,7 @@ fun Application.configureRouting(ideStateKeeper: IdeStateKeeper, logger: Logger) getGitStatus(logger, ideStateKeeper) getGitLog(logger, ideStateKeeper) getGitAdd(logger, ideStateKeeper) + getGitRm(logger, ideStateKeeper) getGitCommit(logger, ideStateKeeper) getGitBranch(logger, ideStateKeeper) getGitCheckout(logger, ideStateKeeper) diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitRm.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitRm.kt new file mode 100644 index 0000000..c9e1c47 --- /dev/null +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitRm.kt @@ -0,0 +1,26 @@ +package org.jetbrains.research.ideFormerPlugin.server.requests.gitRelated + +import io.ktor.server.application.* +import io.ktor.server.routing.* +import org.jetbrains.research.ideFormerPlugin.api.models.gitRelated.GitAdd +import org.jetbrains.research.ideFormerPlugin.api.models.gitRelated.GitRm +import org.jetbrains.research.ideFormerPlugin.server.* +import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper +import org.slf4j.Logger + +fun Routing.getGitRm(logger: Logger, ideStateKeeper: IdeStateKeeper) { + // TODO: add the ability to send a list of file names in the request + get("/git-rm/{${IdeServerConstants.FILE_NAME_REQUEST_PARAM}?}") { + val fileName = call.processFileNameParameter(logger) ?: return@get + logger.info("Server GET git rm for the file '$fileName' is called") + + val gitRm = GitRm(ideStateKeeper.userProject, ideStateKeeper.projectGitRoot, listOf(fileName)) + if (!executeAndRespondError(gitRm, logger)) return@get + + ideStateKeeper.saveReversibleApiMethod(gitRm) + logger.info("Git rm api method was saved on the api methods stack") + + call.respondJson("File $fileName was successfully removed from the Git Index") + logger.info("Server GET git rm for the file '$fileName' is processed") + } +} \ No newline at end of file From 8ad7620cb8a265eebfe4437cd219a18fe04fd65d Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 5 Jan 2024 11:55:16 +0100 Subject: [PATCH 54/81] add git reset --- .../api/models/gitRelated/GitReset.kt | 17 +++++++++++++ .../ideFormerPlugin/server/Routing.kt | 2 ++ .../server/requests/gitRelated/getGitReset.kt | 24 +++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitReset.kt create mode 100644 ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitReset.kt diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitReset.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitReset.kt new file mode 100644 index 0000000..66050ab --- /dev/null +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitReset.kt @@ -0,0 +1,17 @@ +package org.jetbrains.research.ideFormerPlugin.api.models.gitRelated + +import com.intellij.openapi.project.Project +import com.intellij.openapi.vfs.VirtualFile +import git4idea.commands.GitCommand + +class GitReset( + project: Project, + projectGitRoot: VirtualFile, + commitsCount: Int +): GitApiMethod(project, projectGitRoot) { + override var gitCommand: GitCommand? = GitCommand.RESET + override var gitCommandParameters: List? = listOf("HEAD~$commitsCount") + + override var gitReverseCommand: GitCommand? = GitCommand.RESET + override var gitReverseCommandParameters: List? = listOf("HEAD@{1}") +} \ No newline at end of file diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt index 1d6dc8d..6387f58 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt @@ -38,6 +38,7 @@ fun Application.configureRouting(ideStateKeeper: IdeStateKeeper, logger: Logger) getGitAdd(logger, ideStateKeeper) getGitRm(logger, ideStateKeeper) getGitCommit(logger, ideStateKeeper) + getGitReset(logger, ideStateKeeper) getGitBranch(logger, ideStateKeeper) getGitCheckout(logger, ideStateKeeper) } @@ -62,6 +63,7 @@ object IdeServerConstants { const val FUNCTION_NAME_REQUEST_PARAM = "functionName" const val COMMIT_MESSAGE_REQUEST_PARAM = "commitMessage" const val BRANCH_NAME_REQUEST_PARAM = "branchName" + const val COMMITS_COUNT_REQUEST_PARAM = "commitsCount" const val MISSING_REQUEST_PARAM = "Missing request parameter" } \ No newline at end of file diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitReset.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitReset.kt new file mode 100644 index 0000000..9b8994c --- /dev/null +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitReset.kt @@ -0,0 +1,24 @@ +package org.jetbrains.research.ideFormerPlugin.server.requests.gitRelated + +import io.ktor.server.application.* +import io.ktor.server.routing.* +import org.jetbrains.research.ideFormerPlugin.api.models.gitRelated.GitReset +import org.jetbrains.research.ideFormerPlugin.server.* +import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper +import org.slf4j.Logger + +fun Routing.getGitReset(logger: Logger, ideStateKeeper: IdeStateKeeper) { + get("/git-reset/{${IdeServerConstants.COMMITS_COUNT_REQUEST_PARAM}?}") { + val commitsCount = call.parameters[IdeServerConstants.COMMITS_COUNT_REQUEST_PARAM]?.toIntOrNull() ?: return@get + logger.info("Server GET git reset for '$commitsCount' commits is called") + + val gitReset = GitReset(ideStateKeeper.userProject, ideStateKeeper.projectGitRoot, commitsCount) + if (!executeAndRespondError(gitReset, logger)) return@get + + ideStateKeeper.saveReversibleApiMethod(gitReset) + logger.info("Git reset api method was saved on the api methods stack") + + call.respondJson("$commitsCount commits were successfully reset") + logger.info("Server GET git reset for '$commitsCount' commits is processed") + } +} \ No newline at end of file From c28f5e050d9db952bd35c704ca478b3053dd4280 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 5 Jan 2024 14:10:22 +0100 Subject: [PATCH 55/81] add json descriptions for recently created git apis --- .../server/ideApiDescriptions.json | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json index 4638401..9af5cc5 100644 --- a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json +++ b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json @@ -134,6 +134,14 @@ "required_parameters": [], "optional_parameters": [] }, + { + "name": "git_log", + "url": "/git-log", + "description": "Lists the commits made in that repository in reverse chronological order; that is, the most recent commits show up first.", + "method": "GET", + "required_parameters": [], + "optional_parameters": [] + }, { "name": "git_add", "url": "/git-add", @@ -149,6 +157,21 @@ ], "optional_parameters": [] }, + { + "name": "git_rm", + "url": "/git-rm", + "description": "Removes the file from the Git index.", + "method": "GET", + "required_parameters": [ + { + "name": "fileName", + "type": "STRING", + "regex": "^[\\w,\\s-]+\\.[A-Za-z]{2, 3, 4, 5}$", + "description": "The name of the file that should be removed from the Git index." + } + ], + "optional_parameters": [] + }, { "name": "git_commit", "url": "/git-commit", @@ -164,6 +187,21 @@ ], "optional_parameters": [] }, + { + "name": "git_reset", + "url": "/git-reset", + "description": "Is used to undo the changes in the working directory and get back to a specific commit while discarding all the commits made after that one.", + "method": "GET", + "required_parameters": [ + { + "name": "commitsCount", + "type": "INT", + "regex": "^[1-9]\\d*", + "description": "Count of the last commits that should be dropped from the branch." + } + ], + "optional_parameters": [] + }, { "name": "git_branch", "url": "/git-branch", From aeee12da25d663b4439e12817513a3443bff23cf Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 5 Jan 2024 15:02:46 +0100 Subject: [PATCH 56/81] add git merge --- .../api/models/gitRelated/GitMerge.kt | 20 +++++++++++++++ .../api/models/utils/GitUtils.kt | 2 +- .../ideFormerPlugin/server/Routing.kt | 1 + .../server/requests/gitRelated/getGitMerge.kt | 25 +++++++++++++++++++ .../server/ideApiDescriptions.json | 15 +++++++++++ 5 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitMerge.kt create mode 100644 ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitMerge.kt diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitMerge.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitMerge.kt new file mode 100644 index 0000000..fec749a --- /dev/null +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/gitRelated/GitMerge.kt @@ -0,0 +1,20 @@ +package org.jetbrains.research.ideFormerPlugin.api.models.gitRelated + +import com.intellij.openapi.project.Project +import com.intellij.openapi.vfs.VirtualFile +import git4idea.commands.GitCommand + +class GitMerge( + project: Project, + projectGitRoot: VirtualFile, + mergingBranchName: String +) : GitApiMethod(project, projectGitRoot) { + // Invariant: you're staying on the right branch that you want to merge changes to. Git checkout was made recently. + override var gitCommand: GitCommand? = GitCommand.MERGE + override var gitCommandParameters: List? = listOf(mergingBranchName) + + override var gitReverseCommand: GitCommand? = GitCommand.RESET + // --merge as opposed to the --hard doesn't reset uncommited/ unstashed files. + // ORIG_HEAD will point to a commit directly before merge has occurred, so you don't have to hunt for it by yourself. + override var gitReverseCommandParameters: List? = listOf("--merge", "ORIG_HEAD") +} \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/GitUtils.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/GitUtils.kt index 4a58cf1..e198708 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/GitUtils.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/GitUtils.kt @@ -9,7 +9,7 @@ fun executeGitCommand( projectGitRoot: VirtualFile, gitCommand: GitCommand, gitCommandParameters: List? -) : String { +): String { val git = Git.getInstance() val gitCommandHandler = GitLineHandler(project, projectGitRoot, gitCommand) diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt index 6387f58..b46dd30 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt @@ -41,6 +41,7 @@ fun Application.configureRouting(ideStateKeeper: IdeStateKeeper, logger: Logger) getGitReset(logger, ideStateKeeper) getGitBranch(logger, ideStateKeeper) getGitCheckout(logger, ideStateKeeper) + getGitMerge(logger, ideStateKeeper) } } diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitMerge.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitMerge.kt new file mode 100644 index 0000000..c5e29e7 --- /dev/null +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitMerge.kt @@ -0,0 +1,25 @@ +package org.jetbrains.research.ideFormerPlugin.server.requests.gitRelated + +import io.ktor.server.application.* +import io.ktor.server.routing.* +import org.jetbrains.research.ideFormerPlugin.api.models.gitRelated.GitMerge +import org.jetbrains.research.ideFormerPlugin.server.* +import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper +import org.slf4j.Logger + +fun Routing.getGitMerge(logger: Logger, ideStateKeeper: IdeStateKeeper) { + get("/git-merge/{${IdeServerConstants.BRANCH_NAME_REQUEST_PARAM}?}") { + val mergingBranchName = call.processRequestParameter(IdeServerConstants.BRANCH_NAME_REQUEST_PARAM, logger) + ?: return@get + logger.info("Server GET git merge $mergingBranchName into current branch is called") + + val gitMerge = GitMerge(ideStateKeeper.userProject, ideStateKeeper.projectGitRoot, mergingBranchName) + if (!executeAndRespondError(gitMerge, logger)) return@get + + ideStateKeeper.saveReversibleApiMethod(gitMerge) + logger.info("Git merge api method was saved on the api methods stack") + + call.respondJson("Git merge $mergingBranchName into current branch was processed successfully") + logger.info("Server GET git merge $mergingBranchName into current branch is processed") + } +} \ No newline at end of file diff --git a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json index 9af5cc5..c139ef0 100644 --- a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json +++ b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json @@ -232,6 +232,21 @@ ], "optional_parameters": [] }, + { + "name": "git_merge", + "url": "/git-merge", + "description": "Merge commits from the given branch to the current branch of a Git repository.", + "method": "GET", + "required_parameters": [ + { + "name": "branchName", + "type": "STRING", + "regex": "[A-Za-z]*", + "description": "The name of a branch that should be merged to the current branch of a Git repository." + } + ], + "optional_parameters": [] + }, { "name": "reverse_last_api_methods", "url": "/reverse-api-methods", From dde81917cb53ac962ddfaaf61f54ce40b1e18b32 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 5 Jan 2024 17:06:06 +0100 Subject: [PATCH 57/81] add 'create file' api --- .../models/fileSystemRelated/CreateFile.kt | 26 +++++++++++++++++++ .../ideFormerPlugin/server/Routing.kt | 5 ++-- .../fileSystemRelated/getCreateFile.kt | 24 +++++++++++++++++ 3 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/CreateFile.kt create mode 100644 ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getCreateFile.kt diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/CreateFile.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/CreateFile.kt new file mode 100644 index 0000000..9c31e59 --- /dev/null +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/CreateFile.kt @@ -0,0 +1,26 @@ +package org.jetbrains.research.ideFormerPlugin.api.models.fileSystemRelated + +import com.intellij.openapi.command.WriteCommandAction +import com.intellij.psi.PsiDirectory +import org.jetbrains.research.ideFormerPlugin.api.models.ReversibleApiMethod +import org.jetbrains.research.ideFormerPlugin.api.models.utils.findFileRecursively + +class CreateFile( + private val projectDirectory: PsiDirectory, + private val fileName: String +) : ReversibleApiMethod { + + override fun execute() { + // TODO: to fix: if a file is deleted manually from the project, it's still has status 'exist' in the project directory + // TODO: common problem. projectDirectory isn't updated when manually actions are done. + WriteCommandAction.runWriteCommandAction(projectDirectory.project) { + projectDirectory.createFile(fileName) + } + } + + override fun reverse() { + WriteCommandAction.runWriteCommandAction(projectDirectory.project) { + projectDirectory.findFileRecursively(fileName).delete() + } + } +} \ No newline at end of file diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt index b46dd30..b90a7ce 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt @@ -6,9 +6,7 @@ import org.jetbrains.research.ideFormerPlugin.server.requests.* import org.jetbrains.research.ideFormerPlugin.server.requests.fileRelated.getFileClasses import org.jetbrains.research.ideFormerPlugin.server.requests.fileRelated.getFileFunctions import org.jetbrains.research.ideFormerPlugin.server.requests.fileRelated.getFileText -import org.jetbrains.research.ideFormerPlugin.server.requests.fileSystemRelated.getChangeDirectory -import org.jetbrains.research.ideFormerPlugin.server.requests.fileSystemRelated.getListDirectoryContents -import org.jetbrains.research.ideFormerPlugin.server.requests.fileSystemRelated.getProjectModules +import org.jetbrains.research.ideFormerPlugin.server.requests.fileSystemRelated.* import org.jetbrains.research.ideFormerPlugin.server.requests.gitRelated.* import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper import org.slf4j.Logger @@ -31,6 +29,7 @@ fun Application.configureRouting(ideStateKeeper: IdeStateKeeper, logger: Logger) getProjectModules(logger, ideStateKeeper) getListDirectoryContents(logger, ideStateKeeper) getChangeDirectory(logger, ideStateKeeper) + getCreateFile(logger, ideStateKeeper) // git related getGitStatus(logger, ideStateKeeper) diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getCreateFile.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getCreateFile.kt new file mode 100644 index 0000000..c1fef33 --- /dev/null +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getCreateFile.kt @@ -0,0 +1,24 @@ +package org.jetbrains.research.ideFormerPlugin.server.requests.fileSystemRelated + +import io.ktor.server.application.* +import io.ktor.server.routing.* +import org.jetbrains.research.ideFormerPlugin.api.models.fileSystemRelated.CreateFile +import org.jetbrains.research.ideFormerPlugin.server.* +import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper +import org.slf4j.Logger + +fun Routing.getCreateFile(logger: Logger, ideStateKeeper: IdeStateKeeper) { + get("/create-file/{fileName?}") { + val fileName = call.processFileNameParameter(logger) ?: return@get + logger.info("Server GET create '$fileName' file request is called") + + val createFile = CreateFile(ideStateKeeper.currentProjectDirectory, fileName) + if (!executeAndRespondError(createFile, logger)) return@get + + ideStateKeeper.saveReversibleApiMethod(createFile) + logger.info("Create file api method was saved on the api methods stack") + + call.respondJson("A file with name '$fileName' has been created successfully") + logger.info("Server GET create '$fileName' file request is processed") + } +} \ No newline at end of file From ea4a7b356232f9be640431a6f41938f44378b373 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 5 Jan 2024 17:20:27 +0100 Subject: [PATCH 58/81] add 'create file' api --- .../server/requests/fileSystemRelated/getCreateFile.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getCreateFile.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getCreateFile.kt index c1fef33..6f9a986 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getCreateFile.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getCreateFile.kt @@ -18,7 +18,7 @@ fun Routing.getCreateFile(logger: Logger, ideStateKeeper: IdeStateKeeper) { ideStateKeeper.saveReversibleApiMethod(createFile) logger.info("Create file api method was saved on the api methods stack") - call.respondJson("A file with name '$fileName' has been created successfully") + call.respondJson("A file with the name '$fileName' has been created successfully") logger.info("Server GET create '$fileName' file request is processed") } } \ No newline at end of file From 9a1788179c486c231174cf6efe4f677eccfc446f Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 5 Jan 2024 17:41:29 +0100 Subject: [PATCH 59/81] add 'create directory' api --- .../fileSystemRelated/CreateDirectory.kt | 26 +++++++++++++++++ .../ideFormerPlugin/server/Routing.kt | 2 ++ .../fileSystemRelated/getCreateDirectory.kt | 28 +++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/CreateDirectory.kt create mode 100644 ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getCreateDirectory.kt diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/CreateDirectory.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/CreateDirectory.kt new file mode 100644 index 0000000..0202634 --- /dev/null +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/CreateDirectory.kt @@ -0,0 +1,26 @@ +package org.jetbrains.research.ideFormerPlugin.api.models.fileSystemRelated + +import com.intellij.openapi.command.WriteCommandAction +import com.intellij.psi.PsiDirectory +import org.jetbrains.research.ideFormerPlugin.api.models.ReversibleApiMethod +import org.jetbrains.research.ideFormerPlugin.api.models.utils.findSubdirectoryRecursively + +class CreateDirectory( + private val projectDirectory: PsiDirectory, + private val directoryName: String +) : ReversibleApiMethod { + + override fun execute() { + // TODO: to fix: if a directory is deleted manually from the project, it's still has status 'exist' in the project directory + // TODO: common problem. projectDirectory isn't updated when manually actions are done. + WriteCommandAction.runWriteCommandAction(projectDirectory.project) { + projectDirectory.createSubdirectory(directoryName) + } + } + + override fun reverse() { + WriteCommandAction.runWriteCommandAction(projectDirectory.project) { + projectDirectory.findSubdirectoryRecursively(directoryName).delete() + } + } +} \ No newline at end of file diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt index b90a7ce..fe9db71 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt @@ -30,6 +30,7 @@ fun Application.configureRouting(ideStateKeeper: IdeStateKeeper, logger: Logger) getListDirectoryContents(logger, ideStateKeeper) getChangeDirectory(logger, ideStateKeeper) getCreateFile(logger, ideStateKeeper) + getCreateDirectory(logger, ideStateKeeper) // git related getGitStatus(logger, ideStateKeeper) @@ -59,6 +60,7 @@ object IdeServerConstants { // TODO: renaming // requests parameters const val FILE_NAME_REQUEST_PARAM = "fileName" + const val DIRECTORY_NAME_REQUEST_PARAM = "directoryName" const val CLASS_NAME_REQUEST_PARAM = "className" const val FUNCTION_NAME_REQUEST_PARAM = "functionName" const val COMMIT_MESSAGE_REQUEST_PARAM = "commitMessage" diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getCreateDirectory.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getCreateDirectory.kt new file mode 100644 index 0000000..f2f8dd3 --- /dev/null +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getCreateDirectory.kt @@ -0,0 +1,28 @@ +package org.jetbrains.research.ideFormerPlugin.server.requests.fileSystemRelated + +import io.ktor.server.application.* +import io.ktor.server.routing.* +import org.jetbrains.research.ideFormerPlugin.api.models.fileSystemRelated.CreateDirectory +import org.jetbrains.research.ideFormerPlugin.server.* +import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.DIRECTORY_NAME_REQUEST_PARAM +import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper +import org.slf4j.Logger + +fun Routing.getCreateDirectory(logger: Logger, ideStateKeeper: IdeStateKeeper) { + get("/create-directory/{$DIRECTORY_NAME_REQUEST_PARAM?}") { + val directoryName = call.processRequestParameter( + DIRECTORY_NAME_REQUEST_PARAM, + logger + ) ?: return@get + logger.info("Server GET create '$directoryName' directory request is called") + + val createDirectory = CreateDirectory(ideStateKeeper.currentProjectDirectory, directoryName) + if (!executeAndRespondError(createDirectory, logger)) return@get + + ideStateKeeper.saveReversibleApiMethod(createDirectory) + logger.info("Create directory api method was saved on the api methods stack") + + call.respondJson("A directory with the name '$directoryName' has been created successfully") + logger.info("Server GET create '$directoryName' directory request is processed") + } +} \ No newline at end of file From c2d01217257224e9e17fc9c1430d7dadaf0bada3 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 5 Jan 2024 17:49:03 +0100 Subject: [PATCH 60/81] make file creating/ deleting methods separate functions --- .../models/fileSystemRelated/CreateFile.kt | 12 ++++------- .../api/models/utils/PsiElementsUtils.kt | 21 +++++++++++++++++++ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/CreateFile.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/CreateFile.kt index 9c31e59..ec9f63f 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/CreateFile.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/CreateFile.kt @@ -1,9 +1,9 @@ package org.jetbrains.research.ideFormerPlugin.api.models.fileSystemRelated -import com.intellij.openapi.command.WriteCommandAction import com.intellij.psi.PsiDirectory import org.jetbrains.research.ideFormerPlugin.api.models.ReversibleApiMethod -import org.jetbrains.research.ideFormerPlugin.api.models.utils.findFileRecursively +import org.jetbrains.research.ideFormerPlugin.api.models.utils.createFileByName +import org.jetbrains.research.ideFormerPlugin.api.models.utils.deleteFileByName class CreateFile( private val projectDirectory: PsiDirectory, @@ -13,14 +13,10 @@ class CreateFile( override fun execute() { // TODO: to fix: if a file is deleted manually from the project, it's still has status 'exist' in the project directory // TODO: common problem. projectDirectory isn't updated when manually actions are done. - WriteCommandAction.runWriteCommandAction(projectDirectory.project) { - projectDirectory.createFile(fileName) - } + projectDirectory.createFileByName(fileName) } override fun reverse() { - WriteCommandAction.runWriteCommandAction(projectDirectory.project) { - projectDirectory.findFileRecursively(fileName).delete() - } + projectDirectory.deleteFileByName(fileName) } } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt index b9eb543..e4f8138 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt @@ -1,5 +1,6 @@ package org.jetbrains.research.ideFormerPlugin.api.models.utils +import com.intellij.openapi.command.WriteCommandAction import com.intellij.psi.* import com.intellij.psi.util.PsiTreeUtil @@ -35,4 +36,24 @@ inline fun getFilePsiElementsOfType( ): List { val psiFile = projectDirectory.findFileRecursively(fileName) return psiFile.psiElementsOfType() +} + +fun PsiDirectory.createFileByName(fileName: String) { + WriteCommandAction.runWriteCommandAction(this.project) { + this.createFile(fileName) + } +} + +fun PsiDirectory.deleteFileByName(fileName: String) { + WriteCommandAction.runWriteCommandAction(this.project) { + this.findFileRecursively(fileName).delete() + } +} + +fun createDirectory() { + +} + +fun deleteDirectory() { + } \ No newline at end of file From a02c2a1e0b09da4445c7634ba5c65a10016c087d Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 5 Jan 2024 20:35:20 +0100 Subject: [PATCH 61/81] update switching to a tread with a write access --- .../api/models/utils/PsiElementsUtils.kt | 47 ++++++++++++++++--- 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt index e4f8138..707e04f 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt @@ -1,8 +1,10 @@ package org.jetbrains.research.ideFormerPlugin.api.models.utils -import com.intellij.openapi.command.WriteCommandAction +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.editor.Document import com.intellij.psi.* import com.intellij.psi.util.PsiTreeUtil +import java.util.concurrent.CompletableFuture const val PATH_DELIMITER = "/" @@ -16,6 +18,7 @@ fun PsiDirectory.findSubdirectoryRecursively(targetDirectoryPath: String): PsiDi return if (remainingDirectoryPath.isNotEmpty()) currentDirectory.findSubdirectoryRecursively(remainingDirectoryPath) else currentDirectory } + fun PsiDirectory.findFileRecursively(targetFilePath: String): PsiFile { val targetFileDirectory = when (val fileDirectoryPath = targetFilePath.substringBeforeLast(PATH_DELIMITER, "")) { "" -> this @@ -27,6 +30,7 @@ fun PsiDirectory.findFileRecursively(targetFilePath: String): PsiFile { ?: error("No such file in the current directory") } + inline fun PsiFile.psiElementsOfType(): List = PsiTreeUtil.findChildrenOfType(this, T::class.java).toList() @@ -38,15 +42,45 @@ inline fun getFilePsiElementsOfType( return psiFile.psiElementsOfType() } -fun PsiDirectory.createFileByName(fileName: String) { - WriteCommandAction.runWriteCommandAction(this.project) { - this.createFile(fileName) + +fun PsiDirectory.createFileByName(fileName: String) : PsiFile { + val createdFileFuture = CompletableFuture() + + ApplicationManager.getApplication().let { + it.invokeAndWait { + val createdFile = it.runWriteAction { + this.createFile(fileName) + } + createdFileFuture.complete(createdFile) + } } + return createdFileFuture.get() } + fun PsiDirectory.deleteFileByName(fileName: String) { - WriteCommandAction.runWriteCommandAction(this.project) { - this.findFileRecursively(fileName).delete() + ApplicationManager.getApplication().let { + it.invokeAndWait { + it.runWriteAction { + this.findFileRecursively(fileName).delete() + } + } + } +} + +fun PsiFile.setText(text: String) { + ApplicationManager.getApplication().let { + it.invokeAndWait { + it.runWriteAction { + val psiDocumentManager = PsiDocumentManager.getInstance(this.project) + val document: Document? = psiDocumentManager.getDocument(this) + + if (document != null) { + document.setText(text) + psiDocumentManager.commitDocument(document) + } + } + } } } @@ -54,6 +88,7 @@ fun createDirectory() { } + fun deleteDirectory() { } \ No newline at end of file From 343317b1685e56cf8e4e7a61a2939e36e1260877 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Fri, 5 Jan 2024 20:35:42 +0100 Subject: [PATCH 62/81] add 'delete file' api --- .../models/fileSystemRelated/DeleteFile.kt | 24 +++++++++++++++++++ .../ideFormerPlugin/server/Routing.kt | 1 + .../fileSystemRelated/getDeleteFile.kt | 24 +++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/DeleteFile.kt create mode 100644 ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getDeleteFile.kt diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/DeleteFile.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/DeleteFile.kt new file mode 100644 index 0000000..22b6ab7 --- /dev/null +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/DeleteFile.kt @@ -0,0 +1,24 @@ +package org.jetbrains.research.ideFormerPlugin.api.models.fileSystemRelated + +import com.intellij.psi.PsiDirectory +import org.jetbrains.research.ideFormerPlugin.api.models.ReversibleApiMethod +import org.jetbrains.research.ideFormerPlugin.api.models.utils.* + +class DeleteFile( + private val projectDirectory: PsiDirectory, + private val fileName: String +) : ReversibleApiMethod { + private val deletedFileText: String = projectDirectory.findFileRecursively(fileName).text + + override fun execute() { + // TODO: to fix: if a file is deleted manually from the project, it's still has status 'exist' in the project directory + // TODO: common problem. projectDirectory isn't updated when manually actions are done. + projectDirectory.deleteFileByName(fileName) + } + + override fun reverse() { + val psiFile = projectDirectory.createFileByName(fileName) + // TODO: to fix it. a method doesn't work + psiFile.setText(deletedFileText) + } +} \ No newline at end of file diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt index fe9db71..766253e 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt @@ -30,6 +30,7 @@ fun Application.configureRouting(ideStateKeeper: IdeStateKeeper, logger: Logger) getListDirectoryContents(logger, ideStateKeeper) getChangeDirectory(logger, ideStateKeeper) getCreateFile(logger, ideStateKeeper) + getDeleteFile(logger, ideStateKeeper) getCreateDirectory(logger, ideStateKeeper) // git related diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getDeleteFile.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getDeleteFile.kt new file mode 100644 index 0000000..5f1182a --- /dev/null +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getDeleteFile.kt @@ -0,0 +1,24 @@ +package org.jetbrains.research.ideFormerPlugin.server.requests.fileSystemRelated + +import io.ktor.server.application.* +import io.ktor.server.routing.* +import org.jetbrains.research.ideFormerPlugin.api.models.fileSystemRelated.DeleteFile +import org.jetbrains.research.ideFormerPlugin.server.* +import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper +import org.slf4j.Logger + +fun Routing.getDeleteFile(logger: Logger, ideStateKeeper: IdeStateKeeper) { + get("/delete-file/{fileName?}") { + val fileName = call.processFileNameParameter(logger) ?: return@get + logger.info("Server GET delete '$fileName' file request is called") + + val deleteFile = DeleteFile(ideStateKeeper.currentProjectDirectory, fileName) + if (!executeAndRespondError(deleteFile, logger)) return@get + + ideStateKeeper.saveReversibleApiMethod(deleteFile) + logger.info("Delete file api method was saved on the api methods stack") + + call.respondJson("A file with the name '$fileName' has been deleted successfully") + logger.info("Server GET delete '$fileName' file request is processed") + } +} \ No newline at end of file From fb15e3dc9d23e7c9b36bf0e412d31a12ce7e824a Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Mon, 8 Jan 2024 14:54:27 +0100 Subject: [PATCH 63/81] add attempts to init git repo for test project --- .../ideFormerPlugin/api/models/IdeApiModelsTest.kt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/IdeApiModelsTest.kt b/ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/IdeApiModelsTest.kt index 6f41825..9e9f756 100644 --- a/ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/IdeApiModelsTest.kt +++ b/ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/IdeApiModelsTest.kt @@ -26,6 +26,15 @@ abstract class IdeApiModelsTest : BasePlatformTestCase() { myFixture.copyFileToProject("dir1/pyFile.py") myFixture.copyFileToProject("dir1/SomeJavaClass.java") myFixture.copyFileToProject("dir2/subdir/subsubdir/someTextFile.txt") + + // git repo init +// val git = Git.getInstance() +// val commandResult = git.init(project, project.projectFile!!) +// if (!commandResult.success()) { +// println("Git was successfully initialized for this project") +// } else { +// error("Error while git repo init") +// } } protected fun checkListDirectoryContentsExecution( From f3956fb6f9be1f6df99b30065730035a86891a51 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Mon, 8 Jan 2024 14:58:35 +0100 Subject: [PATCH 64/81] move find project base dir method from IdeStateKeeper to utils --- .../ideFormerPlugin/stateKeeper/IdeStateKeeper.kt | 13 ++----------- .../stateKeeper/PsiElementsUtils.kt | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 11 deletions(-) create mode 100644 ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/stateKeeper/PsiElementsUtils.kt diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/stateKeeper/IdeStateKeeper.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/stateKeeper/IdeStateKeeper.kt index 291faa9..45bfc34 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/stateKeeper/IdeStateKeeper.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/stateKeeper/IdeStateKeeper.kt @@ -1,12 +1,10 @@ package org.jetbrains.research.ideFormerPlugin.stateKeeper import com.intellij.openapi.actionSystem.DataContext -import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.project.Project -import com.intellij.openapi.project.guessProjectDir import com.intellij.openapi.vfs.VirtualFile import com.intellij.psi.PsiDirectory -import com.intellij.psi.PsiManager +import findProjectBaseDirectory import git4idea.GitVcs import git4idea.actions.GitPull import git4idea.branch.GitBranchUtil @@ -18,7 +16,7 @@ import java.util.* class IdeStateKeeper(val userProject: Project) { private val apiMethodStack: Stack = Stack() - var currentProjectDirectory: PsiDirectory = findProjectBaseDirectory() + var currentProjectDirectory: PsiDirectory = userProject.findProjectBaseDirectory() private val projectGitRepo: GitRepository val projectGitRoot: VirtualFile @@ -37,13 +35,6 @@ class IdeStateKeeper(val userProject: Project) { projectGitRoot = projectGitRepo.root } - // TODO: to think about null project path processing - private fun findProjectBaseDirectory() = userProject.guessProjectDir()?.let { projectBaseDir -> - ApplicationManager.getApplication().runReadAction { - PsiManager.getInstance(userProject).findDirectory(projectBaseDir) - } - } ?: error("No project file path") - fun saveReversibleApiMethod(apiMethod: ReversibleApiMethod) = apiMethodStack.addElement(apiMethod) fun reverseLastApiMethods(apiMethodsCount: Int = DEFAULT_API_METHODS_COUNT_TO_REVERSE): Int { diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/stateKeeper/PsiElementsUtils.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/stateKeeper/PsiElementsUtils.kt new file mode 100644 index 0000000..54bc753 --- /dev/null +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/stateKeeper/PsiElementsUtils.kt @@ -0,0 +1,14 @@ +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.project.Project +import com.intellij.openapi.project.guessProjectDir +import com.intellij.psi.PsiDirectory +import com.intellij.psi.PsiManager + +// TODO: to think about null project path processing +fun Project.findProjectBaseDirectory() : PsiDirectory { + return this.guessProjectDir()?.let { projectBaseDir -> + ApplicationManager.getApplication().runReadAction { + PsiManager.getInstance(this).findDirectory(projectBaseDir) + } + } ?: error("No project file path") +} \ No newline at end of file From 6225cfaa683be06ca3f48eb2a60b7df56b4c667c Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Mon, 8 Jan 2024 18:29:31 +0100 Subject: [PATCH 65/81] fix bug so now file is saved to local disk after its text changing --- .../api/models/utils/PsiElementsUtils.kt | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt index 707e04f..86f6439 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt @@ -1,7 +1,8 @@ package org.jetbrains.research.ideFormerPlugin.api.models.utils import com.intellij.openapi.application.ApplicationManager -import com.intellij.openapi.editor.Document +import com.intellij.openapi.command.WriteCommandAction +import com.intellij.openapi.fileEditor.FileDocumentManager import com.intellij.psi.* import com.intellij.psi.util.PsiTreeUtil import java.util.concurrent.CompletableFuture @@ -43,12 +44,12 @@ inline fun getFilePsiElementsOfType( } -fun PsiDirectory.createFileByName(fileName: String) : PsiFile { +fun PsiDirectory.createFileByName(fileName: String): PsiFile { val createdFileFuture = CompletableFuture() ApplicationManager.getApplication().let { it.invokeAndWait { - val createdFile = it.runWriteAction { + val createdFile = it.runWriteAction { this.createFile(fileName) } createdFileFuture.complete(createdFile) @@ -68,22 +69,34 @@ fun PsiDirectory.deleteFileByName(fileName: String) { } } +fun getPsiFileText(projectDirectory: PsiDirectory, fileName: String): String { + return projectDirectory.findFileRecursively(fileName).text +} + fun PsiFile.setText(text: String) { ApplicationManager.getApplication().let { it.invokeAndWait { - it.runWriteAction { - val psiDocumentManager = PsiDocumentManager.getInstance(this.project) - val document: Document? = psiDocumentManager.getDocument(this) + val psiDocumentManager = PsiDocumentManager.getInstance(this.project) + val document = psiDocumentManager.getDocument(this) + ?: error("No document for the psi file ${this.name}") - if (document != null) { - document.setText(text) - psiDocumentManager.commitDocument(document) - } + WriteCommandAction.runWriteCommandAction(project) { + document.setText(text) + } + psiDocumentManager.commitDocument(document) + + // Optionally, we can also reformat the code if needed + // CodeStyleManager.getInstance(project).reformat(this) + + // Save the document explicitly to ensure changes are written to disk + this.virtualFile?.run { + FileDocumentManager.getInstance().saveDocument(document) } } } } + fun createDirectory() { } From d7c625c8e5700183acfe25bbc75acc188d339ab0 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Mon, 8 Jan 2024 18:30:12 +0100 Subject: [PATCH 66/81] add set file text api --- .../api/models/fileRelated/SetFileText.kt | 26 +++++++++++++++++++ .../ideFormerPlugin/server/Routing.kt | 6 ++--- .../requests/fileRelated/getSetFileText.kt | 25 ++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/SetFileText.kt create mode 100644 ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getSetFileText.kt diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/SetFileText.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/SetFileText.kt new file mode 100644 index 0000000..097c550 --- /dev/null +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/SetFileText.kt @@ -0,0 +1,26 @@ +package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated + +import com.intellij.psi.PsiDirectory +import org.jetbrains.research.ideFormerPlugin.api.models.ReversibleApiMethod +import org.jetbrains.research.ideFormerPlugin.api.models.utils.* + +class SetFileText( + private val projectDirectory: PsiDirectory, + private val fileName: String, + private val newText: String +) : ReversibleApiMethod { + private var previousText: String? = null + + override fun execute() { + projectDirectory.virtualFile.refresh(false, false) + val psiFile = projectDirectory.findFileRecursively(fileName) + previousText = psiFile.text + psiFile.setText(newText) + } + + override fun reverse() { + val psiFile = projectDirectory.findFileRecursively(fileName) + if (previousText == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) + psiFile.setText(previousText!!) + } +} \ No newline at end of file diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt index 766253e..7449ece 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/Routing.kt @@ -3,9 +3,7 @@ package org.jetbrains.research.ideFormerPlugin.server import io.ktor.server.application.* import io.ktor.server.routing.* import org.jetbrains.research.ideFormerPlugin.server.requests.* -import org.jetbrains.research.ideFormerPlugin.server.requests.fileRelated.getFileClasses -import org.jetbrains.research.ideFormerPlugin.server.requests.fileRelated.getFileFunctions -import org.jetbrains.research.ideFormerPlugin.server.requests.fileRelated.getFileText +import org.jetbrains.research.ideFormerPlugin.server.requests.fileRelated.* import org.jetbrains.research.ideFormerPlugin.server.requests.fileSystemRelated.* import org.jetbrains.research.ideFormerPlugin.server.requests.gitRelated.* import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper @@ -24,6 +22,7 @@ fun Application.configureRouting(ideStateKeeper: IdeStateKeeper, logger: Logger) getFileText(logger, ideStateKeeper) getFileFunctions(logger, ideStateKeeper) getFileClasses(logger, ideStateKeeper) + getSetFileText(logger, ideStateKeeper) // file system related getProjectModules(logger, ideStateKeeper) @@ -64,6 +63,7 @@ object IdeServerConstants { const val DIRECTORY_NAME_REQUEST_PARAM = "directoryName" const val CLASS_NAME_REQUEST_PARAM = "className" const val FUNCTION_NAME_REQUEST_PARAM = "functionName" + const val FILE_TEXT_REQUEST_PARAM = "fileText" const val COMMIT_MESSAGE_REQUEST_PARAM = "commitMessage" const val BRANCH_NAME_REQUEST_PARAM = "branchName" const val COMMITS_COUNT_REQUEST_PARAM = "commitsCount" diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getSetFileText.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getSetFileText.kt new file mode 100644 index 0000000..ac86c71 --- /dev/null +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getSetFileText.kt @@ -0,0 +1,25 @@ +package org.jetbrains.research.ideFormerPlugin.server.requests.fileRelated + +import io.ktor.server.application.* +import io.ktor.server.routing.* +import org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.SetFileText +import org.jetbrains.research.ideFormerPlugin.server.* +import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.FILE_NAME_REQUEST_PARAM +import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.FILE_TEXT_REQUEST_PARAM +import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper +import org.slf4j.Logger + +// TODO: make it a post method +fun Routing.getSetFileText(logger: Logger, ideStateKeeper: IdeStateKeeper) { + get("/set-file-text/{${FILE_NAME_REQUEST_PARAM}?}{${FILE_TEXT_REQUEST_PARAM}?}") { + val fileName = call.processFileNameParameter(logger) ?: return@get + val newText = call.processRequestParameter(FILE_TEXT_REQUEST_PARAM, logger) ?: return@get + logger.info("Server GET set file text request for file '$fileName' is called") + + val setFileText = SetFileText(ideStateKeeper.currentProjectDirectory, fileName, newText) + if (!executeAndRespondError(setFileText, logger)) return@get + + call.respondJson("New text was set to a file") + logger.info("Server GET set file text request for file '$fileName' is processed") + } +} \ No newline at end of file From c29df5e60e378d284ad7ba705d51de6e65a776d7 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Tue, 9 Jan 2024 11:10:31 +0100 Subject: [PATCH 67/81] update delete file api --- .../api/models/fileSystemRelated/DeleteFile.kt | 5 ++--- .../ideFormerPlugin/api/models/utils/PsiElementsUtils.kt | 3 --- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/DeleteFile.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/DeleteFile.kt index 22b6ab7..95a0dc9 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/DeleteFile.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/DeleteFile.kt @@ -11,14 +11,13 @@ class DeleteFile( private val deletedFileText: String = projectDirectory.findFileRecursively(fileName).text override fun execute() { - // TODO: to fix: if a file is deleted manually from the project, it's still has status 'exist' in the project directory - // TODO: common problem. projectDirectory isn't updated when manually actions are done. + // TODO: to add this refreshing everywhere + projectDirectory.virtualFile.refresh(false, false) projectDirectory.deleteFileByName(fileName) } override fun reverse() { val psiFile = projectDirectory.createFileByName(fileName) - // TODO: to fix it. a method doesn't work psiFile.setText(deletedFileText) } } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt index 86f6439..55d67e0 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt @@ -69,9 +69,6 @@ fun PsiDirectory.deleteFileByName(fileName: String) { } } -fun getPsiFileText(projectDirectory: PsiDirectory, fileName: String): String { - return projectDirectory.findFileRecursively(fileName).text -} fun PsiFile.setText(text: String) { ApplicationManager.getApplication().let { From 5da140911ead8ded1b55409b71ce52e1f35b3fd9 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Tue, 9 Jan 2024 11:20:41 +0100 Subject: [PATCH 68/81] rename FileText -> GetFileText; move text file related apis to the separate directory --- .../fileRelated/{FileText.kt => fileText/GetFileText.kt} | 4 ++-- .../api/models/fileRelated/{ => fileText}/SetFileText.kt | 2 +- .../ideFormerPlugin/api/models/IdeApiModelsTest.kt | 4 ++-- .../fileRelated/{FileTextTest.kt => GetFileTextTest.kt} | 2 +- .../server/requests/fileRelated/getFileText.kt | 8 ++++---- .../server/requests/fileRelated/getSetFileText.kt | 2 +- 6 files changed, 11 insertions(+), 11 deletions(-) rename ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/{FileText.kt => fileText/GetFileText.kt} (95%) rename ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/{ => fileText}/SetFileText.kt (98%) rename ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/{FileTextTest.kt => GetFileTextTest.kt} (96%) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/FileText.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileText/GetFileText.kt similarity index 95% rename from ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/FileText.kt rename to ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileText/GetFileText.kt index 693f88f..60245cf 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/FileText.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileText/GetFileText.kt @@ -1,11 +1,11 @@ -package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated +package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileText import com.intellij.psi.PsiDirectory import org.jetbrains.research.ideFormerPlugin.api.models.IdeApiMethod import org.jetbrains.research.ideFormerPlugin.api.models.utils.UNCALLED_EXECUTE_BEFORE_RESULT_GETTING import org.jetbrains.research.ideFormerPlugin.api.models.utils.findFileRecursively -class FileText( +class GetFileText( private val projectDirectory: PsiDirectory, private val fileName: String ) : IdeApiMethod { diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/SetFileText.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileText/SetFileText.kt similarity index 98% rename from ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/SetFileText.kt rename to ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileText/SetFileText.kt index 097c550..09c0d68 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/SetFileText.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileText/SetFileText.kt @@ -1,4 +1,4 @@ -package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated +package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileText import com.intellij.psi.PsiDirectory import org.jetbrains.research.ideFormerPlugin.api.models.ReversibleApiMethod diff --git a/ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/IdeApiModelsTest.kt b/ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/IdeApiModelsTest.kt index 9e9f756..099ffe1 100644 --- a/ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/IdeApiModelsTest.kt +++ b/ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/IdeApiModelsTest.kt @@ -2,7 +2,7 @@ package org.jetbrains.research.ideFormerPlugin.api.models import com.intellij.testFramework.TestDataPath import com.intellij.testFramework.fixtures.BasePlatformTestCase -import org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.FileText +import org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileText.GetFileText import org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileClasses.FileClasses import org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileFunctions.FileFunctions import org.jetbrains.research.ideFormerPlugin.api.models.fileSystemRelated.ChangeDirectory @@ -120,7 +120,7 @@ abstract class IdeApiModelsTest : BasePlatformTestCase() { fileName: String, expectedFileText: String ) { - FileText(ideStateKeeper.currentProjectDirectory, fileName).also { + GetFileText(ideStateKeeper.currentProjectDirectory, fileName).also { it.execute() assertEquals(expectedFileText, it.getFileText()) } diff --git a/ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/FileTextTest.kt b/ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/GetFileTextTest.kt similarity index 96% rename from ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/FileTextTest.kt rename to ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/GetFileTextTest.kt index 8a904dd..b070fdd 100644 --- a/ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/FileTextTest.kt +++ b/ide-former-plugin/ide-core/src/test/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/GetFileTextTest.kt @@ -3,7 +3,7 @@ package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated import org.jetbrains.research.ideFormerPlugin.api.models.IdeApiModelsTest import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper -class FileTextTest : IdeApiModelsTest() { +class GetFileTextTest : IdeApiModelsTest() { fun testFileText() { val ideStateKeeper = IdeStateKeeper(project) diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileText.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileText.kt index 62b2638..e8a45c9 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileText.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getFileText.kt @@ -2,7 +2,7 @@ package org.jetbrains.research.ideFormerPlugin.server.requests.fileRelated import io.ktor.server.application.* import io.ktor.server.routing.* -import org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.FileText +import org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileText.GetFileText import org.jetbrains.research.ideFormerPlugin.server.* import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper import org.slf4j.Logger @@ -12,10 +12,10 @@ fun Routing.getFileText(logger: Logger, ideStateKeeper: IdeStateKeeper) { val fileName = call.processFileNameParameter(logger) ?: return@get logger.info("Server GET file text request for file '$fileName' is called") - val fileText = FileText(ideStateKeeper.currentProjectDirectory, fileName) - if (!executeAndRespondError(fileText, logger)) return@get + val getFileTextMethod = GetFileText(ideStateKeeper.currentProjectDirectory, fileName) + if (!executeAndRespondError(getFileTextMethod, logger)) return@get - call.respondJson(fileText.getFileText()) + call.respondJson(getFileTextMethod.getFileText()) logger.info("Server GET file text request for file '$fileName' is processed") } } \ No newline at end of file diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getSetFileText.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getSetFileText.kt index ac86c71..06c371d 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getSetFileText.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileRelated/getSetFileText.kt @@ -2,7 +2,7 @@ package org.jetbrains.research.ideFormerPlugin.server.requests.fileRelated import io.ktor.server.application.* import io.ktor.server.routing.* -import org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.SetFileText +import org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileText.SetFileText import org.jetbrains.research.ideFormerPlugin.server.* import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.FILE_NAME_REQUEST_PARAM import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.FILE_TEXT_REQUEST_PARAM From 467ac9fb159a0230eee948bc46fa5fdcde1bf89c Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Tue, 9 Jan 2024 11:38:28 +0100 Subject: [PATCH 69/81] add set file text api json desc --- .../server/ideApiDescriptions.json | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json index c139ef0..96efd0a 100644 --- a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json +++ b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json @@ -40,7 +40,7 @@ ] }, { - "name": "file_text", + "name": "get_file_text", "url": "/file-text", "description": "Gets a text of the requested file.", "method": "GET", @@ -54,6 +54,21 @@ ], "optional_parameters": [] }, + { + "name": "set_file_text", + "url": "/set-file-text", + "description": "Sets a new text to the requested file.", + "method": "GET", + "required_parameters": [ + { + "name": "fileName", + "type": "STRING", + "regex": "^[\\w,\\s-]+\\.[A-Za-z]{2, 3, 4, 5}$", + "description": "The name of the file whose text should be changed." + } + ], + "optional_parameters": [] + }, { "name": "file_functions", "url": "/file-functions", From 80e12e3c60a0556d94853f3a0cf865f8f67aebbf Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Wed, 10 Jan 2024 11:26:01 +0100 Subject: [PATCH 70/81] add directory refreshing to all file system related apis --- .../models/fileRelated/fileText/GetFileText.kt | 4 ++-- .../models/fileRelated/fileText/SetFileText.kt | 2 +- .../models/fileSystemRelated/ChangeDirectory.kt | 15 +++++++++------ .../models/fileSystemRelated/CreateDirectory.kt | 5 +++-- .../api/models/fileSystemRelated/CreateFile.kt | 7 +++---- .../api/models/fileSystemRelated/DeleteFile.kt | 4 ++-- .../fileSystemRelated/ListDirectoryContents.kt | 1 + .../models/fileSystemRelated/ProjectModules.kt | 1 + .../api/models/utils/PsiElementsUtils.kt | 6 ++++++ 9 files changed, 28 insertions(+), 17 deletions(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileText/GetFileText.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileText/GetFileText.kt index 60245cf..6cf84ee 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileText/GetFileText.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileText/GetFileText.kt @@ -2,8 +2,7 @@ package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileText import com.intellij.psi.PsiDirectory import org.jetbrains.research.ideFormerPlugin.api.models.IdeApiMethod -import org.jetbrains.research.ideFormerPlugin.api.models.utils.UNCALLED_EXECUTE_BEFORE_RESULT_GETTING -import org.jetbrains.research.ideFormerPlugin.api.models.utils.findFileRecursively +import org.jetbrains.research.ideFormerPlugin.api.models.utils.* class GetFileText( private val projectDirectory: PsiDirectory, @@ -12,6 +11,7 @@ class GetFileText( private var fileText: String? = null override fun execute() { + projectDirectory.refresh() val psiFile = projectDirectory.findFileRecursively(fileName) fileText = psiFile.text } diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileText/SetFileText.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileText/SetFileText.kt index 09c0d68..e38ae7e 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileText/SetFileText.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileText/SetFileText.kt @@ -12,7 +12,7 @@ class SetFileText( private var previousText: String? = null override fun execute() { - projectDirectory.virtualFile.refresh(false, false) + projectDirectory.refresh() val psiFile = projectDirectory.findFileRecursively(fileName) previousText = psiFile.text psiFile.setText(newText) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ChangeDirectory.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ChangeDirectory.kt index e852428..556d826 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ChangeDirectory.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ChangeDirectory.kt @@ -9,21 +9,24 @@ class ChangeDirectory( private val ideStateKeeper: IdeStateKeeper, private val targetDirectoryName: String = DEFAULT_DIRECTORY_NAME ) : ReversibleApiMethod { - private var prevDir: PsiDirectory? = null + private var previousDirectory: PsiDirectory? = null override fun execute() { - val targetDir = ideStateKeeper.currentProjectDirectory.findSubdirectoryRecursively(targetDirectoryName) + val currentProjectDirectory = ideStateKeeper.currentProjectDirectory + currentProjectDirectory.refresh() - prevDir = ideStateKeeper.currentProjectDirectory + val targetDir = currentProjectDirectory.findSubdirectoryRecursively(targetDirectoryName) + + previousDirectory = currentProjectDirectory ideStateKeeper.currentProjectDirectory = targetDir } override fun reverse() { - if (prevDir == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) + if (previousDirectory == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) - prevDir!!.let { + previousDirectory!!.let { ideStateKeeper.currentProjectDirectory = it - prevDir = null + previousDirectory = null } } } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/CreateDirectory.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/CreateDirectory.kt index 0202634..7cb7cd5 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/CreateDirectory.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/CreateDirectory.kt @@ -4,6 +4,7 @@ import com.intellij.openapi.command.WriteCommandAction import com.intellij.psi.PsiDirectory import org.jetbrains.research.ideFormerPlugin.api.models.ReversibleApiMethod import org.jetbrains.research.ideFormerPlugin.api.models.utils.findSubdirectoryRecursively +import org.jetbrains.research.ideFormerPlugin.api.models.utils.refresh class CreateDirectory( private val projectDirectory: PsiDirectory, @@ -11,15 +12,15 @@ class CreateDirectory( ) : ReversibleApiMethod { override fun execute() { - // TODO: to fix: if a directory is deleted manually from the project, it's still has status 'exist' in the project directory - // TODO: common problem. projectDirectory isn't updated when manually actions are done. WriteCommandAction.runWriteCommandAction(projectDirectory.project) { + projectDirectory.refresh() projectDirectory.createSubdirectory(directoryName) } } override fun reverse() { WriteCommandAction.runWriteCommandAction(projectDirectory.project) { + projectDirectory.refresh() projectDirectory.findSubdirectoryRecursively(directoryName).delete() } } diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/CreateFile.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/CreateFile.kt index ec9f63f..6be338e 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/CreateFile.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/CreateFile.kt @@ -2,8 +2,7 @@ package org.jetbrains.research.ideFormerPlugin.api.models.fileSystemRelated import com.intellij.psi.PsiDirectory import org.jetbrains.research.ideFormerPlugin.api.models.ReversibleApiMethod -import org.jetbrains.research.ideFormerPlugin.api.models.utils.createFileByName -import org.jetbrains.research.ideFormerPlugin.api.models.utils.deleteFileByName +import org.jetbrains.research.ideFormerPlugin.api.models.utils.* class CreateFile( private val projectDirectory: PsiDirectory, @@ -11,12 +10,12 @@ class CreateFile( ) : ReversibleApiMethod { override fun execute() { - // TODO: to fix: if a file is deleted manually from the project, it's still has status 'exist' in the project directory - // TODO: common problem. projectDirectory isn't updated when manually actions are done. + projectDirectory.refresh() projectDirectory.createFileByName(fileName) } override fun reverse() { + projectDirectory.refresh() projectDirectory.deleteFileByName(fileName) } } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/DeleteFile.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/DeleteFile.kt index 95a0dc9..676ef15 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/DeleteFile.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/DeleteFile.kt @@ -11,12 +11,12 @@ class DeleteFile( private val deletedFileText: String = projectDirectory.findFileRecursively(fileName).text override fun execute() { - // TODO: to add this refreshing everywhere - projectDirectory.virtualFile.refresh(false, false) + projectDirectory.refresh() projectDirectory.deleteFileByName(fileName) } override fun reverse() { + projectDirectory.refresh() val psiFile = projectDirectory.createFileByName(fileName) psiFile.setText(deletedFileText) } diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ListDirectoryContents.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ListDirectoryContents.kt index e245f5e..b3cf07c 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ListDirectoryContents.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ListDirectoryContents.kt @@ -18,6 +18,7 @@ class ListDirectoryContents( } override fun execute() { + currentProjectDirectory.refresh() val searchDirectory = currentProjectDirectory.findSubdirectoryRecursively(searchDirectoryName) searchDirectoryItems = searchDirectory.fileSystemItems() } diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ProjectModules.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ProjectModules.kt index 2bf4a90..604f478 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ProjectModules.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ProjectModules.kt @@ -11,6 +11,7 @@ class ProjectModules(private val project: Project) : IdeApiMethod { private fun Project.modules(): List = ModuleManager.getInstance(this).modules.toList() override fun execute() { + project.projectFile?.refresh(false, false) projectModules = project.modules() } diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt index 55d67e0..487ca82 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt @@ -39,6 +39,7 @@ inline fun getFilePsiElementsOfType( projectDirectory: PsiDirectory, fileName: String ): List { + projectDirectory.refresh() val psiFile = projectDirectory.findFileRecursively(fileName) return psiFile.psiElementsOfType() } @@ -70,6 +71,11 @@ fun PsiDirectory.deleteFileByName(fileName: String) { } +fun PsiDirectory.refresh() { + this.virtualFile.refresh(false, true) +} + + fun PsiFile.setText(text: String) { ApplicationManager.getApplication().let { it.invokeAndWait { From c29c4cc0de655fa0e76c0c5f0417297a9f80a2c6 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Wed, 10 Jan 2024 11:33:02 +0100 Subject: [PATCH 71/81] set previous data to null when reverse is called in reversible file system apis --- .../api/models/fileRelated/fileText/SetFileText.kt | 8 ++++++-- .../api/models/fileSystemRelated/CreateDirectory.kt | 3 ++- .../api/models/fileSystemRelated/DeleteFile.kt | 8 ++++++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileText/SetFileText.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileText/SetFileText.kt index e38ae7e..4dd0d6a 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileText/SetFileText.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileRelated/fileText/SetFileText.kt @@ -19,8 +19,12 @@ class SetFileText( } override fun reverse() { - val psiFile = projectDirectory.findFileRecursively(fileName) if (previousText == null) error(UNCALLED_EXECUTE_BEFORE_RESULT_GETTING) - psiFile.setText(previousText!!) + + previousText!!.let { + val psiFile = projectDirectory.findFileRecursively(fileName) + psiFile.setText(it) + previousText = null + } } } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/CreateDirectory.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/CreateDirectory.kt index 7cb7cd5..2dc4053 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/CreateDirectory.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/CreateDirectory.kt @@ -21,7 +21,8 @@ class CreateDirectory( override fun reverse() { WriteCommandAction.runWriteCommandAction(projectDirectory.project) { projectDirectory.refresh() - projectDirectory.findSubdirectoryRecursively(directoryName).delete() + val psiDirectory = projectDirectory.findSubdirectoryRecursively(directoryName) + psiDirectory.delete() } } } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/DeleteFile.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/DeleteFile.kt index 676ef15..f48152b 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/DeleteFile.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/DeleteFile.kt @@ -8,7 +8,7 @@ class DeleteFile( private val projectDirectory: PsiDirectory, private val fileName: String ) : ReversibleApiMethod { - private val deletedFileText: String = projectDirectory.findFileRecursively(fileName).text + private var deletedFileText: String? = projectDirectory.findFileRecursively(fileName).text override fun execute() { projectDirectory.refresh() @@ -18,6 +18,10 @@ class DeleteFile( override fun reverse() { projectDirectory.refresh() val psiFile = projectDirectory.createFileByName(fileName) - psiFile.setText(deletedFileText) + + deletedFileText!!.let { + psiFile.setText(it) + deletedFileText = null + } } } \ No newline at end of file From 041eb80d27534cded6583939f8e0e2a7579d5d31 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Wed, 10 Jan 2024 11:49:10 +0100 Subject: [PATCH 72/81] move create/ delete subdirectory functions to utils --- .../models/fileSystemRelated/CreateDirectory.kt | 16 ++++------------ .../api/models/utils/PsiElementsUtils.kt | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/CreateDirectory.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/CreateDirectory.kt index 2dc4053..4acfa6a 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/CreateDirectory.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/CreateDirectory.kt @@ -1,10 +1,9 @@ package org.jetbrains.research.ideFormerPlugin.api.models.fileSystemRelated -import com.intellij.openapi.command.WriteCommandAction import com.intellij.psi.PsiDirectory import org.jetbrains.research.ideFormerPlugin.api.models.ReversibleApiMethod -import org.jetbrains.research.ideFormerPlugin.api.models.utils.findSubdirectoryRecursively -import org.jetbrains.research.ideFormerPlugin.api.models.utils.refresh +import org.jetbrains.research.ideFormerPlugin.api.models.utils.createSubdirectoryByName +import org.jetbrains.research.ideFormerPlugin.api.models.utils.deleteSubdirectoryByName class CreateDirectory( private val projectDirectory: PsiDirectory, @@ -12,17 +11,10 @@ class CreateDirectory( ) : ReversibleApiMethod { override fun execute() { - WriteCommandAction.runWriteCommandAction(projectDirectory.project) { - projectDirectory.refresh() - projectDirectory.createSubdirectory(directoryName) - } + projectDirectory.createSubdirectoryByName(directoryName) } override fun reverse() { - WriteCommandAction.runWriteCommandAction(projectDirectory.project) { - projectDirectory.refresh() - val psiDirectory = projectDirectory.findSubdirectoryRecursively(directoryName) - psiDirectory.delete() - } + projectDirectory.deleteSubdirectoryByName(directoryName) } } \ No newline at end of file diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt index 487ca82..b583fb3 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt @@ -70,6 +70,21 @@ fun PsiDirectory.deleteFileByName(fileName: String) { } } +fun PsiDirectory.createSubdirectoryByName(directoryName: String) { + WriteCommandAction.runWriteCommandAction(project) { + this.refresh() + this.createSubdirectory(directoryName) + } +} + +fun PsiDirectory.deleteSubdirectoryByName(directoryName: String) { + WriteCommandAction.runWriteCommandAction(project) { + this.refresh() + val psiDirectory = this.findSubdirectoryRecursively(directoryName) + psiDirectory.delete() + } +} + fun PsiDirectory.refresh() { this.virtualFile.refresh(false, true) From 073a2bf36c5f95f19c5c70e860cea33068973fce Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Wed, 10 Jan 2024 14:37:58 +0100 Subject: [PATCH 73/81] add create file api json desc --- .../server/ideApiDescriptions.json | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json index 96efd0a..4e392bc 100644 --- a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json +++ b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json @@ -39,6 +39,21 @@ } ] }, + { + "name": "create_file", + "url": "/create-file", + "description": "Creates a new file with a given name in a current working directory.", + "method": "GET", + "required_parameters": [], + "optional_parameters": [ + { + "name": "fileName", + "type": "STRING", + "regex": "^[\\w,\\s-]+\\.[A-Za-z]{2, 3, 4, 5}$", + "description": "The name of the file that should be created." + } + ] + }, { "name": "get_file_text", "url": "/file-text", From 6a97565bc62770c157b98f7e5d3e2e29a3561124 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Wed, 10 Jan 2024 14:42:14 +0100 Subject: [PATCH 74/81] add create directory api json desc --- .../server/ideApiDescriptions.json | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json index 4e392bc..d47577d 100644 --- a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json +++ b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json @@ -39,6 +39,21 @@ } ] }, + { + "name": "create_directory", + "url": "/create-directory", + "description": "Creates a new directory with a given name in a current working directory.", + "method": "GET", + "required_parameters": [], + "optional_parameters": [ + { + "name": "directoryName", + "type": "STRING", + "regex": "[^\"\\{\\}]*?", + "description": "The name of the directory that should be created." + } + ] + }, { "name": "create_file", "url": "/create-file", From 204e404d5e10a51286c112115ab338711b98232c Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Wed, 10 Jan 2024 14:44:14 +0100 Subject: [PATCH 75/81] add delete file api json desc --- .../server/ideApiDescriptions.json | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json index d47577d..a158f57 100644 --- a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json +++ b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json @@ -69,6 +69,21 @@ } ] }, + { + "name": "delete_file", + "url": "/delete-file", + "description": "Deletes a file with a given name in a current working directory.", + "method": "GET", + "required_parameters": [], + "optional_parameters": [ + { + "name": "fileName", + "type": "STRING", + "regex": "[^\"\\{\\}]*?", + "description": "The name of the file that should be deleted." + } + ] + }, { "name": "get_file_text", "url": "/file-text", From 04ae61941a7267a5ba39b2febd11c747833406b5 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Wed, 10 Jan 2024 14:53:30 +0100 Subject: [PATCH 76/81] add some comments todo --- .../ideFormerPlugin/api/models/fileSystemRelated/DeleteFile.kt | 1 + .../server/requests/fileSystemRelated/getChangeDirectory.kt | 1 + .../server/requests/fileSystemRelated/getCreateDirectory.kt | 1 + .../server/requests/fileSystemRelated/getCreateFile.kt | 1 + .../server/requests/fileSystemRelated/getDeleteFile.kt | 1 + .../ideFormerPlugin/server/requests/gitRelated/getGitReset.kt | 1 + 6 files changed, 6 insertions(+) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/DeleteFile.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/DeleteFile.kt index f48152b..4dcc6f6 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/DeleteFile.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/DeleteFile.kt @@ -12,6 +12,7 @@ class DeleteFile( override fun execute() { projectDirectory.refresh() + // TODO: getting deleting file text should be here. when execute() is called it may be different projectDirectory.deleteFileByName(fileName) } diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getChangeDirectory.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getChangeDirectory.kt index 93def68..ef89a79 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getChangeDirectory.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getChangeDirectory.kt @@ -10,6 +10,7 @@ import org.jetbrains.research.ideFormerPlugin.server.respondJson import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper import org.slf4j.Logger +// TODO: make it a post method fun Routing.getChangeDirectory(logger: Logger, ideStateKeeper: IdeStateKeeper) { get("/change-dir/{targetDirName?}") { val targetDirName = call.parameters["targetDirName"] diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getCreateDirectory.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getCreateDirectory.kt index f2f8dd3..af2aa05 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getCreateDirectory.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getCreateDirectory.kt @@ -8,6 +8,7 @@ import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.DIRECTOR import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper import org.slf4j.Logger +// TODO: make it a post method fun Routing.getCreateDirectory(logger: Logger, ideStateKeeper: IdeStateKeeper) { get("/create-directory/{$DIRECTORY_NAME_REQUEST_PARAM?}") { val directoryName = call.processRequestParameter( diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getCreateFile.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getCreateFile.kt index 6f9a986..fffcb6c 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getCreateFile.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getCreateFile.kt @@ -7,6 +7,7 @@ import org.jetbrains.research.ideFormerPlugin.server.* import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper import org.slf4j.Logger +// TODO: make it a post method fun Routing.getCreateFile(logger: Logger, ideStateKeeper: IdeStateKeeper) { get("/create-file/{fileName?}") { val fileName = call.processFileNameParameter(logger) ?: return@get diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getDeleteFile.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getDeleteFile.kt index 5f1182a..acec864 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getDeleteFile.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getDeleteFile.kt @@ -7,6 +7,7 @@ import org.jetbrains.research.ideFormerPlugin.server.* import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper import org.slf4j.Logger +// TODO: make it a post method fun Routing.getDeleteFile(logger: Logger, ideStateKeeper: IdeStateKeeper) { get("/delete-file/{fileName?}") { val fileName = call.processFileNameParameter(logger) ?: return@get diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitReset.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitReset.kt index 9b8994c..cc9b825 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitReset.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/gitRelated/getGitReset.kt @@ -9,6 +9,7 @@ import org.slf4j.Logger fun Routing.getGitReset(logger: Logger, ideStateKeeper: IdeStateKeeper) { get("/git-reset/{${IdeServerConstants.COMMITS_COUNT_REQUEST_PARAM}?}") { + // TODO: fix parsing val commitsCount = call.parameters[IdeServerConstants.COMMITS_COUNT_REQUEST_PARAM]?.toIntOrNull() ?: return@get logger.info("Server GET git reset for '$commitsCount' commits is called") From 57f9777c035530b98352bba34fa67efdd469cdd1 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Mon, 15 Jan 2024 23:20:45 +0100 Subject: [PATCH 77/81] add fileText parameter json desc to the set file text server request --- .../research/ideFormerPlugin/server/ideApiDescriptions.json | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json index a158f57..e85011d 100644 --- a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json +++ b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json @@ -110,6 +110,12 @@ "type": "STRING", "regex": "^[\\w,\\s-]+\\.[A-Za-z]{2, 3, 4, 5}$", "description": "The name of the file whose text should be changed." + }, + { + "name": "fileText", + "type": "STRING", + "regex": "*", + "description": "A new text to be inserted into the file." } ], "optional_parameters": [] From cba5ee62a9c0d90befb6d2039f1f69dbcf963606 Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Mon, 15 Jan 2024 23:23:23 +0100 Subject: [PATCH 78/81] update regexps for fileName and fileText parameters --- .../server/ideApiDescriptions.json | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json index e85011d..c6e70f4 100644 --- a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json +++ b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json @@ -64,7 +64,7 @@ { "name": "fileName", "type": "STRING", - "regex": "^[\\w,\\s-]+\\.[A-Za-z]{2, 3, 4, 5}$", + "regex": "^[\\w,\\s-]+\\.[A-Za-z]{2,5}$", "description": "The name of the file that should be created." } ] @@ -79,7 +79,7 @@ { "name": "fileName", "type": "STRING", - "regex": "[^\"\\{\\}]*?", + "regex": "^[\\w,\\s-]+\\.[A-Za-z]{2,5}$", "description": "The name of the file that should be deleted." } ] @@ -93,7 +93,7 @@ { "name": "fileName", "type": "STRING", - "regex": "^[\\w,\\s-]+\\.[A-Za-z]{2, 3, 4, 5}$", + "regex": "^[\\w,\\s-]+\\.[A-Za-z]{2,5}$", "description": "The name of the file whose text should be returned." } ], @@ -108,13 +108,13 @@ { "name": "fileName", "type": "STRING", - "regex": "^[\\w,\\s-]+\\.[A-Za-z]{2, 3, 4, 5}$", + "regex": "^[\\w,\\s-]+\\.[A-Za-z]{2,5}$", "description": "The name of the file whose text should be changed." }, { "name": "fileText", "type": "STRING", - "regex": "*", + "regex": ".*", "description": "A new text to be inserted into the file." } ], @@ -129,7 +129,7 @@ { "name": "fileName", "type": "STRING", - "regex": "^[\\w,\\s-]+\\.[A-Za-z]{2, 3, 4, 5}$", + "regex": "^[\\w,\\s-]+\\.[A-Za-z]{2,5}$", "description": "The name of the file whose functions should be returned." } ], @@ -144,7 +144,7 @@ { "name": "fileName", "type": "STRING", - "regex": "^[\\w,\\s-]+\\.[A-Za-z]{2, 3, 4, 5}$", + "regex": "^[\\w,\\s-]+\\.[A-Za-z]{2,5}$", "description": "The name of the file in which the requested function code is located." }, { @@ -165,7 +165,7 @@ { "name": "fileName", "type": "STRING", - "regex": "^[\\w,\\s-]+\\.[A-Za-z]{2, 3, 4, 5}$", + "regex": "^[\\w,\\s-]+\\.[A-Za-z]{2,5}$", "description": "The name of the file whose classes should be returned." } ], @@ -180,7 +180,7 @@ { "name": "fileName", "type": "STRING", - "regex": "^[\\w,\\s-]+\\.[A-Za-z]{2, 3, 4, 5}$", + "regex": "^[\\w,\\s-]+\\.[A-Za-z]{2,5}$", "description": "The name of the file in which the requested function code is located." }, { @@ -217,7 +217,7 @@ { "name": "fileName", "type": "STRING", - "regex": "^[\\w,\\s-]+\\.[A-Za-z]{2, 3, 4, 5}$", + "regex": "^[\\w,\\s-]+\\.[A-Za-z]{2,5}$", "description": "The name of the file that should be added to Git index." } ], @@ -232,7 +232,7 @@ { "name": "fileName", "type": "STRING", - "regex": "^[\\w,\\s-]+\\.[A-Za-z]{2, 3, 4, 5}$", + "regex": "^[\\w,\\s-]+\\.[A-Za-z]{2,5}$", "description": "The name of the file that should be removed from the Git index." } ], From f2c481bd2989e926d15c36f61181c1c655a588ef Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Tue, 16 Jan 2024 22:11:34 +0100 Subject: [PATCH 79/81] remove regexps for fileText parameter --- .../research/ideFormerPlugin/server/ideApiDescriptions.json | 1 - 1 file changed, 1 deletion(-) diff --git a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json index c6e70f4..a423f2c 100644 --- a/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json +++ b/ide-former-plugin/ide-server/src/main/resources/org/jetbrains/research/ideFormerPlugin/server/ideApiDescriptions.json @@ -114,7 +114,6 @@ { "name": "fileText", "type": "STRING", - "regex": ".*", "description": "A new text to be inserted into the file." } ], From 459b2349ba3adc64adfdb2e71a503a739a07843b Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Tue, 16 Jan 2024 22:21:40 +0100 Subject: [PATCH 80/81] add opportunity to call 'cd ..' -- change working directory to a parent directory --- .../api/models/fileSystemRelated/ChangeDirectory.kt | 5 ++++- .../research/ideFormerPlugin/api/models/utils/ApiUtils.kt | 1 + .../server/requests/fileSystemRelated/getChangeDirectory.kt | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ChangeDirectory.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ChangeDirectory.kt index 556d826..9053e52 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ChangeDirectory.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ChangeDirectory.kt @@ -15,7 +15,10 @@ class ChangeDirectory( val currentProjectDirectory = ideStateKeeper.currentProjectDirectory currentProjectDirectory.refresh() - val targetDir = currentProjectDirectory.findSubdirectoryRecursively(targetDirectoryName) + val targetDir = when(targetDirectoryName) { + PARENT_DIRECTORY_NAME -> currentProjectDirectory.parentDirectory ?: error("No parent directory") + else -> currentProjectDirectory.findSubdirectoryRecursively(targetDirectoryName) + } previousDirectory = currentProjectDirectory ideStateKeeper.currentProjectDirectory = targetDir diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/ApiUtils.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/ApiUtils.kt index d176958..4ce16de 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/ApiUtils.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/ApiUtils.kt @@ -6,6 +6,7 @@ import org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileClasses import org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileFunctions.* const val DEFAULT_DIRECTORY_NAME = "." +const val PARENT_DIRECTORY_NAME = ".." const val UNCALLED_EXECUTE_BEFORE_RESULT_GETTING = "Execute() must be called before getting the result. The property storing the result has not been initialized yet" diff --git a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getChangeDirectory.kt b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getChangeDirectory.kt index ef89a79..ecfa3f1 100644 --- a/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getChangeDirectory.kt +++ b/ide-former-plugin/ide-server/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/server/requests/fileSystemRelated/getChangeDirectory.kt @@ -26,7 +26,7 @@ fun Routing.getChangeDirectory(logger: Logger, ideStateKeeper: IdeStateKeeper) { logger.info("Change directory api method was saved on the api methods stack") val response = targetDirName?.let { - "$PROJECT_DIR_WAS_CHANGED_TO $targetDirName." + "$PROJECT_DIR_WAS_CHANGED_TO '$targetDirName'." } ?: PROJECT_DIR_REMAINS_THE_SAME call.respondJson(response) From 9114db853c75d57dfaca37f29f804fe24ccc31fd Mon Sep 17 00:00:00 2001 From: "Evgeniia.Fedotova" Date: Tue, 16 Jan 2024 22:32:34 +0100 Subject: [PATCH 81/81] make parent directory searching recursive: move it to the right util function, rename this function --- .../api/models/fileSystemRelated/ChangeDirectory.kt | 5 +---- .../models/fileSystemRelated/ListDirectoryContents.kt | 2 +- .../api/models/utils/PsiElementsUtils.kt | 10 ++++++---- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ChangeDirectory.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ChangeDirectory.kt index 9053e52..5e7f037 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ChangeDirectory.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ChangeDirectory.kt @@ -15,10 +15,7 @@ class ChangeDirectory( val currentProjectDirectory = ideStateKeeper.currentProjectDirectory currentProjectDirectory.refresh() - val targetDir = when(targetDirectoryName) { - PARENT_DIRECTORY_NAME -> currentProjectDirectory.parentDirectory ?: error("No parent directory") - else -> currentProjectDirectory.findSubdirectoryRecursively(targetDirectoryName) - } + val targetDir = currentProjectDirectory.findDirectoryRecursively(targetDirectoryName) previousDirectory = currentProjectDirectory ideStateKeeper.currentProjectDirectory = targetDir diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ListDirectoryContents.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ListDirectoryContents.kt index b3cf07c..8746bb2 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ListDirectoryContents.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/fileSystemRelated/ListDirectoryContents.kt @@ -19,7 +19,7 @@ class ListDirectoryContents( override fun execute() { currentProjectDirectory.refresh() - val searchDirectory = currentProjectDirectory.findSubdirectoryRecursively(searchDirectoryName) + val searchDirectory = currentProjectDirectory.findDirectoryRecursively(searchDirectoryName) searchDirectoryItems = searchDirectory.fileSystemItems() } diff --git a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt index b583fb3..0f3e704 100644 --- a/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt +++ b/ide-former-plugin/ide-core/src/main/kotlin/org/jetbrains/research/ideFormerPlugin/api/models/utils/PsiElementsUtils.kt @@ -9,21 +9,22 @@ import java.util.concurrent.CompletableFuture const val PATH_DELIMITER = "/" -fun PsiDirectory.findSubdirectoryRecursively(targetDirectoryPath: String): PsiDirectory { +fun PsiDirectory.findDirectoryRecursively(targetDirectoryPath: String): PsiDirectory { val currentDirectory = when (val nextDirectoryInPath = targetDirectoryPath.substringBefore(PATH_DELIMITER)) { DEFAULT_DIRECTORY_NAME -> this + PARENT_DIRECTORY_NAME -> this.parentDirectory ?: error("Current directory doesn't have a parent directory") else -> this.findSubdirectory(nextDirectoryInPath) ?: error("No such subdirectory: $nextDirectoryInPath") } val remainingDirectoryPath = targetDirectoryPath.substringAfter(PATH_DELIMITER, "") - return if (remainingDirectoryPath.isNotEmpty()) currentDirectory.findSubdirectoryRecursively(remainingDirectoryPath) else currentDirectory + return if (remainingDirectoryPath.isNotEmpty()) currentDirectory.findDirectoryRecursively(remainingDirectoryPath) else currentDirectory } fun PsiDirectory.findFileRecursively(targetFilePath: String): PsiFile { val targetFileDirectory = when (val fileDirectoryPath = targetFilePath.substringBeforeLast(PATH_DELIMITER, "")) { "" -> this - else -> this.findSubdirectoryRecursively(fileDirectoryPath) + else -> this.findDirectoryRecursively(fileDirectoryPath) } val fileName = targetFilePath.substringAfterLast(PATH_DELIMITER) @@ -64,6 +65,7 @@ fun PsiDirectory.deleteFileByName(fileName: String) { ApplicationManager.getApplication().let { it.invokeAndWait { it.runWriteAction { + // TODO: можно возвращать отсюда старый удаленный файл и как бы из него текст вытаскивать.... this.findFileRecursively(fileName).delete() } } @@ -80,7 +82,7 @@ fun PsiDirectory.createSubdirectoryByName(directoryName: String) { fun PsiDirectory.deleteSubdirectoryByName(directoryName: String) { WriteCommandAction.runWriteCommandAction(project) { this.refresh() - val psiDirectory = this.findSubdirectoryRecursively(directoryName) + val psiDirectory = this.findDirectoryRecursively(directoryName) psiDirectory.delete() } }