Skip to content

Commit

Permalink
Add support for automatic language injection on the minted environment
Browse files Browse the repository at this point in the history
  • Loading branch information
PHPirates committed Dec 13, 2024
1 parent 5f0c25c commit 512e44d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [Unreleased]

### Added
* Add support for automatic language injection on the minted environment
* Do not fold sections in a command definition
* Include optional parameters in spellcheck, if it contains text

Expand Down
24 changes: 18 additions & 6 deletions src/nl/hannahsten/texifyidea/grammar/LatexLexer.flex
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ import static nl.hannahsten.texifyidea.psi.LatexTypes.*;
private int newCommandBracesNesting = 0;

/**
* Also keep track of brackets of verbatim environment optional arguments.
* Also keep track of brackets of verbatim environment arguments.
*/
private int verbatimOptionalArgumentBracketsCount = 0;
private int verbatimRequiredArgumentBracketsCount = 0;

/**
* Keep track of braces in the PARTIAL_DEFINITION state.
Expand Down Expand Up @@ -144,8 +145,8 @@ END_IFS=\\fi
// States are exclusive to avoid matching expressions with an empty set of associated states, i.e. to avoid matching normal LaTeX expressions
%xstates INLINE_VERBATIM_PLAIN_START INLINE_VERBATIM INLINE_VERBATIM_NORMAL_START

%states POSSIBLE_VERBATIM_BEGIN VERBATIM_OPTIONAL_ARG VERBATIM_START VERBATIM_END INLINE_VERBATIM_OPTIONAL_ARG
%xstates VERBATIM POSSIBLE_VERBATIM_OPTIONAL_ARG POSSIBLE_VERBATIM_END
%states POSSIBLE_VERBATIM_BEGIN VERBATIM_OPTIONAL_ARG VERBATIM_REQUIRED_ARG VERBATIM_START VERBATIM_END INLINE_VERBATIM_OPTIONAL_ARG
%xstates VERBATIM POSSIBLE_VERBATIM_ARG POSSIBLE_VERBATIM_END

// algorithmic environment
%states PSEUDOCODE POSSIBLE_PSEUDOCODE_END
Expand Down Expand Up @@ -231,13 +232,14 @@ END_IFS=\\fi

// Jump over the closing } of the \begin{verbatim} before starting verbatim state
<VERBATIM_START> {
{CLOSE_BRACE} { yypopState(); yypushState(POSSIBLE_VERBATIM_OPTIONAL_ARG); return CLOSE_BRACE; }
{CLOSE_BRACE} { yypopState(); yypushState(POSSIBLE_VERBATIM_ARG); return CLOSE_BRACE; }
}

// Check if an optional argument is coming up
// If you start a verbatim with an open bracket and don't close it, this won't work
<POSSIBLE_VERBATIM_OPTIONAL_ARG> {
<POSSIBLE_VERBATIM_ARG> {
{OPEN_BRACKET} { verbatimOptionalArgumentBracketsCount = 1; yypopState(); yypushState(VERBATIM_OPTIONAL_ARG); return OPEN_BRACKET; }
{OPEN_BRACE} { verbatimRequiredArgumentBracketsCount = 1; yypopState(); yypushState(VERBATIM_REQUIRED_ARG); return OPEN_BRACE; }
{WHITE_SPACE} { yypopState(); yypushState(VERBATIM); return com.intellij.psi.TokenType.WHITE_SPACE; }
{ANY_CHAR} { yypopState(); yypushState(VERBATIM); return RAW_TEXT_TOKEN; }
}
Expand All @@ -248,11 +250,21 @@ END_IFS=\\fi
{OPEN_BRACKET} { verbatimOptionalArgumentBracketsCount++; return OPEN_BRACKET; }
{CLOSE_BRACKET} {
verbatimOptionalArgumentBracketsCount--;
if (verbatimOptionalArgumentBracketsCount == 0) { yypopState(); yypushState(VERBATIM); }
// There can be a required arg coming
if (verbatimOptionalArgumentBracketsCount == 0) { yypopState(); yypushState(POSSIBLE_VERBATIM_ARG); }
return CLOSE_BRACKET;
}
}

<VERBATIM_REQUIRED_ARG> {
{OPEN_BRACE} { verbatimRequiredArgumentBracketsCount++; return OPEN_BRACE; }
{CLOSE_BRACE} {
verbatimRequiredArgumentBracketsCount--;
if (verbatimRequiredArgumentBracketsCount == 0) { yypopState(); yypushState(VERBATIM); }
return CLOSE_BRACE;
}
}

<VERBATIM> {
// Also catch whitespace, see LatexParserUtil for more info
{WHITE_SPACE} { return com.intellij.psi.TokenType.WHITE_SPACE; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class LatexParserDefinition : ParserDefinition {
val FILE: IStubFileElementType<*> = object : IStubFileElementType<LatexFileStub>(
"LatexStubFileElementType", Language.findInstance(LatexLanguage::class.java)
) {
override fun getStubVersion(): Int = 77
override fun getStubVersion(): Int = 78
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/nl/hannahsten/texifyidea/psi/LatexLanguageInjector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.intellij.openapi.util.TextRange
import com.intellij.psi.InjectedLanguagePlaces
import com.intellij.psi.LanguageInjector
import com.intellij.psi.PsiLanguageInjectionHost
import nl.hannahsten.texifyidea.lang.DefaultEnvironment
import nl.hannahsten.texifyidea.lang.magic.DefaultMagicKeys
import nl.hannahsten.texifyidea.lang.magic.magicComment
import nl.hannahsten.texifyidea.util.camelCase
Expand All @@ -32,9 +33,12 @@ class LatexLanguageInjector : LanguageInjector {
hasMagicCommentKey -> {
magicComment.value(DefaultMagicKeys.INJECT_LANGUAGE)
}
host.getEnvironmentName() == "lstlisting" -> {
host.getEnvironmentName() == DefaultEnvironment.LISTINGS.environmentName -> {
host.beginCommand.getOptionalParameterMap().toStringMap().getOrDefault("language", null)
}
host.getEnvironmentName() == DefaultEnvironment.MINTED.environmentName -> {
host.beginCommand.getRequiredParameters().getOrNull(1)
}
host.getEnvironmentName() in EnvironmentMagic.languageInjections.keys -> {
EnvironmentMagic.languageInjections[host.getEnvironmentName()]
}
Expand Down

0 comments on commit 512e44d

Please sign in to comment.