Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Command to provide information for OCaml syntax #1706

Merged
merged 75 commits into from
Feb 8, 2024

Conversation

PizieDust
Copy link
Contributor

@PizieDust PizieDust commented Nov 15, 2023

closes #1692

FEATURE: New Merlin Command - Syntax Document

Introduction

The Document command of Merlin returns comments written in code. This PR introduces a new command, the Syntax Document command which takes the syntax under the cursor, analyses it's TypedTree nodes and returns specific documentation for the Syntax.

Code Example

Code

type point = { x:int; y:int }

Output

{
   "name": "Record types",
   "description": "Allows you to define variants with a fixed set of fields, and all of the constructors for a record variant type must have the same fields",
   "url": "https://v2.ocaml.org/manual/extensiblevariants.html"
}

This command can also identify specific

Code

type point = private { x:int; y:int }

Output

{
   "name": "Private Record Types",
   "description": "Values of a record type declared private can be de-structured via the expr . field notation. However, values of these types cannot be constructed directly by record construction.",
   "url": "https://v2.ocaml.org/releases/5.1/htmlman/privatetypes.html#ss:private-types-variant"
}

Scope

This command for a start covers Language Extensions of OCaml. The covered sections include:

  • Recursive definition of values
  • Recursive modules
  • Recovering the type of a module
  • Signature Substitutions (Destructive substitutions, Local substitutions)
  • Extensible Variant types
  • Variant types
  • Abstract types
  • Record types
  • Private types
  • Empty variant types
  • Locally abstract data types
  • Bigarray access
  • Type-level module aliases
  • Overriding in open statements
  • Generalized algebraic datatypes
  • Attributes
  • Generative functors
  • Extension operators **
  • Inline records ==== extensible variants
  • Alerts
  • Binding operators

More sections will be covered and updated to the command along with other parts of the OCaml programming language.

cc @voodoos @pitag-ha

src/analysis/syntax_doc.ml Outdated Show resolved Hide resolved
src/frontend/ocamlmerlin/query_json.ml Outdated Show resolved Hide resolved
src/frontend/query_commands.ml Outdated Show resolved Hide resolved
src/frontend/query_commands.ml Outdated Show resolved Hide resolved
src/frontend/query_commands.ml Outdated Show resolved Hide resolved
@PizieDust PizieDust changed the title add new command boilerplate [NEW FEATURE] Add documentation for OCaml syntax Nov 22, 2023
@PizieDust PizieDust changed the title [NEW FEATURE] Add documentation for OCaml syntax [NEW FEATURE] Add command to provide information for OCaml syntax Nov 22, 2023
Copy link
Collaborator

@voodoos voodoos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's much better !

Now is probably a good time to decide which features we actually want to document.

There are two possibilities here:

  • If the feature is aimed at complete beginner, it might make sense to document as much of the syntax as we can.
  • Or we just want to shed a light on weird / rarely use OCaml features that might puzzle even seasoned developers.

@trefis, @pitag-ha, what's your feeling about this ?

It might also depend on how the feature is used: is it called on every on-hover in LSP ? Or only on specific code actions ?

src/analysis/syntax_doc.ml Outdated Show resolved Hide resolved
@voodoos
Copy link
Collaborator

voodoos commented Nov 23, 2023

@PizieDust we discussed with trefis and Sonja and for the scope of the feature we think you can first focus on "language extensions". These are advanced features that were added to OCaml other the time and are often not well known by beginners.

You can find the list of these features here:
https://v2.ocaml.org/releases/5.1/htmlman/extn.html

About the UI, we can start (after the backend part in Merlin is done) by extending hover with the new information and then make it configurable. (And in fact, rework the hover command in general, it could be more useful than it is now in some situations).

@PizieDust
Copy link
Contributor Author

Amazing. I should be moving this PR from draft soon

Copy link
Collaborator

@voodoos voodoos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a (very incomplete) round of review: could you reformat the file (I provided the result of ocamlformating it in a comment), and refactor / promote the result of the tests so that they can be used to understand the expected behavior ?

src/frontend/query_protocol.ml Outdated Show resolved Hide resolved
src/analysis/syntax_doc.ml Outdated Show resolved Hide resolved
Copy link
Member

@pitag-ha pitag-ha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is super cool, @PizieDust , thanks a lot!

Are you planning to add an mli-file for syntax_doc.ml once you won't change that file that much anymore?

src/analysis/syntax_doc.ml Outdated Show resolved Hide resolved
src/analysis/syntax_doc.ml Outdated Show resolved Hide resolved
@PizieDust PizieDust changed the title [NEW FEATURE] Add command to provide information for OCaml syntax [FEATURE] Command to provide information for OCaml syntax Dec 15, 2023
Copy link
Collaborator

