use of com.intellij.psi.PsiLanguageInjectionHost in project intellij-community by JetBrains.
the class EditInjectionSettingsAction method invokeImpl.
private static void invokeImpl(Project project, Editor editor, PsiFile file) {
final PsiFile psiFile = InjectedLanguageUtil.findInjectedPsiNoCommit(file, editor.getCaretModel().getOffset());
if (psiFile == null)
return;
final PsiLanguageInjectionHost host = InjectedLanguageManager.getInstance(project).getInjectionHost(psiFile);
if (host == null)
return;
final LanguageInjectionSupport support = psiFile.getUserData(LanguageInjectionSupport.SETTINGS_EDITOR);
if (support == null)
return;
try {
if (!support.editInjectionInPlace(host)) {
ShowSettingsUtil.getInstance().editConfigurable(project, new InjectionsSettingsUI(project, Configuration.getProjectInstance(project)));
}
} finally {
FileContentUtil.reparseFiles(project, Collections.<VirtualFile>emptyList(), true);
}
}
use of com.intellij.psi.PsiLanguageInjectionHost in project intellij-plugins by JetBrains.
the class MxmlLanguageInjector method injectInMxmlFile.
private static void injectInMxmlFile(final MultiHostRegistrar registrar, final PsiElement host, final PsiMetaData descriptor, XmlTag tag) {
int offset = host instanceof XmlText ? 0 : 1;
if (descriptor instanceof AnnotationBackedDescriptor && ((XmlElementDescriptorWithCDataContent) descriptor).requiresCdataBracesInContext(tag)) {
final int length = host.getTextLength();
if (length < 2 * offset)
return;
String type = ((AnnotationBackedDescriptor) descriptor).getType();
if (type == null)
type = "*";
@NonNls String prefix = "(function (event:" + type + ") {";
@NonNls String suffix = "})(null);";
if (host instanceof XmlText) {
JSLanguageInjector.injectToXmlText(registrar, host, JavaScriptSupportLoader.ECMA_SCRIPT_L4, prefix, suffix);
} else {
if (JSCommonTypeNames.FUNCTION_CLASS_NAME.equals(type) && host.textContains('{')) {
final String text = StringUtil.stripQuotesAroundValue(host.getText());
if (text.startsWith("{") && text.endsWith("}")) {
prefix = FUNCTION_CALL_PREFIX;
suffix = FUNCTION_CALL_SUFFIX;
offset++;
}
}
TextRange range = new TextRange(offset, length - offset);
registrar.startInjecting(JavaScriptSupportLoader.ECMA_SCRIPT_L4).addPlace(prefix, (host instanceof XmlAttributeValue ? "\n" : "") + suffix, (PsiLanguageInjectionHost) host, range).doneInjecting();
}
} else if (!(host instanceof XmlText) || !hasCDATA((XmlText) host)) {
final String text = StringUtil.stripQuotesAroundValue(host.getText());
int openedBraces = 0;
int start = -1;
boolean addedSomething = false;
boolean quoted = false;
for (int i = 0; i < text.length(); ++i) {
final char ch = text.charAt(i);
if (quoted) {
quoted = false;
continue;
}
if (ch == '\\') {
quoted = true;
} else if (ch == '{') {
if (openedBraces == 0)
start = i + 1;
openedBraces++;
} else if (ch == '}') {
openedBraces--;
if (openedBraces == 0 && start != -1) {
registrar.startInjecting(JavaScriptSupportLoader.ECMA_SCRIPT_L4).addPlace(FUNCTION_CALL_PREFIX, FUNCTION_CALL_SUFFIX, (PsiLanguageInjectionHost) host, new TextRange(offset + start, i + offset)).doneInjecting();
addedSomething = true;
start = -1;
}
}
}
if (!addedSomething) {
final String trimmedText = text.trim();
start = trimmedText.indexOf("@");
if (start == 0 && trimmedText.length() > 1 && Character.isUpperCase(trimmedText.charAt(1))) {
// @id can be reference to attribute
offset += text.indexOf(trimmedText);
registrar.startInjecting(JavaScriptSupportLoader.ECMA_SCRIPT_L4).addPlace(null, null, (PsiLanguageInjectionHost) host, new TextRange(offset, trimmedText.length() + offset)).doneInjecting();
}
}
}
}
use of com.intellij.psi.PsiLanguageInjectionHost in project intellij-plugins by JetBrains.
the class AngularJSProcessor method eventScopeMatches.
private static boolean eventScopeMatches(InjectedLanguageManager injector, PsiElement element, PsiElement parent) {
XmlAttribute attribute = PsiTreeUtil.getNonStrictParentOfType(element, XmlAttribute.class);
if (attribute == null) {
final PsiLanguageInjectionHost elementContainer = injector.getInjectionHost(element);
attribute = PsiTreeUtil.getNonStrictParentOfType(elementContainer, XmlAttribute.class);
}
return attribute != null && CompletionUtil.getOriginalOrSelf(attribute) == CompletionUtil.getOriginalOrSelf(parent);
}
use of com.intellij.psi.PsiLanguageInjectionHost in project intellij-plugins by JetBrains.
the class TilesOgnlInjector method getLanguagesToInject.
@Override
public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement context) {
final PsiFile containingFile = context.getContainingFile();
if (!JamCommonUtil.isPlainXmlFile(containingFile)) {
return;
}
assert context instanceof XmlAttributeValue;
if (!((XmlAttributeValue) context).getValue().startsWith(OGNL_PREFIX)) {
return;
}
PsiElement parent = context.getParent();
if (parent instanceof XmlAttribute) {
String name = ((XmlAttribute) parent).getLocalName();
if ("expression".equals(name) || "templateExpression".equals(name)) {
DomElement domElement = DomManager.getDomManager(context.getProject()).getDomElement((XmlTag) parent.getParent());
if (domElement instanceof Put || domElement instanceof Add || domElement instanceof Definition) {
final TextRange attributeTextRange = ElementManipulators.getValueTextRange(context);
final TextRange ognlTextRange = TextRange.from(attributeTextRange.getStartOffset() + OGNL_PREFIX.length(), attributeTextRange.getLength() - OGNL_PREFIX.length());
registrar.startInjecting(OgnlLanguage.INSTANCE).addPlace(OgnlLanguage.EXPRESSION_PREFIX, OgnlLanguage.EXPRESSION_SUFFIX, (PsiLanguageInjectionHost) context, ognlTextRange).doneInjecting();
}
}
}
}
use of com.intellij.psi.PsiLanguageInjectionHost in project intellij-community by JetBrains.
the class JavaCheckRegexpWithFlagsTest method setUpRegexpInjectionAndGetRegexpFile.
@NotNull
private PsiFile setUpRegexpInjectionAndGetRegexpFile() {
final PsiFile file = getFile();
int offsetWithRegexp = file.getText().indexOf("Pattern.compile(\"") + "Pattern.compile(\"".length();
final PsiElement stringLiteralLeaf = file.findElementAt(offsetWithRegexp);
assertNotNull(stringLiteralLeaf);
assertNotNull(stringLiteralLeaf.getParent());
assertTrue(stringLiteralLeaf.getParent() instanceof PsiLanguageInjectionHost);
final PsiLanguageInjectionHost elementWithInjection = ((PsiLanguageInjectionHost) stringLiteralLeaf.getParent());
InjectedLanguageUtil.enumerate(elementWithInjection, file, false, new PsiLanguageInjectionHost.InjectedPsiVisitor() {
@Override
public void visit(@NotNull final PsiFile injectedPsi, @NotNull List<PsiLanguageInjectionHost.Shred> places) {
}
});
assertTrue(InjectedLanguageUtil.hasInjections(elementWithInjection));
final PsiElement elementInInjected = InjectedLanguageUtil.findElementInInjected(elementWithInjection, offsetWithRegexp);
final PsiFile regexpFile = PsiTreeUtil.getParentOfType(elementInInjected, PsiFile.class);
assertNotNull(regexpFile);
return regexpFile;
}
Aggregations