Skip to content

Commit

Permalink
Protect agains NPE of containingDirectory in case a file is only in m…
Browse files Browse the repository at this point in the history
…emory, #3182
  • Loading branch information
PHPirates committed Aug 7, 2023
1 parent 24922cf commit 0388aba
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ abstract class ExternalReformatAction(title: String, val isValidFile: (file: Psi
val command = getCommand(file)
val process = try {
GeneralCommandLine(command)
.withWorkDirectory(file.containingDirectory.virtualFile.path)
.withWorkDirectory(file.containingDirectory?.virtualFile?.path)
.withParentEnvironmentType(GeneralCommandLine.ParentEnvironmentType.CONSOLE)
.createProcess()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import nl.hannahsten.texifyidea.index.LatexIncludesIndex
import nl.hannahsten.texifyidea.lang.commands.LatexGenericRegularCommand
import nl.hannahsten.texifyidea.psi.LatexCommands
import nl.hannahsten.texifyidea.psi.LatexNormalText
import nl.hannahsten.texifyidea.util.parser.childrenOfType
import nl.hannahsten.texifyidea.util.files.*
import nl.hannahsten.texifyidea.util.magic.cmd
import nl.hannahsten.texifyidea.util.parser.childrenOfType
import java.io.File

/**
Expand All @@ -19,7 +19,7 @@ class LatexGraphicsPathProvider : LatexPathProviderBase() {
override fun selectScanRoots(file: PsiFile): ArrayList<VirtualFile> {
val paths = getProjectRoots()

val rootDirectory = file.findRootFile().containingDirectory.virtualFile
val rootDirectory = file.findRootFile().containingDirectory?.virtualFile ?: return arrayListOf()
getGraphicsPathsInFileSet(file).forEach {
paths.add(rootDirectory.findVirtualFileByAbsoluteOrRelativePath(it) ?: return@forEach)
}
Expand Down Expand Up @@ -95,13 +95,13 @@ class LatexGraphicsPathProvider : LatexPathProviderBase() {
* Get all the graphics paths defined by one \graphicspaths command.
*/
private fun LatexCommands.getGraphicsPaths(): List<String> {
if (name != "\\graphicspath") return emptyList()
if (name != LatexGenericRegularCommand.GRAPHICSPATH.cmd) return emptyList()
return parameterList.firstNotNullOfOrNull { it.requiredParam }
// Each graphics path is in a group.
?.childrenOfType(LatexNormalText::class)
?.map { it.text }
// Relative paths (not starting with /) have to be appended to the directory of the file of the given command.
?.map { if (it.startsWith('/')) it else containingFile.containingDirectory.virtualFile.path + File.separator + it }
?.mapNotNull { if (it.startsWith('/')) it else (containingFile.containingDirectory?.virtualFile?.path ?: return@mapNotNull null) + File.separator + it }
?: emptyList()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class TextidoteAnnotator : DumbAware, ExternalAnnotator<TextidoteAnnotatorInitia

return TextidoteAnnotatorInitialInfo(
file.virtualFile.name,
File(file.containingDirectory.virtualFile.path),
File(file.containingDirectory?.virtualFile?.path ?: return null),
file.project,
editor.document,
)
Expand Down

0 comments on commit 0388aba

Please sign in to comment.