diff --git a/src/nl/hannahsten/texifyidea/formatting/BibtexBlock.kt b/src/nl/hannahsten/texifyidea/formatting/BibtexBlock.kt index b340c86dd..bf7796be5 100644 --- a/src/nl/hannahsten/texifyidea/formatting/BibtexBlock.kt +++ b/src/nl/hannahsten/texifyidea/formatting/BibtexBlock.kt @@ -13,7 +13,7 @@ open class BibtexBlock( node: ASTNode, wrap: Wrap, alignment: Alignment?, - val spacingBuilder: SpacingBuilder + private val spacingBuilder: TexSpacingBuilder ) : AbstractBlock(node, wrap, alignment) { override fun buildChildren(): MutableList { diff --git a/src/nl/hannahsten/texifyidea/formatting/BibtexFormattingModelBuilder.kt b/src/nl/hannahsten/texifyidea/formatting/BibtexFormattingModelBuilder.kt index 2cdd45ec0..1e5977935 100644 --- a/src/nl/hannahsten/texifyidea/formatting/BibtexFormattingModelBuilder.kt +++ b/src/nl/hannahsten/texifyidea/formatting/BibtexFormattingModelBuilder.kt @@ -34,7 +34,7 @@ open class BibtexFormattingModelBuilder : FormattingModelBuilder { element.node, Wrap.createWrap(WrapType.NONE, false), Alignment.createAlignment(), - createSpacingBuilder(settings) + createBibtexSpacingBuilder(settings) ), settings )!! diff --git a/src/nl/hannahsten/texifyidea/formatting/BibtexSpacingRules.kt b/src/nl/hannahsten/texifyidea/formatting/BibtexSpacingRules.kt new file mode 100644 index 000000000..bbff677f8 --- /dev/null +++ b/src/nl/hannahsten/texifyidea/formatting/BibtexSpacingRules.kt @@ -0,0 +1,37 @@ +package nl.hannahsten.texifyidea.formatting + +import com.intellij.formatting.Spacing +import com.intellij.psi.codeStyle.CodeStyleSettings +import nl.hannahsten.texifyidea.BibtexLanguage +import nl.hannahsten.texifyidea.psi.BibtexTypes.* + +fun createBibtexSpacingBuilder(settings: CodeStyleSettings): TexSpacingBuilder { + + val bibtexCommonSettings = settings.getCommonSettings(BibtexLanguage) + + return rules(bibtexCommonSettings) { + + simple { + around(ASSIGNMENT).spaces(1) + before(SEPARATOR).spaces(0) + between(TYPE, OPEN_BRACE).spaces(0) + between(TYPE, OPEN_PARENTHESIS).spaces(0) + between(OPEN_BRACE, ID).spaces(0) + after(OPEN_PARENTHESIS).spaces(1) + around(CONCATENATE).spaces(1) + between(ENTRY_CONTENT, ENDTRY).spaces(1) + } + + custom { + // Only insert a space between two actual words, so when the left word + // is not a left brace, and the right word is not a right brace. + customRule { _, left, right -> + return@customRule if (right.node?.elementType === NORMAL_TEXT_WORD && left.node?.elementType === NORMAL_TEXT_WORD) { + if (left.node?.text == "{" || right.node?.text == "}") null + else Spacing.createSpacing(1, 1, 0, bibtexCommonSettings.KEEP_LINE_BREAKS, bibtexCommonSettings.KEEP_BLANK_LINES_IN_CODE) + } + else null + } + } + } +} \ No newline at end of file diff --git a/src/nl/hannahsten/texifyidea/formatting/LatexBlock.kt b/src/nl/hannahsten/texifyidea/formatting/LatexBlock.kt index 268daf0be..ee91cb306 100644 --- a/src/nl/hannahsten/texifyidea/formatting/LatexBlock.kt +++ b/src/nl/hannahsten/texifyidea/formatting/LatexBlock.kt @@ -17,7 +17,7 @@ class LatexBlock( node: ASTNode, wrap: Wrap?, alignment: Alignment?, - private val spacingBuilder: LatexSpacingBuilder, + private val spacingBuilder: TexSpacingBuilder, private val wrappingStrategy: LatexWrappingStrategy ) : AbstractBlock(node, wrap, alignment) { diff --git a/src/nl/hannahsten/texifyidea/formatting/LatexSpacingRules.kt b/src/nl/hannahsten/texifyidea/formatting/LatexSpacingRules.kt index 2cdd7631f..8e199cf1f 100644 --- a/src/nl/hannahsten/texifyidea/formatting/LatexSpacingRules.kt +++ b/src/nl/hannahsten/texifyidea/formatting/LatexSpacingRules.kt @@ -10,7 +10,7 @@ import nl.hannahsten.texifyidea.settings.codestyle.LatexCodeStyleSettings * * @author Sten Wessel, Abby Berkers */ -fun createSpacingBuilder(settings: CodeStyleSettings): LatexSpacingBuilder { +fun createSpacingBuilder(settings: CodeStyleSettings): TexSpacingBuilder { fun createSpacing(minSpaces: Int, maxSpaces: Int, minLineFeeds: Int, keepLineBreaks: Boolean, keepBlankLines: Int): Spacing = Spacing.createSpacing(minSpaces, maxSpaces, minLineFeeds, keepLineBreaks, keepBlankLines) diff --git a/src/nl/hannahsten/texifyidea/formatting/LatexSpacingBuilder.kt b/src/nl/hannahsten/texifyidea/formatting/TexSpacingBuilder.kt similarity index 94% rename from src/nl/hannahsten/texifyidea/formatting/LatexSpacingBuilder.kt rename to src/nl/hannahsten/texifyidea/formatting/TexSpacingBuilder.kt index 250dce9a0..058835d24 100644 --- a/src/nl/hannahsten/texifyidea/formatting/LatexSpacingBuilder.kt +++ b/src/nl/hannahsten/texifyidea/formatting/TexSpacingBuilder.kt @@ -13,7 +13,7 @@ import com.intellij.psi.tree.TokenSet * * @author Sten Wessel */ -class LatexSpacingBuilder(private val commonSettings: CommonCodeStyleSettings) { +class TexSpacingBuilder(private val commonSettings: CommonCodeStyleSettings) { private val builders = ArrayList() @@ -140,10 +140,10 @@ class LatexSpacingBuilder(private val commonSettings: CommonCodeStyleSettings) { } /** - * Build a [LatexSpacingBuilder] with a set of rules. + * Build a [TexSpacingBuilder] with a set of rules. */ -fun rules(latexSettings: CommonCodeStyleSettings, init: LatexSpacingBuilder.() -> Unit): LatexSpacingBuilder { - val builder = LatexSpacingBuilder(latexSettings) +fun rules(latexSettings: CommonCodeStyleSettings, init: TexSpacingBuilder.() -> Unit): TexSpacingBuilder { + val builder = TexSpacingBuilder(latexSettings) builder.init() return builder } \ No newline at end of file