Skip to content

Commit

Permalink
feat(review): init default file ignore for patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Oct 18, 2023
1 parent fb7df85 commit 48ee07f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
27 changes: 18 additions & 9 deletions src/main/kotlin/cc/unitmesh/devti/actions/vcs/CodeReviewAction.kt
@@ -1,5 +1,7 @@
package cc.unitmesh.devti.actions.vcs

import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.AutoDevNotifications
import cc.unitmesh.devti.actions.chat.base.ChatBaseAction
import cc.unitmesh.devti.gui.chat.ChatActionType
import cc.unitmesh.devti.gui.chat.ChatContext
Expand All @@ -9,14 +11,10 @@ import cc.unitmesh.devti.provider.ContextPrompter
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.diff.impl.patch.IdeaTextPatchBuilder
import com.intellij.openapi.util.io.FileUtilRt
import com.intellij.openapi.vcs.VcsDataKeys
import com.intellij.openapi.vcs.changes.Change
import com.intellij.openapi.vcs.changes.ContentRevision
import com.intellij.openapi.vcs.changes.CurrentContentRevision
import com.intellij.vcs.log.VcsFullCommitDetails
import com.intellij.vcs.log.VcsLogDataKeys
import org.jetbrains.annotations.NotNull
import java.nio.file.FileSystems
import java.nio.file.PathMatcher

class CodeReviewAction : ChatBaseAction() {
override fun getActionType(): ChatActionType = ChatActionType.CODE_REVIEW
Expand All @@ -25,15 +23,26 @@ class CodeReviewAction : ChatBaseAction() {
val log = logger<CodeReviewAction>()
}

val defaultIgnoreFilePatterns: List<PathMatcher> = listOf(
"**/*.md", "**/*.json", "**/*.txt", "**/*.xml", "**/*.yml", "**/*.yaml",
).map {
FileSystems.getDefault().getPathMatcher("glob:$it")
}

override fun actionPerformed(e: AnActionEvent) {
val project = e.project ?: return

// Make changes available for diff action
val vcsLog = e.getData(VcsLogDataKeys.VCS_LOG)
val details = vcsLog?.selectedDetails?.toList() ?: return
val details: List<VcsFullCommitDetails> = vcsLog?.selectedDetails?.toList() ?: return

val vcsPrompting = project.service<VcsPrompting>()
val diff = vcsPrompting.calculateDiff(details, project)
val diff = vcsPrompting.buildDiffPrompt(details, project, defaultIgnoreFilePatterns)

if (diff == null) {
AutoDevNotifications.notify(project, "No code to review.")
return
}

var prompt =
"""You are a seasoned software developer, and I'm seeking your expertise to review the following code:
Expand Down
29 changes: 28 additions & 1 deletion src/main/kotlin/cc/unitmesh/devti/prompting/VcsPrompting.kt
Expand Up @@ -36,6 +36,7 @@ import org.jetbrains.annotations.NotNull
import java.io.IOException
import java.io.StringWriter
import java.nio.file.Path
import java.nio.file.PathMatcher
import java.util.stream.Collectors
import kotlin.math.min

Expand Down Expand Up @@ -82,8 +83,14 @@ class VcsPrompting(private val project: Project) {
}

@Throws(VcsException::class, IOException::class)
fun calculateDiff(list: List<VcsFullCommitDetails>, project: Project): Pair<List<String>, String> {
fun buildDiffPrompt(
list: List<VcsFullCommitDetails>,
project: Project,
ignoreFilePatterns: List<PathMatcher> = listOf(),
): Pair<List<String>, String>? {
val writer = StringWriter()
var isEmpty = true

val summary: MutableList<String> = ArrayList()
for (detail in list) {
writer.write("""Commit Message: ${detail.fullMessage}\n\nCode Changes:\n\n""")
Expand All @@ -92,8 +99,22 @@ class VcsPrompting(private val project: Project) {
summary.add('"'.toString() + subject + "\"")
val filteredChanges = detail.changes.stream()
.filter { change -> !isBinaryOrTooLarge(change!!) }
.filter {
val filePath = it.afterRevision?.file
if (filePath != null) {
ignoreFilePatterns.none { pattern ->
pattern.matches(Path.of(it.afterRevision?.file?.path))
}
} else {
true
}
}
.toList()

if (filteredChanges.isEmpty()) {
continue
}

val patches = IdeaTextPatchBuilder.buildPatch(
project,
filteredChanges.subList(0, min(filteredChanges.size, 500)),
Expand All @@ -102,6 +123,8 @@ class VcsPrompting(private val project: Project) {
true
)

isEmpty = false

UnifiedDiffWriter.write(
project,
project.stateStore.projectBasePath,
Expand All @@ -112,6 +135,10 @@ class VcsPrompting(private val project: Project) {
)
}

if (isEmpty) {
return null
}

val stringWriter = writer.toString()
val diff = trimDiff(stringWriter)
return Pair<List<String>, String>(summary, diff)
Expand Down

0 comments on commit 48ee07f

Please sign in to comment.