use of com.intellij.psi.tree.IElementType in project intellij-community by JetBrains.
the class GroovyEnterHandler method handleInString.
private static boolean handleInString(Editor editor, int caretOffset, DataContext dataContext, EditorActionHandler originalHandler) {
Project project = CommonDataKeys.PROJECT.getData(dataContext);
if (project == null)
return false;
final VirtualFile vfile = FileDocumentManager.getInstance().getFile(editor.getDocument());
assert vfile != null;
PsiFile file = PsiManager.getInstance(project).findFile(vfile);
Document document = editor.getDocument();
String fileText = document.getText();
if (fileText.length() == caretOffset)
return false;
if (!checkStringApplicable(editor, caretOffset))
return false;
if (file == null)
return false;
PsiDocumentManager.getInstance(project).commitDocument(document);
final PsiElement stringElement = inferStringPair(file, caretOffset);
if (stringElement == null)
return false;
ASTNode node = stringElement.getNode();
final IElementType nodeElementType = node.getElementType();
boolean isInsertIndent = isInsertIndent(caretOffset, stringElement.getTextRange().getStartOffset(), fileText);
// For simple String literals like 'abc'
CaretModel caretModel = editor.getCaretModel();
if (nodeElementType == GroovyTokenTypes.mSTRING_LITERAL) {
if (isSingleQuoteString(stringElement)) {
//the case of print '\<caret>'
if (isSlashBeforeCaret(caretOffset, fileText)) {
EditorModificationUtil.insertStringAtCaret(editor, "\n");
} else if (stringElement.getParent() instanceof GrReferenceExpression) {
TextRange range = stringElement.getTextRange();
convertEndToMultiline(range.getEndOffset(), document, fileText, '\'');
document.insertString(range.getStartOffset(), "''");
caretModel.moveToOffset(caretOffset + 2);
EditorModificationUtil.insertStringAtCaret(editor, "\n");
} else {
EditorModificationUtil.insertStringAtCaret(editor, "'+");
originalHandler.execute(editor, dataContext);
EditorModificationUtil.insertStringAtCaret(editor, "'");
PsiDocumentManager.getInstance(project).commitDocument(document);
CodeStyleManager.getInstance(project).reformatRange(file, caretOffset, caretModel.getOffset());
}
} else {
insertLineFeedInString(editor, dataContext, originalHandler, isInsertIndent);
}
return true;
}
if (GSTRING_TOKENS.contains(nodeElementType) || nodeElementType == GroovyElementTypes.GSTRING_CONTENT && GSTRING_TOKENS.contains(node.getFirstChildNode().getElementType()) || nodeElementType == GroovyTokenTypes.mDOLLAR && node.getTreeParent().getTreeParent().getElementType() == GroovyElementTypes.GSTRING) {
PsiElement parent = stringElement.getParent();
if (nodeElementType == GroovyTokenTypes.mGSTRING_LITERAL) {
parent = stringElement;
} else {
while (parent != null && !(parent instanceof GrLiteral)) {
parent = parent.getParent();
}
}
if (parent == null)
return false;
if (isDoubleQuotedString(parent)) {
PsiElement exprSibling = stringElement.getNextSibling();
boolean rightFromDollar = exprSibling instanceof GrExpression && exprSibling.getTextRange().getStartOffset() == caretOffset;
if (rightFromDollar)
caretOffset--;
TextRange parentRange = parent.getTextRange();
if (rightFromDollar || parent.getParent() instanceof GrReferenceExpression) {
convertEndToMultiline(parent.getTextRange().getEndOffset(), document, fileText, '"');
document.insertString(parentRange.getStartOffset(), "\"\"");
caretModel.moveToOffset(caretOffset + 2);
EditorModificationUtil.insertStringAtCaret(editor, "\n");
if (rightFromDollar) {
caretModel.moveCaretRelatively(1, 0, false, false, true);
}
} else if (isSlashBeforeCaret(caretOffset, fileText)) {
EditorModificationUtil.insertStringAtCaret(editor, "\n");
} else {
EditorModificationUtil.insertStringAtCaret(editor, "\"+");
originalHandler.execute(editor, dataContext);
EditorModificationUtil.insertStringAtCaret(editor, "\"");
PsiDocumentManager.getInstance(project).commitDocument(document);
CodeStyleManager.getInstance(project).reformatRange(file, caretOffset, caretModel.getOffset());
}
} else {
insertLineFeedInString(editor, dataContext, originalHandler, isInsertIndent);
}
return true;
}
if (REGEX_TOKENS.contains(nodeElementType) || nodeElementType == GroovyElementTypes.GSTRING_CONTENT && REGEX_TOKENS.contains(node.getFirstChildNode().getElementType()) || nodeElementType == GroovyTokenTypes.mDOLLAR && node.getTreeParent().getTreeParent().getElementType() == GroovyElementTypes.REGEX) {
PsiElement parent = stringElement.getParent();
if (nodeElementType == GroovyTokenTypes.mREGEX_LITERAL || nodeElementType == GroovyTokenTypes.mDOLLAR_SLASH_REGEX_LITERAL) {
parent = stringElement;
} else {
while (parent != null && !(parent instanceof GrLiteral)) {
parent = parent.getParent();
}
}
if (parent == null || parent.getLastChild() instanceof PsiErrorElement)
return false;
PsiElement exprSibling = stringElement.getNextSibling();
boolean rightFromDollar = exprSibling instanceof GrExpression && exprSibling.getTextRange().getStartOffset() == caretOffset;
if (rightFromDollar) {
caretModel.moveToOffset(caretOffset - 1);
}
insertLineFeedInString(editor, dataContext, originalHandler, isInsertIndent);
if (rightFromDollar) {
caretModel.moveCaretRelatively(1, 0, false, false, true);
}
return true;
}
return false;
}
use of com.intellij.psi.tree.IElementType in project intellij-community by JetBrains.
the class ConjunctionPredicate method satisfiedBy.
@Override
public boolean satisfiedBy(PsiElement element) {
if (!(element instanceof GrBinaryExpression)) {
return false;
}
final GrBinaryExpression expression = (GrBinaryExpression) element;
final IElementType tokenType = expression.getOperationTokenType();
if (tokenType == null)
return false;
if (!tokenType.equals(GroovyTokenTypes.mLAND) && !tokenType.equals(GroovyTokenTypes.mLOR)) {
return false;
}
return !ErrorUtil.containsError(element);
}
use of com.intellij.psi.tree.IElementType in project intellij-community by JetBrains.
the class DemorgansLawIntention method isConjunctionExpression.
private static boolean isConjunctionExpression(PsiElement exp, IElementType conjunctionType) {
if (!(exp instanceof GrBinaryExpression))
return false;
final GrBinaryExpression binExp = (GrBinaryExpression) exp;
final IElementType tokenType = binExp.getOperationTokenType();
return conjunctionType.equals(tokenType);
}
use of com.intellij.psi.tree.IElementType in project intellij-community by JetBrains.
the class DemorgansLawIntention method processIntention.
@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
GrBinaryExpression exp = (GrBinaryExpression) element;
final IElementType tokenType = exp.getOperationTokenType();
PsiElement parent = exp.getParent();
while (isConjunctionExpression(parent, tokenType)) {
exp = (GrBinaryExpression) parent;
assert exp != null;
parent = exp.getParent();
}
final String newExpression = convertConjunctionExpression(exp, tokenType);
replaceExpressionWithNegatedExpressionString(newExpression, exp);
}
use of com.intellij.psi.tree.IElementType in project intellij-community by JetBrains.
the class DemorgansLawIntention method getTextForElement.
@Override
protected String getTextForElement(PsiElement element) {
final GrBinaryExpression binaryExpression = (GrBinaryExpression) element;
final IElementType tokenType = binaryExpression.getOperationTokenType();
if (GroovyTokenTypes.mLAND.equals(tokenType)) {
return GroovyIntentionsBundle.message("demorgans.intention.name1");
} else {
return GroovyIntentionsBundle.message("demorgans.intention.name2");
}
}
Aggregations