mirror of
https://github.com/JetBrains/intellij-sdk-code-samples.git
synced 2025-07-28 01:07:49 +08:00
[site] go to symbol contributor
This commit is contained in:
parent
0b28c8a983
commit
2a2cd483df
@ -53,7 +53,7 @@
|
||||
* [Reference Contributor](reference_contributor.html)
|
||||
* [Find Usages Provider](find_usages_provider.html)
|
||||
* [Folding Builder](folding_builder.html)
|
||||
* [Go To Symbol Contributor](TODO)
|
||||
* [Go To Symbol Contributor](go_to_symbol_contributor.html)
|
||||
* [Structure View Factory](TODO)
|
||||
* [Formatter](TODO)
|
||||
* [Code Style Settings](TODO)
|
||||
|
100
go_to_symbol_contributor.md
Normal file
100
go_to_symbol_contributor.md
Normal file
@ -0,0 +1,100 @@
|
||||
---
|
||||
title: Go To Symbol Contributor
|
||||
---
|
||||
|
||||
<!--
|
||||
INITIAL_SOURCE https://confluence.jetbrains.com/display/IntelliJIDEA/Go+To+Symbol+Contributor
|
||||
-->
|
||||
|
||||
*A go to symbol contributor helps user to navigate to any PSI element by it's name.*
|
||||
|
||||
### 1. Define helper method for generated PSI elements
|
||||
|
||||
To specify how a PSI element looks like in the *Go To Symbol* popup window, *Structure* tool window or another components, it should implement *getPresentation* method.
|
||||
|
||||
This means we need to define this method in our utility *com.simpleplugin.parser.SimplePsiImplUtil* and regenerate the parser and PSI classes.
|
||||
|
||||
```java
|
||||
public static ItemPresentation getPresentation(final SimpleProperty element) {
|
||||
return new ItemPresentation() {
|
||||
@Nullable
|
||||
@Override
|
||||
public String getPresentableText() {
|
||||
return element.getKey();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getLocationString() {
|
||||
return element.getContainingFile().getName();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Icon getIcon(boolean unused) {
|
||||
return SimpleIcons.FILE;
|
||||
}
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
### 2. Update grammar and regenerate the parser
|
||||
|
||||
```java
|
||||
property ::= (KEY? SEPARATOR VALUE?) | KEY {mixin="com.simpleplugin.psi.impl.SimpleNamedElementImpl"
|
||||
implements="com.simpleplugin.psi.SimpleNamedElement" methods=[getKey getValue getName setName getNameIdentifier getPresentation]}
|
||||
```
|
||||
|
||||
### 3. Define a go to symbol contributor
|
||||
|
||||
```java
|
||||
package com.simpleplugin;
|
||||
|
||||
import com.intellij.navigation.ChooseByNameContributor;
|
||||
import com.intellij.navigation.NavigationItem;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.simpleplugin.psi.SimpleProperty;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SimpleChooseByNameContributor implements ChooseByNameContributor {
|
||||
@NotNull
|
||||
@Override
|
||||
public String[] getNames(Project project, boolean includeNonProjectItems) {
|
||||
List<SimpleProperty> properties = SimpleUtil.findProperties(project);
|
||||
List<String> names = new ArrayList<String>(properties.size());
|
||||
for (SimpleProperty property : properties) {
|
||||
if (property.getKey() != null && property.getKey().length() > 0) {
|
||||
names.add(property.getKey());
|
||||
}
|
||||
}
|
||||
return names.toArray(new String[names.size()]);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public NavigationItem[] getItemsByName(String name, String pattern, Project project, boolean includeNonProjectItems) {
|
||||
// todo include non project items
|
||||
List<SimpleProperty> properties = SimpleUtil.findProperties(project, name);
|
||||
return properties.toArray(new NavigationItem[properties.size()]);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 4. Register the go to symbol contributor
|
||||
|
||||
```xml
|
||||
<gotoSymbolContributor implementation="com.simpleplugin.SimpleChooseByNameContributor"/>
|
||||
```
|
||||
|
||||
### 5. Run the project
|
||||
|
||||
Now we can navigate to a property definition by name pattern via *⌥⌘⇧N* shortcut.
|
||||
|
||||

|
||||
|
||||
[Previous](folding_builder.html)
|
||||
[Top](cls_tutorial.html)
|
||||
[Next](structure_view_factory)
|
BIN
img/cls_tutorial/go_to_symbol.png
Normal file
BIN
img/cls_tutorial/go_to_symbol.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 52 KiB |
Loading…
x
Reference in New Issue
Block a user