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

Syntax: Don't highlight the LHS of type decl as types #81

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

Julow
Copy link
Contributor

@Julow Julow commented Jul 19, 2022

I would expect ocamlTypeConstr to apply only to type constructors
within type expressions, not to the identifier after type in:

type foo = 'a bar list

foo is not highlighted with:

hi link ocamlTypeConstr Type

This makes the LHS of types, exceptions and constraints be matched as
ocamlTypedef, which is not highlighted by default.

Variance and type variables on the LHS remain highlighted as before.

@Julow
Copy link
Contributor Author

Julow commented Jul 19, 2022

Ping @Maelan

@Maelan
Copy link
Contributor

Maelan commented Jul 19, 2022

Thanks @Julow! I’m fine either way. I’m just slightly concerned by adding complexity to that already complex piece, but I let our maintainers judge.

You’d also have to deal with type t = ... and u = ..., though.

syntax/ocaml.vim Outdated Show resolved Hide resolved
\ matchgroup=ocamlTypedef end="\<\(\w\|'\)*\>"
\ contains=@ocamlAllErrs,ocamlComment,ocamlTypeVariance,ocamlTypeVar
\ skipwhite skipempty
\ nextgroup=ocamlTypeDefImpl
Copy link
Contributor

Choose a reason for hiding this comment

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

I note that with this technique, there must be no comment (* *) between the defined type name and the equal sign =, which may be acceptable (we already have that limitation elsewhere).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't see this. Perhaps because the end regex don't contain \@= ?

Copy link
Contributor

Choose a reason for hiding this comment

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

I haven’t actually tested your code, but from what I understand about nextgroup= and contained, the ocamlTypeDefImpl group will never be tried except immediately after the end of the ocamlTypeDef group.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is what skipwhite is doing, from the :h:

These arguments are only used in combination with "nextgroup".	They can be
used to allow the next group to match after skipping some text:
	skipwhite	skip over space and tab characters
	skipnl		skip over the end of a line
	skipempty	skip over empty lines (implies a "skipnl")

I took thought this was doing something between start and end and nothing outside but it's actually the opposite, they take effect after end and do nothing inside the region (whitespace are skipped by default it seems).

@rgrinberg rgrinberg requested a review from copy July 20, 2022 02:15
@Julow
Copy link
Contributor Author

Julow commented Jul 20, 2022

I fixed the problems with and and restored the highlighting of exception. module-rec-and (which are broken on master) and let-and are not altered.

Comparison before/after with hi link ocamlTypeConstr Type:

image

@copy
Copy link
Collaborator

copy commented Jul 20, 2022

Fundamentally this change is fine, but I think we leave the current behavior configurable (personally, I prefer the current behavior).

This seems to break some things, in particular (from type-linter-test.ml):

  type ('a, 'b) t = 'a list * ('a, 'b) result (* the left pair of parenthesis *)
  type nonrec 'a o = 'a option = private None | Some of 'a (* the second equal sign *)
  type t += C of int (* the of *)

The module-rec-and find is interesting, would you mind adding it to type-linter-test.ml with a fixme?

@Julow
Copy link
Contributor Author

Julow commented Jul 21, 2022

I missed type-linter-test.ml, thanks.

I've added a way to get back the current behavior, what should be the default ? This is governed by hi link ocamlTypeIdentifier x which could be ocamlLCIdentifier (new) or ocamlTypeConstr (previous).

I've fixed the bugs with type params and equal signs but I introduced new bugs around let-bindings, which are matched as type decl. I would like ocamlTypeDefImpl to take effect only when invoked with nextgroup, is here a way to do that ? Otherwise, we need stronger rules for let-bindings.

Julow added 6 commits June 8, 2023 16:20
I would expect 'ocamlTypeConstr' to apply only to type constructors
within type expressions, not to the identifier after 'type' in:

    type foo = 'a bar list

This makes the LHS of types, exceptions and constraints be matched as
'ocamlTypedef', which is not highlighted by default.

Variance and type variables on the LHS remain highlighted as before.
Have stricter rules for the identifiers and allow exception constructors
to be highlighted as before.
A new region is used to avoid matching the 'and' keyword too often (that
would interfere with let-and and module-rec-and.
Fix tupled type params, += and whitespaces around keywords.
Add examples to the test file.
Using the commit date instead of the author date.
@Julow Julow force-pushed the syn_type_decl_lhs branch from cebc78e to 921eed7 Compare June 8, 2023 14:27
@Julow
Copy link
Contributor Author

Julow commented Jun 8, 2023

Any opinion on this ?

Here's how it can be used:

hi link ocamlTypeConstr Type
hi link ocamlTypeIdentifier Identifier

The first line is both before and after this PR. The second line is with the two line above applied:

vimocamlshot

@rgrinberg
Copy link
Member

@copy do you want to make the call on this?

@copy
Copy link
Collaborator

copy commented Jun 9, 2023

This change and the mechanism to configure it looks good.

However, it causes some problems with the highlighting of number literals (highlighted as error across the type-linter-test.ml file), the fun keyword (highlighted like a type on line 332 and 338) and comments (a correct type highlighting is removed from line 349). You can use vim's :TOhtml followed by diff (or vimdiff, patdiff, ...) to check.

Julow added 2 commits June 20, 2023 11:37
It's a `contained` region but was not added to `ocamlContained`.
This is a styling change.
@Julow
Copy link
Contributor Author

Julow commented Jun 20, 2023

Thanks for the review! I fixed this issue, it was a missing ocamlContained declaration. I also changed my editor setup to use this patch everyday to be sure to test it well.

@copy
Copy link
Collaborator

copy commented Jun 26, 2023

Thanks! Super-minor, but it still removes the type highlighting from the following (on the int):

  type t (*c*) =  int

@Julow
Copy link
Contributor Author

Julow commented Jun 27, 2023

Good catch! I'm failing to fix it.

The ocamlTypeDef region ends at t and jump into the ocamlTypeDefImpl region, which starts at = using nextgroup. I couldn't find a way to accept the comment without interfering with nextgroup, which is very handy to handle types without RHS and and types.
Any idea ?

@copy
Copy link
Collaborator

copy commented Jun 27, 2023

This comment from @Maelan looks relevant: #81 (comment)

@Julow
Copy link
Contributor Author

Julow commented Jul 3, 2023

Sorry for the slow reply. I managed to handle the comment at this location while keeping nextgroup working for and types.

@copy
Copy link
Collaborator

copy commented Jul 4, 2023

The latest commit seems to have broken the highlighting of identifiers in some places (e.g. the f in line 49 is recognized as a type). You can use :TOhtml and diff to find any differences between this branch and master.

@Julow
Copy link
Contributor Author

Julow commented Jul 5, 2023

I think I managed to fix the identifier issue. The TOhtml diff seems fine now. The added Error class is due to a newly added test case.

@copy
Copy link
Collaborator

copy commented Jul 6, 2023

On the last commit (817e8f7), some highlights are still incorrect (e.g. the let f on line 49), and show up in the diff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants