Skip to content

Commit

Permalink
Merge pull request #1269 from Hannah-Sten/output-paths
Browse files Browse the repository at this point in the history
Fix lots of bugs
  • Loading branch information
PHPirates authored Feb 22, 2020
2 parents a96d827 + 0266f6d commit 12afc70
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import nl.hannahsten.texifyidea.inspections.TexifyInspectionBase
import nl.hannahsten.texifyidea.lang.magic.MagicCommentScope
import nl.hannahsten.texifyidea.psi.LatexCommands
import nl.hannahsten.texifyidea.util.*
import nl.hannahsten.texifyidea.util.files.commandsInFile
import nl.hannahsten.texifyidea.util.files.commandsInFileSet
import java.util.*

Expand Down Expand Up @@ -39,7 +38,7 @@ open class LatexFigureNotReferencedInspection : TexifyInspectionBase() {
private fun removeReferencedLabels(file: PsiFile, figureLabels: MutableMap<String?, LatexCommands>) {
for (command in file.commandsInFileSet()) {
if (Magic.Command.labelReference.contains(command.name)) {
figureLabels.remove(command.referencedLabelName)
command.referencedLabelNames.forEach { figureLabels.remove(it) }
}
}
}
Expand Down Expand Up @@ -67,5 +66,5 @@ open class LatexFigureNotReferencedInspection : TexifyInspectionBase() {
private val LatexCommands.labelName: String?
get() = requiredParameter(0)

private val LatexCommands.referencedLabelName: String?
get() = requiredParameter(0)
private val LatexCommands.referencedLabelNames: List<String>
get() = requiredParameter(0)?.split(",") ?: emptyList()
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ open class LatexLabelConventionInspection : TexifyInspectionBase() {
val labeledCommand = getLabeledCommand(label) ?: return@forEach
val expectedPrefix = getLabelPrefix(labeledCommand)
val labelName = label.extractLabelName()
if (!labelName.startsWith("$expectedPrefix:")) {
if (!expectedPrefix.isNullOrBlank() && !labelName.startsWith("$expectedPrefix:")) {
descriptors.add(manager.createProblemDescriptor(
label,
"Unconventional label prefix",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ open class LatexMissingLabelInspection : TexifyInspectionBase() {
*/
private class InsertLabelAfterCommandFix : LabelQuickFix() {

override fun getFamilyName() = "Insert label"
override fun getFamilyName() = "Insert label for this command"

override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val command = descriptor.psiElement as LatexCommands
Expand Down Expand Up @@ -154,7 +154,7 @@ open class LatexMissingLabelInspection : TexifyInspectionBase() {
}

private class InsertLabelInEnvironmentFix : LabelQuickFix() {
override fun getFamilyName() = "Insert label"
override fun getFamilyName() = "Insert label for this environment"

override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val command = descriptor.psiElement as LatexEnvironment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ open class LatexNonBreakingSpaceInspection : TexifyInspectionBase() {
* All commands that should not have a forced breaking space.
*/
val IGNORED_COMMANDS = setOf(
"\\citet", "\\citet*", "\\Citet", "\\Citet*", "\\cref", "\\Cref", "\\cpageref", "\\autoref", "\\citeauthor"
"\\citet", "\\citet*", "\\Citet", "\\Citet*", "\\cref", "\\Cref", "\\cpageref", "\\autoref", "\\citeauthor", "\\textcite", "\\Textcite"
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ open class LatexCommandLineState(environment: ExecutionEnvironment, private val
runConfig.generateBibRunConfig()

runConfig.bibRunConfigs.forEach {
val bibSettings = it ?: return@forEach
val bibSettings = it

// Pass necessary latex run configurations settings to the bibtex run configuration.
(bibSettings.configuration as? BibtexRunConfiguration)?.apply {
Expand All @@ -80,7 +80,7 @@ open class LatexCommandLineState(environment: ExecutionEnvironment, private val
?.psiFile(runConfig.project)
?.includedPackages()
?: setOf()
isMakeindexNeeded = includedPackages.intersect(index.asIterable()).isNotEmpty()
isMakeindexNeeded = includedPackages.intersect(index.asIterable()).isNotEmpty() && runConfig.compiler?.includesMakeindex == false

if (isMakeindexNeeded) {
// Some packages do handle makeindex themselves
Expand Down Expand Up @@ -111,10 +111,6 @@ open class LatexCommandLineState(environment: ExecutionEnvironment, private val
return@forEachIndexed
}

if (bibSettings == null) {
return@forEachIndexed
}

// Only run latex after the last one
if (index == runConfig.bibRunConfigs.size - 1) {
handler.addProcessListener(RunBibtexListener(bibSettings, runConfig, environment, true))
Expand Down
31 changes: 23 additions & 8 deletions src/nl/hannahsten/texifyidea/run/latex/LatexRunConfiguration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ class LatexRunConfiguration constructor(project: Project,
var allowFocusChange = true

private var bibRunConfigIds = mutableSetOf<String>()
var bibRunConfigs: Set<RunnerAndConfigurationSettings?>
get() = bibRunConfigIds.map {
var bibRunConfigs: Set<RunnerAndConfigurationSettings>
get() = bibRunConfigIds.mapNotNull {
RunManagerImpl.getInstanceImpl(project).getConfigurationById(it)
}.toSet()
set(bibRunConfigs) {
bibRunConfigIds = mutableSetOf()
bibRunConfigs.forEach {
bibRunConfigIds.add(it?.uniqueID ?: "")
bibRunConfigIds.add(it.uniqueID)
}
}

Expand Down Expand Up @@ -188,7 +188,12 @@ class LatexRunConfiguration constructor(project: Project,
// Read output path
val outputPathString = parent.getChildText(OUTPUT_PATH)
if (outputPathString != null) {
this.outputPath = fileSystem.findFileByPath(outputPathString)
if (outputPathString.endsWith("/bin")) {
this.outputPath = getDefaultOutputPath()
}
else {
this.outputPath = fileSystem.findFileByPath(outputPathString)
}
}

// Read auxil path
Expand Down Expand Up @@ -463,7 +468,12 @@ class LatexRunConfiguration constructor(project: Project,
* Set [outputPath]
*/
override fun setFileOutputPath(fileOutputPath: String) {
this.outputPath = LocalFileSystem.getInstance().findFileByPath(fileOutputPath)
if (fileOutputPath.endsWith("/bin")) {
this.outputPath = getDefaultOutputPath()
}
else {
this.outputPath = LocalFileSystem.getInstance().findFileByPath(fileOutputPath)
}
}

/**
Expand All @@ -475,14 +485,19 @@ class LatexRunConfiguration constructor(project: Project,
}

private fun getDefaultOutputPath(): VirtualFile? {
val moduleRoot = ProjectRootManager.getInstance(project).fileIndex.getContentRootForFile(mainFile!!)
return LocalFileSystem.getInstance().findFileByPath(moduleRoot?.path + "/out")
if (mainFile == null) return null
var defaultOutputPath: VirtualFile? = null
runReadAction {
val moduleRoot = ProjectRootManager.getInstance(project).fileIndex.getContentRootForFile(mainFile!!)
defaultOutputPath = LocalFileSystem.getInstance().findFileByPath(moduleRoot?.path + "/out")
}
return defaultOutputPath
}

/**
* Whether the current output path is the default.
*/
fun isDefaultOutputPath() = getDefaultOutputPath() == outputPath
private fun isDefaultOutputPath() = getDefaultOutputPath() == outputPath

/**
* Assuming the main file is known, set a default auxil path if not already set.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,12 @@ class LatexSettingsEditor(private var project: Project?) : SettingsEditor<LatexR
runConfiguration.setMainFile(filePath)

val outputPathTextField = outputPath.component as TextFieldWithBrowseButton
runConfiguration.setFileOutputPath(outputPathTextField.text)
if (outputPathTextField.text.endsWith("/bin")) {
runConfiguration.setDefaultOutputPath()
}
else {
runConfiguration.setFileOutputPath(outputPathTextField.text)
}

if (auxilPath != null) {
val auxilPathTextField = auxilPath!!.component as TextFieldWithBrowseButton
Expand Down
3 changes: 2 additions & 1 deletion src/nl/hannahsten/texifyidea/util/Magic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,8 @@ object Magic {
* All file extensions that are probably generated by LaTeX and some common packages.
* Because any package can generated new files, this list is not complete.
*/
val generatedFileTypes = auxiliaryFileTypes + arrayOf("blg", "dvi", "fdb_latexmk", "ilg", "log", "out.ps", "pdf", "run.xml", "sagetex.sage", "sagetex.scmd", "sagetex.sout", "synctex", "gz", "synctex(busy)", "upa", "doctest.sage", "xdv")
// Actually run.xml should be included (latexmk) but file extensions with a dot are not found currently, see Utils#File.extension
val generatedFileTypes = auxiliaryFileTypes + arrayOf("blg", "dvi", "fdb_latexmk", "ilg", "log", "out.ps", "pdf", "xml", "sagetex.sage", "sagetex.scmd", "sagetex.sout", "synctex", "gz", "synctex(busy)", "upa", "doctest.sage", "xdv")
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/nl/hannahsten/texifyidea/util/files/Files.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ val PsiFile.fileSearchScope: GlobalSearchScope
/**
* Looks up the PsiFile that corresponds to the Virtual File.
*/
fun VirtualFile.psiFile(project: Project): PsiFile? = PsiManager.getInstance(project).findFile(this)
fun VirtualFile.psiFile(project: Project): PsiFile? {
if (!this.isValid) return null
return PsiManager.getInstance(project).findFile(this)
}

/**
* Looks for a certain file relative to this directory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,41 @@ class FigureNotReferencedInspectionTest : TexifyInspectionTestBase(LatexFigureNo
""".trimIndent())
myFixture.checkHighlighting(false, false, true, false)
}

fun testFigureReferencedNoWarning() {
myFixture.configureByText(LatexFileType, """
\usepackage{listings}
\begin{document}
\begin{figure}
\label{fig:some-figure}
\end{figure}
\ref{fig:some-figure}
\end{document}
""".trimIndent())
myFixture.checkHighlighting(false, false, true, false)
}

fun testFigureReferencedMultipleReferencesNoWarning() {
myFixture.configureByText(LatexFileType, """
\documentclass{article}
\usepackage{cleveref}
\begin{document}
\begin{figure}
Figure text.
\caption{Caption}
\label{fig:some-figure}
\end{figure}
\begin{figure}
Figure text.
\caption{Caption2}
\label{fig:some-figure2}
\end{figure}
I ref~\cref{fig:some-figure,fig:some-figure2}
\end{document}
""".trimIndent())
myFixture.checkHighlighting(false, false, true, false)
}
}

0 comments on commit 12afc70

Please sign in to comment.