Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python projects support, new file related APIs #2

Merged
merged 63 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
91ecf3f
fix respond massage for the reverse server request
diffitask Oct 31, 2023
d7cb323
add findFileByName utils method, add FileText api method
diffitask Oct 31, 2023
f39aa13
add FileClasses api method
diffitask Oct 31, 2023
f6fc2d4
add test for the FileText api method
diffitask Oct 31, 2023
9e24b60
update findFileByName utils func, refactor kt file methods api
diffitask Oct 31, 2023
e604f13
update someKtFile2.kt test example, update tests
diffitask Oct 31, 2023
0074f8c
replace KtNamedFunction with more general KtFunction objects finding …
diffitask Oct 31, 2023
bd2dd07
add PythonCore dependency to use com.jetbrains.python, upgrade platfo…
diffitask Nov 1, 2023
f770e35
rename kt file functions api due to its update
diffitask Nov 1, 2023
1eda054
add py file functions api
diffitask Nov 1, 2023
150fe99
add java file functions api
diffitask Nov 1, 2023
5c56fc9
move ls and cd apis to fileSystemRelated dir
diffitask Nov 1, 2023
d5d1dd2
update JavaFileClasses api
diffitask Nov 2, 2023
770cdfe
add PyFileClasses api
diffitask Nov 2, 2023
a92aeb0
add KtFileClasses api
diffitask Nov 2, 2023
f1cc411
add get java class code method to the JavaFileClasses api
diffitask Nov 2, 2023
26ebe24
add test code for file classes apis, test kt file classes api
diffitask Nov 2, 2023
11b1dac
add tests for java and python files classes apis, add new test files
diffitask Nov 2, 2023
0de6034
add tests for java and python files functions apis
diffitask Nov 2, 2023
83b7689
fix bug with fileExtension in tests
diffitask Nov 2, 2023
38d1662
fix bug in python file name in test
diffitask Nov 2, 2023
f6cb55f
update null list handling in tests, add file extension getting functi…
diffitask Nov 2, 2023
ac2fc17
move utils to utils directory, add more folders for server requests, …
diffitask Nov 2, 2023
d1b3b57
add FileClasses interface
diffitask Nov 2, 2023
0bac876
add FileMethods interface
diffitask Nov 2, 2023
45a5fdb
update get file methods server request
diffitask Nov 2, 2023
28ee4dc
fix get file methods server request
diffitask Nov 2, 2023
c651cb9
add get file classes server request
diffitask Nov 2, 2023
4173a51
add get file text server request
diffitask Nov 2, 2023
cfc04de
update tests due to classes signature changes
diffitask Nov 2, 2023
bd63766
remove unused import
diffitask Nov 2, 2023
86c674d
move psi file initialization to execute func in FileClasses apis
diffitask Nov 2, 2023
453fc59
move psi file initialization to execute func in FileFunctions apis
diffitask Nov 2, 2023
5b75bb3
move psi file initialization to execute func in FileText api
diffitask Nov 2, 2023
27d8ba3
rename file methods to file functions server request
diffitask Nov 2, 2023
dcc2b56
update file functions api json description
diffitask Nov 2, 2023
6f61c20
add file classes api json description
diffitask Nov 2, 2023
7b3e92c
add file text api json description
diffitask Nov 2, 2023
4553efd
update apis description names to snake case
diffitask Nov 2, 2023
af57e74
fix val name in java file functions api
diffitask Nov 3, 2023
eabb013
remove unnecessary psi file casting to language specific psi file in …
diffitask Nov 6, 2023
18b8f9b
replace unnecessary extension functions that gets file classes with e…
diffitask Nov 6, 2023
db7c146
rename variables in file classes api
diffitask Nov 6, 2023
e52fce5
refactor file functions and classes apis tests
diffitask Nov 6, 2023
5a2880b
add generic function for getting specific psi elements from the PsiFile
diffitask Nov 6, 2023
aa0ecfb
add methods for choosing correct file related api because of differen…
diffitask Nov 6, 2023
a28254f
fix get file functions url (return the correspondence between the jso…
diffitask Nov 6, 2023
44b90e7
Merge branch 'main' into plugin-dev
diffitask Nov 6, 2023
cd38f0f
refactor tests: add error throwing when needed
diffitask Nov 6, 2023
5aaab6b
add choosing file api util to server file related requests
diffitask Nov 6, 2023
e98c2ce
move fileName request parameter processing to the separate function
diffitask Nov 6, 2023
418a732
move routing utils to the separate file
diffitask Nov 6, 2023
07a5804
move ProjectModules api to fileSystemRelated dir
diffitask Nov 6, 2023
9ec45b4
split api models test class to several ones
diffitask Nov 7, 2023
5974d26
add plugin.xml for the UI plugin mode
diffitask Nov 7, 2023
5192a51
move unsupported file extension error message to the constant
diffitask Nov 10, 2023
dc908dc
move file extensions constants to the enum class
diffitask Nov 10, 2023
d1f0722
move common execute logic in file related apis to utils
diffitask Nov 10, 2023
df94b2a
Merge branch 'main' into plugin-dev
diffitask Nov 14, 2023
d853005
add fileFileRecursively psi directory extension
diffitask Nov 14, 2023
fc18d8a
apply fileFileRecursively function to the file related psi apis
diffitask Nov 14, 2023
07d843a
move default directory processing to inside the find subdir recursive…
diffitask Nov 14, 2023
8b55c15
fix bug with apiMethods parameter renaming (from apiCalls)
diffitask Nov 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions ide-former-plugin/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ pluginUntilBuild = 232.*

# IntelliJ Platform Properties -> https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html#configuration-intellij-extension
platformType = IC
platformVersion = 2023.2
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
platformPlugins = com.intellij.java, org.jetbrains.kotlin, PythonCore:232.10203.2

# Gradle Releases -> https://github.com/gradle/gradle/releases
gradleVersion = 8.3
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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.findFileRecursively

class FileText(
private val projectDirectory: PsiDirectory,
private val fileName: String
) : IdeApiMethod {
private var fileText: String? = null

override fun execute() {
val psiFile = projectDirectory.findFileRecursively(fileName)
fileText = psiFile.text
}

fun getFileText(): String? = fileText
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileClasses

import org.jetbrains.research.ideFormerPlugin.api.models.IdeApiMethod

interface FileClasses : IdeApiMethod {
fun getClassesNames(): List<String>?
fun getClassCode(className: String): String?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileClasses

import com.intellij.psi.PsiClass
import com.intellij.psi.PsiDirectory
import org.jetbrains.research.ideFormerPlugin.api.models.utils.getFilePsiElementsOfType

class JavaFileClasses(
private val projectDirectory: PsiDirectory,
private val javaFileName: String
) : FileClasses {
private var javaClasses: List<PsiClass>? = null

diffitask marked this conversation as resolved.
Show resolved Hide resolved
override fun execute() {
javaClasses = getFilePsiElementsOfType<PsiClass>(projectDirectory, javaFileName)
}

override fun getClassesNames(): List<String>? =
javaClasses?.mapNotNull { it.name }

override fun getClassCode(className: String): String? = javaClasses
?.firstOrNull { it.name == className }
?.text
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileClasses

import com.intellij.psi.PsiDirectory
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.research.ideFormerPlugin.api.models.utils.getFilePsiElementsOfType

class KtFileClasses(
private val projectDirectory: PsiDirectory,
private val ktFileName: String
) : FileClasses {
private var ktClasses: List<KtClass>? = null

override fun execute() {
ktClasses = getFilePsiElementsOfType<KtClass>(projectDirectory, ktFileName)
}

override fun getClassesNames(): List<String>? = ktClasses?.mapNotNull { it.name }

override fun getClassCode(className: String): String? = ktClasses
?.firstOrNull { it.name == className }
?.text
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileClasses

import com.intellij.psi.PsiDirectory
import com.jetbrains.python.psi.PyClass
import org.jetbrains.research.ideFormerPlugin.api.models.utils.getFilePsiElementsOfType

class PyFileClasses(
private val projectDirectory: PsiDirectory,
private val pyFileName: String
) : FileClasses {
private var pyClasses: List<PyClass>? = null

override fun execute() {
pyClasses = getFilePsiElementsOfType<PyClass>(projectDirectory, pyFileName)
}

override fun getClassesNames(): List<String>? = pyClasses?.mapNotNull { it.name }

override fun getClassCode(className: String): String? = pyClasses
?.firstOrNull { it.name == className }
?.text
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileFunctions

import org.jetbrains.research.ideFormerPlugin.api.models.IdeApiMethod

interface FileFunctions : IdeApiMethod {
fun getFunctionsNames(): List<String>?
fun getFunctionCode(functionName: String): String?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileFunctions

import com.intellij.psi.PsiDirectory
import com.intellij.psi.PsiMethod
import org.jetbrains.research.ideFormerPlugin.api.models.utils.getFilePsiElementsOfType

class JavaFileFunctions(
private val projectDirectory: PsiDirectory,
private val javaFileName: String
) : FileFunctions {
private var javaFunctions: List<PsiMethod>? = null

override fun execute() {
javaFunctions = getFilePsiElementsOfType<PsiMethod>(projectDirectory, javaFileName)
}

override fun getFunctionsNames(): List<String>? = javaFunctions?.map { it.name }
override fun getFunctionCode(functionName: String): String? = javaFunctions
?.firstOrNull { it.name == functionName }
?.text
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileFunctions

import com.intellij.psi.PsiDirectory
import org.jetbrains.kotlin.psi.KtFunction
import org.jetbrains.research.ideFormerPlugin.api.models.utils.getFilePsiElementsOfType

class KtFileFunctions(
private val projectDirectory: PsiDirectory,
private val ktFileName: String
) : FileFunctions {
private var ktFunctions: List<KtFunction>? = null

override fun execute() {
ktFunctions = getFilePsiElementsOfType<KtFunction>(projectDirectory, ktFileName)
}

override fun getFunctionsNames(): List<String>? = ktFunctions?.mapNotNull { it.name }
override fun getFunctionCode(functionName: String): String? = ktFunctions
?.firstOrNull { it.name == functionName }
?.text
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.jetbrains.research.ideFormerPlugin.api.models.fileRelated.fileFunctions

import com.intellij.psi.PsiDirectory
import com.jetbrains.python.psi.PyFunction
import org.jetbrains.research.ideFormerPlugin.api.models.utils.getFilePsiElementsOfType

class PyFileFunctions(
private val projectDirectory: PsiDirectory,
private val pyFileName: String
) : FileFunctions {
private var pyFunctions: List<PyFunction>? = null

override fun execute() {
pyFunctions = getFilePsiElementsOfType<PyFunction>(projectDirectory, pyFileName)
}

override fun getFunctionsNames(): List<String>? = pyFunctions?.mapNotNull { it.name }
override fun getFunctionCode(functionName: String): String? = pyFunctions
?.firstOrNull { it.name == functionName }
?.text
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.jetbrains.research.ideFormerPlugin.api.models
package org.jetbrains.research.ideFormerPlugin.api.models.fileSystemRelated

import com.intellij.psi.PsiDirectory
import org.jetbrains.research.ideFormerPlugin.api.DEFAULT_DIRECTORY_NAME
import org.jetbrains.research.ideFormerPlugin.api.findSubdirectoryRecursively
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.stateKeeper.IdeStateKeeper

class ChangeDirectory(
Expand All @@ -12,12 +13,7 @@ class ChangeDirectory(
private var prevDir: PsiDirectory? = null

override fun execute() {
if (targetDirectoryName == DEFAULT_DIRECTORY_NAME) {
return
}

val targetDir = ideStateKeeper.currentProjectDirectory.findSubdirectoryRecursively(targetDirectoryName)
?: error("No such directory in a project: '$targetDirectoryName'.")

prevDir = ideStateKeeper.currentProjectDirectory
ideStateKeeper.currentProjectDirectory = targetDir
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package org.jetbrains.research.ideFormerPlugin.api.models
package org.jetbrains.research.ideFormerPlugin.api.models.fileSystemRelated

import com.intellij.psi.PsiDirectory
import com.intellij.psi.PsiFileSystemItem
import org.jetbrains.research.ideFormerPlugin.api.DEFAULT_DIRECTORY_NAME
import org.jetbrains.research.ideFormerPlugin.api.findSubdirectoryRecursively
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

class ListDirectoryContents(
private val currentProjectDirectory: PsiDirectory,
Expand All @@ -18,11 +19,7 @@ class ListDirectoryContents(
}

override fun execute() {
val searchDirectory = when (searchDirectoryName) {
DEFAULT_DIRECTORY_NAME -> currentProjectDirectory
else -> currentProjectDirectory.findSubdirectoryRecursively(searchDirectoryName)
?: error("No such subdirectory: '$searchDirectoryName'")
}
val searchDirectory = currentProjectDirectory.findSubdirectoryRecursively(searchDirectoryName)
searchDirectoryItems = searchDirectory.fileSystemItems()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.jetbrains.research.ideFormerPlugin.api.models
package org.jetbrains.research.ideFormerPlugin.api.models.fileSystemRelated

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

class ProjectModules(private val project: Project) : IdeApiMethod {
private var projectModules: List<Module>? = null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.jetbrains.research.ideFormerPlugin.api.models.utils

import com.intellij.psi.PsiDirectory
import com.intellij.util.PathUtil
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"

enum class FileExtensions(val extension: String) {
KT("kt"),
JAVA("java"),
PY("py")
}

fun chooseFileClassesApiForFile(fileName: String, projectDirectory: PsiDirectory): FileClasses =
when (val fileExtension = PathUtil.getFileExtension(fileName)) {
FileExtensions.KT.extension -> KtFileClasses(projectDirectory, fileName)
FileExtensions.JAVA.extension -> JavaFileClasses(projectDirectory, fileName)
FileExtensions.PY.extension -> PyFileClasses(projectDirectory, fileName)
else -> error("$UNSUPPORTED_FILE_EXTENSION_ERROR: $fileExtension")
}

fun chooseFileFunctionsApiForFile(fileName: String, projectDirectory: PsiDirectory): FileFunctions =
when (val fileExtension = PathUtil.getFileExtension(fileName)) {
FileExtensions.KT.extension -> KtFileFunctions(projectDirectory, fileName)
FileExtensions.JAVA.extension -> JavaFileFunctions(projectDirectory, fileName)
FileExtensions.PY.extension -> PyFileFunctions(projectDirectory, fileName)
else -> error("$UNSUPPORTED_FILE_EXTENSION_ERROR: $fileExtension")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.jetbrains.research.ideFormerPlugin.api.models.utils

import com.intellij.psi.*
import com.intellij.psi.util.PsiTreeUtil

const val PATH_DELIMITER = "/"

fun PsiDirectory.findSubdirectoryRecursively(targetDirectoryPath: String): PsiDirectory {
val currentDirectory = when (val nextDirectoryInPath = targetDirectoryPath.substringBefore(PATH_DELIMITER)) {
DEFAULT_DIRECTORY_NAME -> this
else -> this.findSubdirectory(nextDirectoryInPath) ?: error("No such subdirectory: $nextDirectoryInPath")
}
val remainingDirectoryPath = targetDirectoryPath.substringAfter(PATH_DELIMITER, "")

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
else -> this.findSubdirectoryRecursively(fileDirectoryPath)
}

val fileName = targetFilePath.substringAfterLast(PATH_DELIMITER)
return targetFileDirectory.findFile(fileName)
?: error("No such file in the current directory")
}

inline fun <reified T : PsiElement> PsiFile.psiElementsOfType(): List<T> =
PsiTreeUtil.findChildrenOfType(this, T::class.java).toList()

inline fun <reified T : PsiElement> getFilePsiElementsOfType(
projectDirectory: PsiDirectory,
fileName: String
): List<T> {
val psiFile = projectDirectory.findFileRecursively(fileName)
return psiFile.psiElementsOfType<T>()
}
Loading
Loading