IDEA-15412 Expand or Collapse all existing foldings in selection

This commit is contained in:
Dmitry Batrak
2014-09-23 18:56:13 +04:00
parent 75e5e72ce2
commit dc7010ec08
6 changed files with 84 additions and 45 deletions
@@ -0,0 +1,54 @@
/*
* Copyright 2000-2014 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.folding.impl.actions;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.FoldRegion;
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public abstract class BaseFoldingHandler extends EditorActionHandler {
@Override
protected boolean isEnabledForCaret(@NotNull Editor editor, @NotNull Caret caret, DataContext dataContext) {
return editor.getProject() != null;
}
protected List<FoldRegion> getFoldRegionsForSelection(@NotNull Editor editor, @Nullable Caret caret) {
FoldRegion[] allRegions = editor.getFoldingModel().getAllFoldRegions();
if (caret == null) {
caret = editor.getCaretModel().getPrimaryCaret();
}
if (caret.hasSelection()) {
List<FoldRegion> result = new ArrayList<FoldRegion>(allRegions.length);
for (FoldRegion region : allRegions) {
if (region.getStartOffset() >= caret.getSelectionStart() && region.getEndOffset() <= caret.getSelectionEnd()) {
result.add(region);
}
}
if (!result.isEmpty()) {
return result;
}
}
return Arrays.asList(allRegions);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2014 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.
@@ -18,37 +18,36 @@ package com.intellij.codeInsight.folding.impl.actions;
import com.intellij.codeInsight.folding.CodeFoldingManager;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.FoldRegion;
import com.intellij.openapi.editor.actionSystem.EditorAction;
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDocumentManager;
import org.jetbrains.annotations.Nullable;
import java.util.List;
public class CollapseAllRegionsAction extends EditorAction {
public CollapseAllRegionsAction() {
super(new EditorActionHandler() {
super(new BaseFoldingHandler() {
@Override
public void execute(final Editor editor, DataContext dataContext) {
public void doExecute(final Editor editor, @Nullable Caret caret, DataContext dataContext) {
Project project = editor.getProject();
assert project != null;
PsiDocumentManager.getInstance(project).commitAllDocuments();
CodeFoldingManager.getInstance(project).updateFoldRegions(editor);
final List<FoldRegion> regions = getFoldRegionsForSelection(editor, caret);
editor.getFoldingModel().runBatchFoldingOperation(new Runnable() {
@Override
public void run() {
for (FoldRegion region : editor.getFoldingModel().getAllFoldRegions()) {
for (FoldRegion region : regions) {
region.setExpanded(false);
}
}
});
}
@Override
public boolean isEnabled(Editor editor, DataContext dataContext) {
return super.isEnabled(editor, dataContext) && editor.getProject() != null;
}
});
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2014 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.
@@ -19,16 +19,17 @@ package com.intellij.codeInsight.folding.impl.actions;
import com.intellij.codeInsight.folding.CodeFoldingManager;
import com.intellij.codeInsight.folding.impl.FoldingUtil;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.FoldRegion;
import com.intellij.openapi.editor.actionSystem.EditorAction;
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
import org.jetbrains.annotations.Nullable;
public class CollapseRegionAction extends EditorAction {
public CollapseRegionAction() {
super(new EditorActionHandler() {
super(new BaseFoldingHandler() {
@Override
public void execute(final Editor editor, DataContext dataContext) {
public void doExecute(final Editor editor, @Nullable Caret caret, DataContext dataContext) {
CodeFoldingManager foldingManager = CodeFoldingManager.getInstance(editor.getProject());
foldingManager.updateFoldRegions(editor);
@@ -55,12 +56,6 @@ public class CollapseRegionAction extends EditorAction {
};
editor.getFoldingModel().runBatchFoldingOperation(processor);
}
@Override
public boolean isEnabled(Editor editor, DataContext dataContext) {
return super.isEnabled(editor, dataContext) && editor.getProject() != null;
}
});
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2014 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.
@@ -20,29 +20,32 @@ import com.intellij.codeInsight.folding.CodeFoldingManager;
import com.intellij.codeInsight.folding.impl.EditorFoldingInfo;
import com.intellij.codeInsight.folding.impl.FoldingPolicy;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.FoldRegion;
import com.intellij.openapi.editor.actionSystem.EditorAction;
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.Nullable;
import java.util.List;
public class ExpandAllRegionsAction extends EditorAction {
public ExpandAllRegionsAction() {
super(new EditorActionHandler() {
super(new BaseFoldingHandler() {
@Override
public void execute(final Editor editor, DataContext dataContext) {
public void doExecute(final Editor editor, @Nullable Caret caret, DataContext dataContext) {
Project project = editor.getProject();
assert project != null;
PsiDocumentManager.getInstance(project).commitAllDocuments();
CodeFoldingManager.getInstance(project).updateFoldRegions(editor);
final List<FoldRegion> regions = getFoldRegionsForSelection(editor, caret);
editor.getFoldingModel().runBatchFoldingOperation(new Runnable() {
@Override
public void run() {
boolean anythingDone = false;
FoldRegion[] regions = editor.getFoldingModel().getAllFoldRegions();
for (FoldRegion region : regions) {
// try to restore to default state at first
PsiElement element = EditorFoldingInfo.get(editor).getPsiElement(region);
@@ -61,11 +64,6 @@ public class ExpandAllRegionsAction extends EditorAction {
}
});
}
@Override
public boolean isEnabled(Editor editor, DataContext dataContext) {
return super.isEnabled(editor, dataContext) && editor.getProject() != null;
}
});
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2014 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.
@@ -18,29 +18,22 @@ package com.intellij.codeInsight.folding.impl.actions;
import com.intellij.codeInsight.folding.CodeFoldingManager;
import com.intellij.codeInsight.folding.impl.FoldingUtil;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.FoldRegion;
import com.intellij.openapi.editor.actionSystem.EditorAction;
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class ExpandRegionAction extends EditorAction {
public ExpandRegionAction() {
super(new EditorActionHandler() {
super(new BaseFoldingHandler() {
@Override
public void execute(Editor editor, DataContext dataContext) {
public void doExecute(Editor editor, @Nullable Caret caret, DataContext dataContext) {
expandRegionAtCaret(editor.getProject(), editor);
}
@Override
public boolean isEnabled(Editor editor, DataContext dataContext) {
return super.isEnabled(editor, dataContext) && CommonDataKeys.PROJECT.getData(dataContext) != null;
}
});
}
@@ -410,9 +410,9 @@ action.ExpandRegion.description=Expand folding region at caret
action.CollapseRegion.text=_Collapse
action.CollapseRegion.description=Collapse folding region at caret
action.ExpandAllRegions.text=_Expand All
action.ExpandAllRegions.description=Expand all folding regions
action.ExpandAllRegions.description=Expand all folding regions (in selection)
action.CollapseAllRegions.text=Collapse _All
action.CollapseAllRegions.description=Collapse all folding regions
action.CollapseAllRegions.description=Collapse all folding regions (in selection)
action.ExpandDocComments.text=Expand _doc comments
action.ExpandDocComments.description=Expand all documentation comments
action.CollapseDocComments.text=Collapse d_oc comments