Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the \todo and \missingfigure commands from the todonotes package to the todo tool window. #3766

Merged
merged 5 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions resources/META-INF/extensions/index.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@
<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"/>
<indexPatternProvider implementation="nl.hannahsten.texifyidea.index.LatexTodoIndexPatternProvider"/>
<todoIndexer filetype="LaTeX source file" implementationClass="nl.hannahsten.texifyidea.index.LatexTodoIndexer"/>
</extensions>
</idea-plugin>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package nl.hannahsten.texifyidea.index

import com.intellij.psi.search.IndexPattern
import com.intellij.psi.search.IndexPatternProvider
import nl.hannahsten.texifyidea.util.magic.CommandMagic

class LatexTodoIndexPatternProvider : IndexPatternProvider {
override fun getIndexPatterns(): Array<IndexPattern> {
return CommandMagic.todoCommands.map { IndexPattern("${it.replace("\\", "\\\\")}\\b", true) }.toTypedArray()
}
}
29 changes: 29 additions & 0 deletions src/nl/hannahsten/texifyidea/index/LatexTodoIndexer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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.grammar.LatexTokenSets
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 LatexFilterLexer(consumer)
}
}

class LatexFilterLexer(consumer: OccurrenceConsumer) : BaseFilterLexer(LatexLexerAdapter(), consumer) {
override fun advance() {
val tokenType = delegate.tokenType
if (tokenType in LatexTokenSets.COMMENTS || (tokenType == LatexTypes.COMMAND_TOKEN && delegate.tokenText in CommandMagic.todoCommands)) {
advanceTodoItemCountsInToken()
}
delegate.advance()
}
}
36 changes: 36 additions & 0 deletions src/nl/hannahsten/texifyidea/index/LatexTodoSearcher.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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
import nl.hannahsten.texifyidea.util.matches

/**
* Provides the "to do" item in the toolwindow and highlighting in the editor.
PHPirates marked this conversation as resolved.
Show resolved Hide resolved
*/
@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

queryParameters.patternProvider.indexPatterns.forEach { pattern ->
file.commandsInFile().filter { it.name in CommandMagic.todoCommands }.filter { pattern.pattern?.matches(it.name) == true }
.forEach {
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
22 changes: 22 additions & 0 deletions src/nl/hannahsten/texifyidea/lang/commands/LatexTodoCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package nl.hannahsten.texifyidea.lang.commands

import nl.hannahsten.texifyidea.lang.LatexPackage
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
}
6 changes: 6 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,9 @@ object CommandMagic {
val foldableFootnotes = listOf(
FOOTNOTE.cmd, FOOTCITE.cmd
)

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