Folding Special keywords(TODO, FIXME, etc)

I had idea make for special comments(TODO, FIXME) - folding with special attributes. See screenshot.

todo.png

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

18 comments
Comment actions Permalink

work good, but if i open, it never close ?:|

Dont see to

  @Override
  public TextAttributesKey getTextAttributesKey(@NotNull ASTNode node) {
    PsiElement element = node.getPsi();
    if (element instanceof PsiSpecialComment)
      return CodeInsightColors.TODO_FOLDED_DEFAULT_ATTRIBUTES;
    else
      return null;
  }




is new method, for support choosable color for folding

0
Comment actions Permalink

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

0
Comment actions Permalink

:O sorry, sometimes i has problems with laid out the idea.

yeah.

it not close folding, when i (re)open editor.

0
Comment actions Permalink

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

0
Comment actions Permalink

thx Denis i will debug it.)

if (collapseByDefault != !expanded || element == null) {

!= !


funny. :p nice logic

0
Comment actions Permalink

thx again. Find problem

  public FoldingDescriptor[] buildFoldRegions(final @NotNull PsiElement root, @NotNull Document document, boolean quick) {
    if (quick)
      return FoldingDescriptor.EMPTY;



var quick on reopen - is true)

provider is not needed)
0
Comment actions Permalink

Sorry, I don't understand what are you asking here.

Is it assumption that 'quick' argument is always 'true'?

Denis

0
Comment actions Permalink

Когда открывается Editor, вызывается метод com.intellij.codeInsight.folding.impl.DocumentFoldingInfo#setToEditor, там есть ranges = buildRanges(editor, psiFile);


  private static Map<PsiElement, FoldingDescriptor> buildRanges(final Editor editor, final PsiFile psiFile) {
    final FoldingBuilder foldingBuilder = LanguageFolding.INSTANCE.forLanguage(psiFile.getLanguage());
    final ASTNode node = psiFile.getNode();
    if (node == null) return Collections.emptyMap();
    final FoldingDescriptor[] descriptors = LanguageFolding.buildFoldingDescriptors(foldingBuilder, psiFile, editor.getDocument(), true);
    Map<PsiElement, FoldingDescriptor> ranges = new HashMap<PsiElement, FoldingDescriptor>();
    for (FoldingDescriptor descriptor : descriptors) {
      final ASTNode ast = descriptor.getElement();
      final PsiElement psi = ast.getPsi();
      if (psi != null) {
        ranges.put(psi, descriptor);
      }
    }
    return ranges;
  }




final FoldingDescriptor[] descriptors = LanguageFolding.buildFoldingDescriptors(foldingBuilder, psiFile, editor.getDocument(), true);

последний параметр - true, это и есть quick.

Тоисть если даже, будет открыт Folding в Editor-ре, то при открытии значения с myExpandedStates - просто игнорируются(конкретно для SuppressWarningsFoldingBuilder,PropertyFoldingBuilder, ну и моего SpecialKeywordFoldingBuilder)
0
Comment actions Permalink

Хотя Вы и сами знаете) Если будут ищо вопросы спрошу.

thx

0
Comment actions Permalink
  1. This forum is used for international communication, so, please use english;
  2. Please check javadoc for FoldingBuilderEx.buildFoldRegions(). It clearly defines when 'clear' argument has valud of 'true';


The point is that the builder should be called with 'not-quick' argument anyway.

Denis

0
Comment actions Permalink

1. ok.

2.

   * @param quick    whether the result should be providen as soon as possible. Is true, when
   *                 an editor is opened and we need to auto-fold something immediately, like Java imports.
   *                 If true, one should perform no reference resolving and avoid complex checks if possible.


is not my code.

I dont change com.intellij.lang.folding.LanguageFolding#buildFoldingDescriptors
0
Comment actions Permalink

I wanted to emphasise that LanguageFolding.buildFoldingDescriptors() will be called with 'quick = false' eventually.

Denis

0
Comment actions Permalink

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()?:|)

0
Comment actions Permalink

Previously used user-defined fold regions are applied to the editor after 'quick' phase.

Denis

0
Comment actions Permalink

and last

How can i publish changes (i dont use IC, but i want it see in IU)?

It cant maked by plugin(

0
Comment actions Permalink

Are you asking about how to upload your plugin to the plugin repo?

Denis

0
Comment actions Permalink

:|No.

Currently "folding" is not support diffrent TextAttributesKey, i add it, and folding to special keywods(TODO, FIXME).

If u have

int a = 0; //TODO test


is became(in fold)

int a = 0; test


//TODO - for read code is not needed(color already sad that is TODO)

and support to folding with Author(by template)

int a = 0; //TODO [VISTALL] test


is became(in fold)

int a = 0; VISTALL: test


it maybe interest for me only;\

if it has chance be in Master source, i publish it

0
Comment actions Permalink

You can publish that as a standalone plugin to IJ repo and anyone interested would be able to try it then.

Denis

0

Please sign in to leave a comment.