Skip to content

Commit

Permalink
Merge pull request #3768 from Hannah-Sten/subfix
Browse files Browse the repository at this point in the history
Add support for the \subfix command
  • Loading branch information
PHPirates authored Nov 24, 2024
2 parents 54addf9 + 88e844a commit 3cd4655
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## [Unreleased]

### Added
* Support references in \subfix command
* Provide autocompletion for all relevant directories when using subfiles

### Fixed
* Fix 'missing import' false positive in subfiles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ open class LatexCompletionContributor : CompletionContributor() {
.inside(LatexRequiredParam::class.java)
.withPattern("File name completion pattern") { psiElement, processingContext ->
val command = getParentOfType(psiElement, LatexCommands::class.java) ?: return@withPattern false
val name = command.commandToken.text
val name = command.name ?: return@withPattern false
val cmd = LatexRegularCommand[name.substring(1)] ?: return@withPattern false

val args = cmd.first().getArgumentsOf(RequiredFileArgument::class)
Expand All @@ -117,7 +117,7 @@ open class LatexCompletionContributor : CompletionContributor() {
.inside(LatexRequiredParam::class.java)
.withPattern("Folder name completion pattern") { psiElement, processingContext ->
val command = getParentOfType(psiElement, LatexCommands::class.java) ?: return@withPattern false
val name = command.commandToken.text
val name = command.name ?: return@withPattern false
val cmd = LatexRegularCommand[name.substring(1)] ?: return@withPattern false

val args = cmd.first().getArgumentsOf(RequiredFolderArgument::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import nl.hannahsten.texifyidea.completion.handlers.LatexReferenceInsertHandler
import nl.hannahsten.texifyidea.lang.commands.RequiredFileArgument
import nl.hannahsten.texifyidea.psi.LatexRequiredParam
import nl.hannahsten.texifyidea.util.expandCommandsOnce
import nl.hannahsten.texifyidea.util.files.findRootFile
import nl.hannahsten.texifyidea.util.files.findRootFiles
import nl.hannahsten.texifyidea.util.files.isLatexFile
import nl.hannahsten.texifyidea.util.replaceAfterFrom
import java.io.File
Expand Down Expand Up @@ -205,8 +205,8 @@ abstract class LatexPathProviderBase : CompletionProvider<CompletionParameters>(
val baseFile = this.originalFile.virtualFile

if (this.originalFile.isLatexFile()) {
this.originalFile.findRootFile().containingDirectory?.virtualFile?.apply {
resultList.add(this)
this.originalFile.findRootFiles().mapNotNull { it.containingDirectory?.virtualFile }.forEach {
resultList.add(it)
}
}
else resultList.add(baseFile.parent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ enum class LatexGenericRegularCommand(
STRETCH("stretch", "factor".asRequired()),
SUBFILE("subfile", RequiredFileArgument("sourcefile", true, false, "tex"), dependency = LatexPackage.SUBFILES),
SUBFILEINCLUDE("subfileinclude", RequiredFileArgument("sourcefile", true, false, "tex"), dependency = LatexPackage.SUBFILES),
SUBFIX("subfix", RequiredFileArgument("file", true, false, "tex"), dependency = LatexPackage.SUBFILES),
SUBIMPORT("subimport", RequiredFolderArgument("relative path"), RequiredFileArgument("filename", false, false, "tex"), dependency = LatexPackage.IMPORT),
SUBINCLUDEFROM("subincludefrom", RequiredFolderArgument("relative path"), RequiredFileArgument("filename", false, false, "tex"), dependency = LatexPackage.IMPORT),
SUBINPUTFROM("subinputfrom"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ fun LatexCommands.getFileArgumentsReferences(): List<InputFileReference> {
it.shiftRight(requiredParameter.textOffset - this.textOffset)
}
}
else if (requiredParameter.firstChildOfType(LatexParameterText::class)?.children?.size == 1 && requiredParameter.firstChildOfType(LatexCommands::class) != null) {
// Special case if there is a single command instead of text, we ignore it. Example: \subfix from the subfiles package, can be ignored for our purposes
val newRequiredParameter = requiredParameter.firstChildOfType(LatexCommands::class)?.requiredParameters()?.firstOrNull() ?: requiredParameter
listOf(newRequiredParameter.textRange.shrink(1).shiftLeft(this.textOffset))
}
else {
listOf(requiredParameter.textRange.shrink(1).shiftLeft(this.textOffset))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ class InputFileReferenceTest : BasePlatformTestCase() {
myFixture.renameElementAtCaret("newmain.tex")
myFixture.checkResultByFile("simplesubfile/subdir/onedown-after.tex")
}

fun testRenameSubfix() {
myFixture.configureByFiles("subfix/main.tex", "subfix/subdir2/chapter2.tex", "subfix/subdir2/references.bib")
myFixture.renameElementAtCaret("newreferences.bib")
myFixture.checkResultByFile("subfix/main-after.tex")
}
}
7 changes: 7 additions & 0 deletions test/resources/reference/subfix/main-after.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
\documentclass[11pt]{report}
\usepackage{subfiles}
\usepackage{biblatex}
\addbibresource{\subfix{subdir2/newreferences.bib}}
\begin{document}
\subfile{subdir2/chapter2.tex}
\end{document}
7 changes: 7 additions & 0 deletions test/resources/reference/subfix/main.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
\documentclass[11pt]{report}
\usepackage{subfiles}
\usepackage{biblatex}
\addbibresource{\subfix{subdir2/refer<caret>ences.bib}}
\begin{document}
\subfile{subdir2/chapter2.tex}
\end{document}
6 changes: 6 additions & 0 deletions test/resources/reference/subfix/subdir2/chapter2.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
\documentclass[../main.tex]{subfiles}
\begin{document}
\chapter{Some 2}
I~\cite{tectonic}
\printbibliography
\end{document}
6 changes: 6 additions & 0 deletions test/resources/reference/subfix/subdir2/references.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@online{tectonic,
title = {The Tectonic Typesetting System — The Tectonic Typesetting System},
url = {https://tectonic-typesetting.github.io/en-US/index.html},
urldate = {2018-12-27},
}

0 comments on commit 3cd4655

Please sign in to comment.