---
# PostgreSQL-style C formatting for Tapir extension
# Optimized for 79-character line limit with proper alignment
BasedOnStyle: LLVM

# Basic formatting
ColumnLimit: 79
IndentWidth: 4
TabWidth: 4
UseTab: Always
ContinuationIndentWidth: 8

# Function formatting (key for PostgreSQL style)
AllowAllParametersOfDeclarationOnNextLine: true
AllowAllArgumentsOnNextLine: true
AlwaysBreakAfterReturnType: AllDefinitions
BinPackArguments: false
BinPackParameters: false
IndentWrappedFunctionNames: false

# Alignment (PostgreSQL style with macro alignment)
AlignAfterOpenBracket: AlwaysBreak
AlignConsecutiveAssignments: Consecutive
AlignConsecutiveDeclarations: Consecutive
AlignConsecutiveMacros: Consecutive
AlignEscapedNewlines: Left
AlignOperands: AlignAfterOperator
AlignTrailingComments: true

# Braces (PostgreSQL style: opening brace on new line)
BreakBeforeBraces: Allman
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false

# Spacing
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false

# Comments and strings
ReflowComments: true
BreakStringLiterals: true

# Pointer alignment
PointerAlignment: Right
DerivePointerAlignment: false

# Include handling (postgres.h first, then system, then local with blank lines)
SortIncludes: CaseSensitive
IncludeBlocks: Regroup
# Disable main file priority to treat all quoted includes equally
IncludeIsMainRegex: '$^'
IncludeCategories:
  - Regex: '^<postgres\.h>'
    Priority: -3
    SortPriority: -3
  - Regex: '^<.*\.h>'
    Priority: 1
    SortPriority: 1
  - Regex: '^".*\.h"'
    Priority: 2
    SortPriority: 2

# PostgreSQL-specific macros
ForEachMacros:
  - foreach
  - forboth
  - for_each_cell
  - for_both_cell
  - forthree
  - MemSet
  - MemSetAligned
  - MemSetLoop

# Switch/case formatting
IndentCaseLabels: false

# Penalty tuning for better line breaks
PenaltyBreakAssignment: 100
PenaltyBreakBeforeFirstCallParameter: 200
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 75

# Other formatting
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 1
CompactNamespaces: false

# Encourage blank lines around function declarations
SeparateDefinitionBlocks: Always
