From 04b24f69a10eb211fc98f980f39a13620378ce8b Mon Sep 17 00:00:00 2001 From: "Denis.Zhdanov" Date: Thu, 4 Aug 2011 15:53:01 +0400 Subject: [PATCH] IDEA-72791 Critical bug in Code formatting (Java source files) Delegating data context delegates UserDataHolder functionality as well now --- .../source/codeStyle/CodeFormatterFacade.java | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeFormatterFacade.java b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeFormatterFacade.java index 496db4eed3cb..fa1e018dfa0f 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeFormatterFacade.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeFormatterFacade.java @@ -35,6 +35,7 @@ import com.intellij.openapi.fileEditor.FileDocumentManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Key; import com.intellij.openapi.util.TextRange; +import com.intellij.openapi.util.UserDataHolder; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiElement; @@ -402,7 +403,7 @@ public class CodeFormatterFacade { // There is a possible case that formatting is performed from project view and editor is not opened yet. The problem is that // its data context doesn't contain information about project then. So, we explicitly support that here (see IDEA-72791). final DataContext baseDataContext = DataManager.getInstance().getDataContext(editor.getComponent()); - final DataContext dataContext = new DataContext() { + final DataContext dataContext = new DelegatingDataContext(baseDataContext) { @Override public Object getData(@NonNls String dataId) { Object result = baseDataContext.getData(dataId); @@ -461,5 +462,38 @@ public class CodeFormatterFacade { maxLine++; } } + + private static class DelegatingDataContext implements DataContext, UserDataHolder { + + private final DataContext myDataContextDelegate; + private final UserDataHolder myDataHolderDelegate; + + DelegatingDataContext(DataContext delegate) { + myDataContextDelegate = delegate; + if (delegate instanceof UserDataHolder) { + myDataHolderDelegate = (UserDataHolder)delegate; + } + else { + myDataHolderDelegate = null; + } + } + + @Override + public Object getData(@NonNls String dataId) { + return myDataContextDelegate.getData(dataId); + } + + @Override + public T getUserData(@NotNull Key key) { + return myDataHolderDelegate == null ? null : myDataHolderDelegate.getUserData(key); + } + + @Override + public void putUserData(@NotNull Key key, @Nullable T value) { + if (myDataHolderDelegate != null) { + myDataHolderDelegate.putUserData(key, value); + } + } + } }