<name>KohanaStorm</name> <description>KohanaStorm framework integration for PhpStorm<br/> Authors: zenden2k@gmail.com </description> <version>0.1</version> <vendor url="http://zenden.ws/" email="zenden2k@gmail.com">zenden.ws</vendor> <idea-version since-build="8000"/>
<depends>com.intellij.modules.platform</depends>
public class MyReference implements PsiReference { @Override public String toString() { } public PsiElement getElement() { } public TextRange getRangeInElement() { return textRange; } public PsiElement handleElementRename(String newElementName) } public PsiElement bindToElement(PsiElement element) throws IncorrectOperationException { } public boolean isReferenceTo(PsiElement element) { return resolve() == element; } public Object[] getVariants() { return new Object[0]; } public boolean isSoft() { return false; } @Nullable public PsiElement resolve() { } @Override public String getCanonicalText() { } }
public class MyPsiReferenceProvider extends PsiReferenceProvider { @Override public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull final ProcessingContext context) { } }
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull final ProcessingContext context) { Project project = element.getProject(); PropertiesComponent properties = PropertiesComponent.getInstance(project); String kohanaAppDir = properties.getValue("kohanaAppPath", "application/"); VirtualFile appDir = project.getBaseDir().findFileByRelativePath(kohanaAppDir); if (appDir == null) { return PsiReference.EMPTY_ARRAY; } String className = element.getClass().getName(); Class elementClass = element.getClass(); // , StringLiteralExpressionImpl if (className.endsWith("StringLiteralExpressionImpl")) { try { // getValueRange, , Method method = elementClass.getMethod("getValueRange"); Object obj = method.invoke(element); TextRange textRange = (TextRange) obj; Class _PhpPsiElement = elementClass.getSuperclass().getSuperclass().getSuperclass(); // getText, PHP- Method phpPsiElementGetText = _PhpPsiElement.getMethod("getText"); Object obj2 = phpPsiElementGetText.invoke(element); String str = obj2.toString(); String uri = str.substring(textRange.getStartOffset(), textRange.getEndOffset()); int start = textRange.getStartOffset(); int len = textRange.getLength(); // , PHP- ( ) if (uri.endsWith(".tpl") || uri.startsWith("smarty:") || isViewFactoryCall(element)) { PsiReference ref = new MyReference(uri, element, new TextRange(start, start + len), project, appDir); return new PsiReference[]{ref}; } } catch (Exception e) { } } return PsiReference.EMPTY_ARRAY; }
public static boolean isViewFactoryCall(PsiElement element) { PsiElement prevEl = element.getParent(); String elClassName; if (prevEl != null) { elClassName = prevEl.getClass().getName(); } prevEl = prevEl.getParent(); if (prevEl != null) { elClassName = prevEl.getClass().getName(); if (elClassName.endsWith("MethodReferenceImpl")) { try { Method phpPsiElementGetName = prevEl.getClass().getMethod("getName"); String name = (String) phpPsiElementGetName.invoke(prevEl); if (name.toLowerCase().equals("factory")) { Method getClassReference = prevEl.getClass().getMethod("getClassReference"); Object classRef = getClassReference.invoke(prevEl); PrintElementClassDescription(classRef); String phpClassName = (String) phpPsiElementGetName.invoke(classRef); if (phpClassName.toLowerCase().equals("view")) { return true; } } } catch (Exception ex) { } } } return false; }
@Override public void registerReferenceProviders(PsiReferenceRegistrar registrar) { registrar.registerReferenceProvider(StandardPatterns.instanceOf(PsiElement.class), provider); }
registrar.registerReferenceProvider(StandardPatterns.instanceOf(XmlAttributeValue.class), provider);
<extensions defaultExtensionNs="com.intellij"> <psi.referenceContributor implementation="MyPsiReferenceContributor"/> </extensions>
public class KohanaStormSettingsPage implements Configurable { private JTextField appPathTextField; private JCheckBox enableKohanaStorm; private JTextField secretKeyTextField; Project project; public KohanaStormSettingsPage(Project project) { this.project = project; } @Nls @Override public String getDisplayName() { return "KohanaStorm"; } @Override public JComponent createComponent() { JPanel panel = new JPanel(); panel.setLayout(new BoxLayout (panel, BoxLayout.Y_AXIS)); JPanel panel1 = new JPanel(); panel1.setLayout(new BoxLayout(panel1, BoxLayout.X_AXIS)); enableKohanaStorm = new JCheckBox("Enable Kohana Storm for this project"); ... PropertiesComponent properties = PropertiesComponent.getInstance(project); appPathTextField.setText(properties.getValue("kohanaAppPath", DefaultSettings.kohanaAppPath)); return panel; } @Override public void apply() throws ConfigurationException { PropertiesComponent properties = PropertiesComponent.getInstance(project); properties.setValue("kohanaAppPath", appPathTextField.getText()); properties.setValue("enableKohanaStorm", String.valueOf(enableKohanaStorm.isSelected()) ); properties.setValue("kohanaStormSecretKey", secretKeyTextField.getText()); } @Override public boolean isModified() { return true; } @Override public String getHelpTopic() { return null; } @Override public void disposeUIResources() { } @Override public void reset() { } }
<extensions defaultExtensionNs="com.intellij"> <psi.referenceContributor implementation="MyPsiReferenceContributor"/> <projectConfigurable implementation="KohanaStormSettingsPage"></projectConfigurable > </extensions>
Source: https://habr.com/ru/post/161877/
All Articles