Skip to content

Commit

Permalink
Merge pull request #1168 from Hannah-Sten/bibtex-formatting
Browse files Browse the repository at this point in the history
Fixes #1027
  • Loading branch information
PHPirates authored Jan 9, 2020
2 parents 0efc51f + 994b229 commit 0db806e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/nl/hannahsten/texifyidea/formatting/BibtexBlock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Block> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ open class BibtexFormattingModelBuilder : FormattingModelBuilder {
element.node,
Wrap.createWrap(WrapType.NONE, false),
Alignment.createAlignment(),
createSpacingBuilder(settings)
createBibtexSpacingBuilder(settings)
),
settings
)!!
Expand Down
37 changes: 37 additions & 0 deletions src/nl/hannahsten/texifyidea/formatting/BibtexSpacingRules.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}
2 changes: 1 addition & 1 deletion src/nl/hannahsten/texifyidea/formatting/LatexBlock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Builder>()

Expand Down Expand Up @@ -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
}

0 comments on commit 0db806e

Please sign in to comment.