Quickly viewing OCaml function documentation

I’m new to OCaml and was previously accustomed to man open (C) or go doc os open (Go) to view documentation for a specific function. It looks like maybe ocp-index print is the OCaml equivalent. I wrote a shell function to make the UI more to my liking. Am I missing a better tool than ocp-index?

Here’s my shell function, in case anyone’s interested:

od() {
    usage="usage: od [-s] identifier"
    if [ $# -eq 0 ]; then
        echo "$usage"
        return 1
    elif [ "$1" = "-s" ]; then
        shift
        if [ $# -eq 0 ]; then
            echo "$usage"
            return 1
        fi
        tmp=$(mktemp -t od-XXXXXX)
        if ! ocp-index locate "$1" >"$tmp"; then
            echo "Found nothing"
            return 1
        fi
        IFS=: read path line column <"$tmp"
        uctags --language-force=OCaml -f - "$path" >"$tmp"
        less -T "$tmp" +$line "$path"
        rm "$tmp"
    else
        ocp-index print "$1"
    fi
}

Usage: od open_in shows the documentation for opening a file. od -s open_in lets me read the source in a pager. After seeing that open_in is a wrapper around open_in_gen, I type -t open_in_gen (in less) to jump to the definition. So far, Universal Ctags seems pretty accurate for OCaml.

3 Likes

If you use GNU+Emacs and use Merlin for type information, the merlin emacs package defines a function merlin-document which brings up the documentation for a given function in a separate buffer. For some reason this isn’t bound by default, but I find C-c C-h is a good option for quick usage:

       (bind-key (kbd "C-c C-h") #'merlin-document tuareg-mode-map)
2 Likes

Note that stdlib modules are available with man. To have the doc for open_in you can do man Stdlib. For another module it’s the same, e.g. man Ephemeron.

4 Likes

obligatory ‘Vim-has-it-too’ comment: in Vim it’s :MerlinDocument. Also, in case you have not found that yet, odig provides browsable API docs. Install it by opam, then odig doc generates local html documentation which should open in your browser with a global index of installed packages. odig doc <package> is more direct. Some people like to use CLI browsers to view odig documentation.

1 Like

I do use emacs (somewhat grudgingly because of its bloat), so I may try Merlin. Thanks for the tip.

2 Likes

I spent some time today using Merlin integration in emacs. I’m a convert: it was surprisingly snappy and productive. Thanks again @Gopiandcode and @n4323 for the helpful suggestions.

Also, in case these keywords help others locate this topic later, the tools in this thread are also a good substitute if you’re accustomed to commands like pydoc open or perldoc -f open.

3 Likes