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(language-service): provide hover for microsyntax in structural directive #34847

Closed

Conversation

ivanwonder
Copy link
Contributor

In #34564, I only support hover over the directive name. And have a problem, when hovering over *ngFor="exp", it will show ngForOf property of NgForOf class, this pr will show (directive) CommonModule.NgForOf: class. This PR also supports the microsyntax(e.g. trackBy , of).

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.io application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@ivanwonder ivanwonder requested a review from a team as a code owner January 18, 2020 12:36
@ivanwonder ivanwonder force-pushed the provide-hover-for-microsyntax branch 2 times, most recently from 053acb9 to 0d749ab Compare January 18, 2020 13:01
packages/language-service/src/locate_symbol.ts Outdated Show resolved Hide resolved
packages/language-service/src/locate_symbol.ts Outdated Show resolved Hide resolved
packages/language-service/src/locate_symbol.ts Outdated Show resolved Hide resolved
packages/language-service/src/locate_symbol.ts Outdated Show resolved Hide resolved
packages/language-service/src/locate_symbol.ts Outdated Show resolved Hide resolved
packages/language-service/src/locate_symbol.ts Outdated Show resolved Hide resolved
packages/language-service/src/locate_symbol.ts Outdated Show resolved Hide resolved
packages/language-service/src/locate_symbol.ts Outdated Show resolved Hide resolved
packages/language-service/src/locate_symbol.ts Outdated Show resolved Hide resolved
@petebacondarwin petebacondarwin added area: language-service Issues related to Angular's VS Code language service action: review The PR is still awaiting reviews from at least one requested reviewer target: patch This PR is targeted for the next patch release feature Issue that requests a new feature labels Jan 20, 2020
@ngbot ngbot bot modified the milestone: needsTriage Jan 20, 2020
Copy link
Member

@ayazhafiz ayazhafiz left a comment

Choose a reason for hiding this comment

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

My apologies for the delayed review. I will try to respond as soon as possible next time..

packages/language-service/test/definitions_spec.ts Outdated Show resolved Hide resolved
packages/language-service/src/locate_symbol.ts Outdated Show resolved Hide resolved
Copy link
Member

@ayazhafiz ayazhafiz left a comment

Choose a reason for hiding this comment

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

lgtm just a couple notes

packages/language-service/src/locate_symbol.ts Outdated Show resolved Hide resolved
Copy link
Member

@ayazhafiz ayazhafiz left a comment

Choose a reason for hiding this comment

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

thanks!

Copy link
Contributor

@kyliau kyliau left a comment

Choose a reason for hiding this comment

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

Minor changes. Thank you for adding this feature!

packages/language-service/src/locate_symbol.ts Outdated Show resolved Hide resolved
packages/language-service/src/locate_symbol.ts Outdated Show resolved Hide resolved
@ivanwonder ivanwonder force-pushed the provide-hover-for-microsyntax branch 2 times, most recently from 1ae65e3 to 38587ec Compare January 22, 2020 16:11
@ivanwonder ivanwonder force-pushed the provide-hover-for-microsyntax branch 2 times, most recently from a624fde to 8796adb Compare January 23, 2020 04:34
@ayazhafiz
Copy link
Member

The property for NgForOf I think is. I have noticed the same thing with a commit I was working on today. I will take a look and fix in a few hours if that is okay. My apologies for the trouble.

@ayazhafiz
Copy link
Member

@ivanwonder I think for now, the easiest and most correct thing to do for this PR will be to overwrite the symbol found for the structural directive in visitDirectiveProperty to be a directive symbol, via e.g. symbol = symbol && new OverrideKindSymbol(symbol, DirectiveKind.DIRECTIVE). This doesn't fix the CommonModule. prefix missing, but that requires a PR that I don't want to block this feature on. Let me know if that sounds good..

@ivanwonder
Copy link
Contributor Author

@ayazhafiz So, I also need to make the assertion work for the length of definitions?

