use of com.intellij.psi.PsiLanguageInjectionHost in project intellij-community by JetBrains.
the class HtmlScriptLanguageInjector method getLanguagesToInject.
@Override
public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement host) {
if (!host.isValid() || !(host instanceof XmlText) || !HtmlUtil.isHtmlTagContainingFile(host)) {
return;
}
XmlTag scriptTag = ((XmlText) host).getParentTag();
if (scriptTag == null) {
return;
}
final Language language = getScriptLanguageToInject(scriptTag);
if (language == null || HtmlScriptInjectionBlockerExtension.isInjectionBlocked(scriptTag, language)) {
return;
}
if (LanguageUtil.isInjectableLanguage(language)) {
registrar.startInjecting(language).addPlace(null, null, (PsiLanguageInjectionHost) host, TextRange.create(0, host.getTextLength())).doneInjecting();
}
}
use of com.intellij.psi.PsiLanguageInjectionHost in project intellij-community by JetBrains.
the class PyRegexpTest method doTestInjectedText.
@NotNull
private PsiElement doTestInjectedText(@NotNull String text, @NotNull String expected) {
myFixture.configureByText(PythonFileType.INSTANCE, text);
final InjectedLanguageManager languageManager = InjectedLanguageManager.getInstance(myFixture.getProject());
final PsiLanguageInjectionHost host = languageManager.getInjectionHost(getElementAtCaret());
assertNotNull(host);
final List<Pair<PsiElement, TextRange>> files = languageManager.getInjectedPsiFiles(host);
assertNotNull(files);
assertFalse(files.isEmpty());
final PsiElement injected = files.get(0).getFirst();
assertEquals(expected, injected.getText());
return injected;
}
use of com.intellij.psi.PsiLanguageInjectionHost in project intellij-community by JetBrains.
the class UnresolvedRefCreateFunctionQuickFix method applyFix.
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PyCallExpression callExpr = myCallExpr.getElement();
final PyReferenceExpression referenceExpr = myReferenceExpr.getElement();
if (callExpr == null || !callExpr.isValid() || referenceExpr == null || !referenceExpr.isValid()) {
return;
}
final PyFunctionBuilder functionBuilder = new PyFunctionBuilder(referenceExpr.getText(), callExpr);
// if function is actually an argument of a call, don't use other arguments of the call to create parameter list of new function
final PyArgumentList argumentList = callExpr.getArgumentList();
if (argumentList != null && !PsiTreeUtil.isAncestor(argumentList, referenceExpr, false)) {
for (PyExpression param : argumentList.getArguments()) {
if (param instanceof PyKeywordArgument) {
functionBuilder.parameter(((PyKeywordArgument) param).getKeyword());
} else if (param instanceof PyReferenceExpression) {
PyReferenceExpression refex = (PyReferenceExpression) param;
functionBuilder.parameter(refex.getReferencedName());
} else {
functionBuilder.parameter("param");
}
}
} else {
functionBuilder.parameter("args");
}
PyFunction function = functionBuilder.buildFunction(project, LanguageLevel.getDefault());
final InjectedLanguageManager instance = InjectedLanguageManager.getInstance(project);
final PsiLanguageInjectionHost host = instance.getInjectionHost(callExpr);
final PsiElement insertAnchor = host != null ? host : callExpr;
final PyFunction parentFunction = PsiTreeUtil.getTopmostParentOfType(insertAnchor, PyFunction.class);
if (parentFunction != null) {
final PyClass parentClass = PsiTreeUtil.getTopmostParentOfType(parentFunction, PyClass.class);
if (parentClass != null) {
final PsiElement parent = parentClass.getParent();
function = (PyFunction) parent.addBefore(function, parentClass);
} else {
final PsiElement parent = parentFunction.getParent();
function = (PyFunction) parent.addBefore(function, parentFunction);
}
} else {
final PyStatement statement = PsiTreeUtil.getTopmostParentOfType(insertAnchor, PyStatement.class);
if (statement != null) {
final PsiElement parent = statement.getParent();
if (parent != null) {
function = (PyFunction) parent.addBefore(function, statement);
}
}
}
function = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(function);
final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(function);
ParamHelper.walkDownParamArray(function.getParameterList().getParameters(), new ParamHelper.ParamVisitor() {
public void visitNamedParameter(PyNamedParameter param, boolean first, boolean last) {
builder.replaceElement(param, param.getName());
}
});
builder.replaceElement(function.getStatementList(), PyNames.PASS);
final FileEditor editor = FileEditorManager.getInstance(project).getSelectedEditor(insertAnchor.getContainingFile().getVirtualFile());
if (!(editor instanceof TextEditor)) {
return;
}
builder.run(((TextEditor) editor).getEditor(), false);
}
use of com.intellij.psi.PsiLanguageInjectionHost in project intellij-community by JetBrains.
the class PyDocstringTest method doTestIndentation.
private void doTestIndentation(boolean hasErrors) {
String inputDataFileName = getTestName(true) + ".py";
myFixture.configureByFile(inputDataFileName);
final InjectedLanguageManager languageManager = InjectedLanguageManager.getInstance(myFixture.getProject());
final PsiLanguageInjectionHost host = languageManager.getInjectionHost(myFixture.getElementAtCaret());
assertNotNull(host);
final List<Pair<PsiElement, TextRange>> files = languageManager.getInjectedPsiFiles(host);
assertNotNull(files);
for (Pair<PsiElement, TextRange> pair : files) {
assertEquals(hasErrors, PsiTreeUtil.hasErrorElements(pair.getFirst()));
}
}
use of com.intellij.psi.PsiLanguageInjectionHost in project intellij-community by JetBrains.
the class PyTypingTest method doTestNoInjectedText.
private void doTestNoInjectedText(@NotNull String text) {
myFixture.configureByText(PythonFileType.INSTANCE, text);
final InjectedLanguageManager languageManager = InjectedLanguageManager.getInstance(myFixture.getProject());
final PsiLanguageInjectionHost host = languageManager.getInjectionHost(getElementAtCaret());
assertNull(host);
}
Aggregations