use of com.intellij.psi.tree.IElementType in project intellij-community by JetBrains.
the class GroovyLiteralCopyPasteProcessor method findLiteralTokenType.
@Override
@Nullable
protected PsiElement findLiteralTokenType(PsiFile file, int selectionStart, int selectionEnd) {
PsiElement elementAtSelectionStart = file.findElementAt(selectionStart);
if (elementAtSelectionStart == null) {
return null;
}
IElementType elementType = elementAtSelectionStart.getNode().getElementType();
if ((elementType == GroovyTokenTypes.mREGEX_END || elementType == GroovyTokenTypes.mDOLLAR_SLASH_REGEX_END || elementType == GroovyTokenTypes.mGSTRING_END) && elementAtSelectionStart.getTextOffset() == selectionStart) {
elementAtSelectionStart = elementAtSelectionStart.getPrevSibling();
if (elementAtSelectionStart == null)
return null;
elementType = elementAtSelectionStart.getNode().getElementType();
}
if (elementType == GroovyTokenTypes.mDOLLAR) {
elementAtSelectionStart = elementAtSelectionStart.getParent();
elementType = elementAtSelectionStart.getNode().getElementType();
}
if (!isStringLiteral(elementAtSelectionStart)) {
return null;
}
if (elementAtSelectionStart.getTextRange().getEndOffset() < selectionEnd) {
final PsiElement elementAtSelectionEnd = file.findElementAt(selectionEnd);
if (elementAtSelectionEnd == null) {
return null;
}
if (elementAtSelectionEnd.getNode().getElementType() == elementType && elementAtSelectionEnd.getTextRange().getStartOffset() < selectionEnd) {
return elementAtSelectionStart;
}
}
final TextRange textRange = elementAtSelectionStart.getTextRange();
//content elements don't have quotes, so they are shorter than whole string literals
if (elementType == GroovyTokenTypes.mREGEX_CONTENT || elementType == GroovyTokenTypes.mGSTRING_CONTENT || elementType == GroovyTokenTypes.mDOLLAR_SLASH_REGEX_CONTENT || elementType == GroovyElementTypes.GSTRING_INJECTION) {
selectionStart++;
selectionEnd--;
}
if (textRange.getLength() > 0 && (selectionStart <= textRange.getStartOffset() || selectionEnd >= textRange.getEndOffset())) {
return null;
}
if (elementType == GroovyElementTypes.GSTRING_CONTENT) {
elementAtSelectionStart = elementAtSelectionStart.getFirstChild();
}
return elementAtSelectionStart;
}
use of com.intellij.psi.tree.IElementType in project intellij-community by JetBrains.
the class GroovyLiteralCopyPasteProcessor method escapeCharCharacters.
@NotNull
@Override
protected String escapeCharCharacters(@NotNull String s, @NotNull PsiElement token) {
if (s.isEmpty())
return s;
IElementType tokenType = token.getNode().getElementType();
if (tokenType == GroovyTokenTypes.mREGEX_CONTENT || tokenType == GroovyTokenTypes.mREGEX_LITERAL) {
return GrStringUtil.escapeSymbolsForSlashyStrings(s);
}
if (tokenType == GroovyTokenTypes.mDOLLAR_SLASH_REGEX_CONTENT || tokenType == GroovyTokenTypes.mDOLLAR_SLASH_REGEX_LITERAL) {
return GrStringUtil.escapeSymbolsForDollarSlashyStrings(s);
}
if (tokenType == GroovyTokenTypes.mGSTRING_CONTENT || tokenType == GroovyTokenTypes.mGSTRING_LITERAL || tokenType == GroovyElementTypes.GSTRING_INJECTION) {
boolean singleLine = !GrStringUtil.findContainingLiteral(token).getText().contains("\"\"\"");
StringBuilder b = new StringBuilder();
GrStringUtil.escapeStringCharacters(s.length(), s, singleLine ? "\"" : "", singleLine, true, b);
GrStringUtil.unescapeCharacters(b, singleLine ? "'" : "'\"", true);
LOG.assertTrue(b.length() > 0, "s=" + s);
for (int i = b.length() - 2; i >= 0; i--) {
if (b.charAt(i) == '$') {
final char next = b.charAt(i + 1);
if (next != '{' && !Character.isLetter(next)) {
b.insert(i, '\\');
}
}
}
if (b.charAt(b.length() - 1) == '$') {
b.insert(b.length() - 1, '\\');
}
return b.toString();
}
if (tokenType == GroovyTokenTypes.mSTRING_LITERAL) {
return GrStringUtil.escapeSymbolsForString(s, !token.getText().contains("'''"), false);
}
return super.escapeCharCharacters(s, token);
}
use of com.intellij.psi.tree.IElementType in project intellij-community by JetBrains.
the class GroovyLiteralSelectioner method select.
@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
List<TextRange> result = super.select(e, editorText, cursorOffset, editor);
if (e instanceof GrListOrMap)
return result;
int startOffset = -1;
int endOffset = -1;
final String text = e.getText();
final int stringOffset = e.getTextOffset();
final IElementType elementType = e.getNode().getElementType();
if (elementType == GroovyTokenTypes.mGSTRING_CONTENT || elementType == GroovyTokenTypes.mREGEX_CONTENT || elementType == GroovyTokenTypes.mDOLLAR_SLASH_REGEX_CONTENT) {
int cur;
int index = -1;
while (true) {
cur = text.indexOf('\n', index + 1);
if (cur < 0 || cur + stringOffset > cursorOffset)
break;
index = cur;
}
if (index >= 0) {
startOffset = stringOffset + index + 1;
}
index = text.indexOf('\n', cursorOffset - stringOffset);
if (index >= 0) {
endOffset = stringOffset + index + 1;
}
}
if (startOffset >= 0 && endOffset >= 0) {
result.add(new TextRange(startOffset, endOffset));
}
final String content = GrStringUtil.removeQuotes(text);
final int offset = stringOffset + text.indexOf(content);
result.add(new TextRange(offset, offset + content.length()));
return result;
}
use of com.intellij.psi.tree.IElementType in project intellij-community by JetBrains.
the class StubsTest method doTest.
public void doTest() throws Exception {
final List<String> data = TestUtils.readInput(getTestDataPath() + "/" + getTestName(true) + ".test");
String fileText = data.get(0);
PsiFile psiFile = TestUtils.createPseudoPhysicalGroovyFile(getProject(), fileText);
ASTNode node = psiFile.getNode();
Assert.assertNotNull(node);
IElementType type = node.getElementType();
Assert.assertTrue(type instanceof IStubFileElementType);
IStubFileElementType stubFileType = (IStubFileElementType) type;
StubBuilder builder = stubFileType.getBuilder();
StubElement element = builder.buildStubTree(psiFile);
StringBuffer buffer = new StringBuffer();
getStubsTreeImpl(element, buffer, "");
String stubTree = buffer.toString().trim();
assertEquals(data.get(1), stubTree);
}
use of com.intellij.psi.tree.IElementType in project intellij-community by JetBrains.
the class PySplitIfIntention method isAvailable.
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!(file instanceof PyFile)) {
return false;
}
PsiElement elementAtOffset = file.findElementAt(editor.getCaretModel().getOffset());
if (elementAtOffset == null || elementAtOffset.getNode() == null) {
return false;
}
// PY-745
final IElementType elementType = elementAtOffset.getNode().getElementType();
if (elementType == PyTokenTypes.COLON) {
elementAtOffset = elementAtOffset.getPrevSibling();
elementAtOffset = PyPsiUtils.getPrevNonCommentSibling(elementAtOffset, false);
} else if (elementType == PyTokenTypes.IF_KEYWORD) {
elementAtOffset = elementAtOffset.getNextSibling();
elementAtOffset = PyPsiUtils.getNextNonCommentSibling(elementAtOffset, false);
}
PsiElement element = PsiTreeUtil.getParentOfType(elementAtOffset, PyBinaryExpression.class, false);
if (element == null) {
return false;
}
while (element.getParent() instanceof PyBinaryExpression) {
element = element.getParent();
}
if (((PyBinaryExpression) element).getOperator() != PyTokenTypes.AND_KEYWORD || ((PyBinaryExpression) element).getRightExpression() == null) {
return false;
}
final PsiElement parent = element.getParent();
if (!(parent instanceof PyIfPart)) {
return false;
}
setText(PyBundle.message("INTN.split.if.text"));
return true;
}
Aggregations