Skip to content

Commit

Permalink
1. In CommandAutofixAction.kt:
Browse files Browse the repository at this point in the history
   - Refactored the `isGitignore` function to work with both `VirtualFile` and `Path` objects.
   - Improved the gitignore pattern matching logic.

2. In `GenerateDocumentationAction.kt`:
   - Enhanced the `transformContent` function to include project structure information in the API request.
   - Added a `findGitRoot` function to locate the Git root directory.
   - Improved the logic for generating individual output paths for documentation files.

3. In `TestResultAutofixAction.kt`:
   - Added a new `getFiles` function that works with `Path` objects.
   - Refactored the `getProjectStructure` function to work with both `VirtualFile` and `Path` objects.
   - Added a `findGitRoot` function that works with `Path` objects.
  • Loading branch information
acharneski committed Jun 23, 2024
1 parent e6cae42 commit 8638023
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -463,19 +463,21 @@ class CommandAutofixAction : BaseAction() {
companion object {
private val log = LoggerFactory.getLogger(CommandAutofixAction::class.java)
val tripleTilde = "`" + "``" // This is a workaround for the markdown parser when editing this file
fun isGitignore(file: VirtualFile): Boolean {
var currentDir = file.toNioPath().toFile().parentFile
fun isGitignore(file: VirtualFile) = isGitignore(file.toNioPath())

fun isGitignore(path: Path): Boolean {
var currentDir = path.toFile().parentFile
currentDir ?: return false
while (!currentDir.resolve(".git").exists()) {
currentDir.resolve(".gitignore").let {
if (it.exists()) {
val gitignore = it.readText()
if (gitignore.split("\n").any { line ->
val pattern = line.trim().trimEnd('/').replace(".", "\\.").replace("*", ".*")
line.trim().isNotEmpty()
&& !line.startsWith("#")
&& file.name.trimEnd('/').matches(Regex(pattern))
}) {
val pattern = line.trim().trimEnd('/').replace(".", "\\.").replace("*", ".*")
line.trim().isNotEmpty()
&& !line.startsWith("#")
&& path.fileName.toString().trimEnd('/').matches(Regex(pattern))
}) {
return true
}
}
Expand All @@ -489,7 +491,7 @@ class CommandAutofixAction : BaseAction() {
val pattern = line.trim().trimEnd('/').replace(".", "\\.").replace("*", ".*")
line.trim().isNotEmpty()
&& !line.startsWith("#")
&& file.name.trimEnd('/').matches(Regex(pattern))
&& path.fileName.toString().trimEnd('/').matches(Regex(pattern))
}) {
return true
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.simiacryptus.aicoder.actions.generic

import com.github.simiacryptus.aicoder.actions.FileContextAction
import com.github.simiacryptus.aicoder.actions.test.TestResultAutofixAction.Companion.getProjectStructure
import com.github.simiacryptus.aicoder.config.AppSettingsState
import com.github.simiacryptus.aicoder.config.AppSettingsState.Companion.chatModel
import com.github.simiacryptus.aicoder.config.Name
Expand Down Expand Up @@ -117,13 +118,21 @@ class GenerateDocumentationAction : FileContextAction<GenerateDocumentationActio
executorService.submit<Path?> {
val fileContent =
IOUtils.toString(FileInputStream(path.toFile()), "UTF-8") ?: return@submit null
val transformContent = transformContent(fileContent, transformationMessage)
val transformContent = transformContent(path, fileContent, transformationMessage)
if (config?.settings?.singleOutputFile == true) {
markdownContent.append("# ${root.relativize(path)}\n\n")
markdownContent.append(transformContent.replace("(?s)(?<![^\\n])#".toRegex(), "\n##"))
} else {
root.relativize(path).let { it.parent.resolve(it.fileName.toString().split('.').dropLast(1).joinToString(".") + "_" + outputPath.fileName) }
val individualOutputPath = root.resolve("${root.relativize(path)}.md")
val individualOutputPath = root.resolve(
"${
root.relativize(
path.parent.resolve(
path.fileName.toString().split('.').dropLast(1)
.joinToString(".") + "." + outputPath.fileName
)
)
}.md"
)
Files.write(individualOutputPath, transformContent.toByteArray())
}
path
Expand All @@ -149,23 +158,46 @@ class GenerateDocumentationAction : FileContextAction<GenerateDocumentationActio
}
}

private fun transformContent(fileContent: String, transformationMessage: String) = api.chat(
private fun transformContent(path: Path, fileContent: String, transformationMessage: String) = api.chat(
ApiModel.ChatRequest(
model = AppSettingsState.instance.smartModel.chatModel().modelName,
temperature = AppSettingsState.instance.temperature,
messages = listOf(
ApiModel.ChatMessage(
ApiModel.Role.system, """
You will combine natural language instructions with a user provided code example to document code.
""".trimIndent().toContentList(), null
You will combine natural language instructions with a user provided code example to document code.
""".trimIndent().toContentList(), null
),
ApiModel.ChatMessage(
ApiModel.Role.user,
"""
|## Project:
|${findGitRoot(path)?.let { getProjectStructure(it) }}
|
|## $path:
|```
|$fileContent
|```
|
|Instructions: $transformationMessage
""".trimMargin().toContentList()
),
ApiModel.ChatMessage(ApiModel.Role.user, fileContent.toContentList()),
ApiModel.ChatMessage(ApiModel.Role.user, transformationMessage.toContentList()),
),
),
AppSettingsState.instance.smartModel.chatModel()
).choices.first().message?.content?.trim() ?: fileContent

fun findGitRoot(path: Path?): Path? {
var current: Path? = path
while (current != null) {
if (current.resolve(".git").toFile().exists()) {
return current
}
current = current.parent
}
return null
}

companion object {
private val scheduledPool = Executors.newScheduledThreadPool(1)
fun open(project: Project, outputPath: Path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,28 @@ class TestResultAutofixAction : BaseAction() {
}
return codeFiles
}
fun getFiles(
virtualFiles: Array<out Path>?
): MutableSet<Path> {
val codeFiles = mutableSetOf<Path>() // Set to avoid duplicates
virtualFiles?.forEach { file ->
if(file.fileName.startsWith(".")) return@forEach
if(CommandAutofixAction.isGitignore(file)) return@forEach
if (file.toFile().isDirectory) {
codeFiles.addAll(getFiles(file.toFile().listFiles().map { it.toPath() }.toTypedArray()))
} else {
codeFiles.add(file)
}
}
return codeFiles
}

fun getProjectStructure(projectPath: VirtualFile?): String {
if (projectPath == null) return "Project path is null"
val root = Path.of(projectPath!!.path)
return getProjectStructure(Path.of((projectPath?: return "Project path is null").path))
}

val codeFiles = getFiles(arrayOf(projectPath!!))
fun getProjectStructure(root: Path): String {
val codeFiles = getFiles(arrayOf(root))
.filter { it.toFile().length() < 1024 * 1024 / 2 } // Limit to 0.5MB
.map { root.relativize(it) ?: it }.toSet()
val str = codeFiles
Expand All @@ -71,6 +88,17 @@ class TestResultAutofixAction : BaseAction() {
return str
}

fun findGitRoot(path: Path?): Path? {
var current: Path? = path
while (current != null) {
if (current.resolve(".git").toFile().exists()) {
return current
}
current = current.parent
}
return null
}

fun findGitRoot(virtualFile: VirtualFile?): VirtualFile? {
var current: VirtualFile? = virtualFile
while (current != null) {
Expand Down

0 comments on commit 8638023

Please sign in to comment.