Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #1027 #1168

Merged
merged 2 commits into from
Jan 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
PHPirates marked this conversation as resolved.
Show resolved Hide resolved

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
}