use of com.intellij.psi.impl.source.tree.LeafPsiElement in project intellij-community by JetBrains.
the class PyElementGeneratorImpl method insertItemIntoListRemoveRedundantCommas.
@Override
@NotNull
public PsiElement insertItemIntoListRemoveRedundantCommas(@NotNull final PyElement list, @Nullable final PyExpression afterThis, @NotNull final PyExpression toInsert) {
// TODO: #insertItemIntoList is probably buggy. In such case, fix it and get rid of this method
final PsiElement result = insertItemIntoList(list, afterThis, toInsert);
final LeafPsiElement[] leafs = PsiTreeUtil.getChildrenOfType(list, LeafPsiElement.class);
if (leafs != null) {
final Deque<LeafPsiElement> commas = Queues.newArrayDeque(Collections2.filter(Arrays.asList(leafs), COMMAS_ONLY));
if (!commas.isEmpty()) {
final LeafPsiElement lastComma = commas.getLast();
if (PsiTreeUtil.getNextSiblingOfType(lastComma, PyExpression.class) == null) {
//Comma has no expression after it
lastComma.delete();
}
}
}
return result;
}
use of com.intellij.psi.impl.source.tree.LeafPsiElement in project intellij-community by JetBrains.
the class Xsd2InstanceUtils method processAndSaveAllSchemas.
public static String processAndSaveAllSchemas(@NotNull XmlFile file, @NotNull final Map<String, String> scannedToFileName, @NotNull final SchemaReferenceProcessor schemaReferenceProcessor) {
final String fileName = file.getName();
String previous = scannedToFileName.get(fileName);
if (previous != null)
return previous;
scannedToFileName.put(fileName, fileName);
final StringBuilder result = new StringBuilder();
file.acceptChildren(new XmlRecursiveElementVisitor() {
@Override
public void visitElement(PsiElement psiElement) {
super.visitElement(psiElement);
if (psiElement instanceof LeafPsiElement) {
final String text = psiElement.getText();
result.append(text);
}
}
@Override
public void visitXmlAttribute(XmlAttribute xmlAttribute) {
boolean replaced = false;
if (xmlAttribute.isNamespaceDeclaration()) {
replaced = true;
final String value = xmlAttribute.getValue();
result.append(xmlAttribute.getText()).append(" ");
if (!scannedToFileName.containsKey(value)) {
final XmlNSDescriptor nsDescriptor = xmlAttribute.getParent().getNSDescriptor(value, true);
if (nsDescriptor != null) {
processAndSaveAllSchemas(nsDescriptor.getDescriptorFile(), scannedToFileName, schemaReferenceProcessor);
}
}
} else if ("schemaLocation".equals(xmlAttribute.getName())) {
final PsiReference[] references = xmlAttribute.getValueElement().getReferences();
if (references.length > 0) {
PsiElement psiElement = references[0].resolve();
if (psiElement instanceof XmlFile) {
final String s = processAndSaveAllSchemas(((XmlFile) psiElement), scannedToFileName, schemaReferenceProcessor);
if (s != null) {
result.append(xmlAttribute.getName()).append("='").append(s).append('\'');
replaced = true;
}
}
}
}
if (!replaced)
result.append(xmlAttribute.getText());
}
});
final VirtualFile virtualFile = file.getVirtualFile();
final String content = result.toString();
byte[] bytes;
if (virtualFile != null) {
bytes = content.getBytes(virtualFile.getCharset());
} else {
try {
final String charsetName = XmlUtil.extractXmlEncodingFromProlog(content.getBytes());
bytes = charsetName != null ? content.getBytes(charsetName) : content.getBytes();
} catch (UnsupportedEncodingException e) {
bytes = content.getBytes();
}
}
schemaReferenceProcessor.processSchema(fileName, bytes);
return fileName;
}
use of com.intellij.psi.impl.source.tree.LeafPsiElement in project intellij-postfix-templates by xylo.
the class CptAnnotator method annotate.
@Override
public void annotate(@NotNull final PsiElement element, @NotNull AnnotationHolder holder) {
if (element instanceof LeafPsiElement) {
final LeafPsiElement psiElement = (LeafPsiElement) element;
if (psiElement.getElementType().equals(CptTypes.CLASS_NAME)) {
final String className = element.getText();
SupportedLanguages.getCptLang(element).ifPresent(lang -> {
final CptLangAnnotator annotator = lang.getAnnotator();
if (!annotator.isMatchingType(psiElement, className)) {
holder.createErrorAnnotation(element.getTextRange(), "Class not found");
}
});
}
}
}
use of com.intellij.psi.impl.source.tree.LeafPsiElement in project intellij-elixir by KronicDeth.
the class Block method getSpacing.
/**
* Returns a spacing object indicating what spaces and/or line breaks are added between two
* specified children of this block.
*
* @param child1 the first child for which spacing is requested;
* <code>null</code> if given <code>'child2'</code> block is the first document block
* @param child2 the second child for which spacing is requested.
* @return the spacing instance, or null if no special spacing is required. If null is returned,
* the formatter does not insert or delete spaces between the child blocks, but may insert
* a line break if the line wraps at the position between the child blocks.
* @see Spacing#createSpacing(int, int, int, boolean, int)
* @see Spacing#getReadOnlySpacing()
*/
@Nullable
@Override
public Spacing getSpacing(@Nullable com.intellij.formatting.Block child1, @NotNull com.intellij.formatting.Block child2) {
Spacing spacing = null;
// Prevent `_ &&& &2` from becoming ` _ &&&& 2`, which has no meaning
if (child1 instanceof ASTBlock && child2 instanceof ASTBlock) {
ASTBlock child1ASTBlock = (ASTBlock) child1;
ASTNode child1Node = child1ASTBlock.getNode();
if (child1Node instanceof LeafPsiElement) {
LeafPsiElement child1LeafPsiElement = (LeafPsiElement) child1Node;
// capture (`&`) or and symbol operators (`&&` or `&&&`)
if (child1LeafPsiElement.charAt(child1LeafPsiElement.getTextLength() - 1) == '&') {
ASTBlock child2ASTBlock = (ASTBlock) child2;
ASTNode firstLeafElementASTNode = child2ASTBlock.getNode().findLeafElementAt(0);
if (firstLeafElementASTNode != null && firstLeafElementASTNode instanceof LeafPsiElement && ((LeafPsiElement) firstLeafElementASTNode).charAt(0) == '&') {
spacing = Spacing.createSpacing(1, 1, 0, true, 0);
}
}
}
}
if (spacing == null) {
spacing = spacingBuilder.getSpacing(this, child1, child2);
}
return spacing;
}
use of com.intellij.psi.impl.source.tree.LeafPsiElement in project Intellij-Plugin by getgauge.
the class SpecInspectionProviderTest method testGetElementReturnsSpecHeading.
@Test
public void testGetElementReturnsSpecHeading() throws Exception {
PsiElement e = mock(PsiElement.class);
LeafPsiElement leafPsiElement = mock(LeafPsiElement.class);
when(leafPsiElement.getElementType()).thenReturn(SpecTokenTypes.SPEC_HEADING);
when(e.getParent()).thenReturn(leafPsiElement);
PsiElement element = new SpecInspectionProvider().getElement(e);
assertEquals(leafPsiElement, element);
}
Aggregations