@voodoos voodoos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, you're making good progress on this :-)

I made a few comments, and you should also try to sort out the rebase mess :-)

src/analysis/syntax_doc.ml Outdated Show resolved Hide resolved
src/analysis/syntax_doc.ml Outdated Show resolved Hide resolved
src/frontend/query_protocol.ml Outdated Show resolved Hide resolved
src/analysis/syntax_doc.ml Outdated Show resolved Hide resolved
src/analysis/syntax_doc.ml Outdated Show resolved Hide resolved
Copy link
Collaborator

@voodoos voodoos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I think we just need to improve the locally abstract data types case before merging a first version of this PR.

src/analysis/syntax_doc.ml Outdated Show resolved Hide resolved
Copy link
Collaborator

@voodoos voodoos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the PR is almost ready.
I just made a few formatting remarks.

Do you want to rebase and squash your changes into meaningful commits ?
If not I can simply squash everything before merging.

src/frontend/ocamlmerlin/query_json.ml Outdated Show resolved Hide resolved
src/frontend/query_commands.ml Show resolved Hide resolved
CHANGES.md Outdated Show resolved Hide resolved
@voodoos voodoos added this to Planned for backporting in Backport to 501 via automation Feb 2, 2024
@voodoos voodoos merged commit 0f64255 into ocaml:master Feb 8, 2024
1 check passed
Backport to 501 automation moved this from Planned for backporting to Ready for backporting Feb 8, 2024
@voodoos voodoos moved this from Ready for backporting to 4.14-500 in Backport to 501 Feb 20, 2024
voodoos added a commit to voodoos/merlin that referenced this pull request Feb 20, 2024
* add new command boilerplate

* add identifier to syntax_doc definition

* update query json to use new identifier

* include new return types for query protocol

* update test to check type variant declarations

* syntax documentation boiler plate

* poc implementation for syntax_doc command

* update the test description

* remove optional identifier and obsolete code

* remove optional identifier

* add new variant examples

* handle type declarations

* remove redundant parent_node matching

* remove comments

* Bump version for release 4.13

* delete intial testing file

* add custom documentation

* add language extension tests

* refined node matching for better docs

* add more tests

* remove invalid_identifier ouput

* ocamlformat, limit lines to 80 chars

* add mli for syntax_doc

* proper naming

* update docs, move type to query_protocol, leave as record

* add info type for syntax_docment command json

* update to record for json output

* change from string output to json

* delete redundant test files

* delete tests files

* move tests code here and promote

* remove typedtree nodes

* change variable name to a more informative name

* Make syntax_doc_result optional

* remove redundant cases

* use singular and more shorter names

* dune promote name changes

* lint

* url builder function for syntax documentation url

* lint

* dune promote correct urls

* concat urls before returning to query_json

* make command return record option

* delete test file

* correct formatting to original

* refactor to be more meaningful

* use versbose names

* test: start making more precise tests

* Apply suggestions from code review

Co-authored-by: Ulysse <5031221+voodoos@users.noreply.github.com>

* Edit descriptions to be less verbose

* dune promote description changes

* use syn_doc alias

* merge duplicate case results

* lint

* reduce verboseness in test

* add eof

* Update src/frontend/ocamlmerlin/new/new_commands.ml

Co-authored-by: Ulysse <5031221+voodoos@users.noreply.github.com>

* cover more test

* update some match cases

* remove trailing whitespaces

* use plural form

* refactor private and public types for same nodes into one match case

* lint and seperate abstract types for public and private

* better targeting first class modules

* more test for first class modules and capitalization corrections

* pass location position to syntaxdoc logic

* use cursor position to better target locally abstract datatypes

* test case where locally abstract dt shouldnt be triggered

* test for first class module where it shouldnt be triggered

* use 3rd person singular

* add changelog

* fix indentation

* remove excess whitespace

* proper heading

---------

Co-authored-by: Ulysse Gérard <thevoodoos@gmail.com>
Co-authored-by: Ulysse <5031221+voodoos@users.noreply.github.com>
voodoos added a commit to voodoos/opam-repository that referenced this pull request Feb 22, 2024
CHANGES:

