Skip to content

Commit

Permalink
Merge branch 'go-to-definition'
Browse files Browse the repository at this point in the history
  • Loading branch information
PHPirates committed Mar 5, 2021
2 parents d14390e + 74a6a13 commit fca2441
Show file tree
Hide file tree
Showing 8 changed files with 688 additions and 663 deletions.
1,294 changes: 661 additions & 633 deletions gen/nl/hannahsten/texifyidea/grammar/LatexLexer.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<html>
<body>
When including a bibliography using <tt>\bibliography</tt>, you also have to provide it with <b>exctly one</b> bibliography style using
When including a bibliography using <tt>\bibliography</tt>, you also have to provide it with <b>exactly one</b> bibliography style using
<tt>\bibliographystyle</tt>.
<!-- tooltip end -->
</body>
Expand Down
9 changes: 4 additions & 5 deletions src/nl/hannahsten/texifyidea/grammar/LatexLexer.flex
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ LATEX3_ON=\\ExplSyntaxOn
LATEX3_OFF=\\ExplSyntaxOff
NEWENVIRONMENT=\\(re)?newenvironment
NEWDOCUMENTENVIRONMENT=\\(New|Renew|Provide|Declare)DocumentEnvironment
VERBATIM_COMMAND=\\verb | \\verb\* | \\directlua | \\luaexec | \\lstinline
// These can contain unescaped % for example
| \\url | \\path | \\href

// Comments
MAGIC_COMMENT_PREFIX=("!"|" !"[tT][eE][xX])
Expand Down Expand Up @@ -121,11 +124,7 @@ END_PSEUDOCODE_BLOCK="\\EndFor" | "\\EndIf" | "\\EndWhile" | "\\Until" | "\\EndL
*/

// Use a separate state to start verbatim, to be able to return a command token for \verb
\\verb |
\\verb\* |
\\directlua |
\\luaexec |
\\lstinline { yypushState(INLINE_VERBATIM_START); return COMMAND_TOKEN; }
{VERBATIM_COMMAND} { yypushState(INLINE_VERBATIM_START); return COMMAND_TOKEN; }