@ayazhafiz
Copy link
Member

Yes. I do not think that is a bug.

…ral directive

For the structural directive, the 'path' will contain multiple `BoundDirectivePropertyAst` which depends on the number of directive property in the attribute value(e.g. '*ngFor="let item of []; trackBy: test;"', it has 2 `BoundDirectivePropertyAst`, 'ngForOf' and 'ngForTrackBy').
Comment on lines +43 to +46
// For the structural directive, only care about the last template AST.
if (attribute?.name.startsWith('*')) {
toVisit.splice(0, toVisit.length - 1);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the structural directive, the 'path' will contain multiple BoundDirectivePropertyAst which depends on the number of directive property in the attribute value(e.g. '*ngFor="let item of []; trackBy: test;"', it has 2 BoundDirectivePropertyAst, 'ngForOf' and 'ngForTrackBy').

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ayazhafiz I think this is why the length of definitions has doubled.

Copy link
Member

Choose a reason for hiding this comment

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

You're right, this is because the directive property AST spans the entire attribute value. So the binding is parsed twice, and we end up with two pairs of each definition. I will open a PR to fix this by pruning duplicate values, but feel free to go ahead and just validate two of them for now; again, this PR should not be blocked by the issue (most language service protocol implementations will ignore the duplicate values anyway).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can review my PR again. If it’s ok, can you approve it?
I test it in vscode, the number of definitions show in the tooltip doesn’t ignore the duplication.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

image

expect(definitions !.length).toBe(2);

When you click it, it will show only one definition.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe we should filter the definitions here.

return {
definitions,
textSpan: symbols[0].span,
};

Copy link
Member

Choose a reason for hiding this comment

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

Yes, we will filter the definitions. The PR looks good to me. I think for now this is fine, will fixup after the definition-pruning PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

There is a uniqueBySpan function in diagnostics.ts that could be useful here.

@ayazhafiz ayazhafiz added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Jan 27, 2020
ayazhafiz added a commit to ayazhafiz/angular that referenced this pull request Jan 27, 2020
Sometimes, a request for definitions will return multiple of the same
definition. This can happen in at least the cases of

- two-way bindings (one of the same definition for the property and
  event binding)
- multiple template binding expressions in the same attribute
  - something like "*ngFor="let i of items; trackBy: test" has two
    template bindings, resulting in two template binding ASTs at the
    same location (the attribute span). The language service then parses
    both of these bindings individually, resulting in two independent
    but identical definitions. For more context, see angular#34847 (comment).

This commit prunes duplicate definitions by signing definitions with
their location, and checking if that location signature has been seen in
a previous definition returned to the client.
ayazhafiz added a commit to ayazhafiz/angular that referenced this pull request Jan 27, 2020
Sometimes, a request for definitions will return multiple of the same
definition. This can happen in at least the cases of

- two-way bindings (one of the same definition for the property and
  event binding)
- multiple template binding expressions in the same attribute
  - something like "*ngFor="let i of items; trackBy: test" has two
    template bindings, resulting in two template binding ASTs at the
    same location (the attribute span). The language service then parses
    both of these bindings individually, resulting in two independent
    but identical definitions. For more context, see angular#34847 (comment).

This commit prunes duplicate definitions by signing definitions with
their location, and checking if that location signature has been seen in
a previous definition returned to the client.
AndrewKushnir pushed a commit that referenced this pull request Jan 27, 2020
AndrewKushnir pushed a commit that referenced this pull request Jan 27, 2020
…ral directive (#34847)

For the structural directive, the 'path' will contain multiple `BoundDirectivePropertyAst` which depends on the number of directive property in the attribute value(e.g. '*ngFor="let item of []; trackBy: test;"', it has 2 `BoundDirectivePropertyAst`, 'ngForOf' and 'ngForTrackBy').

PR Close #34847
AndrewKushnir pushed a commit that referenced this pull request Jan 27, 2020
…ral directive (#34847)

For the structural directive, the 'path' will contain multiple `BoundDirectivePropertyAst` which depends on the number of directive property in the attribute value(e.g. '*ngFor="let item of []; trackBy: test;"', it has 2 `BoundDirectivePropertyAst`, 'ngForOf' and 'ngForTrackBy').

PR Close #34847
@ivanwonder ivanwonder deleted the provide-hover-for-microsyntax branch January 29, 2020 03:31
ayazhafiz added a commit to ayazhafiz/angular that referenced this pull request Jan 29, 2020
Sometimes, a request for definitions will return multiple of the same
definition. This can happen in at least the cases of

- two-way bindings (one of the same definition for the property and
  event binding)
- multiple template binding expressions in the same attribute
  - something like "*ngFor="let i of items; trackBy: test" has two
    template bindings, resulting in two template binding ASTs at the
    same location (the attribute span). The language service then parses
    both of these bindings individually, resulting in two independent
    but identical definitions. For more context, see angular#34847 (comment).

This commit prunes duplicate definitions by signing definitions with
their location, and checking if that location signature has been seen in
a previous definition returned to the client.
AndrewKushnir pushed a commit that referenced this pull request Jan 29, 2020
Sometimes, a request for definitions will return multiple of the same
definition. This can happen in at least the cases of

- two-way bindings (one of the same definition for the property and
  event binding)
- multiple template binding expressions in the same attribute
  - something like "*ngFor="let i of items; trackBy: test" has two
    template bindings, resulting in two template binding ASTs at the
    same location (the attribute span). The language service then parses
    both of these bindings individually, resulting in two independent
    but identical definitions. For more context, see #34847 (comment).

This commit prunes duplicate definitions by signing definitions with
their location, and checking if that location signature has been seen in
a previous definition returned to the client.

PR Close #34995
AndrewKushnir pushed a commit that referenced this pull request Jan 29, 2020
Sometimes, a request for definitions will return multiple of the same
definition. This can happen in at least the cases of

- two-way bindings (one of the same definition for the property and
  event binding)
- multiple template binding expressions in the same attribute
  - something like "*ngFor="let i of items; trackBy: test" has two
    template bindings, resulting in two template binding ASTs at the
    same location (the attribute span). The language service then parses
    both of these bindings individually, resulting in two independent
    but identical definitions. For more context, see #34847 (comment).

This commit prunes duplicate definitions by signing definitions with
their location, and checking if that location signature has been seen in
a previous definition returned to the client.

PR Close #34995
sonukapoor pushed a commit to sonukapoor/angular that referenced this pull request Feb 13, 2020
sonukapoor pushed a commit to sonukapoor/angular that referenced this pull request Feb 13, 2020
…ral directive (angular#34847)

For the structural directive, the 'path' will contain multiple `BoundDirectivePropertyAst` which depends on the number of directive property in the attribute value(e.g. '*ngFor="let item of []; trackBy: test;"', it has 2 `BoundDirectivePropertyAst`, 'ngForOf' and 'ngForTrackBy').

PR Close angular#34847
sonukapoor pushed a commit to sonukapoor/angular that referenced this pull request Feb 13, 2020
…34995)

Sometimes, a request for definitions will return multiple of the same
definition. This can happen in at least the cases of

- two-way bindings (one of the same definition for the property and
  event binding)
- multiple template binding expressions in the same attribute
  - something like "*ngFor="let i of items; trackBy: test" has two
    template bindings, resulting in two template binding ASTs at the
    same location (the attribute span). The language service then parses
    both of these bindings individually, resulting in two independent
    but identical definitions. For more context, see angular#34847 (comment).

This commit prunes duplicate definitions by signing definitions with
their location, and checking if that location signature has been seen in
a previous definition returned to the client.

PR Close angular#34995
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Feb 29, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: language-service Issues related to Angular's VS Code language service cla: yes feature Issue that requests a new feature target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants