Skip to content

Instantly share code, notes, and snippets.

@Leneshka-jb
Created January 15, 2019 15:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Leneshka-jb/5e7d2ad19d87cfc00d1a2c44282b656b to your computer and use it in GitHub Desktop.
Save Leneshka-jb/5e7d2ad19d87cfc00d1a2c44282b656b to your computer and use it in GitHub Desktop.
XBlade-style php injection
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
}
}
@brendt
Copy link

brendt commented Feb 11, 2019

Could you help me with the following issue? import com.jetbrains.php.lang.PhpLanguage doesn't seem to work:

screen shot 2019-02-11 at 12 13 38

screen shot 2019-02-11 at 12 13 44

The PHP jar is added to the project though:

screen shot 2019-02-11 at 12 15 41

@brendt
Copy link

brendt commented Feb 11, 2019

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