Skip to content

Commit

Permalink
Auto reload when either template or target property file changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pfyod authored and nizienko committed Feb 12, 2024
1 parent ac752ea commit 126faaf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ import com.intellij.openapi.components.service
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.ProjectLocator
import com.intellij.openapi.project.ProjectManager
import com.intellij.openapi.project.guessProjectDir
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.startup.ProjectActivity
import com.intellij.openapi.vfs.*
import com.intellij.openapi.vfs.newvfs.BulkFileListener
import com.intellij.openapi.vfs.newvfs.events.VFileEvent
import com.intellij.openapi.wm.WindowManager
import com.intellij.openapi.wm.impl.status.widget.StatusBarWidgetsManager
import java.io.InputStreamReader
Expand All @@ -31,6 +35,23 @@ internal class PropertySwitcherStarter : ProjectActivity {
}
}

internal class PropertySwitcherListener : BulkFileListener {
override fun after(events: MutableList<out VFileEvent>) {
ProjectManager.getInstance().openProjects
.filter { project ->
events.any { event ->
if (event.file != null) {
project.switcher().isPropertyFile(event.file!!) ||
(event.file!!.fileType == PropertiesTemplateFileType.INSTANCE
&& ProjectRootManager.getInstance(project).fileIndex.isInContent(event.file!!))
} else {
false
}
}
}.forEach { it.switcher().reload() }
}
}

@Service(Service.Level.PROJECT)
internal class PropertySwitcherService(private val project: Project) {
private val switchableFiles: MutableList<SwitchablePropertyFile> = Collections.synchronizedList(mutableListOf())
Expand Down Expand Up @@ -106,6 +127,10 @@ internal class PropertySwitcherService(private val project: Project) {

fun getSwitchableFiles() = switchableFiles.toList()

fun isPropertyFile(file: VirtualFile): Boolean {
return switchableFiles.any { it.propertyFile == file }
}

@Suppress("SENSELESS_COMPARISON")
private fun parseJsonFile(file: VirtualFile): PropertiesTemplate? {
val content = VfsUtilCore.loadText(file)
Expand Down Expand Up @@ -204,7 +229,9 @@ internal class SwitchablePropertyFile(
propertiesToAdd[it.name] = it.options.first()
}
}
addProperties(propertiesToAdd)
if(propertiesToAdd.any()) {
addProperties(propertiesToAdd)
}
}

private fun notifyChanges() {
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,8 @@ Simplify your property file management, boost productivity, and effortlessly rep
<add-to-group group-id="NewGroup"/>
</action>
</actions>
<applicationListeners>
<listener class="com.github.nizienko.propertiesSwitcher.PropertySwitcherListener"
topic="com.intellij.openapi.vfs.newvfs.BulkFileListener"/>
</applicationListeners>
</idea-plugin>

0 comments on commit 126faaf

Please sign in to comment.