use of com.intellij.psi.impl.source.tree.LeafPsiElement in project intellij-community by JetBrains.
the class MavenPluginParamInfo method isSimpleText.
public static boolean isSimpleText(@NotNull XmlText paramValue) {
PsiElement prevSibling = paramValue.getPrevSibling();
if (!(prevSibling instanceof LeafPsiElement) || ((LeafPsiElement) prevSibling).getElementType() != XmlTokenType.XML_TAG_END) {
return false;
}
PsiElement nextSibling = paramValue.getNextSibling();
if (!(nextSibling instanceof LeafPsiElement) || ((LeafPsiElement) nextSibling).getElementType() != XmlTokenType.XML_END_TAG_START) {
return false;
}
return true;
}
use of com.intellij.psi.impl.source.tree.LeafPsiElement in project intellij-community by JetBrains.
the class StatementEffectFunctionCallQuickFix method replaceExec.
private static void replaceExec(@NotNull final PsiElement expression) {
final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(expression.getProject());
final String expressionText = expression.getText();
final StringBuilder stringBuilder = new StringBuilder(expressionText + " (");
final PsiElement next = getNextElement(expression);
if (next == null) {
stringBuilder.append(")");
expression.replace(elementGenerator.createFromText(LanguageLevel.forElement(expression), PyExpression.class, stringBuilder.toString()));
return;
}
final String commentText = getComment(next);
if (next instanceof PyExpressionStatement) {
final PyExpression expr = ((PyExpressionStatement) next).getExpression();
if (expr instanceof PyBinaryExpression) {
final PsiElement operator = ((PyBinaryExpression) expr).getPsiOperator();
if (operator instanceof LeafPsiElement && ((LeafPsiElement) operator).getElementType() == PyTokenTypes.IN_KEYWORD) {
addInArguments(stringBuilder, (PyBinaryExpression) expr);
} else {
stringBuilder.append(next.getText());
}
} else if (expr instanceof PyTupleExpression) {
final PyExpression[] elements = ((PyTupleExpression) expr).getElements();
if (elements.length > 1) {
if (elements[0] instanceof PyBinaryExpression) {
addInArguments(stringBuilder, (PyBinaryExpression) elements[0]);
}
stringBuilder.append(", ");
stringBuilder.append(elements[1].getText());
}
} else {
stringBuilder.append(((PyExpressionStatement) next).getExpression().getText());
}
} else {
stringBuilder.append(next.getText());
}
next.delete();
stringBuilder.append(")");
if (commentText != null) {
stringBuilder.append(commentText);
}
expression.replace(elementGenerator.createFromText(LanguageLevel.forElement(expression), PyExpression.class, stringBuilder.toString()));
}
use of com.intellij.psi.impl.source.tree.LeafPsiElement in project intellij-community by JetBrains.
the class ShuffleNamesAction method shuffleIds.
private static boolean shuffleIds(PsiFile file, Editor editor) {
final Map<String, String> map = new THashMap<>();
final StringBuilder sb = new StringBuilder();
final StringBuilder quote = new StringBuilder();
final ArrayList<String> split = new ArrayList<>(100);
file.acceptChildren(new PsiRecursiveElementWalkingVisitor() {
@Override
public void visitElement(PsiElement element) {
if (element instanceof LeafPsiElement) {
String type = ((LeafPsiElement) element).getElementType().toString();
String text = element.getText();
if (text.isEmpty())
return;
for (int i = 0, len = text.length(); i < len / 2; i++) {
char c = text.charAt(i);
if (c == text.charAt(len - i - 1) && !Character.isLetter(c)) {
quote.append(c);
} else
break;
}
boolean isQuoted = quote.length() > 0;
boolean isNumber = false;
if (isQuoted || type.equals("ID") || type.contains("IDENT") && !"ts".equals(text) || (isNumber = text.matches("[0-9]+"))) {
String replacement = map.get(text);
if (replacement == null) {
split.addAll(Arrays.asList((isQuoted ? text.substring(quote.length(), text.length() - quote.length()).replace("''", "") : text).split("")));
if (!isNumber) {
for (ListIterator<String> it = split.listIterator(); it.hasNext(); ) {
String s = it.next();
if (s.isEmpty()) {
it.remove();
continue;
}
int c = s.charAt(0);
int cap = c & 32;
c &= ~cap;
c = (char) ((c >= 'A') && (c <= 'Z') ? ((c - 'A' + 7) % 26 + 'A') : c) | cap;
it.set(String.valueOf((char) c));
}
}
Collections.shuffle(split);
if (isNumber && "0".equals(split.get(0))) {
split.set(0, "1");
}
replacement = StringUtil.join(split, "");
if (isQuoted) {
replacement = quote + replacement + quote.reverse();
}
map.put(text, replacement);
}
text = replacement;
}
sb.append(text);
quote.setLength(0);
split.clear();
}
super.visitElement(element);
}
});
editor.getDocument().setText(sb.toString());
return true;
}
use of com.intellij.psi.impl.source.tree.LeafPsiElement in project intellij-community by JetBrains.
the class GroovyFoldingBuilder method processImports.
private static void processImports(final List<FoldingDescriptor> descriptors, GrImportStatement[] imports) {
if (imports.length < 2)
return;
PsiElement first = imports[0];
while (first != null) {
PsiElement marker = first;
PsiElement next = first.getNextSibling();
while (next instanceof GrImportStatement || next instanceof LeafPsiElement) {
if (next instanceof GrImportStatement)
marker = next;
next = next.getNextSibling();
}
if (marker != first) {
int start = first.getTextRange().getStartOffset();
int end = marker.getTextRange().getEndOffset();
int tail = "import ".length();
if (start + tail < end && !JavaFoldingBuilderBase.hasErrorElementsNearby(first.getContainingFile(), start, end)) {
FoldingDescriptor descriptor = new FoldingDescriptor(first.getNode(), new TextRange(start + tail, end));
// imports are often added/removed automatically, so we enable autoupdate of folded region for foldings even if it's collapsed
descriptor.setCanBeRemovedWhenCollapsed(true);
descriptors.add(descriptor);
}
}
while (!(next instanceof GrImportStatement) && next != null) next = next.getNextSibling();
first = next;
}
}
use of com.intellij.psi.impl.source.tree.LeafPsiElement in project intellij-community by JetBrains.
the class GrVariableBaseImpl method delete.
@Override
public void delete() throws IncorrectOperationException {
PsiElement parent = getParent();
PsiElement prev = PsiUtil.getPrevNonSpace(this);
PsiElement next = PsiUtil.getNextNonSpace(this);
ASTNode parentNode = parent.getNode();
assert parentNode != null;
super.delete();
if (prev != null && prev.getNode() != null && prev.getNode().getElementType() == GroovyTokenTypes.mCOMMA) {
prev.delete();
} else if (next instanceof LeafPsiElement && next.getNode() != null && next.getNode().getElementType() == GroovyTokenTypes.mCOMMA) {
next.delete();
}
if (parent instanceof GrVariableDeclaration && ((GrVariableDeclaration) parent).getVariables().length == 0) {
parent.delete();
}
}
Aggregations