From 06ab690fa7cfac3bf33f24c305b11f7e14353086 Mon Sep 17 00:00:00 2001 From: Yaroslav Lepenkin Date: Mon, 17 Jul 2017 16:41:27 +0300 Subject: [PATCH] [comments formatting] minor, move class --- .../formatting/blocks/CStyleComment.kt | 52 +----------------- .../formatting/blocks/FormatterTreeUtil.kt | 28 ++++++++++ .../formatting/blocks/TextLineBlock.kt | 55 +++++++++++++++++++ 3 files changed, 86 insertions(+), 49 deletions(-) create mode 100644 platform/lang-impl/src/com/intellij/formatting/blocks/FormatterTreeUtil.kt create mode 100644 platform/lang-impl/src/com/intellij/formatting/blocks/TextLineBlock.kt diff --git a/platform/lang-impl/src/com/intellij/formatting/blocks/CStyleComment.kt b/platform/lang-impl/src/com/intellij/formatting/blocks/CStyleComment.kt index 30d07a870a12..055798384826 100644 --- a/platform/lang-impl/src/com/intellij/formatting/blocks/CStyleComment.kt +++ b/platform/lang-impl/src/com/intellij/formatting/blocks/CStyleComment.kt @@ -15,23 +15,14 @@ */ package com.intellij.formatting.blocks -import com.intellij.formatting.* +import com.intellij.formatting.Block +import com.intellij.formatting.Indent +import com.intellij.formatting.Spacing import com.intellij.lang.ASTNode import com.intellij.openapi.util.TextRange -import com.intellij.psi.TokenType import com.intellij.psi.formatter.common.AbstractBlock -fun ASTNode.prev(): ASTNode? { - var prev = treePrev - while (prev != null && prev.elementType == TokenType.WHITE_SPACE) { - prev = prev.treePrev - } - if (prev != null) return prev - return if (treeParent != null) treeParent.prev() else null -} - - class CStyleCommentBlock(comment: ASTNode, private val indent: Indent?): AbstractBlock(comment, null, null) { private val lines by lazy { lineBlocks() } @@ -93,43 +84,6 @@ class CStyleCommentBlock(comment: ASTNode, private val indent: Indent?): Abstrac private class LineInfo(val text: String, val textRange: TextRange) -class TextLineBlock( - val text: String, - private val textRange: TextRange, - private val alignment: Alignment?, - private val indent: Indent?, - val spacing: Spacing? -) : Block { - - override fun getTextRange(): TextRange { - return textRange - } - - override fun getSubBlocks(): List = emptyList() - - override fun getWrap() = null - - override fun getIndent() = indent - - override fun getAlignment() = alignment - - override fun getSpacing(child1: Block?, child2: Block) = spacing - - override fun getChildAttributes(newChildIndex: Int): ChildAttributes { - throw UnsupportedOperationException("Should not be called") - } - - override fun isIncomplete() = false - - override fun isLeaf() = true - - override fun toString(): String { - return "TextLineBlock(text='$text', textRange=$textRange)" - } - -} - - fun List.split(predicate: (T) -> Boolean): List> { if (indices.isEmpty()) return listOf() val result = mutableListOf>() diff --git a/platform/lang-impl/src/com/intellij/formatting/blocks/FormatterTreeUtil.kt b/platform/lang-impl/src/com/intellij/formatting/blocks/FormatterTreeUtil.kt new file mode 100644 index 000000000000..61cc6946aaf2 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/formatting/blocks/FormatterTreeUtil.kt @@ -0,0 +1,28 @@ +/* + * Copyright 2000-2017 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.blocks + +import com.intellij.lang.ASTNode +import com.intellij.psi.TokenType + +fun ASTNode.prev(): ASTNode? { + var prev = treePrev + while (prev != null && prev.elementType == TokenType.WHITE_SPACE) { + prev = prev.treePrev + } + if (prev != null) return prev + return if (treeParent != null) treeParent.prev() else null +} diff --git a/platform/lang-impl/src/com/intellij/formatting/blocks/TextLineBlock.kt b/platform/lang-impl/src/com/intellij/formatting/blocks/TextLineBlock.kt new file mode 100644 index 000000000000..7ad77b0ce0b7 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/formatting/blocks/TextLineBlock.kt @@ -0,0 +1,55 @@ +/* + * Copyright 2000-2017 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.blocks + +import com.intellij.formatting.* +import com.intellij.openapi.util.TextRange + +class TextLineBlock( + val text: String, + private val textRange: TextRange, + private val alignment: Alignment?, + private val indent: Indent?, + val spacing: Spacing? +) : Block { + + override fun getTextRange(): TextRange { + return textRange + } + + override fun getSubBlocks(): List = emptyList() + + override fun getWrap() = null + + override fun getIndent() = indent + + override fun getAlignment() = alignment + + override fun getSpacing(child1: Block?, child2: Block) = spacing + + override fun getChildAttributes(newChildIndex: Int): ChildAttributes { + throw UnsupportedOperationException("Should not be called") + } + + override fun isIncomplete() = false + + override fun isLeaf() = true + + override fun toString(): String { + return "TextLineBlock(text='$text', textRange=$textRange)" + } + +}