-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2966 from jojo2357/foldcomments
Fold Comments Fix
- Loading branch information
Showing
7 changed files
with
119 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/nl/hannahsten/texifyidea/editor/folding/LatexCommentFoldingBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |