Update Go to Class/Symbol content

This commit is contained in:
Karol Lewandowski 2023-03-01 10:54:51 +01:00
parent 57ee96618d
commit c2ae57932d
3 changed files with 30 additions and 16 deletions

View File

@ -24,7 +24,8 @@ public class SimpleChooseByNameContributor implements ChooseByNameContributorEx
@NotNull GlobalSearchScope scope,
@Nullable IdFilter filter) {
Project project = Objects.requireNonNull(scope.getProject());
List<String> propertyKeys = ContainerUtil.map(SimpleUtil.findProperties(project), SimpleProperty::getKey);
List<String> propertyKeys = ContainerUtil.map(
SimpleUtil.findProperties(project), SimpleProperty::getKey);
ContainerUtil.process(propertyKeys, processor);
}
@ -33,7 +34,8 @@ public class SimpleChooseByNameContributor implements ChooseByNameContributorEx
@NotNull Processor<? super NavigationItem> processor,
@NotNull FindSymbolParameters parameters) {
List<NavigationItem> properties = ContainerUtil.map(
SimpleUtil.findProperties(parameters.getProject(), name), property -> (NavigationItem) property);
SimpleUtil.findProperties(parameters.getProject(), name),
property -> (NavigationItem) property);
ContainerUtil.process(properties, processor);
}

View File

