Skip to content

Commit

Permalink
Change the file listener to use a invokeLater action
Browse files Browse the repository at this point in the history
  • Loading branch information
olivernybroe committed Aug 12, 2021
1 parent 87d65e4 commit 45568f8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 33 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
# PEST IntelliJ Changelog

## [Unreleased]
### Fixed
- Invoke the FileListener PSI part later (should fix indexing issues)

## [1.0.5]
### Changed
- Bumped min IntelliJ version to 2021.1

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

pluginGroup = com.pestphp
pluginName = PEST PHP
pluginVersion = 1.0.5
pluginVersion = 1.0.6

# See https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
# for insight into build numbers and IntelliJ Platform versions.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pestphp.pest.fileListeners

import com.intellij.openapi.application.invokeLater
import com.intellij.openapi.components.service
import com.intellij.openapi.externalSystem.autoimport.AsyncFileChangeListenerBase
import com.intellij.openapi.project.NoAccessDuringPsiEvents
Expand All @@ -26,25 +27,24 @@ class ExtendAsyncFileListener : AsyncFileChangeListenerBase() {
}

override fun updateFile(file: VirtualFile, event: VFileEvent) {
try {
ProjectManager.getInstance()
.openProjects
// Only look at projects where the file is inside.
.filter { ProjectFileIndex.getInstance(it).isInContent(file) }
// Get the PSI file inside each of the projects.
.mapNotNull { PsiManager.getInstance(it).findFile(file) }
// Only look at PHP files
.filterIsInstance<PhpFile>()
.filter { it.expectExtends.isNotEmpty() }
.forEach {
val expectationFileService = it.project.service<ExpectationFileService>()
ProjectManager.getInstance()
.openProjects
// Only look at projects where the file is inside.
.filter { ProjectFileIndex.getInstance(it).isInContent(file) }
// Get the PSI file inside each of the projects.
.mapNotNull { PsiManager.getInstance(it).findFile(file) }
// Only look at PHP files
.filterIsInstance<PhpFile>()
.forEach {
val expectationFileService = it.project.service<ExpectationFileService>()

// In case file is deleted.
if (event is VFileDeleteEvent) {
expectationFileService.removeExtends(it.virtualFile)
return@forEach
}
// In case file is deleted.
if (event is VFileDeleteEvent) {
expectationFileService.removeExtends(it.virtualFile)
return@forEach
}

invokeLater {
// Add all the extends
val hasChanges = expectationFileService.updateExtends(it)

Expand All @@ -53,20 +53,7 @@ class ExtendAsyncFileListener : AsyncFileChangeListenerBase() {
expectationFileService.generateFile()
}
}
} catch (exception: Exception) {
// Ignore up to date stub mismatch exceptions
if (exception.javaClass.simpleName.equals("UpToDateStubIndexMismatch")) {
return
}
if (exception.stackTrace.any { it.methodName.equals("stubTreeAndIndexDoNotMatch") }) {
return
}
if (exception.stackTrace.any { it.className.equals(NoAccessDuringPsiEvents::class.java.name)}) {
return
}

throw exception
}
}

override fun isRelevant(file: VirtualFile, event: VFileEvent): Boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.pestphp.pest.services

import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.invokeLater
import com.intellij.openapi.application.runWriteAction
import com.intellij.openapi.progress.runSuspendingAction
import com.intellij.openapi.project.DumbService
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
Expand Down Expand Up @@ -73,8 +76,8 @@ class ExpectationFileService(val project: Project) {
val file = generator.generateToFile(project)

// Save the file in vendor folder
ApplicationManager.getApplication().invokeLater {
ApplicationManager.getApplication().runWriteAction {
invokeLater {
runWriteAction {
DumbService.getInstance(project).suspendIndexingAndRun(
"Indexing Pest expect extends"
) {
Expand Down

0 comments on commit 45568f8

Please sign in to comment.