Fork channel

Create a new channel as a copy of main.

Rename channel

Rename main to:

Delete channel

Delete main? This cannot be undone.

PijulVirtualFileStatusProvider.kt
/**
 * Dracon - An IntelliJ-Pijul integration.
 * Copyright 2021 JonathanxD <jhrldev@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.github.jonathanxd.dracon.vfs

import com.github.jonathanxd.dracon.cache.FileStatusCache
import com.github.jonathanxd.dracon.context.PijulVcsContext
import com.github.jonathanxd.dracon.coroutine.blocking
import com.github.jonathanxd.dracon.ignore.IgnoreUtil
import com.github.jonathanxd.dracon.pijul.Pijul
import com.github.jonathanxd.dracon.pijul.SuccessStatusCode
import com.github.jonathanxd.dracon.pijul.pijul
import com.intellij.openapi.components.service
import com.intellij.openapi.project.Project
import com.intellij.openapi.vcs.FileStatus
import com.intellij.openapi.vcs.ProjectLevelVcsManager
import com.intellij.openapi.vcs.impl.FileStatusProvider
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.vcsUtil.VcsUtil
import java.nio.file.Paths
import java.time.Duration
import java.time.Instant
import java.util.concurrent.atomic.AtomicReference

class PijulVirtualFileStatusProvider(val project: Project): FileStatusProvider {

    private val cache = project.service<FileStatusCache>()

    override fun getFileStatus(virtualFile: VirtualFile): FileStatus? {
        if (!Pijul.isUnderPijul(virtualFile) || isIdeaRoot(virtualFile) || !belongsToProject(virtualFile))
            return null

        if (virtualFile.name == ".idea" || virtualFile.name == ".pijul")
            return FileStatus.IGNORED

        val status = this.cache.load(Paths.get(VcsUtil.getFilePath(virtualFile).path))

        return if (status.statusCode is SuccessStatusCode) {
            if (status.result == FileStatus.UNKNOWN && isIgnored(virtualFile)) {
                FileStatus.IGNORED
            } else {
                status.result
            }
        } else {
            if (isIgnored(virtualFile)) {
                FileStatus.IGNORED
            } else {
                null
            }
        }
    }

    fun isIgnored(virtualFile: VirtualFile) =
        IgnoreUtil.ignoredFiles(this.project.service<PijulVcsContext>().root, emptyList())
            .any { it == Paths.get(VcsUtil.getFilePath(virtualFile).path) }


    fun belongsToProject(virtualFile: VirtualFile): Boolean =
        virtualFile.path.startsWith(project.basePath!!)

    fun isIdeaRoot(virtualFile: VirtualFile): Boolean {
        val root = ProjectLevelVcsManager.getInstance(project).getVcsRootFor(virtualFile)
        if (root != null) {
            return root == virtualFile || virtualFile.toNioPath().toAbsolutePath().toString().startsWith(
                root.toNioPath().toAbsolutePath().resolve(".idea").toString()
            )
        } else {
            return false
        }
    }
}