PijulDiffProvider.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.provider
import com.github.jonathanxd.dracon.content.PijulContentRevision
import com.github.jonathanxd.dracon.pijul.pijul
import com.github.jonathanxd.dracon.revision.PijulRevisionNumber
import com.intellij.openapi.components.Service
import com.intellij.openapi.project.Project
import com.intellij.openapi.vcs.FilePath
import com.intellij.openapi.vcs.FileStatus
import com.intellij.openapi.vcs.ProjectLevelVcsManager
import com.intellij.openapi.vcs.changes.ContentRevision
import com.intellij.openapi.vcs.diff.DiffProvider
import com.intellij.openapi.vcs.diff.ItemLatestState
import com.intellij.openapi.vcs.history.VcsRevisionNumber
import com.intellij.openapi.vfs.VirtualFile
import java.nio.file.Paths
import kotlin.io.path.ExperimentalPathApi
import kotlin.io.path.relativeTo
@Service(Service.Level.PROJECT)
class PijulDiffProvider(val project: Project) : DiffProvider {
override fun getCurrentRevision(file: VirtualFile): VcsRevisionNumber? {
val root = ProjectLevelVcsManager.getInstance(this.project).getVcsRootFor(file)?.toNioPath() ?: return null
return pijul(project).latestRevisionNumberForPath(this.project, root, file.toNioPath()).result
}
@OptIn(ExperimentalPathApi::class)
fun getCurrentRevision(path: FilePath): VcsRevisionNumber? {
val root = ProjectLevelVcsManager.getInstance(this.project).getVcsRootFor(path)?.toNioPath() ?: return null
val filePath = path.ioFile.toPath().relativeTo(root)
return pijul(project).latestRevisionNumberForPath(this.project, root, filePath).result
}
override fun getLastRevision(virtualFile: VirtualFile): ItemLatestState? {
val current = this.getCurrentRevision(virtualFile) ?: return null
return ItemLatestState(current, virtualFile.exists(), true)
}
override fun getLastRevision(filePath: FilePath): ItemLatestState? {
val current = this.getCurrentRevision(filePath) ?: return null
return ItemLatestState(current, filePath.ioFile.exists(), true)
}
override fun createFileContent(revisionNumber: VcsRevisionNumber, selectedFile: VirtualFile): ContentRevision? {
val root = ProjectLevelVcsManager.getInstance(this.project).getVcsRootFor(selectedFile)?.toNioPath() ?: return null
return PijulContentRevision(
root,
selectedFile.toNioPath(),
revisionNumber as? PijulRevisionNumber ?: return null,
this.project
)
}
override fun getLatestCommittedRevision(vcsRoot: VirtualFile?): VcsRevisionNumber? {
return null
}
}