mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Provide an API to distinguish between 'format' and 'adjust line' operations
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
+2
-2
@@ -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);
|
||||
|
||||
+4
-4
@@ -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<Integer>(this) {
|
||||
return new CodeStyleManagerRunnable<Integer>(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<Object>(this) {
|
||||
new CodeStyleManagerRunnable<Object>(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<String>(this) {
|
||||
return new CodeStyleManagerRunnable<String>(this, FormattingMode.ADJUST_INDENT) {
|
||||
@Override
|
||||
protected boolean useDocumentBaseFormattingModel() {
|
||||
return false;
|
||||
|
||||
+11
-9
@@ -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<T> {
|
||||
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<T> {
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user