Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.intellij.psi.util.elementType
import com.intellij.psi.util.prevLeafs
import com.intellij.util.ProcessingContext
import org.domaframework.doma.intellij.common.sql.directive.DirectiveCompletion
import org.domaframework.doma.intellij.common.util.StringUtil.SINGLE_SPACE
import org.domaframework.doma.intellij.psi.SqlCustomElCommentExpr
import org.domaframework.doma.intellij.psi.SqlElClass
import org.domaframework.doma.intellij.psi.SqlElIdExpr
Expand Down Expand Up @@ -135,7 +136,7 @@ object PsiPatternUtil {
element: PsiElement,
symbol: String,
): String {
val text = originalFile.containingFile?.text ?: " "
val text = originalFile.containingFile?.text ?: SINGLE_SPACE
val offset = element.textOffset
val builder = StringBuilder()
for (i in offset - 1 downTo 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.domaframework.doma.intellij.common.sql

import org.domaframework.doma.intellij.common.util.StringUtil
import org.domaframework.doma.intellij.common.util.StringUtil.SINGLE_SPACE

/**
* Exclude extra strings and block symbols added by IntelliJ operations a
Expand All @@ -29,5 +30,5 @@ fun cleanString(str: String): String {
// TODO: Temporary support when using operators.
// Remove the "== a" element because it is attached to the end.
// Make it possible to obtain the equilateral elements of the left side individually.
.substringBefore(" ")
.substringBefore(SINGLE_SPACE)
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.domaframework.doma.intellij.common.psi.PsiPatternUtil
import org.domaframework.doma.intellij.common.sql.directive.collector.FunctionCallCollector
import org.domaframework.doma.intellij.common.sql.directive.collector.StaticClassPackageCollector
import org.domaframework.doma.intellij.common.sql.directive.collector.StaticPropertyCollector
import org.domaframework.doma.intellij.common.util.StringUtil.SINGLE_SPACE
import org.domaframework.doma.intellij.psi.SqlElClass
import org.domaframework.doma.intellij.psi.SqlElStaticFieldAccessExpr
import org.domaframework.doma.intellij.psi.SqlTypes
Expand Down Expand Up @@ -117,7 +118,7 @@ class StaticDirectiveHandler(
val prev = PsiTreeUtil.prevLeaf(element, true)
val staticFieldAccess =
PsiTreeUtil.getParentOfType(prev, SqlElStaticFieldAccessExpr::class.java)
val sqlElClassWords = PsiPatternUtil.getBindSearchWord(element.containingFile, element, " ")
val sqlElClassWords = PsiPatternUtil.getBindSearchWord(element.containingFile, element, SINGLE_SPACE)
return (
staticFieldAccess != null && staticFieldAccess.elIdExprList.isEmpty()
) ||
Expand All @@ -141,7 +142,7 @@ class StaticDirectiveHandler(
.getChildOfType(prev, SqlElClass::class.java)
?: PsiTreeUtil.getChildOfType(PsiTreeUtil.prevLeaf(element)?.parent, SqlElClass::class.java)

val sqlElClassWords = PsiPatternUtil.getBindSearchWord(element.containingFile, element, " ")
val sqlElClassWords = PsiPatternUtil.getBindSearchWord(element.containingFile, element, SINGLE_SPACE)
val sqlElClassName = PsiTreeUtil.getChildrenOfTypeAsList(clazzRef, PsiElement::class.java).joinToString("") { it.text }
val fqdn = if (sqlElClassName.isNotEmpty()) sqlElClassName else sqlElClassWords.replace("@", "")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package org.domaframework.doma.intellij.common.util

object StringUtil {
const val LINE_SEPARATE: String = "\n"
const val SINGLE_SPACE: String = " "

fun getSqlElClassText(text: String): String =
text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.intellij.openapi.project.Project
import com.intellij.psi.PsiClassType
import com.intellij.psi.PsiType
import org.domaframework.doma.intellij.common.psi.PsiTypeChecker
import org.domaframework.doma.intellij.common.util.StringUtil.SINGLE_SPACE
import org.domaframework.doma.intellij.extension.getJavaClazz
import org.domaframework.doma.intellij.extension.psi.getClassAnnotation
import org.domaframework.doma.intellij.extension.psi.isDomain
Expand Down Expand Up @@ -79,13 +80,13 @@ object TypeUtil {
* Checks if the given type is a valid Map<String, Object>.
*/
fun isValidMapType(type: PsiType?): Boolean {
val canonical = type?.canonicalText?.replace(" ", "") ?: return false
val canonical = type?.canonicalText?.replace(SINGLE_SPACE, "") ?: return false
val expected =
DomaClassName.MAP
.getGenericParamCanonicalText(
DomaClassName.STRING.className,
DomaClassName.OBJECT.className,
).replace(" ", "")
).replace(SINGLE_SPACE, "")
return canonical == expected
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import org.domaframework.doma.intellij.common.util.ForDirectiveUtil
import org.domaframework.doma.intellij.common.util.PluginLoggerUtil
import org.domaframework.doma.intellij.common.util.SqlCompletionUtil.createMethodLookupElement
import org.domaframework.doma.intellij.common.util.StringUtil
import org.domaframework.doma.intellij.common.util.StringUtil.SINGLE_SPACE
import org.domaframework.doma.intellij.common.validation.result.ValidationCompleteResult
import org.domaframework.doma.intellij.contributor.sql.processor.SqlCompletionDirectiveBlockProcessor
import org.domaframework.doma.intellij.contributor.sql.processor.SqlCompletionOtherBlockProcessor
Expand Down Expand Up @@ -207,7 +208,7 @@ class SqlParameterCompletionProvider : CompletionProvider<CompletionParameters>(
val fqdn =
StringUtil.getSqlElClassText(
PsiPatternUtil
.getBindSearchWord(originalFile, top, " "),
.getBindSearchWord(originalFile, top, SINGLE_SPACE),
)
topElementType = getElementTypeByPrevSqlElClassWords(project, fqdn, topText)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
import org.domaframework.doma.intellij.formatter.util.SqlBlockGenerator
import org.domaframework.doma.intellij.psi.SqlTypes

class SqlFileBlock(
open class SqlFileBlock(
node: ASTNode,
wrap: Wrap?,
alignment: Alignment?,
Expand Down Expand Up @@ -221,7 +221,7 @@ class SqlFileBlock(
return if (lastGroup is SqlWithCommonTableGroupBlock) {
SqlWithCommonTableGroupBlock(child, defaultFormatCtx)
} else {
blockUtil.getBlockCommentBlock(child, createBlockCommentSpacingBuilder())
blockUtil.getBlockCommentBlock(child, createBlockDirectiveCommentSpacingBuilder())
}
}

Expand Down Expand Up @@ -416,15 +416,11 @@ class SqlFileBlock(
}

/**
* Creates a spacing builder specifically for block comments.
* Creates a spacing builder specifically for directive block comments.
*/
private fun createBlockCommentSpacingBuilder(): SqlCustomSpacingBuilder =
protected fun createBlockDirectiveCommentSpacingBuilder(): SqlCustomSpacingBuilder =
SqlCustomSpacingBuilder()
.withSpacing(
SqlTypes.BLOCK_COMMENT_START,
SqlTypes.BLOCK_COMMENT_CONTENT,
Spacing.createSpacing(0, 0, 0, true, 0),
).withSpacing(
SqlTypes.BLOCK_COMMENT_START,
SqlTypes.EL_ID_EXPR,
Spacing.createSpacing(1, 1, 0, true, 0),
Expand Down Expand Up @@ -572,10 +568,6 @@ class SqlFileBlock(
SqlTypes.EL_STATIC_FIELD_ACCESS_EXPR,
SqlTypes.BLOCK_COMMENT_END,
Spacing.createSpacing(1, 1, 0, true, 0),
).withSpacing(
SqlTypes.BLOCK_COMMENT_CONTENT,
SqlTypes.BLOCK_COMMENT_END,
Spacing.createSpacing(0, 0, 0, true, 0),
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,57 @@
*/
package org.domaframework.doma.intellij.formatter.block.comment

import com.intellij.formatting.Block
import com.intellij.formatting.Spacing
import com.intellij.lang.ASTNode
import com.intellij.psi.PsiWhiteSpace
import com.intellij.psi.formatter.common.AbstractBlock
import com.intellij.psi.util.PsiTreeUtil
import org.domaframework.doma.intellij.common.util.StringUtil
import org.domaframework.doma.intellij.formatter.block.SqlBlock
import org.domaframework.doma.intellij.formatter.block.SqlUnknownBlock
import org.domaframework.doma.intellij.formatter.builder.SqlCustomSpacingBuilder
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
import org.domaframework.doma.intellij.psi.SqlTypes

open class SqlBlockCommentBlock(
node: ASTNode,
context: SqlBlockFormattingContext,
private val customSpacingBuilder: SqlCustomSpacingBuilder,
private val context: SqlBlockFormattingContext,
) : SqlDefaultCommentBlock(
node,
context,
) {
override fun buildChildren(): MutableList<AbstractBlock> {
val blocks = mutableListOf<AbstractBlock>()
var child = node.firstChildNode
while (child != null) {
if (child !is PsiWhiteSpace) {
val block = getBlock(child)
blocks.add(block)
}
child = child.treeNext
}
return blocks
}

override fun getBlock(child: ASTNode): SqlBlock {
val elementType = child.elementType
return when (elementType) {
SqlTypes.BLOCK_COMMENT_START -> SqlCommentStartBlock(child, context)
SqlTypes.BLOCK_COMMENT_END -> SqlCommentEndBlock(child, context)
SqlTypes.BLOCK_COMMENT_CONTENT -> SqlCommentContentBlock(child, context)
else -> SqlUnknownBlock(child, context)
}
}

override fun isSaveSpace(lastGroup: SqlBlock?): Boolean =
PsiTreeUtil.prevLeaf(node.psi)?.text?.contains(StringUtil.LINE_SEPARATE) == true

override fun getSpacing(
child1: Block?,
child2: Block,
): Spacing? =
customSpacingBuilder.getCustomSpacing(child1, child2)
?: SqlCustomSpacingBuilder.normalSpacing
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright Doma Tools Authors
*
* 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
*
* https://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 org.domaframework.doma.intellij.formatter.block.comment

import com.intellij.lang.ASTNode
import org.domaframework.doma.intellij.formatter.block.SqlBlock
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext

class SqlCommentContentBlock(
node: ASTNode,
context: SqlBlockFormattingContext,
) : SqlCommentBlock(node, context) {
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean = false

override fun createBlockIndentLen(): Int = parentBlock?.indent?.indentLen ?: 1

override fun createGroupIndentLen(): Int = indent.indentLen
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright Doma Tools Authors
*
* 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
*
* https://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 org.domaframework.doma.intellij.formatter.block.comment

import com.intellij.lang.ASTNode
import com.intellij.psi.PsiComment
import com.intellij.psi.util.PsiTreeUtil
import org.domaframework.doma.intellij.formatter.block.SqlBlock
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext

class SqlCommentEndBlock(
node: ASTNode,
context: SqlBlockFormattingContext,
) : SqlCommentSeparateBlock(node, context) {
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
parentBlock?.let { parent ->
val contents =
PsiTreeUtil.getChildOfType<PsiComment>(parent.node.psi, PsiComment::class.java)
return contents != null
}
return false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright Doma Tools Authors
*
* 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
*
* https://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 org.domaframework.doma.intellij.formatter.block.comment

import com.intellij.lang.ASTNode
import org.domaframework.doma.intellij.formatter.block.SqlBlock
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext

abstract class SqlCommentSeparateBlock(
node: ASTNode,
context: SqlBlockFormattingContext,
) : SqlBlock(
node,
context.wrap,
context.alignment,
context.spacingBuilder,
context.enableFormat,
context.formatMode,
) {
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean = false

override fun createBlockIndentLen(): Int = parentBlock?.indent?.indentLen ?: 0

override fun createGroupIndentLen(): Int = indent.indentLen
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright Doma Tools Authors
*
* 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
*
* https://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 org.domaframework.doma.intellij.formatter.block.comment

import com.intellij.lang.ASTNode
import org.domaframework.doma.intellij.formatter.block.SqlBlock
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext

class SqlCommentStartBlock(
node: ASTNode,
context: SqlBlockFormattingContext,
) : SqlCommentSeparateBlock(node, context) {
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ open class SqlElBlockCommentBlock(
createFieldAccessSpacingBuilder(),
)

SqlTypes.BLOCK_COMMENT_START -> SqlCommentStartBlock(child, context)

SqlTypes.BLOCK_COMMENT_END -> SqlCommentEndBlock(child, context)

SqlTypes.EL_STATIC_FIELD_ACCESS_EXPR ->
SqlElStaticFieldAccessBlock(
child,
Expand All @@ -121,7 +125,7 @@ open class SqlElBlockCommentBlock(
)

SqlTypes.BLOCK_COMMENT_CONTENT ->
SqlBlockCommentBlock(child, context)
SqlBlockCommentBlock(child, createBlockCommentSpacingBuilder(), context)

else -> SqlUnknownBlock(child, context)
}
Expand All @@ -146,6 +150,14 @@ open class SqlElBlockCommentBlock(
Spacing.createSpacing(0, 0, 0, false, 0),
)

protected fun createBlockCommentSpacingBuilder(): SqlCustomSpacingBuilder =
SqlCustomSpacingBuilder()
.withSpacing(
SqlTypes.BLOCK_COMMENT_START,
SqlTypes.BLOCK_COMMENT_CONTENT,
Spacing.createSpacing(1, 1, 0, false, 0),
)

override fun getSpacing(
child1: Block?,
child2: Block,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,6 @@ class SqlElConditionLoopCommentBlock(
context,
)

SqlTypes.BLOCK_COMMENT_CONTENT ->
SqlBlockCommentBlock(child, context)

else -> SqlUnknownBlock(child, context)
}

Expand Down
Loading
Loading