From 7ccf462e4bbb6d57ec2cdd2769c0ad997c01f7f5 Mon Sep 17 00:00:00 2001 From: "Denis.Zhdanov" Date: Wed, 22 Aug 2012 15:07:07 +0400 Subject: [PATCH] Provide an API to distinguish between 'format' and 'adjust line' operations --- .../formatting/FormattingModelBuilder.java | 1 + .../formatting/CoreFormatterUtil.java | 16 +++++++- .../intellij/formatting/FormattingMode.java | 25 ++++++++++++ .../formatting/FormattingModelBuilderEx.java | 39 +++++++++++++++++++ .../source/codeStyle/CodeFormatterFacade.java | 4 +- .../codeStyle/CodeStyleManagerImpl.java | 8 ++-- .../codeStyle/CodeStyleManagerRunnable.java | 20 +++++----- 7 files changed, 97 insertions(+), 16 deletions(-) create mode 100644 platform/lang-impl/src/com/intellij/formatting/FormattingMode.java create mode 100644 platform/lang-impl/src/com/intellij/formatting/FormattingModelBuilderEx.java diff --git a/platform/lang-api/src/com/intellij/formatting/FormattingModelBuilder.java b/platform/lang-api/src/com/intellij/formatting/FormattingModelBuilder.java index 91166f887176..84e2cb7e660b 100644 --- a/platform/lang-api/src/com/intellij/formatting/FormattingModelBuilder.java +++ b/platform/lang-api/src/com/intellij/formatting/FormattingModelBuilder.java @@ -36,6 +36,7 @@ import org.jetbrains.annotations.Nullable; */ public interface FormattingModelBuilder { + /** * Requests building the formatting model for a section of the file containing * the specified PSI element and its children. diff --git a/platform/lang-impl/src/com/intellij/formatting/CoreFormatterUtil.java b/platform/lang-impl/src/com/intellij/formatting/CoreFormatterUtil.java index d9a9cdb6f5ae..dfef57ecf4d9 100644 --- a/platform/lang-impl/src/com/intellij/formatting/CoreFormatterUtil.java +++ b/platform/lang-impl/src/com/intellij/formatting/CoreFormatterUtil.java @@ -15,6 +15,7 @@ */ package com.intellij.formatting; +import com.intellij.psi.PsiElement; import com.intellij.psi.codeStyle.CodeStyleSettings; import com.intellij.psi.codeStyle.CommonCodeStyleSettings; import org.jetbrains.annotations.NotNull; @@ -34,7 +35,20 @@ public class CoreFormatterUtil { private CoreFormatterUtil() { } - + + @NotNull + public static FormattingModel buildModel(@NotNull FormattingModelBuilder builder, + @NotNull PsiElement element, + @NotNull CodeStyleSettings settings, + @NotNull FormattingMode mode) { + if (builder instanceof FormattingModelBuilderEx) { + return ((FormattingModelBuilderEx)builder).createModel(element, settings, mode); + } + else { + return builder.createModel(element, settings); + } + } + /** * Checks if there is an {@link AlignmentImpl} object that should be used during adjusting * {@link AbstractBlockWrapper#getWhiteSpace() white space} of the given block. diff --git a/platform/lang-impl/src/com/intellij/formatting/FormattingMode.java b/platform/lang-impl/src/com/intellij/formatting/FormattingMode.java new file mode 100644 index 000000000000..cd3f06d4fb11 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/formatting/FormattingMode.java @@ -0,0 +1,25 @@ +/* + * 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. + * 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.formatting; + +/** + * @author Denis Zhdanov + * @since 8/22/12 2:44 PM + */ +public enum FormattingMode { + + REFORMAT, ADJUST_INDENT +} diff --git a/platform/lang-impl/src/com/intellij/formatting/FormattingModelBuilderEx.java b/platform/lang-impl/src/com/intellij/formatting/FormattingModelBuilderEx.java new file mode 100644 index 000000000000..47e05e9aeb0c --- /dev/null +++ b/platform/lang-impl/src/com/intellij/formatting/FormattingModelBuilderEx.java @@ -0,0 +1,39 @@ +/* + * 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. + * 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.formatting; + +import com.intellij.psi.PsiElement; +import com.intellij.psi.codeStyle.CodeStyleSettings; +import org.jetbrains.annotations.NotNull; + +/** + * @author Denis Zhdanov + * @since 8/22/12 2:23 PM + */ +public interface FormattingModelBuilderEx extends FormattingModelBuilder { + + /** + * Requests building the formatting model for a section of the file containing + * the specified PSI element and its children. + * + * @param element the top element for which formatting is requested. + * @param settings the code style settings used for formatting. + * @param mode formatting mode + * @return the formatting model for the file. + */ + @NotNull + FormattingModel createModel(@NotNull final PsiElement element, @NotNull final CodeStyleSettings settings, @NotNull FormattingMode mode); +} 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 d9af7a4b628a..53eae24794de 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 @@ -100,7 +100,7 @@ public class CodeFormatterFacade { } //final SmartPsiElementPointer pointer = SmartPointerManager.getInstance(psiElement.getProject()).createSmartPsiElementPointer(psiElement); - final FormattingModel model = builder.createModel(elementToFormat, mySettings); + final FormattingModel model = CoreFormatterUtil.buildModel(builder, elementToFormat, mySettings, FormattingMode.REFORMAT); if (file.getTextLength() > 0) { try { FormatterEx.getInstanceEx().format( @@ -193,7 +193,7 @@ public class CodeFormatterFacade { return; } - final FormattingModel originalModel = builder.createModel(file, mySettings); + final FormattingModel originalModel = CoreFormatterUtil.buildModel(builder, file, mySettings, FormattingMode.REFORMAT); final FormattingModel model = new DocumentBasedFormattingModel(originalModel.getRootBlock(), document, project, mySettings, file.getFileType(), file); diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerImpl.java b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerImpl.java index 37e6c9a1e270..c9f491cc6006 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerImpl.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerImpl.java @@ -285,7 +285,7 @@ public class CodeStyleManagerImpl extends CodeStyleManager { final FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(containingFile); if (builder != null) { - final FormattingModel model = builder.createModel(containingFile, getSettings()); + final FormattingModel model = CoreFormatterUtil.buildModel(builder, containingFile, getSettings(), FormattingMode.REFORMAT); FormatterEx.getInstanceEx().formatAroundRange(model, getSettings(), textRange, containingFile.getFileType()); } @@ -334,7 +334,7 @@ public class CodeStyleManagerImpl extends CodeStyleManager { } private int doAdjustLineIndentByOffset(@NotNull PsiFile file, int offset) { - return new CodeStyleManagerRunnable(this) { + return new CodeStyleManagerRunnable(this, FormattingMode.ADJUST_INDENT) { @Override protected Integer doPerform(int offset, TextRange range) { return FormatterEx.getInstanceEx().adjustLineIndent(myModel, mySettings, myIndentOptions, offset, mySignificantRange); @@ -354,7 +354,7 @@ public class CodeStyleManagerImpl extends CodeStyleManager { @Override public void adjustLineIndent(@NotNull PsiFile file, TextRange rangeToAdjust) throws IncorrectOperationException { - new CodeStyleManagerRunnable(this) { + new CodeStyleManagerRunnable(this, FormattingMode.ADJUST_INDENT) { @Override protected Object doPerform(int offset, TextRange range) { FormatterEx.getInstanceEx().adjustLineIndentsForRange(myModel, mySettings, myIndentOptions, range); @@ -366,7 +366,7 @@ public class CodeStyleManagerImpl extends CodeStyleManager { @Override @Nullable public String getLineIndent(@NotNull PsiFile file, int offset) { - return new CodeStyleManagerRunnable(this) { + return new CodeStyleManagerRunnable(this, FormattingMode.ADJUST_INDENT) { @Override protected boolean useDocumentBaseFormattingModel() { return false; diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerRunnable.java b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerRunnable.java index de0243002b23..ccbe0ea99661 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerRunnable.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerRunnable.java @@ -15,8 +15,7 @@ */ package com.intellij.psi.impl.source.codeStyle; -import com.intellij.formatting.FormattingModel; -import com.intellij.formatting.FormattingModelBuilder; +import com.intellij.formatting.*; import com.intellij.injected.editor.DocumentWindow; import com.intellij.lang.ASTNode; import com.intellij.lang.LanguageFormatting; @@ -31,20 +30,23 @@ import com.intellij.psi.impl.source.SourceTreeToPsiMap; import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil; import com.intellij.psi.util.PsiUtilCore; import com.intellij.util.text.CharArrayUtil; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** * @author nik */ abstract class CodeStyleManagerRunnable { - protected CodeStyleSettings mySettings; - protected CommonCodeStyleSettings.IndentOptions myIndentOptions; - protected FormattingModel myModel; - protected TextRange mySignificantRange; - private final CodeStyleManagerImpl myCodeStyleManager; + protected CodeStyleSettings mySettings; + protected CommonCodeStyleSettings.IndentOptions myIndentOptions; + protected FormattingModel myModel; + protected TextRange mySignificantRange; + private final CodeStyleManagerImpl myCodeStyleManager; + @NotNull private final FormattingMode myMode; - CodeStyleManagerRunnable(CodeStyleManagerImpl codeStyleManager) { + CodeStyleManagerRunnable(CodeStyleManagerImpl codeStyleManager, @NotNull FormattingMode mode) { myCodeStyleManager = codeStyleManager; + myMode = mode; } public T perform(PsiFile file, int offset, @Nullable TextRange range, T defaultValue) { @@ -91,7 +93,7 @@ abstract class CodeStyleManagerRunnable { mySettings = CodeStyleSettingsManager.getSettings(myCodeStyleManager.getProject()); myIndentOptions = mySettings.getIndentOptions(file.getFileType()); mySignificantRange = offset != -1 ? getSignificantRange(file, offset) : null; - myModel = builder.createModel(file, mySettings); + myModel = CoreFormatterUtil.buildModel(builder, file, mySettings, myMode); if (document != null && useDocumentBaseFormattingModel()) { myModel = new DocumentBasedFormattingModel(myModel.getRootBlock(), document, myCodeStyleManager.getProject(), mySettings, file.getFileType(), file);