From 0b15a1dd5f557a4d70dd3f8b629d50023e21b7de Mon Sep 17 00:00:00 2001 From: "Denis.Zhdanov" Date: Fri, 25 May 2012 11:05:59 +0400 Subject: [PATCH] IDEA-86084 Foldings drop after 'rename file' --- .../folding/impl/FoldingUpdate.java | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/codeInsight/folding/impl/FoldingUpdate.java b/platform/lang-impl/src/com/intellij/codeInsight/folding/impl/FoldingUpdate.java index 32d11c7fa6ed..51cf80b83161 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/folding/impl/FoldingUpdate.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/folding/impl/FoldingUpdate.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2012 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. @@ -32,6 +32,7 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Key; import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.TextRange; +import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.*; import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil; import com.intellij.psi.util.CachedValueProvider; @@ -49,6 +50,7 @@ public class FoldingUpdate { private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.folding.impl.FoldingUpdate"); private static final Key>> CODE_FOLDING_KEY = Key.create("code folding"); + private static final Key CODE_FOLDING_FILE_EXTENSION_KEY = Key.create("code folding file extension"); private static final Comparator COMPARE_BY_OFFSET = new Comparator() { public int compare(PsiElement element, PsiElement element1) { @@ -67,13 +69,30 @@ public class FoldingUpdate { final Project project = file.getProject(); final Document document = editor.getDocument(); LOG.assertTrue(!PsiDocumentManager.getInstance(project).isUncommited(document)); + + String currentFileExtension = null; + final VirtualFile virtualFile = file.getVirtualFile(); + if (virtualFile != null) { + currentFileExtension = virtualFile.getExtension(); + } ParameterizedCachedValue> value = editor.getUserData(CODE_FOLDING_KEY); + if (value != null) { + // There was a problem that old fold regions have been cached on file extension change (e.g. *.java -> *.groovy). + // We want to drop them in such circumstances. + final String oldExtension = editor.getUserData(CODE_FOLDING_FILE_EXTENSION_KEY); + if (oldExtension == null ? currentFileExtension != null : !oldExtension.equals(currentFileExtension)) { + value = null; + editor.putUserData(CODE_FOLDING_KEY, null); + } + } + editor.putUserData(CODE_FOLDING_FILE_EXTENSION_KEY, currentFileExtension); + if (value != null && value.hasUpToDateValue() && !applyDefaultState) return null; if (quick) return getUpdateResult(file, document, quick, project, editor, applyDefaultState).getValue(); - return CachedValuesManager.getManager(project).getParameterizedCachedValue(editor, CODE_FOLDING_KEY, - new ParameterizedCachedValueProvider>() { + return CachedValuesManager.getManager(project).getParameterizedCachedValue( + editor, CODE_FOLDING_KEY, new ParameterizedCachedValueProvider>() { public CachedValueProvider.Result compute(Pair param) { Document document = editor.getDocument(); PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(document);