From 8e28c8c8ee947e55e6b7fc660f8f023290a0c3df Mon Sep 17 00:00:00 2001 From: jojo2357 <66704796+jojo2357@users.noreply.github.com> Date: Tue, 21 Mar 2023 14:10:31 -0600 Subject: [PATCH 1/8] Fold Comments revision 1 --- .../folding/LatexCommentFoldingBuilder.kt | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/nl/hannahsten/texifyidea/editor/folding/LatexCommentFoldingBuilder.kt diff --git a/src/nl/hannahsten/texifyidea/editor/folding/LatexCommentFoldingBuilder.kt b/src/nl/hannahsten/texifyidea/editor/folding/LatexCommentFoldingBuilder.kt new file mode 100644 index 000000000..9f615162b --- /dev/null +++ b/src/nl/hannahsten/texifyidea/editor/folding/LatexCommentFoldingBuilder.kt @@ -0,0 +1,92 @@ +package nl.hannahsten.texifyidea.editor.folding + +import com.intellij.lang.ASTNode +import com.intellij.lang.folding.FoldingBuilderEx +import com.intellij.lang.folding.FoldingDescriptor +import com.intellij.openapi.editor.Document +import com.intellij.openapi.project.DumbAware +import com.intellij.openapi.util.TextRange +import com.intellij.psi.PsiComment +import com.intellij.psi.PsiElement +import com.intellij.psi.PsiWhiteSpace +import com.intellij.refactoring.suggested.endOffset +import com.intellij.refactoring.suggested.startOffset +import nl.hannahsten.texifyidea.util.childrenOfType + +/** + * Adds folding regions for LaTeX environments. + * + * Enables folding of `\footnote{}`. + * + * @author jojo2357 + */ +class LatexCommentFoldingBuilder : FoldingBuilderEx(), DumbAware { + + override fun isCollapsedByDefault(node: ASTNode) = false + + override fun getPlaceholderText(node: ASTNode): String { + val parsedText = node.text.trim() + return if (parsedText.length > 8) parsedText.substring(0, 8) + "..." + else parsedText.substring( + 0, + parsedText.length - 1 + ) + } + + override fun buildFoldRegions(root: PsiElement, document: Document, quick: Boolean): Array { + val descriptors = ArrayList() + val comments = root.childrenOfType(PsiComment::class) + val whitespaces = root.childrenOfType(PsiWhiteSpace::class) + + val whitespaceLocations = ArrayList(whitespaces.map { TextRange(it.startOffset, it.endOffset) }) + + // meld friends + for (i in whitespaceLocations.size - 1 downTo 1) { + if (whitespaceLocations[i].startOffset == whitespaceLocations[i - 1].endOffset) { + whitespaceLocations[i - 1] = + TextRange(whitespaceLocations[i - 1].startOffset, whitespaceLocations[i].endOffset) + whitespaceLocations.removeAt(i) + } + } + + var collectedTextRange: TextRange? = null + var parentCollapse: PsiElement? = null + + for (comment in comments) { + if (collectedTextRange == null) { + collectedTextRange = TextRange(comment.startOffset, comment.endOffset) + parentCollapse = comment.originalElement + } + else { + if (whitespaceLocations.any { it.startOffset == collectedTextRange!!.endOffset && it.endOffset == comment.startOffset }) { + collectedTextRange = TextRange(collectedTextRange.startOffset, comment.endOffset) + } + else { + parentCollapse?.let { + descriptors.add( + FoldingDescriptor( + parentCollapse!!, + TextRange(collectedTextRange!!.startOffset, collectedTextRange!!.endOffset) + ) + ) + } + collectedTextRange = TextRange(comment.startOffset, comment.endOffset) + parentCollapse = comment.originalElement + } + } + if (!whitespaceLocations.any { it.startOffset == collectedTextRange.endOffset }) { + if (collectedTextRange.endOffset > collectedTextRange.startOffset) + parentCollapse?.let { + descriptors.add( + FoldingDescriptor( + parentCollapse, + TextRange(collectedTextRange.startOffset, collectedTextRange.endOffset) + ) + ) + } + } + } + + return descriptors.toTypedArray() + } +} From 0f07aa253cc7276dfd250542d1571b76368613fe Mon Sep 17 00:00:00 2001 From: jojo2357 <66704796+jojo2357@users.noreply.github.com> Date: Tue, 21 Mar 2023 15:46:52 -0600 Subject: [PATCH 2/8] Actually enable folding --- resources/META-INF/extensions/editor/editor.xml | 1 + .../texifyidea/editor/folding/LatexCommentFoldingBuilder.kt | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/resources/META-INF/extensions/editor/editor.xml b/resources/META-INF/extensions/editor/editor.xml index 955d40cc9..a0d459181 100644 --- a/resources/META-INF/extensions/editor/editor.xml +++ b/resources/META-INF/extensions/editor/editor.xml @@ -4,6 +4,7 @@ + diff --git a/src/nl/hannahsten/texifyidea/editor/folding/LatexCommentFoldingBuilder.kt b/src/nl/hannahsten/texifyidea/editor/folding/LatexCommentFoldingBuilder.kt index 9f615162b..94e6982f0 100644 --- a/src/nl/hannahsten/texifyidea/editor/folding/LatexCommentFoldingBuilder.kt +++ b/src/nl/hannahsten/texifyidea/editor/folding/LatexCommentFoldingBuilder.kt @@ -16,7 +16,7 @@ import nl.hannahsten.texifyidea.util.childrenOfType /** * Adds folding regions for LaTeX environments. * - * Enables folding of `\footnote{}`. + * Enables folding of comments. * * @author jojo2357 */ From 22261c7de207b1f1c9072625c1a7cccbdf4e79df Mon Sep 17 00:00:00 2001 From: jojo2357 <66704796+jojo2357@users.noreply.github.com> Date: Tue, 21 Mar 2023 15:54:04 -0600 Subject: [PATCH 3/8] Fix issue where comment not at end of file would not be folded --- .../folding/LatexCommentFoldingBuilder.kt | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/nl/hannahsten/texifyidea/editor/folding/LatexCommentFoldingBuilder.kt b/src/nl/hannahsten/texifyidea/editor/folding/LatexCommentFoldingBuilder.kt index 94e6982f0..718b944a4 100644 --- a/src/nl/hannahsten/texifyidea/editor/folding/LatexCommentFoldingBuilder.kt +++ b/src/nl/hannahsten/texifyidea/editor/folding/LatexCommentFoldingBuilder.kt @@ -74,19 +74,35 @@ class LatexCommentFoldingBuilder : FoldingBuilderEx(), DumbAware { parentCollapse = comment.originalElement } } - if (!whitespaceLocations.any { it.startOffset == collectedTextRange.endOffset }) { + if (!whitespaceLocations.any { it.startOffset == collectedTextRange!!.endOffset }) { if (collectedTextRange.endOffset > collectedTextRange.startOffset) parentCollapse?.let { descriptors.add( FoldingDescriptor( - parentCollapse, - TextRange(collectedTextRange.startOffset, collectedTextRange.endOffset) + parentCollapse!!, + TextRange(collectedTextRange!!.startOffset, collectedTextRange!!.endOffset) ) ) } + parentCollapse = null + collectedTextRange = null } } + if (parentCollapse != null && collectedTextRange != null) { + if (collectedTextRange.endOffset > collectedTextRange.startOffset) + parentCollapse.let { + descriptors.add( + FoldingDescriptor( + parentCollapse!!, + TextRange(collectedTextRange!!.startOffset, collectedTextRange!!.endOffset) + ) + ) + } + parentCollapse = null + collectedTextRange = null + } + return descriptors.toTypedArray() } } From 7ab46beaac544eed01a8f5771a6421c052205161 Mon Sep 17 00:00:00 2001 From: jojo2357 <66704796+jojo2357@users.noreply.github.com> Date: Tue, 21 Mar 2023 16:05:18 -0600 Subject: [PATCH 4/8] Fix Highlighting for `\begin{comment}` --- .../hannahsten/texifyidea/highlighting/LatexAnnotator.kt | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/nl/hannahsten/texifyidea/highlighting/LatexAnnotator.kt b/src/nl/hannahsten/texifyidea/highlighting/LatexAnnotator.kt index 49f8a973d..c447c3da3 100644 --- a/src/nl/hannahsten/texifyidea/highlighting/LatexAnnotator.kt +++ b/src/nl/hannahsten/texifyidea/highlighting/LatexAnnotator.kt @@ -58,6 +58,12 @@ open class LatexAnnotator : Annotator { else if (psiElement is LatexCommands) { annotateCommands(psiElement, annotationHolder) } + else if (psiElement.isComment()) { + annotationHolder.newSilentAnnotation(HighlightSeverity.INFORMATION) + .range(psiElement.textRange) + .textAttributes(LatexSyntaxHighlighter.COMMENT) + .create() + } } /** @@ -157,7 +163,7 @@ open class LatexAnnotator : Annotator { annotationHolder: AnnotationHolder ) { for ( - element in optionalParamElement.optionalParamContentList + element in optionalParamElement.optionalParamContentList ) { if (element !is LatexOptionalParamContent) { continue @@ -206,6 +212,7 @@ open class LatexAnnotator : Annotator { in CommandMagic.bibliographyItems -> { LatexSyntaxHighlighter.BIBLIOGRAPHY_DEFINITION } + else -> return } From f6cc56f3495101aec97405ca6a18a7c8055dc2fc Mon Sep 17 00:00:00 2001 From: Thomas Schouten Date: Thu, 23 Mar 2023 10:06:33 +0100 Subject: [PATCH 5/8] Add a few comments and a test --- .../folding/LatexCommentFoldingBuilder.kt | 17 +++++++++-------- .../texifyidea/highlighting/LatexAnnotator.kt | 2 +- .../texifyidea/editor/LatexFoldingTest.kt | 4 ++++ test/resources/editor/folding/comments.tex | 9 +++++++++ 4 files changed, 23 insertions(+), 9 deletions(-) create mode 100644 test/resources/editor/folding/comments.tex diff --git a/src/nl/hannahsten/texifyidea/editor/folding/LatexCommentFoldingBuilder.kt b/src/nl/hannahsten/texifyidea/editor/folding/LatexCommentFoldingBuilder.kt index 718b944a4..cab4dac64 100644 --- a/src/nl/hannahsten/texifyidea/editor/folding/LatexCommentFoldingBuilder.kt +++ b/src/nl/hannahsten/texifyidea/editor/folding/LatexCommentFoldingBuilder.kt @@ -14,9 +14,7 @@ import com.intellij.refactoring.suggested.startOffset import nl.hannahsten.texifyidea.util.childrenOfType /** - * Adds folding regions for LaTeX environments. - * - * Enables folding of comments. + * Enables folding of multiple comments on successive lines. * * @author jojo2357 */ @@ -26,11 +24,8 @@ class LatexCommentFoldingBuilder : FoldingBuilderEx(), DumbAware { override fun getPlaceholderText(node: ASTNode): String { val parsedText = node.text.trim() - return if (parsedText.length > 8) parsedText.substring(0, 8) + "..." - else parsedText.substring( - 0, - parsedText.length - 1 - ) + return if (parsedText.length > 9) parsedText.substring(0, 8) + "..." + else parsedText } override fun buildFoldRegions(root: PsiElement, document: Document, quick: Boolean): Array { @@ -53,15 +48,19 @@ class LatexCommentFoldingBuilder : FoldingBuilderEx(), DumbAware { var parentCollapse: PsiElement? = null for (comment in comments) { + + // Initialization: start with the first comment in a possible sequence if (collectedTextRange == null) { collectedTextRange = TextRange(comment.startOffset, comment.endOffset) parentCollapse = comment.originalElement } else { + // If the next comment follows directly after the previous one, add it to the sequence if (whitespaceLocations.any { it.startOffset == collectedTextRange!!.endOffset && it.endOffset == comment.startOffset }) { collectedTextRange = TextRange(collectedTextRange.startOffset, comment.endOffset) } else { + // Otherwise, stop the sequence and fold what we have so far parentCollapse?.let { descriptors.add( FoldingDescriptor( @@ -74,6 +73,7 @@ class LatexCommentFoldingBuilder : FoldingBuilderEx(), DumbAware { parentCollapse = comment.originalElement } } + // todo why is this code here? if (!whitespaceLocations.any { it.startOffset == collectedTextRange!!.endOffset }) { if (collectedTextRange.endOffset > collectedTextRange.startOffset) parentCollapse?.let { @@ -89,6 +89,7 @@ class LatexCommentFoldingBuilder : FoldingBuilderEx(), DumbAware { } } + // Fold single comments if (parentCollapse != null && collectedTextRange != null) { if (collectedTextRange.endOffset > collectedTextRange.startOffset) parentCollapse.let { diff --git a/src/nl/hannahsten/texifyidea/highlighting/LatexAnnotator.kt b/src/nl/hannahsten/texifyidea/highlighting/LatexAnnotator.kt index c447c3da3..f2c4f7cd3 100644 --- a/src/nl/hannahsten/texifyidea/highlighting/LatexAnnotator.kt +++ b/src/nl/hannahsten/texifyidea/highlighting/LatexAnnotator.kt @@ -163,7 +163,7 @@ open class LatexAnnotator : Annotator { annotationHolder: AnnotationHolder ) { for ( - element in optionalParamElement.optionalParamContentList + element in optionalParamElement.optionalParamContentList ) { if (element !is LatexOptionalParamContent) { continue diff --git a/test/nl/hannahsten/texifyidea/editor/LatexFoldingTest.kt b/test/nl/hannahsten/texifyidea/editor/LatexFoldingTest.kt index 690d6c4c5..0cfbb8358 100644 --- a/test/nl/hannahsten/texifyidea/editor/LatexFoldingTest.kt +++ b/test/nl/hannahsten/texifyidea/editor/LatexFoldingTest.kt @@ -41,4 +41,8 @@ class LatexFoldingTest : BasePlatformTestCase() { fun testCustomFoldingRegions() { myFixture.testFolding("$testDataPath/custom-regions.tex") } + + fun testCommentFolding() { + myFixture.testFolding("$testDataPath/comments.tex") + } } \ No newline at end of file diff --git a/test/resources/editor/folding/comments.tex b/test/resources/editor/folding/comments.tex new file mode 100644 index 000000000..dbb848d19 --- /dev/null +++ b/test/resources/editor/folding/comments.tex @@ -0,0 +1,9 @@ +% First comment +% Second comment + +Text. +% First comment + +% Second comment after newline +Text. +% Single comment From 73d2679e6c1c5366f51e5a140afa1be643e1f4b1 Mon Sep 17 00:00:00 2001 From: jojo2357 <66704796+jojo2357@users.noreply.github.com> Date: Thu, 23 Mar 2023 12:43:50 -0600 Subject: [PATCH 6/8] Add requested comments --- .../folding/LatexCommentFoldingBuilder.kt | 36 +++++++++---------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/src/nl/hannahsten/texifyidea/editor/folding/LatexCommentFoldingBuilder.kt b/src/nl/hannahsten/texifyidea/editor/folding/LatexCommentFoldingBuilder.kt index cab4dac64..8131708b4 100644 --- a/src/nl/hannahsten/texifyidea/editor/folding/LatexCommentFoldingBuilder.kt +++ b/src/nl/hannahsten/texifyidea/editor/folding/LatexCommentFoldingBuilder.kt @@ -55,7 +55,7 @@ class LatexCommentFoldingBuilder : FoldingBuilderEx(), DumbAware { parentCollapse = comment.originalElement } else { - // If the next comment follows directly after the previous one, add it to the sequence + // If the next comment follows directly after the previous one (and was broken by whitespace) add it to the sequence if (whitespaceLocations.any { it.startOffset == collectedTextRange!!.endOffset && it.endOffset == comment.startOffset }) { collectedTextRange = TextRange(collectedTextRange.startOffset, comment.endOffset) } @@ -63,42 +63,32 @@ class LatexCommentFoldingBuilder : FoldingBuilderEx(), DumbAware { // Otherwise, stop the sequence and fold what we have so far parentCollapse?.let { descriptors.add( - FoldingDescriptor( - parentCollapse!!, - TextRange(collectedTextRange!!.startOffset, collectedTextRange!!.endOffset) - ) + buildDescriptor(parentCollapse, collectedTextRange) ) } collectedTextRange = TextRange(comment.startOffset, comment.endOffset) parentCollapse = comment.originalElement } } - // todo why is this code here? + // if no whitespace exists at the end of this block we are building, if (!whitespaceLocations.any { it.startOffset == collectedTextRange!!.endOffset }) { + // And we have accumulated at least something, if (collectedTextRange.endOffset > collectedTextRange.startOffset) + // finish the collapse parentCollapse?.let { - descriptors.add( - FoldingDescriptor( - parentCollapse!!, - TextRange(collectedTextRange!!.startOffset, collectedTextRange!!.endOffset) - ) - ) + descriptors.add(buildDescriptor(parentCollapse, collectedTextRange)) } + // and discard the built objects parentCollapse = null collectedTextRange = null } } - // Fold single comments + // Fold what we were building in the event we run out of comments (aka trailing whitespace in file) if (parentCollapse != null && collectedTextRange != null) { if (collectedTextRange.endOffset > collectedTextRange.startOffset) parentCollapse.let { - descriptors.add( - FoldingDescriptor( - parentCollapse!!, - TextRange(collectedTextRange!!.startOffset, collectedTextRange!!.endOffset) - ) - ) + descriptors.add(buildDescriptor(parentCollapse, collectedTextRange)) } parentCollapse = null collectedTextRange = null @@ -106,4 +96,12 @@ class LatexCommentFoldingBuilder : FoldingBuilderEx(), DumbAware { return descriptors.toTypedArray() } + + private fun buildDescriptor( + parentCollapse: PsiElement?, + collectedTextRange: TextRange? + ) = FoldingDescriptor( + parentCollapse!!, + TextRange(collectedTextRange!!.startOffset, collectedTextRange.endOffset) + ) } From 21d628c5afd403f74150049b15f16439ef2058db Mon Sep 17 00:00:00 2001 From: Thomas Schouten Date: Fri, 31 Mar 2023 10:37:03 +0200 Subject: [PATCH 7/8] Remove unused code --- CHANGELOG.md | 2 ++ .../editor/folding/LatexCommentFoldingBuilder.kt | 14 +------------- test/resources/editor/folding/comments.tex | 2 +- 3 files changed, 4 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f4b77b8eb..0c5951cb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ ## [Unreleased] ### Added +* Fold blocks of comments, by @jojo2357 +* Highlight comment environment as a comment, by @jojo2357 ### Fixed diff --git a/src/nl/hannahsten/texifyidea/editor/folding/LatexCommentFoldingBuilder.kt b/src/nl/hannahsten/texifyidea/editor/folding/LatexCommentFoldingBuilder.kt index 8131708b4..0be7f0d5c 100644 --- a/src/nl/hannahsten/texifyidea/editor/folding/LatexCommentFoldingBuilder.kt +++ b/src/nl/hannahsten/texifyidea/editor/folding/LatexCommentFoldingBuilder.kt @@ -55,7 +55,7 @@ class LatexCommentFoldingBuilder : FoldingBuilderEx(), DumbAware { parentCollapse = comment.originalElement } else { - // If the next comment follows directly after the previous one (and was broken by whitespace) add it to the sequence + // If the next comment follows directly after the previous one (and was broken by whitespace) add the next comment to the sequence if (whitespaceLocations.any { it.startOffset == collectedTextRange!!.endOffset && it.endOffset == comment.startOffset }) { collectedTextRange = TextRange(collectedTextRange.startOffset, comment.endOffset) } @@ -70,18 +70,6 @@ class LatexCommentFoldingBuilder : FoldingBuilderEx(), DumbAware { parentCollapse = comment.originalElement } } - // if no whitespace exists at the end of this block we are building, - if (!whitespaceLocations.any { it.startOffset == collectedTextRange!!.endOffset }) { - // And we have accumulated at least something, - if (collectedTextRange.endOffset > collectedTextRange.startOffset) - // finish the collapse - parentCollapse?.let { - descriptors.add(buildDescriptor(parentCollapse, collectedTextRange)) - } - // and discard the built objects - parentCollapse = null - collectedTextRange = null - } } // Fold what we were building in the event we run out of comments (aka trailing whitespace in file) diff --git a/test/resources/editor/folding/comments.tex b/test/resources/editor/folding/comments.tex index dbb848d19..75c9d62fb 100644 --- a/test/resources/editor/folding/comments.tex +++ b/test/resources/editor/folding/comments.tex @@ -6,4 +6,4 @@ % Second comment after newline Text. -% Single comment +% Single comment From 95426d1bef61c5dd29bebc797012cc41feffa921 Mon Sep 17 00:00:00 2001 From: Thomas Schouten Date: Fri, 31 Mar 2023 10:59:21 +0200 Subject: [PATCH 8/8] Fix test --- test/nl/hannahsten/texifyidea/psi/LatexParserTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/nl/hannahsten/texifyidea/psi/LatexParserTest.kt b/test/nl/hannahsten/texifyidea/psi/LatexParserTest.kt index 079dfd24d..67f81186e 100644 --- a/test/nl/hannahsten/texifyidea/psi/LatexParserTest.kt +++ b/test/nl/hannahsten/texifyidea/psi/LatexParserTest.kt @@ -90,7 +90,7 @@ class LatexParserTest : BasePlatformTestCase() { myFixture.configureByText( LatexFileType, """ - % Not a preamble option, so treat like usual + % Not a preamble option, so treat like usual \begin{frame} \only<1>{${'$'}a_1${'$'}} \end{frame}