use of com.intellij.psi.PsiLanguageInjectionHost in project intellij-community by JetBrains.
the class InjectedLanguageBlockBuilder method addInjectedBlocks.
public boolean addInjectedBlocks(List<Block> result, final ASTNode injectionHost, Wrap wrap, Alignment alignment, Indent indent) {
final PsiFile[] injectedFile = new PsiFile[1];
final Ref<TextRange> injectedRangeInsideHost = new Ref<>();
final Ref<Integer> prefixLength = new Ref<>();
final Ref<Integer> suffixLength = new Ref<>();
final Ref<ASTNode> injectionHostToUse = new Ref<>(injectionHost);
final PsiLanguageInjectionHost.InjectedPsiVisitor injectedPsiVisitor = new PsiLanguageInjectionHost.InjectedPsiVisitor() {
@Override
public void visit(@NotNull final PsiFile injectedPsi, @NotNull final List<PsiLanguageInjectionHost.Shred> places) {
if (places.size() != 1) {
return;
}
final PsiLanguageInjectionHost.Shred shred = places.get(0);
TextRange textRange = shred.getRangeInsideHost();
PsiLanguageInjectionHost shredHost = shred.getHost();
if (shredHost == null) {
return;
}
ASTNode node = shredHost.getNode();
if (node == null) {
return;
}
if (node != injectionHost) {
int shift = 0;
boolean canProcess = false;
for (ASTNode n = injectionHost.getTreeParent(), prev = injectionHost; n != null; prev = n, n = n.getTreeParent()) {
shift += n.getStartOffset() - prev.getStartOffset();
if (n == node) {
textRange = textRange.shiftRight(shift);
canProcess = true;
break;
}
}
if (!canProcess) {
return;
}
}
String childText;
if ((injectionHost.getTextLength() == textRange.getEndOffset() && textRange.getStartOffset() == 0) || (canProcessFragment((childText = injectionHost.getText()).substring(0, textRange.getStartOffset()), injectionHost) && canProcessFragment(childText.substring(textRange.getEndOffset()), injectionHost))) {
injectedFile[0] = injectedPsi;
injectedRangeInsideHost.set(textRange);
prefixLength.set(shred.getPrefix().length());
suffixLength.set(shred.getSuffix().length());
}
}
};
final PsiElement injectionHostPsi = injectionHost.getPsi();
InjectedLanguageUtil.enumerate(injectionHostPsi, injectionHostPsi.getContainingFile(), false, injectedPsiVisitor);
if (injectedFile[0] != null) {
final Language childLanguage = injectedFile[0].getLanguage();
final FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(childLanguage, injectionHostPsi);
if (builder != null) {
final int startOffset = injectedRangeInsideHost.get().getStartOffset();
final int endOffset = injectedRangeInsideHost.get().getEndOffset();
TextRange range = injectionHostToUse.get().getTextRange();
int childOffset = range.getStartOffset();
if (startOffset != 0) {
final ASTNode leaf = injectionHostToUse.get().findLeafElementAt(startOffset - 1);
result.add(createBlockBeforeInjection(leaf, wrap, alignment, indent, new TextRange(childOffset, childOffset + startOffset)));
}
addInjectedLanguageBlockWrapper(result, injectedFile[0].getNode(), indent, childOffset + startOffset, new TextRange(prefixLength.get(), injectedFile[0].getTextLength() - suffixLength.get()));
if (endOffset != injectionHostToUse.get().getTextLength()) {
final ASTNode leaf = injectionHostToUse.get().findLeafElementAt(endOffset);
result.add(createBlockAfterInjection(leaf, wrap, alignment, indent, new TextRange(childOffset + endOffset, range.getEndOffset())));
}
return true;
}
}
return false;
}
use of com.intellij.psi.PsiLanguageInjectionHost in project intellij-community by JetBrains.
the class IElementTypeTest method testManipulatorRegistered.
public void testManipulatorRegistered() {
LanguageExtensionPoint[] extensions = Extensions.getExtensions(new ExtensionPointName<LanguageExtensionPoint>("com.intellij.lang.parserDefinition"));
Set<String> classes = new HashSet<>();
List<String> failures = new ArrayList<>();
int total = 0;
for (LanguageExtensionPoint e : extensions) {
ParserDefinition definition = (ParserDefinition) e.getInstance();
for (IElementType type : IElementType.enumerate(IElementType.TRUE)) {
if (type instanceof ILeafElementType)
continue;
try {
CompositeElement treeElement = ASTFactory.composite(type);
total++;
PsiElement element = treeElement instanceof PsiElement ? (PsiElement) treeElement : definition.createElement(treeElement);
if (element instanceof PsiLanguageInjectionHost && classes.add(element.getClass().getName())) {
boolean ok = ElementManipulators.getManipulator(element) != null;
System.out.println((ok ? "OK " : "FAIL") + " " + element.getClass().getSimpleName() + " [" + definition.getClass().getSimpleName() + "]");
if (!ok)
failures.add(element.getClass().getName());
}
} catch (Throwable ignored) {
}
}
}
System.out.println("count: " + classes.size() + ", total: " + total);
assertEmpty("PsiLanguageInjectionHost requires " + ElementManipulators.EP_NAME, failures);
}
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;
}
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);
}
Aggregations