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

feat(dhall-docs): add jump-to-definition support for record-like expressions #1991

Merged
merged 33 commits into from
Aug 17, 2020

Conversation

german1608
Copy link
Collaborator

(this is a quite big PR, please read the full description of this PR before reviewing the code)

Summary

This PR adds JTD support for record-like expressions. dhall-docs now tries to infer (really simple, not like Dhall.TypeCheck does, for example) the type of a dhall expression. As a result of this, the JTD-related features on record-like expressions are:

  • Fields on field-access expressions jumps to the definition of that field in the source code, where the left side of that expression was defined;
  • Deep field expressions are also supported (see JumpToRecordFieldWhenVarIsOfRecordTypeDeep test case);
  • Fields that use dot-syntax are correctly highlighted depending on the accessed field;
  • Punned entries are handled without jumping to the definition of that entry, but it's enough I think;
  • It supports transitivity of expressions, which allows jumping to the real definition of an expression if dhall-docs is able to keep the inferred type in a transitivity chain.

You can check the written test cases to see more specific uses, and also you can suggest more test cases as well.

How to review this

This work is divided into two parts:

  1. Modifications of how we preserve whitespace on the AST:
    1. The addition of the FieldAccess data-type for preserving whitespace on Field expression
    2. The correct whitespace preservation on dot-syntax labels, which is documented in the RecordField haddock
    3. Modify the rest of the repository packages to catch-up the change on Field
  2. Modifications on how the source code on rendered in fragments:
    • We have a DhallType ADT to aim a simple type-inference algorithm.
    • Now we use a Writer [SourceCodeFragment] DhallType. Generating [SourceCodeFragment] for an expression will return the inferred 'DhallType'

The part 1 generates a lot of noise on the PR, but I was modifying the parser as I was developing record-JTD since some changes were really specific for dhall-docs. If you need to, I can split (not so easy, though) the dhall work in a separate PR that should be merged before this one.

Copy link
Collaborator

@sjakobi sjakobi left a comment

Choose a reason for hiding this comment

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

@german1608, can you show us some docs that show off the new features?

I have a few comments on the changes to dhall for now. I'm not done with the parser changes yet. I think it would be good if @Gabriel439 would take a closer look at these too.

dhall/src/Dhall/Syntax.hs Outdated Show resolved Hide resolved
dhall/src/Dhall/Syntax.hs Outdated Show resolved Hide resolved

data RecursiveType a = RecursiveType (RecursiveType a)
newtype RecursiveType a = RecursiveType (RecursiveType a)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I assume that this was changed by stylish-haskell?!

@Gabriel439, could you check that this is OK?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I changed that manually, I have a TOC that males me change to newtype when possible :) I can rollback the change, though

dhall/src/Dhall/Syntax.hs Outdated Show resolved Hide resolved
dhall/src/Dhall/Syntax.hs Outdated Show resolved Hide resolved
dhall/src/Dhall/Parser/Expression.hs Outdated Show resolved Hide resolved
dhall/src/Dhall/Normalize.hs Outdated Show resolved Hide resolved
@german1608
Copy link
Collaborator Author

@german1608, can you show us some docs that show off the new features?

I'm unable to provide specific examples right now since I'm not home, but you can check the generated docs for the test package and the prelude. renderAs on Prelude/JSON has a lot of stuff

@german1608
Copy link
Collaborator Author

@german1608, can you show us some docs that show off the new features?

I'm unable to provide specific examples right now since I'm not home, but you can check the generated docs for the test package and the prelude. renderAs on Prelude/JSON has a lot of stuff

On the Prelude there isn't a lot of examples, some JTD features can be seen in the Prelude/JSON subpackage.

The following examples on the test packages shows off the new features:

...or you can manually check on the index package on all the examples that starts with JumpToRecordFieldWhen...

