Skip to content

Commit

Permalink
Support todonotes commands as todo items, fixes #3673
Browse files Browse the repository at this point in the history
  • Loading branch information
slideclimb committed Nov 10, 2024
1 parent 836552b commit e29fc91
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 1 deletion.
2 changes: 2 additions & 0 deletions resources/META-INF/extensions/index.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@
<fileBasedIndex implementation="nl.hannahsten.texifyidea.index.file.LatexExternalEnvironmentIndex" />
<fileBasedIndex implementation="nl.hannahsten.texifyidea.index.file.LatexExternalPackageInclusionIndex" />
<indexedRootsProvider implementation="nl.hannahsten.texifyidea.index.file.LatexIndexableSetContributor" />
<indexPatternSearch implementation="nl.hannahsten.texifyidea.index.LatexTodoSearcher"/>
<todoIndexer filetype="LaTeX source file" implementationClass="nl.hannahsten.texifyidea.index.LatexTodoIndexer"/>
</extensions>
</idea-plugin>
26 changes: 26 additions & 0 deletions src/nl/hannahsten/texifyidea/index/LatexTodoIndexer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package nl.hannahsten.texifyidea.index

import com.intellij.lexer.Lexer
import com.intellij.psi.impl.cache.impl.BaseFilterLexer
import com.intellij.psi.impl.cache.impl.OccurrenceConsumer
import com.intellij.psi.impl.cache.impl.todo.LexerBasedTodoIndexer
import nl.hannahsten.texifyidea.grammar.LatexLexerAdapter
import nl.hannahsten.texifyidea.psi.LatexTypes
import nl.hannahsten.texifyidea.util.magic.CommandMagic

/**
* Counts the "to do" item, so that it shows up in the Project "to do" window and the count of the number of items is correct.
*/
class LatexTodoIndexer : LexerBasedTodoIndexer() {
override fun createLexer(consumer: OccurrenceConsumer): Lexer {
return object : BaseFilterLexer(LatexLexerAdapter(), consumer) {
override fun advance() {
val tokenType = delegate.tokenType
if (tokenType == LatexTypes.COMMAND_TOKEN && delegate.tokenText in CommandMagic.todoCommands) {
advanceTodoItemCountsInToken()
}
delegate.advance()
}
}
}
}
37 changes: 37 additions & 0 deletions src/nl/hannahsten/texifyidea/index/LatexTodoSearcher.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package nl.hannahsten.texifyidea.index

import com.intellij.openapi.application.QueryExecutorBase
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiFile
import com.intellij.psi.search.IndexPattern
import com.intellij.psi.search.IndexPatternOccurrence
import com.intellij.psi.search.searches.IndexPatternSearch
import com.intellij.util.Processor
import nl.hannahsten.texifyidea.file.LatexFile
import nl.hannahsten.texifyidea.util.files.commandsInFile
import nl.hannahsten.texifyidea.util.magic.CommandMagic

/**
* Provides the "to do" item in the toolwindow and highlighting in the editor.
*/
@Suppress("UnstableApiUsage")
class LatexTodoSearcher : QueryExecutorBase<IndexPatternOccurrence, IndexPatternSearch.SearchParameters>() {
override fun processQuery(queryParameters: IndexPatternSearch.SearchParameters, consumer: Processor<in IndexPatternOccurrence>) {
val file = queryParameters.file as? LatexFile ?: return
val pattern = queryParameters.pattern
?: queryParameters.patternProvider.indexPatterns.firstOrNull { it.patternString.contains("todo", true) }
?: return

file.commandsInFile().filter { it.name in CommandMagic.todoCommands }
.forEach {
println("${it.text}: ${it.textRange}")
consumer.process(LatexTodoOccurrence(file, it.textRange, pattern))
}
}
}

