diff --git a/src/main/kotlin/com/watermelon/context/services/MyProjectService.kt b/src/main/kotlin/com/watermelon/context/services/MyProjectService.kt index 11e98e3..8306d43 100644 --- a/src/main/kotlin/com/watermelon/context/services/MyProjectService.kt +++ b/src/main/kotlin/com/watermelon/context/services/MyProjectService.kt @@ -1,8 +1,8 @@ import com.intellij.openapi.components.Service import com.intellij.openapi.fileEditor.FileDocumentManager +import com.intellij.openapi.fileEditor.FileEditorManager import com.intellij.openapi.project.Project import com.intellij.vcsUtil.VcsUtil -import com.intellij.openapi.fileEditor.FileEditorManager import git4idea.commands.Git import git4idea.commands.GitCommand import git4idea.commands.GitCommandResult @@ -23,73 +23,30 @@ class MyProjectService(private val project: Project) { return Git.getInstance().runCommand(lineHandler) } - fun getGitBlame(): List? { + fun getGitBlame(startLine: Int? = null, endLine: Int? = null): List? { val editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return null val document = editor.document val file = FileDocumentManager.getInstance().getFile(document) ?: return null val directory = file.parent ?: return null val filePath = VcsUtil.getFilePath(file) - println("File path : $filePath") - val blameRun = GitLineHandler(project, directory, GitCommand.BLAME).apply { - addParameters(filePath.path) - } - - val blameCommandResponse = runCommand(blameRun) - - // Use regex to extract details from git blame - val regex = """(\w+) \((.+?)\s+([\d-]+ [\d:]+ [+-]\d+)\s+(\d+)\)\s*(.*)""".toRegex() - - val commitsDetailsList = mutableListOf() - - // Extract details and map to a list of CommitDetails - blameCommandResponse.output.forEach { line -> - regex.find(line)?.let { matchResult -> - val hash = matchResult.groups[1]?.value.orEmpty() - val author = matchResult.groups[2]?.value.orEmpty() - val date = matchResult.groups[3]?.value.orEmpty() - val lineNumber = matchResult.groups[4]?.value?.toIntOrNull() ?: 0 - val content = matchResult.groups[5]?.value.orEmpty() - // Fetch commit message for unique hashes - val logRun = GitLineHandler(project, directory, GitCommand.LOG).apply { - addParameters("-1", "--pretty=format:%s", hash) - } - val logCommandResponse = runCommand(logRun) - val message = logCommandResponse.output.firstOrNull().orEmpty() - - commitsDetailsList.add( - CommitDetails(hash, author, date, lineNumber, message, content) - ) - } + val blameParameters = mutableListOf() + if (startLine != null && endLine != null) { + val adjustedStartLine = startLine + 1 + val adjustedEndLine = endLine + 1 + blameParameters.add("-L$adjustedStartLine,$adjustedEndLine") } - - return commitsDetailsList.distinctBy { it.hash } - - } - - fun getPartialGitBlame(startLine: Int, endLine: Int): List? { - val editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return null - val document = editor.document - val file = FileDocumentManager.getInstance().getFile(document) ?: return null - val directory = file.parent ?: return null - val filePath = VcsUtil.getFilePath(file) - // Adjust the line numbers to be 1-indexed - val adjustedStartLine = startLine + 1 - val adjustedEndLine = endLine + 1 + blameParameters.add(filePath.path) val blameRun = GitLineHandler(project, directory, GitCommand.BLAME).apply { - addParameters("-L$adjustedStartLine,$adjustedEndLine", filePath.path) + addParameters(*blameParameters.toTypedArray()) } val blameCommandResponse = runCommand(blameRun) - - // Use regex to extract details from git blame val regex = """(\w+) \((.+?)\s+([\d-]+ [\d:]+ [+-]\d+)\s+(\d+)\)\s*(.*)""".toRegex() - val commitsDetailsList = mutableListOf() - // Extract details and map to a list of CommitDetails blameCommandResponse.output.forEach { line -> regex.find(line)?.let { matchResult -> val hash = matchResult.groups[1]?.value.orEmpty() @@ -98,7 +55,6 @@ class MyProjectService(private val project: Project) { val lineNumber = matchResult.groups[4]?.value?.toIntOrNull() ?: 0 val content = matchResult.groups[5]?.value.orEmpty() - // Fetch commit message for unique hashes val logRun = GitLineHandler(project, directory, GitCommand.LOG).apply { addParameters("-1", "--pretty=format:%s", hash) } @@ -112,7 +68,6 @@ class MyProjectService(private val project: Project) { } return commitsDetailsList.distinctBy { it.hash } - - } + } diff --git a/src/main/kotlin/com/watermelon/context/toolWindow/MyToolWindowFactory.kt b/src/main/kotlin/com/watermelon/context/toolWindow/MyToolWindowFactory.kt index 42a6801..b34ddf5 100644 --- a/src/main/kotlin/com/watermelon/context/toolWindow/MyToolWindowFactory.kt +++ b/src/main/kotlin/com/watermelon/context/toolWindow/MyToolWindowFactory.kt @@ -3,27 +3,25 @@ package com.watermelon.context.toolWindow import MyProjectService import com.intellij.credentialStore.CredentialAttributes import com.intellij.ide.BrowserUtil +import com.intellij.ide.passwordSafe.PasswordSafe import com.intellij.openapi.components.service import com.intellij.openapi.project.Project import com.intellij.openapi.wm.ToolWindow import com.intellij.openapi.wm.ToolWindowFactory import com.intellij.ui.components.JBLabel import com.intellij.ui.components.JBPanel -import com.intellij.ui.content.ContentFactory -import com.intellij.ide.passwordSafe.PasswordSafe import com.intellij.ui.components.JBScrollPane +import com.intellij.ui.content.ContentFactory +import git4idea.GitUtil +import git4idea.repo.GitRepository import kotlinx.serialization.json.* import java.awt.* import java.awt.event.MouseAdapter import java.awt.event.MouseEvent +import java.io.IOException import java.net.HttpURLConnection import java.net.URL import javax.swing.* -import git4idea.GitUtil -import git4idea.repo.GitRepository -import java.awt.Font -import java.io.IOException -import java.awt.FontFormatException class MyToolWindowFactory : ToolWindowFactory { @@ -221,7 +219,7 @@ class MyToolWindowFactory : ToolWindowFactory { val commitHashes = if (startLine == 0 && endLine == 0) { service.getGitBlame() } else { - service.getPartialGitBlame(startLine, endLine) + service.getGitBlame(startLine, endLine) } fun getCurrentGitRepo(project: Project): GitRepository? {