dhall/src/Dhall/Parser/Expression.hs Outdated Show resolved Hide resolved
dhall/src/Dhall/Syntax.hs Outdated Show resolved Hide resolved
dhall/src/Dhall/Syntax.hs Outdated Show resolved Hide resolved
dhall/src/Dhall/Normalize.hs Outdated Show resolved Hide resolved
dhall/src/Dhall/Parser/Expression.hs Outdated Show resolved Hide resolved
Copy link
Collaborator

@sjakobi sjakobi left a comment

Choose a reason for hiding this comment

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

In https://hydra.dhall-lang.org/build/69960/download/1/docs/JSON/renderAs.dhall.html I noticed that every single label in a record literal seems to be highlighted. But when I hover over these or click them, nothing interesting happens. What's the reason for highlighting these labels?

@german1608
Copy link
Collaborator Author

In https://hydra.dhall-lang.org/build/69960/download/1/docs/JSON/renderAs.dhall.html I noticed that every single label in a record literal seems to be highlighted. But when I hover over these or click them, nothing interesting happens. What's the reason for highlighting these labels?

That is because the implementation doesn't recognize where a record (literal or type) is stored and in any case it renders the highlight. On this moment, we recognize when a record literal has been bound to a let binding and when a lambda name is of record-type, but the rest of the cases are not handled.

I guess that we could remove the highlight on unused names, although when handling imported names (as I explained here) it would get messy. Something that a couple of tests doesn't solve.

@german1608
Copy link
Collaborator Author

I'll be quite bussy today, so I'll address remaining comments this night.

@sjakobi
Copy link
Collaborator

sjakobi commented Aug 15, 2020

I guess that we could remove the highlight on unused names, although when handling imported names (as I explained here) it would get messy. Something that a couple of tests doesn't solve.

OK. Currently it seems to me that there's not much point in highlighting things that don't offer more information in the same view. So when a label in one file is only used in other files, I think it's probably not worth highlighting in the first file.

Maybe this issue could also be addressed by using a more sophisticated highlighting approach that uses different visual cues instead of the current one-size-fits-all design with the underlining. I'm not sure.

I noticed that some types in the index pages also contain highlighting, e.g. in https://hydra.dhall-lang.org/build/70067/download/1/docs/List/index.html. Can these labels be jumped to from anywhere?

dhall-docs/src/Dhall/Docs/CodeRenderer.hs Outdated Show resolved Hide resolved
dhall-docs/src/Dhall/Docs/CodeRenderer.hs Show resolved Hide resolved
dhall-docs/src/Dhall/Docs/CodeRenderer.hs Outdated Show resolved Hide resolved
@german1608
Copy link
Collaborator Author

OK. Currently it seems to me that there's not much point in highlighting things that don't offer more information in the same view. So when a label in one file is only used in other files, I think it's probably not worth highlighting in the first file.

I see. Then let me do some work to remove the unused highlights. It shouldn't be difficult

@german1608
Copy link
Collaborator Author

@sjakobi Alright, I've applied your suggestions and I removed the highlighting on unused names. Note that unused let-bindings are not highlighted as well. Check this test case

A thing that I'd like to do is to rename var to name when possible since I feel that var is more specific to let-bindings, but I'll do it in a follow-up PR to not add more noise here

Copy link
Collaborator

@sjakobi sjakobi left a comment

Choose a reason for hiding this comment

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

LGTM apart from a few more wibbles.

Great job, @german1608! 👍

dhall-docs/src/Dhall/Docs/CodeRenderer.hs Outdated Show resolved Hide resolved
dhall-docs/src/Dhall/Docs/CodeRenderer.hs Outdated Show resolved Hide resolved
dhall-docs/src/Dhall/Docs/CodeRenderer.hs Outdated Show resolved Hide resolved
dhall-docs/src/Dhall/Docs/CodeRenderer.hs Outdated Show resolved Hide resolved
@german1608
Copy link
Collaborator Author

Great job, @german1608!

Thanks for your good reviews!

@mergify mergify bot merged commit 8d89b44 into master Aug 17, 2020
@mergify mergify bot deleted the feat/jump-to-record-labels branch August 17, 2020 03:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants