带有 ocamlformat 的 OCaml 多行字符串会产生丑陋的结果

问题描述 投票:0回答:1

我有这个sql查询来在Sqlite3中构建一个表,我使用的是一个工作正常的多行字符串。

let create_blog_table db =
  let sql =
    "
        CREATE TABLE IF NOT EXISTS blog(
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        author TEXT NOT NULL,
        content TEXT NOT NULL);
    "
  in
  match exec db sql with Rc.OK -> () | _ -> failwith "Error creating table"

但是当我运行

dune fmt
(安装了 ocamlformat)时,结果是这样的:

let create_blog_table db =
  let sql =
    "\n\
    \        CREATE TABLE IF NOT EXISTS blog(\n\
    \        id INTEGER PRIMARY KEY AUTOINCREMENT,\n\
    \        author TEXT NOT NULL,\n\
    \        content TEXT NOT NULL);\n\
    \    "
  in
  match exec db sql with Rc.OK -> () | _ -> failwith "Error creating table"

出于某种原因,这种格式是否必要或可取?

如果没有,我可以告诉它不要这样做吗?

我找到了规则

break-string-literals
,但只允许
auto
never

.ocamlformat

profile = default 
version = 0.26.0

break-string-literals=auto
ocaml ocamlformat
1个回答
0
投票

never
设置为
--break-string-literals
会保留您的格式。

wtd@DESKTOP-1R9FKRK:/mnt/c/Users/rubyg$ cat > test.ml
let x = "
  hello
  world
"

let () = print_endline x
$ ocamlformat --enable-outside-detected-project test.ml
let x = "\n  hello\n  world\n"
let () = print_endline x
$ cat > test.ml
let x = "
  hello
  world
"

let () = print_endline x
$ ocamlformat --enable-outside-detected-project --break-string-literals=never test.ml
let x = "
  hello
  world
"

let () = print_endline x
$

就您的示例而言,将

--break-string-literals
设置为
auto
,可以保持缩进。这似乎没有必要,因为您的代码一开始就已经缩进了。

© www.soinside.com 2019 - 2024. All rights reserved.