IDEA-150943 Strange code folding in a method with lambda

This commit is contained in:
Dmitry Batrak
2016-02-01 15:55:33 +03:00
parent ae4b5edbf5
commit 6fb3647245
5 changed files with 71 additions and 7 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -24,6 +24,7 @@ import com.intellij.openapi.application.ex.PathManagerEx
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.editor.FoldRegion
import com.intellij.openapi.editor.ex.DocumentEx
import com.intellij.openapi.editor.ex.EditorEx
import com.intellij.openapi.editor.ex.FoldingModelEx
import com.intellij.openapi.editor.impl.FoldingModelImpl
import com.intellij.openapi.fileEditor.impl.text.TextEditorProvider
@@ -36,7 +37,6 @@ import com.intellij.psi.search.GlobalSearchScope
import com.intellij.testFramework.EditorTestUtil
import com.intellij.testFramework.LightProjectDescriptor
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
/**
* @author Denis Zhdanov
* @since 1/17/11 1:00 PM
@@ -1185,6 +1185,38 @@ class Foo {
myFixture.doHighlighting()
assertTopLevelFoldRegionsState "[FoldRegion +(57:144), placeholder='(Runnable) () → { ', FoldRegion +(165:189), placeholder=' }']"
}
public void "test folding update after external change"() {
configure """\
class Foo {
void m1() {
System.out.println(1);
System.out.println(2);
}
void m2() {
System.out.println(3);
System.out.println(4);
}
}
"""
myFixture.performEditorAction(IdeActions.ACTION_COLLAPSE_ALL_REGIONS)
assertTopLevelFoldRegionsState "[FoldRegion +(24:83), placeholder='{...}', FoldRegion +(99:158), placeholder='{...}']"
def virtualFile = ((EditorEx)myFixture.getEditor()).virtualFile
myFixture.saveText(virtualFile, """\
class Foo {
void m1() {
System.out.println(1);
System.out.println(4);
}
}
""")
virtualFile.refresh(false, false)
myFixture.doHighlighting()
assertTopLevelFoldRegionsState "[FoldRegion +(24:83), placeholder='{...}']"
}
private void assertTopLevelFoldRegionsState(String expectedState) {
assertEquals(expectedState, myFixture.editor.foldingModel.toString())
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -93,6 +93,8 @@ class UpdateFoldRegionsOperation implements Runnable {
List<FoldRegion> newRegions = addNewRegions(info, foldingModel, elementsToFold, rangeToExpandStatusMap, shouldExpand, groupExpand);
applyExpandStatus(newRegions, shouldExpand, groupExpand);
foldingModel.clearExternalChangeFlag();
}
private static void applyExpandStatus(@NotNull List<FoldRegion> newRegions,
@@ -248,7 +250,10 @@ class UpdateFoldRegionsOperation implements Runnable {
}
private boolean regionCanBeRemovedWhenCollapsed(FoldRegion region) {
return Boolean.TRUE.equals(region.getUserData(CAN_BE_REMOVED_WHEN_COLLAPSED)) || !region.isValid() || isRegionInCaretLine(region);
return Boolean.TRUE.equals(region.getUserData(CAN_BE_REMOVED_WHEN_COLLAPSED)) ||
((FoldingModelEx)myEditor.getFoldingModel()).documentHasBeenChangedExternally() ||
!region.isValid() ||
isRegionInCaretLine(region);
}
private boolean isRegionInCaretLine(FoldRegion region) {
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -200,6 +200,15 @@ class FoldingModelWindow implements FoldingModelEx{
return result;
}
@Override
public boolean documentHasBeenChangedExternally() {
return false;
}
@Override
public void clearExternalChangeFlag() {
}
@Override
public void clearFoldRegions() {
myDelegate.clearFoldRegions();
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -64,4 +64,8 @@ public interface FoldingModelEx extends FoldingModel {
@NotNull
List<FoldRegion> getGroupedRegions(FoldingGroup group);
boolean documentHasBeenChangedExternally();
void clearExternalChangeFlag();
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -26,6 +26,7 @@ package com.intellij.openapi.editor.impl;
import com.intellij.diagnostic.Dumpable;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ex.ApplicationManagerEx;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.*;
@@ -36,6 +37,7 @@ import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.ModificationTracker;
import com.intellij.psi.ExternalChangeAction;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.MultiMap;
import org.jetbrains.annotations.NotNull;
@@ -65,6 +67,7 @@ public class FoldingModelImpl implements FoldingModelEx, PrioritizedInternalDocu
private int mySavedCaretShift;
private final MultiMap<FoldingGroup, FoldRegion> myGroups = new MultiMap<FoldingGroup, FoldRegion>();
private boolean myDocumentChangeProcessed = true;
private boolean myDocumentHasBeenChangedExternally;
private final AtomicLong myExpansionCounter = new AtomicLong();
public FoldingModelImpl(EditorImpl editor) {
@@ -532,6 +535,7 @@ public class FoldingModelImpl implements FoldingModelEx, PrioritizedInternalDocu
if (!((DocumentEx)event.getDocument()).isInBulkUpdate()) {
updateCachedOffsets();
}
if (ApplicationManager.getApplication().hasWriteAction(ExternalChangeAction.class)) myDocumentHasBeenChangedExternally = true;
}
finally {
myDocumentChangeProcessed = true;
@@ -593,4 +597,14 @@ public class FoldingModelImpl implements FoldingModelEx, PrioritizedInternalDocu
public long getModificationCount() {
return myExpansionCounter.get();
}
@Override
public boolean documentHasBeenChangedExternally() {
return myDocumentHasBeenChangedExternally;
}
@Override
public void clearExternalChangeFlag() {
myDocumentHasBeenChangedExternally = false;
}
}