Created
January 15, 2019 15:44
-
-
Save Leneshka-jb/5e7d2ad19d87cfc00d1a2c44282b656b to your computer and use it in GitHub Desktop.
XBlade-style php injection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.jetbrains.php.blade.injection; | |
import com.intellij.openapi.util.TextRange; | |
import com.intellij.openapi.vfs.VirtualFile; | |
import com.intellij.psi.*; | |
import com.intellij.psi.xml.XmlAttribute; | |
import com.intellij.psi.xml.XmlAttributeValue; | |
import com.jetbrains.php.blade.BladeFileType; | |
import com.jetbrains.php.lang.PhpLanguage; | |
import org.jetbrains.annotations.NotNull; | |
/** | |
* Should be registered in plugin.xml with | |
* {@code <languageInjector implementation="com.jetbrains.php.blade.injection.XBladeLanguageInjector"/> } | |
*/ | |
public class XBladeLanguageInjector implements LanguageInjector { | |
@Override | |
public void getLanguagesToInject(@NotNull PsiLanguageInjectionHost host, @NotNull InjectedLanguagePlaces injectionPlacesRegistrar) { | |
if (!(host instanceof XmlAttributeValue)) return; | |
if (host.getTextLength() < 2) return; | |
PsiElement parent = host.getParent(); | |
if (!(parent instanceof XmlAttribute)) return; | |
if (!isXBladeAttribute((XmlAttribute)parent)) return; | |
injectionPlacesRegistrar.addPlace(PhpLanguage.INSTANCE, new TextRange(1, host.getTextLength() - 1), "<?php ", ";?>"); | |
} | |
private static boolean isXBladeAttribute(@NotNull XmlAttribute attribute) { | |
String attributeName = attribute.getName(); | |
if (!attributeName.startsWith(":")) return false; | |
PsiFile file = attribute.getContainingFile(); | |
if (file == null) return false; | |
VirtualFile virtualFile = file.getVirtualFile(); | |
if (virtualFile == null) return false; | |
return virtualFile.getFileType() == BladeFileType.INSTANCE; //todo: add additional conditions | |
} | |
} |
Roman pointed out that I should add php-openapi.jar
, that issue is solved 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you help me with the following issue?
import com.jetbrains.php.lang.PhpLanguage
doesn't seem to work:The PHP jar is added to the project though: