Skip to content

Commit

Permalink
Address issues reported by the Plugin Verifier
Browse files Browse the repository at this point in the history
  • Loading branch information
hsz committed Apr 5, 2024
1 parent a466e42 commit 78726ba
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 75 deletions.
17 changes: 7 additions & 10 deletions src/main/kotlin/mobi/hsz/idea/gitignore/IgnoreManager.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package mobi.hsz.idea.gitignore

import com.intellij.ProjectTopics
import com.intellij.openapi.Disposable
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.ModalityState
Expand All @@ -10,11 +9,9 @@ import com.intellij.openapi.components.service
import com.intellij.openapi.fileTypes.ExactFileNameMatcher
import com.intellij.openapi.fileTypes.FileTypeManager
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.project.DumbAware
import com.intellij.openapi.project.DumbService
import com.intellij.openapi.project.*
import com.intellij.openapi.project.DumbService.DumbModeListener
import com.intellij.openapi.project.NoAccessDuringPsiEvents
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ModuleRootListener
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.util.text.StringUtil
import com.intellij.openapi.vcs.FileStatusManager
Expand All @@ -34,7 +31,7 @@ import com.intellij.util.ui.EDT
import com.jetbrains.rd.util.concurrentMapOf
import mobi.hsz.idea.gitignore.file.type.IgnoreFileType
import mobi.hsz.idea.gitignore.indexing.IgnoreEntryOccurrence
import mobi.hsz.idea.gitignore.indexing.IgnoreFilesIndex
import mobi.hsz.idea.gitignore.indexing.getEntries
import mobi.hsz.idea.gitignore.lang.IgnoreLanguage
import mobi.hsz.idea.gitignore.services.IgnoreMatcher
import mobi.hsz.idea.gitignore.settings.IgnoreSettings
Expand Down Expand Up @@ -66,7 +63,7 @@ class IgnoreManager(private val project: Project) : DumbAware, Disposable {
private val commonRunnableListeners = CommonRunnableListeners(debouncedStatusesChanged)
private var messageBus = project.messageBus.connect(this)
private val cachedIgnoreFilesIndex = CachedConcurrentMap.create<IgnoreFileType, List<IgnoreEntryOccurrence>> {
IgnoreFilesIndex.getEntries(project, it)
getEntries(project, it)
}

private val expiringStatusCache = ExpiringMap<VirtualFile, Boolean>(Time.SECOND)
Expand Down Expand Up @@ -220,9 +217,9 @@ class IgnoreManager(private val project: Project) : DumbAware, Disposable {
}
}
)
messageBus.subscribe(ProjectTopics.PROJECT_ROOTS, commonRunnableListeners)
messageBus.subscribe(ModuleRootListener.TOPIC, commonRunnableListeners)
messageBus.subscribe(RefreshStatusesListener.REFRESH_STATUSES, commonRunnableListeners)
messageBus.subscribe(ProjectTopics.MODULES, commonRunnableListeners)
messageBus.subscribe(ModuleListener.TOPIC, commonRunnableListeners)
working = true
}

