Skip to content

Commit

Permalink
Fix tutorial to remove type created in later step
Browse files Browse the repository at this point in the history
  • Loading branch information
citizenmatt committed Oct 6, 2017
1 parent 1ade5f3 commit 2201846
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 24 deletions.
57 changes: 56 additions & 1 deletion tutorials/custom_language_support/psi_helper_and_utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,64 @@ If we want to have custom methods in PSI classes we need to define them separate
Let's define an utility class with these helper methods.

```java
{% include /code_samples/simple_language_plugin/src/com/simpleplugin/psi/impl/SimplePsiImplUtil.java %}
package com.simpleplugin.psi.impl;

import com.intellij.lang.ASTNode;
import com.intellij.navigation.ItemPresentation;
import com.intellij.psi.*;
import com.simpleplugin.SimpleIcons;
import com.simpleplugin.psi.*;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

public class SimplePsiImplUtil {
public static String getKey(SimpleProperty element) {
ASTNode keyNode = element.getNode().findChildByType(SimpleTypes.KEY);
if (keyNode != null) {
// IMPORTANT: Convert embedded escaped spaces to simple spaces
return keyNode.getText().replaceAll("\\\\ ", " ");
} else {
return null;
}
}

public static String getValue(SimpleProperty element) {
ASTNode valueNode = element.getNode().findChildByType(SimpleTypes.VALUE);
if (valueNode != null) {
return valueNode.getText();
} else {
return null;
}
}

public static ItemPresentation getPresentation(final SimpleProperty element) {
return new ItemPresentation() {
@Nullable
@Override
public String getPresentableText() {
return element.getKey();
}

@Nullable
@Override
public String getLocationString() {
PsiFile containingFile = element.getContainingFile();
return containingFile == null ? null : containingFile.getName();
}

@Nullable
@Override
public Icon getIcon(boolean unused) {
return SimpleIcons.FILE;
}
};
}
}
```

Note that the `SimpleProperty` interface referenced in the code above is generated by the parser.

### 6.2. Update grammar and regenerate the parser

Now we tell to use this utility class in the grammar file via *psiImplUtilClass* attribute.
Expand Down
53 changes: 30 additions & 23 deletions tutorials/custom_language_support/reference_contributor.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,38 @@ Resolving references means the ability to go from the usage of an element to its

### 10.2. Define helper methods for generated PSI elements

Since we need to implement new methods in PSI class, we should define them in our utility.
Since we need to implement new methods in PSI class, we should define them in the `SimplePsiImplUtil` class:

```java
public static String getName(SimpleProperty element) {
return getKey(element);
}

public static PsiElement setName(SimpleProperty element, String newName) {
ASTNode keyNode = element.getNode().findChildByType(SimpleTypes.KEY);
if (keyNode != null) {

SimpleProperty property = SimpleElementFactory.createProperty(element.getProject(), newName);
ASTNode newKeyNode = property.getFirstChild().getNode();
element.getNode().replaceChild(keyNode, newKeyNode);
}
return element;
}

public static PsiElement getNameIdentifier(SimpleProperty element) {
ASTNode keyNode = element.getNode().findChildByType(SimpleTypes.KEY);
if (keyNode != null) {
return keyNode.getPsi();
} else {
return null;
}
public class SimplePsiImplUtil {

// ...

public static String getName(SimpleProperty element) {
return getKey(element);
}

public static PsiElement setName(SimpleProperty element, String newName) {
ASTNode keyNode = element.getNode().findChildByType(SimpleTypes.KEY);
if (keyNode != null) {

SimpleProperty property = SimpleElementFactory.createProperty(element.getProject(), newName);
ASTNode newKeyNode = property.getFirstChild().getNode();
element.getNode().replaceChild(keyNode, newKeyNode);
}
return element;
}

public static PsiElement getNameIdentifier(SimpleProperty element) {
ASTNode keyNode = element.getNode().findChildByType(SimpleTypes.KEY);
if (keyNode != null) {
return keyNode.getPsi();
} else {
return null;
}
}

// ...
}
```

Expand Down

0 comments on commit 2201846

Please sign in to comment.