Skip to content

Commit

Permalink
Merge pull request #2966 from jojo2357/foldcomments
Browse files Browse the repository at this point in the history
Fold Comments Fix
  • Loading branch information
PHPirates authored Mar 31, 2023
2 parents 668837a + 95426d1 commit 4443ca2
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Added
* Support @Comment comments in bibtex
* Fold blocks of comments, by @jojo2357
* Highlight comment environment as a comment, by @jojo2357

### Fixed
* Fixed usage of IntelliJ api deprecated in 2023.1
Expand Down
1 change: 1 addition & 0 deletions resources/META-INF/extensions/editor/editor.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<lang.foldingBuilder language="Latex" implementationClass="nl.hannahsten.texifyidea.editor.folding.LatexEnvironmentFoldingBuilder"/>
<lang.foldingBuilder language="Latex" implementationClass="nl.hannahsten.texifyidea.editor.folding.LatexImportFoldingBuilder"/>
<lang.foldingBuilder language="Latex" implementationClass="nl.hannahsten.texifyidea.editor.folding.LatexFootnoteFoldingBuilder"/>
<lang.foldingBuilder language="Latex" implementationClass="nl.hannahsten.texifyidea.editor.folding.LatexCommentFoldingBuilder"/>
<lang.foldingBuilder language="Latex" implementationClass="nl.hannahsten.texifyidea.editor.folding.LatexSectionFoldingBuilder"/>
<lang.foldingBuilder language="Latex" implementationClass="nl.hannahsten.texifyidea.editor.folding.LatexEscapedSymbolFoldingBuilder"/>
<lang.foldingBuilder language="Latex" implementationClass="nl.hannahsten.texifyidea.editor.folding.LatexSymbolFoldingBuilder"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
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

/**
* Enables folding of multiple comments on successive lines.
*
* @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 > 9) parsedText.substring(0, 8) + "..."
else parsedText
}

override fun buildFoldRegions(root: PsiElement, document: Document, quick: Boolean): Array<FoldingDescriptor> {
val descriptors = ArrayList<FoldingDescriptor>()
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) {

// 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 (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)
}
else {
// Otherwise, stop the sequence and fold what we have so far
parentCollapse?.let {
descriptors.add(
buildDescriptor(parentCollapse, collectedTextRange)
)
}
collectedTextRange = TextRange(comment.startOffset, comment.endOffset)
parentCollapse = comment.originalElement
}
}
}

// 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(buildDescriptor(parentCollapse, collectedTextRange))
}
parentCollapse = null
collectedTextRange = null
}

return descriptors.toTypedArray()
}

private fun buildDescriptor(
parentCollapse: PsiElement?,
collectedTextRange: TextRange?
) = FoldingDescriptor(
parentCollapse!!,
TextRange(collectedTextRange!!.startOffset, collectedTextRange.endOffset)
)
}
7 changes: 7 additions & 0 deletions src/nl/hannahsten/texifyidea/highlighting/LatexAnnotator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

/**
Expand Down Expand Up @@ -206,6 +212,7 @@ open class LatexAnnotator : Annotator {
in CommandMagic.bibliographyItems -> {
LatexSyntaxHighlighter.BIBLIOGRAPHY_DEFINITION
}

else -> return
}

Expand Down
4 changes: 4 additions & 0 deletions test/nl/hannahsten/texifyidea/editor/LatexFoldingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ class LatexFoldingTest : BasePlatformTestCase() {
fun testCustomFoldingRegions() {
myFixture.testFolding("$testDataPath/custom-regions.tex")
}

fun testCommentFolding() {
myFixture.testFolding("$testDataPath/comments.tex")
}
}
2 changes: 1 addition & 1 deletion test/nl/hannahsten/texifyidea/psi/LatexParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class LatexParserTest : BasePlatformTestCase() {
myFixture.configureByText(
LatexFileType,
"""
% Not a preamble option, so treat like usual
<info descr="null">% Not a preamble option, so treat like usual</info>
\begin{frame}
\only<1>{<info descr="null">${'$'}<info textAttributesKey=LATEX_INLINE_MATH>a_1${'$'}</info></info>}
\end{frame}
Expand Down
9 changes: 9 additions & 0 deletions test/resources/editor/folding/comments.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<fold text='% First ...'>% First comment
% Second comment</fold>

Text.
<fold text='% First ...'>% First comment

% Second comment after newline</fold>
Text.
<fold text='% Single...'>% Single comment</fold>

0 comments on commit 4443ca2

Please sign in to comment.