@ -1,6 +1,8 @@
[//]: # (title: Go to Class and Go to Symbol)
<!-- Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. -->
<!-- Copyright 2000-2022 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -->
# Go to Class and Go to Symbol
<link-summary>"Go to Class/Symbol" contributors allowing for quick searching and navigating to classes and symbols.</link-summary>
<tldr>
@ -10,15 +12,17 @@
A custom language plugin can provide its items to be included in the lists shown when the user chooses the <ui-path>Navigate | Class</ui-path> or <ui-path>Navigate | Symbol</ui-path> action.
Provide implementations of [`ChooseByNameContributor`](%gh-ic%/platform/lang-api/src/com/intellij/navigation/ChooseByNameContributor.java) interface (separate implementations need to be provided for <control>Class</control> and <control>Symbol</control>, respectively), and register them in the `com.intellij.gotoClassContributor` and `com.intellij.gotoSymbolContributor` extension points.
Provide implementations of [`ChooseByNameContributorEx`](%gh-ic%/platform/lang-impl/src/com/intellij/navigation/ChooseByNameContributorEx.java) interface (separate implementations need to be provided for <control>Class</control> and <control>Symbol</control>, respectively), and register them in the `com.intellij.gotoClassContributor` and `com.intellij.gotoSymbolContributor` extension points.
> Please consider implementing [`ChooseByNameContributorEx`](%gh-ic%/platform/lang-impl/src/com/intellij/navigation/ChooseByNameContributorEx.java) for better performance.
>
Each `ChooseByNameContributorEx` implementation must provide the following methods:
* `processNames(@NotNull Processor<? super String> processor, @NotNull GlobalSearchScope scope, @Nullable IdFilter filter)`
Each contributor must return a complete list of names to show in the list for a specified project, which the IDE will then filter according to the text typed by the user in the dialog.
Using [File-based or Stub indices](indexing_and_psi_stubs.md) to obtain matching candidates is highly recommended to improve performance.
Feeds the processor with a complete list of names available in a specified scope, which the IDE will then filter according to the text typed by the user in the dialog.
Using [File-based or Stub indices](indexing_and_psi_stubs.md) to obtain matching candidates is highly recommended to improve performance.
* `processElementsWithName(String name, Processor<? super NavigationItem> processor, FindSymbolParameters parameters)`
For each name in that list, the contributor needs to provide a list of [`NavigationItem`](%gh-ic%/platform/core-api/src/com/intellij/navigation/NavigationItem.java) instances (typically [`PsiElement`](%gh-ic%/platform/core-api/src/com/intellij/psi/PsiElement.java)), which specify the destinations to jump to when a specific item is selected from the list.
Feeds the processor with a list of [`NavigationItem`](%gh-ic%/platform/core-api/src/com/intellij/navigation/NavigationItem.java) instances (typically [`PsiElement`](%gh-ic%/platform/core-api/src/com/intellij/psi/PsiElement.java)) matching the given name and parameters.
Processed `NavigationItem`s specify the destinations to jump to when a specific item is selected from the list.
**Example:**
- [Custom Language Support Tutorial: Go To Symbol Contributor](go_to_symbol_contributor.md)

View File

@ -1,24 +1,26 @@
# 13. Go To Symbol Contributor
<!-- Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. -->
# 13. Go To Symbol Contributor
<link-summary>Sample implementation of a "Go to Symbol" contributor allowing for searching and navigating to Simple language properties definitions.</link-summary>
<tldr>
**Reference**: [](go_to_class_and_go_to_symbol.md)
**Code**: [`SimplePsiImplUtil`](%gh-sdk-samples%/simple_language_plugin/src/main/java/org/intellij/sdk/language/psi/impl/SimplePsiImplUtil.java),
[`SimpleChooseByNameContributor`](%gh-sdk-samples%/simple_language_plugin/src/main/java/org/intellij/sdk/language/SimpleChooseByNameContributor.java)
**Code**:
[`SimpleChooseByNameContributor`](%gh-sdk-samples%/simple_language_plugin/src/main/java/org/intellij/sdk/language/SimpleChooseByNameContributor.java),
[`SimplePsiImplUtil`](%gh-sdk-samples%/simple_language_plugin/src/main/java/org/intellij/sdk/language/psi/impl/SimplePsiImplUtil.java)
</tldr>
<include from="language_and_filetype.md" element-id="custom_language_tutorial_header"></include>
A _Go to Symbol Contributor_ helps the user to navigate to any PSI element by its name.
## Define a Helper Method for Generated PSI Elements
To specify how a PSI element looks like in the <ui-path>Navigate | Symbol</ui-path> popup window, <control>Structure</control> tool window, or other components, it should implement `getPresentation()`.
To specify what a PSI element looks like in the <ui-path>Navigate | Symbol</ui-path> popup window, <control>Structure</control> tool window, or other components, it should implement `getPresentation()`.
This method gets defined in the utility class `SimplePsiImplUtil`, and the parser and PSI classes must be regenerated.
Add the following method to [`SimplePsiImplUtil`](%gh-sdk-samples%/simple_language_plugin/src/main/java/org/intellij/sdk/language/psi/impl/SimplePsiImplUtil.java):
@ -26,6 +28,12 @@ Add the following method to [`SimplePsiImplUtil`](%gh-sdk-samples%/simple_langua
```
{src="simple_language_plugin/src/main/java/org/intellij/sdk/language/psi/impl/SimplePsiImplUtil.java" include-symbol="getPresentation"}
In addition, to provide an icon for the displayed items, extend [`IconProvider`](%gh-ic%/platform/core-api/src/com/intellij/ide/IconProvider.java) and register it in `com.intellij.iconProvider` extension point. See [`SimplePropertyIconProvider`](%gh-sdk-samples%/simple_language_plugin/src/main/java/org/intellij/sdk/language/SimplePropertyIconProvider.java):
```java
```
{src="simple_language_plugin/src/main/java/org/intellij/sdk/language/SimplePropertyIconProvider.java" include-symbol="SimplePropertyIconProvider"}
## Update Grammar and Regenerate the Parser
Now add the `SimplePsiImplUtil.getPresentation()` to the `property` methods definition in the <path>Simple.bnf</path> grammar file by replacing the `property` definition with the lines below.
@ -42,7 +50,7 @@ property ::= (KEY? SEPARATOR VALUE?) | KEY {
## Define a Go To Symbol Contributor
To contribute items to <ui-path>Navigate | Symbol</ui-path> results, subclass [`ChooseByNameContributor`](%gh-ic%/platform/lang-api/src/com/intellij/navigation/ChooseByNameContributor.java)
To contribute items to <ui-path>Navigate | Symbol</ui-path> results, subclass [`ChooseByNameContributorEx`](%gh-ic%/platform/lang-impl/src/com/intellij/navigation/ChooseByNameContributorEx.java)
to create [`SimpleChooseByNameContributor`](%gh-sdk-samples%/simple_language_plugin/src/main/java/org/intellij/sdk/language/SimpleChooseByNameContributor.java):
```java