Folding Special keywords(TODO, FIXME, etc)
I had idea make for special comments(TODO, FIXME) - folding with special attributes. See screenshot.
Has problem - when i open folding, is not closed with reopen file
Some part of code(IdeaPlugin.xml i registered it)
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInsight.daemon.impl.actions;
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.editor.colors.CodeInsightColors;
import com.intellij.openapi.editor.colors.TextAttributesKey;
import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
/**
* @author VISTALL
* @date 16:34/22.10.2011
*/
public class SpecialKeywordFoldingBuilder extends FoldingBuilderEx {
public static class SpecialKeywordFoldingElementWalkingVisitor extends PsiElementVisitor {
private final PsiWalkingState myWalkingState = new PsiWalkingState(this) {
public void elementFinished(@NotNull PsiElement element) {
SpecialKeywordFoldingElementWalkingVisitor.this.elementFinished(element);
}
};
@Override
public void visitElement(PsiElement element) {
myWalkingState.elementStarted(element);
}
@SuppressWarnings({"UnusedDeclaration"})
protected void elementFinished(PsiElement element) {
}
public void stopWalking() {
myWalkingState.stopWalking();
}
}
@NotNull
@Override
public FoldingDescriptor[] buildFoldRegions(final @NotNull PsiElement root, @NotNull Document document, boolean quick) {
final List<FoldingDescriptor> result = new ArrayList<FoldingDescriptor>();
root.accept(new SpecialKeywordFoldingElementWalkingVisitor() {
public void visitComment(PsiComment comment) {
if(comment instanceof PsiSpecialComment)
result.add(new FoldingDescriptor(comment, comment.getTextRange()));
super.visitComment(comment);
}
});
return result.toArray(new FoldingDescriptor[result.size()]);
}
@Override
public String getPlaceholderText(@NotNull ASTNode node) {
PsiElement element = node.getPsi();
if (element instanceof PsiSpecialComment) {
PsiSpecialComment comment = (PsiSpecialComment)element;
return comment.getAuthor() + ": " + comment.getCommentText();
}
else {
return element.getText();
}
}
@Override
public TextAttributesKey getTextAttributesKey(@NotNull ASTNode node) {
PsiElement element = node.getPsi();
if (element instanceof PsiSpecialComment)
return CodeInsightColors.TODO_FOLDED_DEFAULT_ATTRIBUTES;
else
return null;
}
@Override
public boolean isCollapsedByDefault(@NotNull ASTNode node) {
return true;
}
}
And in java\java-impl\src\com\intellij\codeInsight\folding\impl\JavaFoldingBuilder.java i ignored PsiSpecialComment
private static void addCommentFolds(@NotNull PsiComment comment, @NotNull Set<PsiElement> processedComments,
@NotNull List<FoldingDescriptor> foldElements)
{
if (processedComments.contains(comment) || comment.getTokenType() != JavaTokenType.END_OF_LINE_COMMENT || comment instanceof PsiSpecialComment) {
return;
}
Maybe i miss something) and i dont known good IDEA source
Please sign in to leave a comment.
work good, but if i open, it never close ?:|
Dont see to
is new method, for support choosable color for folding
Hi Valery,
I'm afraid I don't quite understand what is the question. Do you ask about restoring manually defined foldings state on editor (re-)opening?
Denis
:O sorry, sometimes i has problems with laid out the idea.
yeah.
it not close folding, when i (re)open editor.
You need to provide your own implementation of 'com.intellij.codeInsight.folding.impl.ElementSignatureProvider' then.
Feel free to check the folding processing at com.intellij.codeInsight.folding.impl.DocumentFoldingInfo.readExternal()/writeExternal()
Denis
thx Denis i will debug it.)
funny. :p nice logic
thx again. Find problem
var quick on reopen - is true)
provider is not needed)
Sorry, I don't understand what are you asking here.
Is it assumption that 'quick' argument is always 'true'?
Denis
Когда открывается Editor, вызывается метод com.intellij.codeInsight.folding.impl.DocumentFoldingInfo#setToEditor, там есть ranges = buildRanges(editor, psiFile);
final FoldingDescriptor[] descriptors = LanguageFolding.buildFoldingDescriptors(foldingBuilder, psiFile, editor.getDocument(), true);
последний параметр - true, это и есть quick.
Тоисть если даже, будет открыт Folding в Editor-ре, то при открытии значения с myExpandedStates - просто игнорируются(конкретно для SuppressWarningsFoldingBuilder,PropertyFoldingBuilder, ну и моего SpecialKeywordFoldingBuilder)
Хотя Вы и сами знаете) Если будут ищо вопросы спрошу.
thx
The point is that the builder should be called with 'not-quick' argument anyway.
Denis
1. ok.
2.
is not my code.
I dont change com.intellij.lang.folding.LanguageFolding#buildFoldingDescriptors
I wanted to emphasise that LanguageFolding.buildFoldingDescriptors() will be called with 'quick = false' eventually.
Denis
yeah - i known it, but when u reopen Editor - quick makes folding to close(on reopen) - dont like it, maybe make better as isCollapseByDefault(isCollapseOnReopenEditor()?:|)
Previously used user-defined fold regions are applied to the editor after 'quick' phase.
Denis
and last
How can i publish changes (i dont use IC, but i want it see in IU)?
It cant maked by plugin(
Are you asking about how to upload your plugin to the plugin repo?
Denis
:|No.
Currently "folding" is not support diffrent TextAttributesKey, i add it, and folding to special keywods(TODO, FIXME).
If u have
is became(in fold)
//TODO - for read code is not needed(color already sad that is TODO)
and support to folding with Author(by template)
is became(in fold)
it maybe interest for me only;\
if it has chance be in Master source, i publish it
You can publish that as a standalone plugin to IJ repo and anyone interested would be able to try it then.
Denis