<INLINE_VERBATIM_START> {
// Experimental syntax of \lstinline: \lstinline{verbatim}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import com.intellij.psi.PsiFile
import nl.hannahsten.texifyidea.index.LatexCommandsIndex
import nl.hannahsten.texifyidea.inspections.InsightGroup
import nl.hannahsten.texifyidea.inspections.TexifyInspectionBase
import nl.hannahsten.texifyidea.lang.commands.LatexNewDefinitionCommand
import nl.hannahsten.texifyidea.util.forcedFirstRequiredParameterAsCommand
import nl.hannahsten.texifyidea.util.magic.CommandMagic
import nl.hannahsten.texifyidea.util.magic.cmd
import org.jetbrains.annotations.Nls

/**
Expand Down Expand Up @@ -38,8 +40,8 @@ class LatexMightBreakTexifyInspection : TexifyInspectionBase() {
for (command in commands) {
// Error when \newcommand is used on existing command
if (CommandMagic.redefinitions.contains(command.name)) {
val newCommand = command.forcedFirstRequiredParameterAsCommand() ?: continue
if (CommandMagic.fragile.contains(newCommand.name)) {
val newCommand = command.forcedFirstRequiredParameterAsCommand()
if (CommandMagic.fragile.contains(newCommand?.name) || command.name == LatexNewDefinitionCommand.CATCODE.cmd) {
descriptors.add(
manager.createProblemDescriptor(
command,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ enum class LatexNewDefinitionCommand(
val collapse: Boolean = false
) : LatexCommand {

CATCODE("catcode"),
NEWCOMMAND("newcommand", "cmd".asRequired(), "args".asOptional(), "default".asOptional(), "def".asRequired(Argument.Type.TEXT)),
NEWCOMMAND_STAR("newcommand*", "cmd".asRequired(), "args".asOptional(), "default".asOptional(), "def".asRequired(Argument.Type.TEXT)),
NEWIF("newif", "cmd".asRequired()),
Expand Down
26 changes: 11 additions & 15 deletions src/nl/hannahsten/texifyidea/psi/LatexCommandsImplUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,33 @@ import java.util.regex.Pattern

/**
* Get the references for this command.
* For example for a \ref{label1,label2} command, then label1 and label2 are the references.
*/
fun getReferences(element: LatexCommands): Array<PsiReference> {
val firstParam = readFirstParam(element)

// If it is a reference to a label
val references = mutableListOf<PsiReference>()

// If it is a reference to a label (used for autocompletion, do not confuse with reference resolving from LatexParameterText)
if (element.project.getLabelReferenceCommands().contains(element.commandToken.text) && firstParam != null) {
val references = extractLabelReferences(element, firstParam)
return references.toTypedArray()
references.addAll(extractLabelReferences(element, firstParam))
}

// If it is a reference to a file
val references: List<PsiReference> = element.getFileArgumentsReferences()
if (firstParam != null && references.isNotEmpty()) {
return references.toTypedArray()
}
references.addAll(element.getFileArgumentsReferences())

if (CommandMagic.urls.contains(element.name) && firstParam != null) {
return element.extractUrlReferences(firstParam)
references.addAll(element.extractUrlReferences(firstParam))
}

// Else, we assume the command itself is important instead of its parameters,
// and the user is interested in the location of the command definition
val reference = CommandDefinitionReference(element)
val definitionReference = CommandDefinitionReference(element)
// Only create a reference if there is something to resolve to, otherwise autocompletion won't work
return if (reference.multiResolve(false).isEmpty()) {
emptyArray()
}
else {
arrayOf(reference)
if (definitionReference.multiResolve(false).isNotEmpty()) {
references.add(definitionReference)
}

return references.toTypedArray()
}

/**
Expand Down
7 changes: 2 additions & 5 deletions src/nl/hannahsten/texifyidea/psi/LatexParameterTextUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ import com.intellij.psi.PsiReference
import nl.hannahsten.texifyidea.reference.BibtexIdReference
import nl.hannahsten.texifyidea.reference.LatexEnvironmentReference
import nl.hannahsten.texifyidea.reference.LatexLabelParameterReference
import nl.hannahsten.texifyidea.util.extractLabelName
import nl.hannahsten.texifyidea.util.firstParentOfType
import nl.hannahsten.texifyidea.util.getLabelDefinitionCommands
import nl.hannahsten.texifyidea.util.*
import nl.hannahsten.texifyidea.util.magic.CommandMagic
import nl.hannahsten.texifyidea.util.magic.EnvironmentMagic
import nl.hannahsten.texifyidea.util.parentOfType

/**
* If the normal text is the parameter of a \ref-like command, get the references to the label declaration.
Expand All @@ -20,7 +17,7 @@ fun getReferences(element: LatexParameterText): Array<PsiReference> {
// If the command is a label reference
// NOTE When adding options here, also update getNameIdentifier below
return when {
CommandMagic.labelReferenceWithoutCustomCommands.contains(element.firstParentOfType(LatexCommands::class)?.name) -> {
element.project.getLabelReferenceCommands().contains(element.firstParentOfType(LatexCommands::class)?.name) -> {
arrayOf<PsiReference>(LatexLabelParameterReference(element))
}
// If the command is a bibliography reference
Expand Down
6 changes: 4 additions & 2 deletions src/nl/hannahsten/texifyidea/util/magic/CommandMagic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ object CommandMagic {
*
* This will check if the cache of user defined commands needs to be updated, based on the given project, and therefore may take some time.
*/
fun getLabelDefinitionCommands(project: Project): Set<String> {
fun getLabelDefinitionCommands(project: Project): Set<String>? {
// Check if updates are needed
CommandManager.updateAliases(labelDefinitionsWithoutCustomCommands, project)
return CommandManager.getAliases(labelDefinitionsWithoutCustomCommands.first())
Expand Down Expand Up @@ -195,7 +195,8 @@ object CommandMagic {
DECLAREDOCUMENTCOMMAND,
DEF,
LET,
RENEWENVIRONMENT
RENEWENVIRONMENT,
CATCODE, // Not really redefining commands, but characters
).map { it.cmd }

/**
Expand Down Expand Up @@ -294,6 +295,7 @@ object CommandMagic {
REQUIREPACKAGE.cmd to hashSetOf("sty"),
USEPACKAGE.cmd to hashSetOf("sty"),
DOCUMENTCLASS.cmd to hashSetOf("cls"),
LOADCLASS.cmd to hashSetOf("cls"),
EXTERNALDOCUMENT.cmd to hashSetOf("tex") // Not completely true, as it only includes labels
)

Expand Down

0 comments on commit fca2441

Please sign in to comment.