Expand Down Expand Up @@ -284,7 +281,7 @@ class IgnoreManager(private val project: Project) : DumbAware, Disposable {
FILE_TYPES_ASSOCIATION_QUEUE.remove(fileName)
}
},
ModalityState.NON_MODAL
ModalityState.nonModal(),
)
} else if (!FILE_TYPES_ASSOCIATION_QUEUE.containsKey(fileName)) {
FILE_TYPES_ASSOCIATION_QUEUE[fileName] = fileType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.intellij.openapi.vcs.FileStatusManager
import com.intellij.openapi.vcs.ProjectLevelVcsManager
import com.intellij.openapi.vcs.changes.ChangeListManager
import mobi.hsz.idea.gitignore.IgnoreBundle
import mobi.hsz.idea.gitignore.vcs.IGNORED
import mobi.hsz.idea.gitignore.vcs.IgnoreFileStatusProvider

/**
Expand All @@ -26,7 +27,7 @@ class CloseIgnoredEditorsAction : CloseEditorsActionBase() {
val fileStatusManager = FileStatusManager.getInstance(project) ?: return false
val changeListManager = ChangeListManager.getInstance(project)
return editor.file.run {
fileStatusManager.getStatus(this) == IgnoreFileStatusProvider.IGNORED || changeListManager.isIgnoredFile(this)
fileStatusManager.getStatus(this) == IGNORED || changeListManager.isIgnoredFile(this)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,14 @@ import mobi.hsz.idea.gitignore.util.Glob
import java.io.DataInput
import java.io.DataOutput
import java.io.IOException
import java.util.Collections
import java.util.*

/**
* Implementation of [AbstractIgnoreFilesIndex] that allows to index all ignore files content using native
* IDE mechanisms and increase indexing performance.
*/
class IgnoreFilesIndex : AbstractIgnoreFilesIndex<IgnoreFileTypeKey, IgnoreEntryOccurrence>() {

companion object {
val KEY = ID.create<IgnoreFileTypeKey, IgnoreEntryOccurrence>("IgnoreFilesIndex")
private const val VERSION = 5
private val DATA_EXTERNALIZER = object : DataExternalizer<IgnoreEntryOccurrence> {

@Throws(IOException::class)
override fun save(out: DataOutput, entry: IgnoreEntryOccurrence) = IgnoreEntryOccurrence.serialize(out, entry)

@Throws(IOException::class)
override fun read(input: DataInput) = IgnoreEntryOccurrence.deserialize(input)
}

/**
* Returns collection of indexed [IgnoreEntryOccurrence] for given [Project] and [IgnoreFileType].
*
* @param project current project
* @param fileType filetype
* @return [IgnoreEntryOccurrence] collection
*/
fun getEntries(project: Project, fileType: IgnoreFileType): List<IgnoreEntryOccurrence> {
try {
if (ApplicationManager.getApplication().isReadAccessAllowed) {
val scope = IgnoreSearchScope[project]
return FileBasedIndex.getInstance().getValues(KEY, IgnoreFileTypeKey(fileType), scope)
}
} catch (ignored: RuntimeException) {
}
return emptyList()
}
}

override fun getName(): ID<IgnoreFileTypeKey, IgnoreEntryOccurrence> = KEY

@Suppress("ReturnCount")
Expand All @@ -74,18 +43,15 @@ class IgnoreFilesIndex : AbstractIgnoreFilesIndex<IgnoreFileTypeKey, IgnoreEntry
}

val items = mutableListOf<Pair<String, Boolean>>()
inputDataPsi.acceptChildren(
object : IgnoreVisitor() {
override fun visitEntry(entry: IgnoreEntry) {
val regex = Glob.getRegex(entry.value, entry.syntax, false)
items.add(Pair.create(regex, entry.isNegated))
}
inputDataPsi.acceptChildren(object : IgnoreVisitor() {
override fun visitEntry(entry: IgnoreEntry) {
val regex = Glob.getRegex(entry.value, entry.syntax, false)
items.add(Pair.create(regex, entry.isNegated))
}
)
})

return Collections.singletonMap(
IgnoreFileTypeKey((inputData.fileType as IgnoreFileType)),
IgnoreEntryOccurrence(inputData.file.url, items)
IgnoreFileTypeKey((inputData.fileType as IgnoreFileType)), IgnoreEntryOccurrence(inputData.file.url, items)
)
}

Expand All @@ -98,17 +64,42 @@ class IgnoreFilesIndex : AbstractIgnoreFilesIndex<IgnoreFileTypeKey, IgnoreEntry
@Synchronized
@Throws(IOException::class)
override fun read(input: DataInput): IgnoreFileTypeKey = input.readUTF().run {
IgnoreBundle.LANGUAGES
.asSequence()
.map { it.fileType }
.firstOrNull { it.languageName == this }
.let { IgnoreFileTypeKey(it ?: IgnoreFileType.INSTANCE) }
IgnoreBundle.LANGUAGES.asSequence().map { it.fileType }.firstOrNull { it.languageName == this }.let { IgnoreFileTypeKey(it ?: IgnoreFileType.INSTANCE) }
}

override fun getValueExternalizer() = DATA_EXTERNALIZER

override fun getVersion() = VERSION

override fun acceptInput(file: VirtualFile) =
file.fileType is IgnoreFileType || IgnoreManager.FILE_TYPES_ASSOCIATION_QUEUE.containsKey(file.name)
override fun acceptInput(file: VirtualFile) = file.fileType is IgnoreFileType || IgnoreManager.FILE_TYPES_ASSOCIATION_QUEUE.containsKey(file.name)
}