Thu Feb 22 14:00:42 CET 2024

  + merlin binary
    - Add a "heap_mbytes" field to Merlin server responses to report heap usage (ocaml/merlin#1717)
    - Add cache stats to telemetry (ocaml/merlin#1711)
    - Add new SyntaxDocument command to find information about the node under the cursor (ocaml/merlin#1706)
    - Fix `FLG -pp ppx.exe -as-pp/-dump-ast` use of invalid shell redirection when
    direct process launch on Windows. (ocaml/merlin#1723, fixes ocaml/merlin#1722)
    - Add a query_num field to the `ocamlmerlin` responses to detect server crashes (ocaml/merlin#1716)
    - Jump to cases within a match statement (ocaml/merlin#1726)
    - Jump to `module-type` (ocaml/merlin#1728, partially fixes ocaml/merlin#1656)
    - Exposes stable functions for configuration handling and pattern variable
      destruction. (ocaml/merlin#1730)
  + editor modes
    - vim: load merlin under the ocamlinterface and ocamllex filetypes (ocaml/merlin#1340)
    - Fix merlinpp not using binary file open (ocaml/merlin#1725, fixes ocaml/merlin#1724)
voodoos added a commit to voodoos/opam-repository that referenced this pull request Feb 22, 2024
CHANGES:

Thu Feb 22 14:00:42 CET 2024

  + merlin binary
    - Add a "heap_mbytes" field to Merlin server responses to report heap usage (ocaml/merlin#1717)
    - Add cache stats to telemetry (ocaml/merlin#1711)
    - Add new SyntaxDocument command to find information about the node under the cursor (ocaml/merlin#1706)
    - Fix `FLG -pp ppx.exe -as-pp/-dump-ast` use of invalid shell redirection when
    direct process launch on Windows. (ocaml/merlin#1723, fixes ocaml/merlin#1722)
    - Add a query_num field to the `ocamlmerlin` responses to detect server crashes (ocaml/merlin#1716)
    - Jump to cases within a match statement (ocaml/merlin#1726)
    - Jump to `module-type` (ocaml/merlin#1728, partially fixes ocaml/merlin#1656)
    - Exposes stable functions for configuration handling and pattern variable
      destruction. (ocaml/merlin#1730)
  + editor modes
    - vim: load merlin under the ocamlinterface and ocamllex filetypes (ocaml/merlin#1340)
    - Fix merlinpp not using binary file open (ocaml/merlin#1725, fixes ocaml/merlin#1724)
voodoos added a commit to voodoos/opam-repository that referenced this pull request Feb 22, 2024
CHANGES:

Thu Feb 22 14:00:42 CET 2024

  + merlin binary
    - Add a "heap_mbytes" field to Merlin server responses to report heap usage (ocaml/merlin#1717)
    - Add cache stats to telemetry (ocaml/merlin#1711)
    - Add new SyntaxDocument command to find information about the node under the cursor (ocaml/merlin#1706)
    - Fix `FLG -pp ppx.exe -as-pp/-dump-ast` use of invalid shell redirection when
    direct process launch on Windows. (ocaml/merlin#1723, fixes ocaml/merlin#1722)
    - Add a query_num field to the `ocamlmerlin` responses to detect server crashes (ocaml/merlin#1716)
    - Jump to cases within a match statement (ocaml/merlin#1726)
    - Jump to `module-type` (ocaml/merlin#1728, partially fixes ocaml/merlin#1656)
    - Exposes stable functions for configuration handling and pattern variable
      destruction. (ocaml/merlin#1730)
  + editor modes
    - vim: load merlin under the ocamlinterface and ocamllex filetypes (ocaml/merlin#1340)
    - Fix merlinpp not using binary file open (ocaml/merlin#1725, fixes ocaml/merlin#1724)
voodoos added a commit to voodoos/opam-repository that referenced this pull request Feb 22, 2024
CHANGES:

Thu Feb 22 14:00:42 CET 2024

  + merlin binary
    - Add a "heap_mbytes" field to Merlin server responses to report heap usage (ocaml/merlin#1717)
    - Add cache stats to telemetry (ocaml/merlin#1711)
    - Add new SyntaxDocument command to find information about the node under the cursor (ocaml/merlin#1706)
    - Fix `FLG -pp ppx.exe -as-pp/-dump-ast` use of invalid shell redirection when
    direct process launch on Windows. (ocaml/merlin#1723, fixes ocaml/merlin#1722)
    - Add a query_num field to the `ocamlmerlin` responses to detect server crashes (ocaml/merlin#1716)
    - Jump to cases within a match statement (ocaml/merlin#1726)
    - Jump to `module-type` (ocaml/merlin#1728, partially fixes ocaml/merlin#1656)
    - Exposes stable functions for configuration handling and pattern variable
      destruction. (ocaml/merlin#1730)
  + editor modes
    - vim: load merlin under the ocamlinterface and ocamllex filetypes (ocaml/merlin#1340)
    - Fix merlinpp not using binary file open (ocaml/merlin#1725, fixes ocaml/merlin#1724)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

Feature: provide information about the syntax
3 participants