Skip to content

Commit 5361e14

Browse files
authored
feat(formatter): allow to skip function bodies (#681)
1 parent f2056fc commit 5361e14

6 files changed

Lines changed: 41 additions & 14 deletions

File tree

crates/pgls_configuration/src/format.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ pub struct FormatConfiguration {
101101
/// Data type casing (text, varchar, int): "upper" or "lower". Default: "lower".
102102
#[partial(bpaf(long("type-case")))]
103103
pub type_case: KeywordCase,
104+
/// If `true`, skip formatting of SQL function bodies (keep them verbatim). Default: `false`.
105+
#[partial(bpaf(long("skip-fn-bodies")))]
106+
pub skip_fn_bodies: bool,
104107
/// A list of Unix shell style patterns. The formatter will ignore files/folders that will match these patterns.
105108
#[partial(bpaf(hide))]
106109
pub ignore: StringSet,
@@ -119,6 +122,7 @@ impl Default for FormatConfiguration {
119122
keyword_case: KeywordCase::default(),
120123
constant_case: KeywordCase::default(),
121124
type_case: KeywordCase::default(),
125+
skip_fn_bodies: false,
122126
ignore: Default::default(),
123127
include: Default::default(),
124128
}

crates/pgls_workspace/src/settings.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ fn to_formatter_settings(
376376
keyword_case: conf.keyword_case,
377377
constant_case: conf.constant_case,
378378
type_case: conf.type_case,
379+
skip_fn_bodies: conf.skip_fn_bodies,
379380
ignored_files: to_matcher(working_directory.clone(), Some(&conf.ignore))?,
380381
included_files: to_matcher(working_directory.clone(), Some(&conf.include))?,
381382
})
@@ -566,6 +567,9 @@ pub struct FormatterSettings {
566567
/// Data type casing (text, varchar, int): upper or lower. Default: lower.
567568
pub type_case: KeywordCase,
568569

570+
/// If true, skip formatting of SQL function bodies (keep them verbatim). Default: false.
571+
pub skip_fn_bodies: bool,
572+
569573
/// List of ignored paths/files to match
570574
pub ignored_files: Matcher,
571575

@@ -583,6 +587,7 @@ impl Default for FormatterSettings {
583587
keyword_case: KeywordCase::default(),
584588
constant_case: KeywordCase::default(),
585589
type_case: KeywordCase::default(),
590+
skip_fn_bodies: false,
586591
ignored_files: Matcher::empty(),
587592
included_files: Matcher::empty(),
588593
}

crates/pgls_workspace/src/workspace/server.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -892,24 +892,27 @@ impl Workspace for WorkspaceServer {
892892
let format_candidates: Vec<_> = doc.iter(FormatStatementMapper).collect();
893893
let mut formatted_sql_fn_bodies = HashMap::new();
894894

895-
for (id, _stmt_range, _text, ast_result) in &format_candidates {
896-
if !id.is_child() {
897-
continue;
898-
}
895+
// Only format SQL function bodies if skip_fn_bodies is false
896+
if !settings.formatter.skip_fn_bodies {
897+
for (id, _stmt_range, _text, ast_result) in &format_candidates {
898+
if !id.is_child() {
899+
continue;
900+
}
899901

900-
let Some(parent_id) = id.parent() else {
901-
continue;
902-
};
902+
let Some(parent_id) = id.parent() else {
903+
continue;
904+
};
903905

904-
let Ok(ast) = ast_result else {
905-
continue;
906-
};
906+
let Ok(ast) = ast_result else {
907+
continue;
908+
};
907909

908-
let Ok(result) = pgls_pretty_print::format_statement(ast, &config) else {
909-
continue;
910-
};
910+
let Ok(result) = pgls_pretty_print::format_statement(ast, &config) else {
911+
continue;
912+
};
911913

912-
formatted_sql_fn_bodies.insert(parent_id, result.formatted);
914+
formatted_sql_fn_bodies.insert(parent_id, result.formatted);
915+
}
913916
}
914917

915918
for (id, stmt_range, text, ast_result) in format_candidates {

docs/schema.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,13 @@
534534
"format": "uint16",
535535
"minimum": 0.0
536536
},
537+
"skipFnBodies": {
538+
"description": "If `true`, skip formatting of SQL function bodies (keep them verbatim). Default: `false`.",
539+
"type": [
540+
"boolean",
541+
"null"
542+
]
543+
},
537544
"typeCase": {
538545
"description": "Data type casing (text, varchar, int): \"upper\" or \"lower\". Default: \"lower\".",
539546
"anyOf": [

packages/@postgres-language-server/backend-jsonrpc/src/workspace.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,10 @@ export interface PartialFormatConfiguration {
452452
* Maximum line width before breaking. Default: 100.
453453
*/
454454
lineWidth?: number;
455+
/**
456+
* If `true`, skip formatting of SQL function bodies (keep them verbatim). Default: `false`.
457+
*/
458+
skipFnBodies?: boolean;
455459
/**
456460
* Data type casing (text, varchar, int): "upper" or "lower". Default: "lower".
457461
*/

packages/@postgrestools/backend-jsonrpc/src/workspace.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,10 @@ export interface PartialFormatConfiguration {
452452
* Maximum line width before breaking. Default: 100.
453453
*/
454454
lineWidth?: number;
455+
/**
456+
* If `true`, skip formatting of SQL function bodies (keep them verbatim). Default: `false`.
457+
*/
458+
skipFnBodies?: boolean;
455459
/**
456460
* Data type casing (text, varchar, int): "upper" or "lower". Default: "lower".
457461
*/

0 commit comments

Comments
 (0)