Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
\begin \end folding
Browse files Browse the repository at this point in the history
  • Loading branch information
hsz committed Jan 23, 2015
1 parent 6e86026 commit 5e778d6
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
3 changes: 3 additions & 0 deletions META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
<lang.commenter
implementationClass="mobi.hsz.idea.latex.lang.LatexCommenter"
language="LaTeX"/>
<lang.foldingBuilder
implementationClass="mobi.hsz.idea.latex.lang.LatexFoldingBuilder"
language="LaTeX"/>
<lang.parserDefinition
implementationClass="mobi.hsz.idea.latex.lang.LatexParserDefinition"
language="LaTeX"/>
Expand Down
3 changes: 2 additions & 1 deletion resources/messages/LatexBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ colorSettings.displayName=LaTeX
highlighter.instruction=Instruction
highlighter.argument=Argument
highlighter.bracket=Brackets
highlighter.comment=Comment
highlighter.comment=Comment
folding.placeholder=...
95 changes: 95 additions & 0 deletions src/mobi/hsz/idea/latex/lang/LatexFoldingBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 hsz Jakub Chrzanowski <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package mobi.hsz.idea.latex.lang;

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.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.search.PsiElementProcessor;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.containers.ContainerUtil;
import mobi.hsz.idea.latex.LatexBundle;
import mobi.hsz.idea.latex.psi.LatexFile;
import mobi.hsz.idea.latex.psi.LatexTypes;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

/**
* LaTeX folding builder
*
* @author Jakub Chrzanowski <[email protected]>
* @since 0.2
*/
public class LatexFoldingBuilder extends FoldingBuilderEx {
@NotNull
@Override
public FoldingDescriptor[] buildFoldRegions(@NotNull PsiElement root, @NotNull Document document, boolean quick) {
if (!(root instanceof LatexFile)) {
return FoldingDescriptor.EMPTY;
}
LatexFile file = (LatexFile) root;

final List<FoldingDescriptor> result = ContainerUtil.newArrayList();

final List<PsiElement> begins = ContainerUtil.newArrayList();

if (!quick) {
PsiTreeUtil.processElements(file, new PsiElementProcessor() {
@Override
public boolean execute(@NotNull PsiElement element) {
IElementType type = element.getNode().getElementType();
if (type.equals(LatexTypes.INSTRUCTION_BEGIN)) {
begins.add(element);
} else if (type.equals(LatexTypes.INSTRUCTION_END)) {
PsiElement last = begins.get(begins.size() - 1);
TextRange range = new TextRange(last.getTextRange().getStartOffset(), element.getTextRange().getEndOffset());
result.add(new FoldingDescriptor(last, range));
begins.remove(last);
}
return true;
}
});
}

return result.toArray(new FoldingDescriptor[result.size()]);
}

@Nullable
@Override
public String getPlaceholderText(@NotNull ASTNode node) {
return node.getTreeParent().getText() + LatexBundle.message("folding.placeholder");
}

@Override
public boolean isCollapsedByDefault(@NotNull ASTNode node) {
return true;
}
}

0 comments on commit 5e778d6

Please sign in to comment.