mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
[java-formatter] minor, extracted class, added test
This commit is contained in:
@@ -1281,16 +1281,13 @@ public abstract class AbstractJavaBlock extends AbstractBlock implements JavaBlo
|
||||
public SyntheticCodeBlock createCodeBlockBlock(final List<Block> localResult, final Indent indent, final int childrenIndent) {
|
||||
final SyntheticCodeBlock result = new SyntheticCodeBlock(localResult, null, getSettings(), myJavaSettings, indent, null);
|
||||
result.setChildAttributes(new ChildAttributes(getCodeBlockInternalIndent(childrenIndent), null));
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public ExtraReformatRanges getExtraRangesToFormat(FormatTextRanges ranges) {
|
||||
if (!Registry.is("smart.reformat.vcs.changes")) return null;
|
||||
|
||||
int startOffset = getTextRange().getStartOffset();
|
||||
if (ranges.isOnInsertedLine(startOffset) && myNode.textContains('\n')) {
|
||||
if (Registry.is("smart.reformat.vcs.changes") && ranges.isInsertedBlock(this) && myNode.textContains('\n')) {
|
||||
List<TextRange> extra = calculateExtraRanges(myNode);
|
||||
return new ExtraReformatRanges(extra);
|
||||
}
|
||||
@@ -1299,36 +1296,16 @@ public abstract class AbstractJavaBlock extends AbstractBlock implements JavaBlo
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private List<TextRange> calculateExtraRanges(ASTNode node) {
|
||||
Project project = getProject(node);
|
||||
Document document = retrieveDocument(node, project);
|
||||
TextRange range = node.getTextRange();
|
||||
private List<TextRange> calculateExtraRanges(@NotNull ASTNode node) {
|
||||
Document document = retrieveDocument(node, getProject(node));
|
||||
if (document != null) {
|
||||
int startLine = document.getLineNumber(range.getStartOffset());
|
||||
int endLine = document.getLineNumber(range.getEndOffset());
|
||||
return extractIndentSpaces(document, startLine, endLine);
|
||||
TextRange ranges = node.getTextRange();
|
||||
return new IndentRangesCalculator(document, ranges).calcIndentRanges();
|
||||
}
|
||||
|
||||
return ContainerUtil.newArrayList(myNode.getTextRange());
|
||||
}
|
||||
|
||||
private static List<TextRange> extractIndentSpaces(Document document, int startLine, int endLine) {
|
||||
List<TextRange> extra = ContainerUtil.newArrayList();
|
||||
|
||||
CharSequence chars = document.getCharsSequence();
|
||||
|
||||
for (int line = startLine + 1; line <= endLine; line++) {
|
||||
int lineStartOffset = document.getLineStartOffset(line);
|
||||
int lineEndOffset = document.getLineEndOffset(line);
|
||||
|
||||
int firstNonWsChar = CharArrayUtil.shiftForward(chars, lineStartOffset, lineEndOffset + 1, " \t");
|
||||
if (firstNonWsChar <= lineEndOffset + 1) {
|
||||
extra.add(new TextRange(lineStartOffset, firstNonWsChar));
|
||||
}
|
||||
}
|
||||
|
||||
return extra;
|
||||
}
|
||||
|
||||
|
||||
private static Document retrieveDocument(@NotNull ASTNode node, @NotNull Project project) {
|
||||
PsiFile file = node.getPsi().getContainingFile();
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2000-2016 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.psi.formatter.java
|
||||
|
||||
import com.intellij.openapi.editor.Document
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.util.text.CharArrayUtil
|
||||
|
||||
class IndentRangesCalculator(private val document: Document,
|
||||
private val textRange: TextRange)
|
||||
{
|
||||
private val startOffset = textRange.startOffset
|
||||
private val endOffset = textRange.endOffset
|
||||
|
||||
fun calcIndentRanges(): List<TextRange> {
|
||||
val startLine = document.getLineNumber(startOffset)
|
||||
val endLine = document.getLineNumber(endOffset)
|
||||
val chars = document.charsSequence
|
||||
|
||||
val indentRanges = mutableListOf<TextRange>()
|
||||
|
||||
for (line in startLine..endLine) {
|
||||
val lineStartOffset = document.getLineStartOffset(line)
|
||||
val lineEndOffset = document.getLineEndOffset(line)
|
||||
val firstNonWsChar = CharArrayUtil.shiftForward(chars, lineStartOffset, lineEndOffset + 1, " \t")
|
||||
indentRanges.add(TextRange(lineStartOffset, firstNonWsChar))
|
||||
}
|
||||
|
||||
return indentRanges
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user