Skip to content
This repository has been archived by the owner on Jan 2, 2021. It is now read-only.

Support extending constructors #916

Merged
merged 9 commits into from
Dec 6, 2020
Merged

Conversation

berberman
Copy link
Contributor

Perhaps we no longer need to parse the extended binding, completely depending on the diagnostic message, since we now have ExportsMap. Closes haskell/haskell-language-server#609:
2020-11-20 11-12

@jneira
Copy link
Member

jneira commented Nov 20, 2020

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@pepeiborra
Copy link
Collaborator

Thank you for this change, this was so annoying!
Do we need a better test?

Could you undo all the auto formatting? It will lead to unnecessary merge conflicts for other contributions, and its best done by maintainers when the time is right. Thank you!

@berberman
Copy link
Contributor Author

Sorry for that, I've restored changes done by the formatter.
As for tests, hmmm, I don't have a good idea. Old tests can pass after the changes. Should I add the case in the screenshot?

@pepeiborra
Copy link
Collaborator

Yes, I think encoding the screenshot as a test would be good enough. Thanks!

@berberman
Copy link
Contributor Author

I have a question about this test case,

,  (`xfail` "known broken") $ testSession "extend single line import with constructor" $ template
      (T.unlines
            [ "module ModuleA where"
            , "data A = Constructor"
            ])
      (T.unlines
            [ "module ModuleB where"
            , "import ModuleA (A)"
            , "b :: A"
            , "b = Constructor"
            ])
      (Range (Position 2 5) (Position 2 5))
      "Add Constructor to the import list of ModuleA"
      (T.unlines
            [ "module ModuleB where"
            , "import ModuleA (A(Constructor))"
            , "b :: A"
            , "b = Constructor"
            ])

which is marked as "known broken". Expected failure here means "user should not write code like that" or "currently we don't support this feature"?

@pepeiborra
Copy link
Collaborator

That test case is marked as known broken because it reproduces a known bug that needs to be fixed. Which your PR does

@berberman
Copy link
Contributor Author

Now we can extend () to (A(Constructor)), but not (A) to (A(Constructor)). So this test is failing, breaking the CI, since the reult becomes (A(Constructor),A). The later one could be implemented, however, I don't think the user would add the constructor to the import list without the parent like this...

@berberman berberman marked this pull request as draft November 25, 2020 03:58
@berberman berberman changed the title Use exports map in suggestExtendImport Support extending constructors Nov 25, 2020
@berberman
Copy link
Contributor Author

berberman commented Nov 25, 2020

The test which was marked as "known broken" previously can pass now, and I added a new test to demonstrate extending constructors. However, current implementation breaks the "extend multi line import with value", because spaces are dropped before removing the trailing comma, causing multi line collapsed. This can be fixed by removing the comma in the end of the import list but keeping spaces.

@berberman berberman marked this pull request as ready for review November 25, 2020 07:42
@berberman berberman requested a review from pepeiborra November 25, 2020 08:39
Comment on lines +1116 to +1125
let rest' = case parent of
"" -> ", " <> rest
_ -> case T.breakOn parent rest of
(h, T.stripPrefix parent -> Just r) -> case T.uncons (T.dropWhile isSpace r) of
Just (')', _) -> ")" <> h <> r
Just ('(', xs) -> let imported = T.takeWhile (/= ')') xs in T.concat ["," ,imported , "), " , h , removeHeadingComma (T.tail (T.dropWhile (/= ')') r))]
_ -> "), " <> h <> r
_ -> "), " <> rest
binding' = (if T.null parent then id else T.init) renderedBinding
in removeTrailingComma $ T.concat [pre, "(", binding', rest']
Copy link
Collaborator

@pepeiborra pepeiborra Nov 25, 2020

Choose a reason for hiding this comment

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

I find this code very hard to read. Could you try to golf it a bit to make it more readable? Some concrete suggestions:

  • whitespace and layout
  • both rest' and binding' perform branching on whether parent is null. Make this more apparent
  • introduce more names or add more comments, there's a lot of character matching that is hard to follow
  • consider replacing some of the character matching with regexes if that makes things simpler.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for your suggestions! I totally agree. It lacks of extensibility and is hard to read 😢

Copy link
Collaborator

@pepeiborra pepeiborra Dec 5, 2020

Choose a reason for hiding this comment

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

@berberman are you planning to implement this suggestion?

@adamse
Copy link
Contributor

adamse commented Dec 5, 2020

Anything I can do to help move this forward? :)

@berberman
Copy link
Contributor Author

Sorry for the delay; I‘ve been pretty busy recently...
I may be free next weekend, and helps are welcome :)
Current implementation works but is ugly, so my plan is something like handling two branches (whether the thing we are going to extend is a constructor of a data type) separately, and using regexs or parsers to improve the readability and robustness.

@pepeiborra
Copy link
Collaborator

I'll merge this as it is, please send a new PR for clean-ups.

@pepeiborra pepeiborra merged commit eff14cb into haskell:master Dec 6, 2020
@berberman berberman deleted the patch2 branch December 9, 2020 16:28
pepeiborra pushed a commit to pepeiborra/ide that referenced this pull request Dec 29, 2020
* Use exports map

* Use exports map in suggestExtendImport

* Update test

* Support extend constructor

* Revert format changes

* Support extending constructors

* Fix multi line
pepeiborra pushed a commit to pepeiborra/ide that referenced this pull request Dec 29, 2020
* Use exports map

* Use exports map in suggestExtendImport

* Update test

* Support extend constructor

* Revert format changes

* Support extending constructors

* Fix multi line
pepeiborra pushed a commit to pepeiborra/ide that referenced this pull request Dec 29, 2020
* Use exports map

* Use exports map in suggestExtendImport

* Update test

* Support extend constructor

* Revert format changes

* Support extending constructors

* Fix multi line
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Constructors are not added to import lists correctly when the module is already present in imports
4 participants