使用Merlin2.5.4,在我的项目中打印OCaml文件签名的正确方法是什么?例如,假设我有:
(* foo.ml *)
let x = 1我想要:
val x : int正确的命令(或命令的顺序)是什么?
我试过的是:
我暂时将文件包装在一个子模块:module Foo = struct let x = 1 end中,然后运行:
$ ocamlmerlin
["type","expression","Foo","at",{"line":1,"col":7}]但我明白:
["error",{"start":{"line":1,"col":0},"end":{"line":1,"col":3},"type":"type","sub":[],"valid":true,"message":"Error: Unbound constructor Foo"}]这是有意义的,因为我没有真正提到我要查询的文件,也没有查询( https://github.com/ocaml/merlin/blob/master/doc/dev/OLD-PROTOCOL.md#type-checking )允许我查询的文件。
E我要提到的是,我使用的是BuckleScript,而不是ocamlc,而且ocamlc -i也只能在指定模块及其所有模块依赖项时才能工作;我正在寻找自动管理这些依赖项的东西。
发布于 2017-08-27 04:28:11
让Merlin输出一个模块的推断签名的一种方法是向它提供一系列命令(如它在https://github.com/ocaml/merlin/blob/master/doc/dev/OLD-PROTOCOL.md#type-checking上的协议中所描述的),这些命令首先定义模块,然后请求它的签名。我们可以准备一个包含此命令序列的临时文件,并将其作为标准输入提供给Merlin。
棘手的部分是:用正确的命令包装输入;在输入文件中转义双引号字符,使它们不与Merlin的输入格式混在一起;以及展开输出以丢弃Merlin的协议格式。以下是关键的命令:
it=~/tmp/it
echo '["tell","start","end","module It = struct' >$it
sed 's/"/\\"/g' ${1%i} >>$it
echo ' end let () = ()"]' >>$it
echo '["type","expression","It","at","end"]' >>$it
ocamlmerlin <$it | sed -e '/^\["return",true\]$/d' -e 's/^\["return","sig *//' -e 's/ *end"\]$//' -e 's/\\"/"/g' -e 's/\\n/\
/g' | sed -e '/^ *$/d' -e 's/^ //'注意事项:
touch和sed的Unix-y系统,以及命令路径中的ocamlmerlin。[@...])在https://gist.github.com/yawaramin/b86557ae81cbd019fcb9e071abe594de上可以找到一个更成熟的脚本。
https://stackoverflow.com/questions/45900407
复制相似问题