|
| 1 | +/* |
| 2 | + * Copyright 2015 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.googlejavaformat.intellij; |
| 18 | + |
| 19 | +import com.intellij.formatting.FormattingMode; |
| 20 | +import com.intellij.lang.ASTNode; |
| 21 | +import com.intellij.openapi.editor.Document; |
| 22 | +import com.intellij.openapi.fileTypes.FileType; |
| 23 | +import com.intellij.openapi.project.Project; |
| 24 | +import com.intellij.openapi.util.Computable; |
| 25 | +import com.intellij.openapi.util.TextRange; |
| 26 | +import com.intellij.psi.PsiElement; |
| 27 | +import com.intellij.psi.PsiFile; |
| 28 | +import com.intellij.psi.codeStyle.ChangedRangesInfo; |
| 29 | +import com.intellij.psi.codeStyle.CodeStyleManager; |
| 30 | +import com.intellij.psi.codeStyle.FormattingModeAwareIndentAdjuster; |
| 31 | +import com.intellij.psi.codeStyle.Indent; |
| 32 | +import com.intellij.util.IncorrectOperationException; |
| 33 | +import com.intellij.util.ThrowableRunnable; |
| 34 | +import java.util.Collection; |
| 35 | +import javax.annotation.Nullable; |
| 36 | +import org.jetbrains.annotations.NotNull; |
| 37 | + |
| 38 | +/** |
| 39 | + * Decorates the {@link CodeStyleManager} abstract class by delegating to a concrete implementation |
| 40 | + * instance (likely IJ's default instance). |
| 41 | + * |
| 42 | + * @author [email protected] (Brian Chang) |
| 43 | + */ |
| 44 | +@SuppressWarnings("deprecation") |
| 45 | +class CodeStyleManagerDecorator extends CodeStyleManager |
| 46 | + implements FormattingModeAwareIndentAdjuster { |
| 47 | + |
| 48 | + @NotNull private final CodeStyleManager delegate; |
| 49 | + |
| 50 | + CodeStyleManagerDecorator(@NotNull CodeStyleManager delegate) { |
| 51 | + this.delegate = delegate; |
| 52 | + } |
| 53 | + |
| 54 | + @NotNull |
| 55 | + CodeStyleManager getDelegate() { |
| 56 | + return delegate; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + @NotNull |
| 61 | + public Project getProject() { |
| 62 | + return delegate.getProject(); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + @NotNull |
| 67 | + public PsiElement reformat(@NotNull PsiElement element) throws IncorrectOperationException { |
| 68 | + return delegate.reformat(element); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + @NotNull |
| 73 | + public PsiElement reformat(@NotNull PsiElement element, boolean canChangeWhiteSpacesOnly) |
| 74 | + throws IncorrectOperationException { |
| 75 | + return delegate.reformat(element, canChangeWhiteSpacesOnly); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public PsiElement reformatRange(@NotNull PsiElement element, int startOffset, int endOffset) |
| 80 | + throws IncorrectOperationException { |
| 81 | + return delegate.reformatRange(element, startOffset, endOffset); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public PsiElement reformatRange( |
| 86 | + @NotNull PsiElement element, int startOffset, int endOffset, boolean canChangeWhiteSpacesOnly) |
| 87 | + throws IncorrectOperationException { |
| 88 | + return delegate.reformatRange(element, startOffset, endOffset, canChangeWhiteSpacesOnly); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void reformatText(@NotNull PsiFile file, int startOffset, int endOffset) |
| 93 | + throws IncorrectOperationException { |
| 94 | + delegate.reformatText(file, startOffset, endOffset); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void reformatText(@NotNull PsiFile file, @NotNull Collection<TextRange> ranges) |
| 99 | + throws IncorrectOperationException { |
| 100 | + delegate.reformatText(file, ranges); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void reformatTextWithContext( |
| 105 | + @NotNull PsiFile psiFile, @NotNull ChangedRangesInfo changedRangesInfo) |
| 106 | + throws IncorrectOperationException { |
| 107 | + delegate.reformatTextWithContext(psiFile, changedRangesInfo); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public void adjustLineIndent(@NotNull PsiFile file, TextRange rangeToAdjust) |
| 112 | + throws IncorrectOperationException { |
| 113 | + delegate.adjustLineIndent(file, rangeToAdjust); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public int adjustLineIndent(@NotNull PsiFile file, int offset) |
| 118 | + throws IncorrectOperationException { |
| 119 | + return delegate.adjustLineIndent(file, offset); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public int adjustLineIndent(@NotNull Document document, int offset) { |
| 124 | + return delegate.adjustLineIndent(document, offset); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public boolean isLineToBeIndented(@NotNull PsiFile file, int offset) { |
| 129 | + return delegate.isLineToBeIndented(file, offset); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + @Nullable |
| 134 | + public String getLineIndent(@NotNull PsiFile file, int offset) { |
| 135 | + return delegate.getLineIndent(file, offset); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + @Nullable |
| 140 | + public String getLineIndent(@NotNull Document document, int offset) { |
| 141 | + return delegate.getLineIndent(document, offset); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public Indent getIndent(String text, FileType fileType) { |
| 146 | + return delegate.getIndent(text, fileType); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public String fillIndent(Indent indent, FileType fileType) { |
| 151 | + return delegate.fillIndent(indent, fileType); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public Indent zeroIndent() { |
| 156 | + return delegate.zeroIndent(); |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public void reformatNewlyAddedElement(@NotNull ASTNode block, @NotNull ASTNode addedElement) |
| 161 | + throws IncorrectOperationException { |
| 162 | + delegate.reformatNewlyAddedElement(block, addedElement); |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public boolean isSequentialProcessingAllowed() { |
| 167 | + return delegate.isSequentialProcessingAllowed(); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public void performActionWithFormatterDisabled(Runnable r) { |
| 172 | + delegate.performActionWithFormatterDisabled(r); |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public <T extends Throwable> void performActionWithFormatterDisabled(ThrowableRunnable<T> r) |
| 177 | + throws T { |
| 178 | + delegate.performActionWithFormatterDisabled(r); |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public <T> T performActionWithFormatterDisabled(Computable<T> r) { |
| 183 | + return delegate.performActionWithFormatterDisabled(r); |
| 184 | + } |
| 185 | + |
| 186 | + /** Uses same fallback as {@link CodeStyleManager#getCurrentFormattingMode}. */ |
| 187 | + @Override |
| 188 | + public FormattingMode getCurrentFormattingMode() { |
| 189 | + if (delegate instanceof FormattingModeAwareIndentAdjuster) { |
| 190 | + return ((FormattingModeAwareIndentAdjuster) delegate).getCurrentFormattingMode(); |
| 191 | + } |
| 192 | + return FormattingMode.REFORMAT; |
| 193 | + } |
| 194 | + |
| 195 | + @Override |
| 196 | + public int adjustLineIndent( |
| 197 | + @NotNull final Document document, final int offset, FormattingMode mode) |
| 198 | + throws IncorrectOperationException { |
| 199 | + if (delegate instanceof FormattingModeAwareIndentAdjuster) { |
| 200 | + return ((FormattingModeAwareIndentAdjuster) delegate) |
| 201 | + .adjustLineIndent(document, offset, mode); |
| 202 | + } |
| 203 | + return offset; |
| 204 | + } |
| 205 | +} |
0 commit comments