use of com.intellij.psi.impl.source.tree.LeafPsiElement in project intellij-community by JetBrains.
the class GroovyBlockGenerator method getSpockTable.
private static List<LeafPsiElement> getSpockTable(GrStatement statement) {
List<LeafPsiElement> result = new ArrayList<>();
statement.accept(new GroovyElementVisitor() {
@Override
public void visitBinaryExpression(@NotNull GrBinaryExpression expression) {
if (isTablePart(expression)) {
result.add((LeafPsiElement) expression.getOperationToken());
expression.acceptChildren(this);
}
}
});
result.sort((l1, l2) -> l1.getStartOffset() - l2.getStartOffset());
return result;
}
use of com.intellij.psi.impl.source.tree.LeafPsiElement in project intellij-community by JetBrains.
the class GroovyBlockGenerator method alignSpockTable.
private void alignSpockTable(List<GrStatement> group) {
if (group.size() < 2) {
return;
}
GrStatement inner = group.get(0);
boolean embedded = inner != null && isTablePart(inner);
GrStatement first = embedded ? inner : group.get(1);
List<AlignmentProvider.Aligner> alignments = ContainerUtil.map2List(getSpockTable(first), leaf -> myAlignmentProvider.createAligner(leaf, true, Alignment.Anchor.RIGHT));
int second = embedded ? 1 : 2;
for (int i = second; i < group.size(); i++) {
List<LeafPsiElement> table = getSpockTable(group.get(i));
for (int j = 0; j < Math.min(table.size(), alignments.size()); j++) {
alignments.get(j).append(table.get(j));
}
}
}
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-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-plugins by JetBrains.
the class AngularJSReferencesContributor method uiViewPattern.
private static PsiElementPattern.Capture<PsiElement> uiViewPattern() {
return PlatformPatterns.psiElement(PsiElement.class).and(new FilterPattern(new ElementFilter() {
@Override
public boolean isAcceptable(Object element, @Nullable PsiElement context) {
if (!(element instanceof PsiElement))
return false;
if (element instanceof JSLiteralExpression || element instanceof LeafPsiElement && ((LeafPsiElement) element).getNode().getElementType() == JSTokenTypes.STRING_LITERAL) {
if (!(((PsiElement) element).getParent() instanceof JSProperty))
return false;
// started typing property, variant
PsiElement current = moveUpChain((PsiElement) element, JSLiteralExpression.class, JSReferenceExpression.class, JSProperty.class);
if (!(current instanceof JSProperty) || !acceptablePropertyValue((JSProperty) current))
return false;
current = current.getParent();
if (current != null && checkParentViewsObject(current))
return AngularIndexUtil.hasAngularJS(current.getProject());
}
return false;
}
private boolean acceptablePropertyValue(JSProperty element) {
return element.getNameIdentifier() != null && StringUtil.isQuotedString(element.getNameIdentifier().getText()) && (element.getValue() instanceof JSObjectLiteralExpression || element.getValue() instanceof JSReferenceExpression || element.getValue() == null);
}
private PsiElement moveUpChain(@Nullable final PsiElement element, @NotNull final Class<? extends PsiElement>... clazz) {
PsiElement current = element;
for (Class<? extends PsiElement> aClass : clazz) {
current = current != null && aClass.isInstance(current.getParent()) ? current.getParent() : current;
}
return current;
}
private boolean checkParentViewsObject(final PsiElement mustBeObject) {
if (mustBeObject instanceof JSObjectLiteralExpression) {
final PsiElement viewsProperty = mustBeObject.getParent();
if (viewsProperty instanceof JSProperty && "views".equals(((JSProperty) viewsProperty).getName())) {
// by now will not go further todo other cases
return true;
}
}
return false;
}
@Override
public boolean isClassAcceptable(Class hintClass) {
return true;
}
}));
}
Aggregations