private data class LatexTodoOccurrence(private val file: LatexFile, private val textRange: TextRange, private val pattern: IndexPattern) : IndexPatternOccurrence {
override fun getFile(): PsiFile = file
override fun getTextRange(): TextRange = textRange
override fun getPattern(): IndexPattern = pattern
}
1 change: 1 addition & 0 deletions src/nl/hannahsten/texifyidea/lang/LatexPackage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ open class LatexPackage @JvmOverloads constructor(
val TCOLORBOX = LatexPackage("tcolorbox")
val TEXTCOMP = LatexPackage("textcomp")
val TIKZ = LatexPackage("tikz")
val TODONOTES = LatexPackage("todonotes")
val ULEM = LatexPackage("ulem")
val VARIOREF = LatexPackage("varioref")
val WASYSYM = LatexPackage("wasysym")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ object LatexRegularCommand {
private val LISTINGS: Set<LatexCommand> = LatexListingCommand.entries.toSet()
private val LOREM_IPSUM: Set<LatexCommand> = LatexLoremIpsumCommand.entries.toSet()
private val GLOSSARY: Set<LatexCommand> = LatexGlossariesCommand.entries.toSet()
private val TODO: Set<LatexCommand> = LatexTodoCommand.entries.toSet()

val ALL: Set<LatexCommand> = GENERIC + TEXTCOMP + EURO + TEXT_SYMBOLS + NEW_DEFINITIONS + MATHTOOLS +
XCOLOR + XPARSE + NATBIB + BIBLATEX + SIUNITX + ALGORITHMICX + IFS + LISTINGS + LOREM_IPSUM + GLOSSARY
XCOLOR + XPARSE + NATBIB + BIBLATEX + SIUNITX + ALGORITHMICX + IFS + LISTINGS + LOREM_IPSUM + GLOSSARY + TODO

private val lookup = HashMap<String, NonEmptySet<LatexCommand>>()
private val lookupDisplay = HashMap<String, NonEmptySet<LatexCommand>>()
Expand Down
23 changes: 23 additions & 0 deletions src/nl/hannahsten/texifyidea/lang/commands/LatexTodoCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package nl.hannahsten.texifyidea.lang.commands

import nl.hannahsten.texifyidea.lang.LatexPackage
import nl.hannahsten.texifyidea.lang.LatexPackage.Companion.ALGPSEUDOCODE
import nl.hannahsten.texifyidea.lang.LatexPackage.Companion.TODONOTES

enum class LatexTodoCommand(
override val command: String,
override vararg val arguments: Argument = emptyArray(),
override val dependency: LatexPackage = LatexPackage.DEFAULT,
override val display: String? = null,
override val isMathMode: Boolean = false,
val collapse: Boolean = false
) : LatexCommand {

TODO("todo", "note".asRequired(), dependency = TODONOTES),
MISSINGFIGURE("missingfigure", "note".asRequired(), dependency = TODONOTES),
LISTOFTODOS("listoftodos", "name".asOptional(), dependency = TODONOTES)
;

override val identifier: String
get() = name
}
8 changes: 8 additions & 0 deletions src/nl/hannahsten/texifyidea/util/magic/CommandMagic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import nl.hannahsten.texifyidea.lang.commands.LatexNatbibCommand.*
import nl.hannahsten.texifyidea.lang.commands.LatexNewDefinitionCommand.*
import nl.hannahsten.texifyidea.lang.commands.LatexOperatorCommand.*
import nl.hannahsten.texifyidea.lang.commands.LatexRegularCommand
import nl.hannahsten.texifyidea.lang.commands.LatexTodoCommand
import nl.hannahsten.texifyidea.lang.commands.LatexUncategorizedStmaryrdSymbols.BIG_SQUARE_CAP
import nl.hannahsten.texifyidea.lang.commands.LatexXparseCommand.*

Expand Down Expand Up @@ -499,4 +500,11 @@ object CommandMagic {
val foldableFootnotes = listOf(
FOOTNOTE.cmd, FOOTCITE.cmd
)

/**
* Commands that should be contributed to the todo toolwindow.
*/
val todoCommands = setOf(
LatexTodoCommand.TODO.cmd, LatexTodoCommand.MISSINGFIGURE.cmd
)
}

0 comments on commit e29fc91

Please sign in to comment.