mirror of
https://github.com/JetBrains/intellij-sdk-code-samples.git
synced 2025-07-27 16:57:49 +08:00
"Navigating the PSI" and "PSI References" sections
This commit is contained in:
parent
d086363559
commit
08a65f735c
13
psi_demo.iml
Normal file
13
psi_demo.iml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="PLUGIN_MODULE" version="4">
|
||||
<component name="DevKit.ModuleBuildProperties" url="file://$MODULE_DIR$/resources/META-INF/plugin.xml" />
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
26
resources/META-INF/plugin.xml
Normal file
26
resources/META-INF/plugin.xml
Normal file
@ -0,0 +1,26 @@
|
||||
<idea-plugin>
|
||||
<id>com.jetbrains.psidemo</id>
|
||||
<name>PSI demo</name>
|
||||
<version>1.0</version>
|
||||
<vendor email="support@yourcompany.com" url="http://www.yourcompany.com">YourCompany</vendor>
|
||||
|
||||
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description -->
|
||||
<idea-version since-build="173.0"/>
|
||||
|
||||
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
|
||||
on how to target different products -->
|
||||
<!-- uncomment to enable plugin in all products
|
||||
<depends>com.intellij.modules.lang</depends>
|
||||
-->
|
||||
|
||||
<extensions defaultExtensionNs="com.intellij">
|
||||
<!-- Add your extensions here -->
|
||||
</extensions>
|
||||
|
||||
<actions>
|
||||
<action class="com.intellij.tutorials.psi.PsiNavigationDemoAction" id="PsiNavigationDemo" text="PSI Navigation Demo...">
|
||||
<add-to-group group-id="ToolsMenu" anchor="last"/>
|
||||
</action>
|
||||
</actions>
|
||||
|
||||
</idea-plugin>
|
54
src/com/intellij/tutorials/psi/PsiNavigationDemoAction.java
Normal file
54
src/com/intellij/tutorials/psi/PsiNavigationDemoAction.java
Normal file
@ -0,0 +1,54 @@
|
||||
package com.intellij.tutorials.psi;
|
||||
|
||||
import com.intellij.openapi.actionSystem.AnAction;
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent;
|
||||
import com.intellij.openapi.actionSystem.CommonDataKeys;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.ui.Messages;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
|
||||
public class PsiNavigationDemoAction extends AnAction {
|
||||
@Override
|
||||
public void actionPerformed(AnActionEvent anActionEvent) {
|
||||
Editor editor = anActionEvent.getData(CommonDataKeys.EDITOR);
|
||||
PsiFile psiFile = anActionEvent.getData(CommonDataKeys.PSI_FILE);
|
||||
if (editor == null || psiFile == null) return;
|
||||
int offset = editor.getCaretModel().getOffset();
|
||||
|
||||
final StringBuilder infoBuilder = new StringBuilder();
|
||||
PsiElement element = psiFile.findElementAt(offset);
|
||||
infoBuilder.append("Element at caret: ").append(element).append("\n");
|
||||
if (element != null) {
|
||||
PsiMethod containingMethod = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
|
||||
infoBuilder
|
||||
.append("Containing method: ")
|
||||
.append(containingMethod != null ? containingMethod.getName() : "none")
|
||||
.append("\n");
|
||||
if (containingMethod != null) {
|
||||
PsiClass containingClass = containingMethod.getContainingClass();
|
||||
infoBuilder
|
||||
.append("Containing class: ")
|
||||
.append(containingClass != null ? containingClass.getName() : "none")
|
||||
.append("\n");
|
||||
|
||||
infoBuilder.append("Local variables:\n");
|
||||
containingMethod.accept(new JavaRecursiveElementVisitor() {
|
||||
@Override
|
||||
public void visitLocalVariable(PsiLocalVariable variable) {
|
||||
super.visitLocalVariable(variable);
|
||||
infoBuilder.append(variable.getName()).append("\n");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Messages.showMessageDialog(anActionEvent.getProject(), infoBuilder.toString(), "PSI Info", null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(AnActionEvent e) {
|
||||
Editor editor = e.getData(CommonDataKeys.EDITOR);
|
||||
PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
|
||||
e.getPresentation().setEnabled(editor != null && psiFile != null);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user