Skip to content

Commit

Permalink
Merge pull request #3766 from Hannah-Sten/3673-todo
Browse files Browse the repository at this point in the history
Add the \todo and \missingfigure commands from the todonotes package to the todo tool window.
  • Loading branch information
PHPirates authored Dec 6, 2024
2 parents 8054b28 + ccf430e commit 052a869
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 1 deletion.
16 changes: 16 additions & 0 deletions Writerside/topics/Code-navigation.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,19 @@ An example:

On Linux, the Mendeley format is also supported, which is of the form
`file = {:home/user/.local/share/data/Mendeley Ltd./Mendeley Desktop/Downloaded/filename.pdf:pdf;:home/user/.local/share/data/Mendeley Ltd./Mendeley Desktop/Downloaded/filename2.pdf:pdf}`

## TODO view

_Since b0.9.9_

<ui-path>View | Tool Windows | TODO</ui-path>

The TODO view by default shows all `todo` comments and `\todo{}` and `\missingfigure{}` commands.
Highlighting of the commands in the IDE by default is currently not enabled due to API limitations, but can be achieved by adding the following patterns to <ui-path>File | Settings | Editor | TODO </ui-path>:

- `\\todo\b`, case sensitive is `true`
- `\\missingfigure\b`, case sensitive is `true`

However, this adds the items to the tool window a second time.
To avoid this, create a filter and include all patterns.
Magic.
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.
*/
@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 @@ -89,6 +89,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 UPGREEK = LatexPackage("upgreek")
val VARIOREF = LatexPackage("varioref")
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 @@ -501,4 +502,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)
}

0 comments on commit 052a869

Please sign in to comment.