val KEY = ID.create<IgnoreFileTypeKey, IgnoreEntryOccurrence>("IgnoreFilesIndex")
private val DATA_EXTERNALIZER = object : DataExternalizer<IgnoreEntryOccurrence> {

@Throws(IOException::class)
override fun save(out: DataOutput, entry: IgnoreEntryOccurrence) = IgnoreEntryOccurrence.serialize(out, entry)

@Throws(IOException::class)
override fun read(input: DataInput) = IgnoreEntryOccurrence.deserialize(input)
}

private const val VERSION = 5

/**
* Returns collection of indexed [IgnoreEntryOccurrence] for given [Project] and [IgnoreFileType].
*
* @param project current project
* @param fileType filetype
* @return [IgnoreEntryOccurrence] collection
*/
fun getEntries(project: Project, fileType: IgnoreFileType): List<IgnoreEntryOccurrence> {
try {
if (ApplicationManager.getApplication().isReadAccessAllowed) {
val scope = IgnoreSearchScope[project]
return FileBasedIndex.getInstance().getValues(KEY, IgnoreFileTypeKey(fileType), scope)
}
} catch (ignored: RuntimeException) {
}
return emptyList()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ package mobi.hsz.idea.gitignore.ui
import com.intellij.icons.AllIcons
import com.intellij.ide.highlighter.XmlFileType
import com.intellij.openapi.Disposable
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.DefaultActionGroup
import com.intellij.openapi.actionSystem.LangDataKeys
import com.intellij.openapi.actionSystem.*
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.command.CommandProcessor
import com.intellij.openapi.editor.Editor
Expand Down Expand Up @@ -253,7 +250,7 @@ class IgnoreSettingsPanel : Disposable {
val items = currentItems
val document = Document(IgnoreSettings.createTemplatesElement(items))
try {
JDOMUtil.writeDocument(document, wrapper.file, Constants.NEWLINE)
JDOMUtil.writeDocument(document, wrapper.file.outputStream(), Constants.NEWLINE)
Messages.showInfoMessage(
templatesListPanel,
message("action.exportTemplates.success", items.size),
Expand All @@ -271,7 +268,9 @@ class IgnoreSettingsPanel : Disposable {
override fun update(e: AnActionEvent) {
e.presentation.isEnabled = currentItems.isNotEmpty()
}
}

override fun getActionUpdateThread() = ActionUpdateThread.BGT
},
)
}
decorator.setActionGroup(group)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package mobi.hsz.idea.gitignore.vcs

import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.service
import com.intellij.openapi.extensions.PluginId
import com.intellij.openapi.project.DumbAware
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Computable
import com.intellij.openapi.vcs.FileStatus
import com.intellij.openapi.vcs.FileStatusFactory
import com.intellij.openapi.vcs.impl.FileStatusProvider
import com.intellij.openapi.vfs.VirtualFile
Expand All @@ -21,15 +21,6 @@ class IgnoreFileStatusProvider(project: Project) : FileStatusProvider, DumbAware

private val manager = project.service<IgnoreManager>()

companion object {
/** Ignored status. */
val IGNORED: FileStatus = FileStatusFactory.getInstance().createFileStatus(
"IGNORE.PROJECT_VIEW.IGNORED",
IgnoreBundle.message("projectView.ignored"),
JBColor.GRAY
)
}

/**
* Returns the [.IGNORED] status if file is ignored or `null`.
*
Expand All @@ -44,3 +35,11 @@ class IgnoreFileStatusProvider(project: Project) : FileStatusProvider, DumbAware
}
})
}

/** Ignored status. */
val IGNORED = FileStatusFactory.getInstance().createFileStatus(

Check notice on line 40 in src/main/kotlin/mobi/hsz/idea/gitignore/vcs/IgnoreFileStatusProvider.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Function or property has platform type

Declaration has type inferred from a platform call, which can lead to unchecked nullability issues. Specify type explicitly as nullable or non-nullable.
"IGNORE.PROJECT_VIEW.IGNORED",
{ IgnoreBundle.message("projectView.ignored") },
JBColor.GRAY,
PluginId.getId("mobi.hsz.idea.gitignore"),
)

0 comments on commit 78726ba

Please sign in to comment.