Skip to content

Commit

Permalink
Merge pull request #403 from takahirom/takahirom/fix-caret-focus-beha…
Browse files Browse the repository at this point in the history
…vior/2024-06-09

[Idea Plugin] Fix the issue where the image is not selected as intended
  • Loading branch information
takahirom authored Jun 11, 2024
2 parents f6fda21 + 03851df commit c3faf4f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
2 changes: 1 addition & 1 deletion roborazzi-idea-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = "io.github.takahirom.roborazzi"
version = "1.1.0"
version = "1.2.0"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,26 +104,41 @@ class PreviewViewModel {
refreshList(project)
}

fun onSelectedFileChanged(project: Project) {
roborazziLog("onSelectedFileChanged")
coroutineScope.launch {
updateListJob?.cancel()
refreshListProcess(project)
selectListIndexByCaret(project)
}
}

fun onCaretPositionChanged(project: Project) {
roborazziLog("onCaretPositionChanged")
coroutineScope.launch {
updateListJob?.cancel()
refreshListProcess(project)
val editor = FileEditorManager.getInstance(project).selectedTextEditor
val offset = editor?.caretModel?.offset
if (offset != null) {
val psiFile = PsiDocumentManager.getInstance(project).getPsiFile(editor.document) as? KtFile
?: return@launch
val kotlinFile = psiFile as? KtFile ?: return@launch
val pe: PsiElement = kotlinFile.findElementAt(editor.caretModel.offset) ?: return@launch
val method: KtFunction = findFunction(pe) ?: return@launch
imagesStateFlow.value.indexOfFirst {
it.first.substringAfterLast(File.separator).contains(method.name ?: "")
}
.let {
shouldSeeIndex.value = it
}
selectListIndexByCaret(project)
}
}

private fun selectListIndexByCaret(project: Project) {
val editor = FileEditorManager.getInstance(project).selectedTextEditor
val offset = editor?.caretModel?.offset
if (offset != null) {
val psiFile = PsiDocumentManager.getInstance(project).getPsiFile(editor.document) as? KtFile
?: return
val kotlinFile = psiFile as? KtFile ?: return
val pe: PsiElement = kotlinFile.findElementAt(editor.caretModel.offset) ?: return
val method: KtFunction = findFunction(pe) ?: return
roborazziLog("imagesStateFlow.value = ${imagesStateFlow.value}")
imagesStateFlow.value.indexOfFirst {
it.first.substringAfterLast(File.separator).contains(method.name ?: "")
}
.let {
roborazziLog("shouldSeeIndex.value = $it")
shouldSeeIndex.value = it
}
}
}

Expand Down Expand Up @@ -169,14 +184,17 @@ class PreviewViewModel {
yield()
val editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return run {
statusText.value = "No editor found"
imagesStateFlow.value = emptyList()
}

val psiFile: PsiFile =
PsiDocumentManager.getInstance(project).getPsiFile(editor.document) ?: return run {
statusText.value = "No psi file found"
imagesStateFlow.value = emptyList()
}
val kotlinFile = psiFile as? KtFile ?: return run {
statusText.value = "No kotlin file found"
imagesStateFlow.value = emptyList()
}
if (lastEditingFileName.value != kotlinFile.name) {
imagesStateFlow.value = emptyList()
Expand Down Expand Up @@ -274,6 +292,10 @@ class PreviewViewModel {
fun onHide() {
cancel()
}

fun onShouldSeeIndexHandled() {
shouldSeeIndex.value = -1
}
}

class RoborazziPreviewPanel(project: Project) : JPanel(BorderLayout()) {
Expand Down Expand Up @@ -359,7 +381,7 @@ class RoborazziPreviewPanel(project: Project) : JPanel(BorderLayout()) {
}

override fun selectionChanged(event: FileEditorManagerEvent) {
viewModel?.onCaretPositionChanged(project)
viewModel?.onSelectedFileChanged(project)
val editor = FileEditorManager.getInstance(project).selectedTextEditor

editor?.caretModel?.removeCaretListener(caretListener)
Expand All @@ -386,8 +408,12 @@ class RoborazziPreviewPanel(project: Project) : JPanel(BorderLayout()) {
}
viewModel?.coroutineScope?.launch {
viewModel?.shouldSeeIndex?.collect {
if (it == -1) {
return@collect
}
roborazziLog("shouldSeeIndex.collect $it")
imageList.selectedIndex = it
viewModel?.onShouldSeeIndexHandled()
}
}
}
Expand Down

0 comments on commit c3faf4f

Please sign in to comment.