use of com.intellij.psi.PsiLiteralExpression in project intellij-community by JetBrains.
the class ReplaceOctalEscapeWithUnicodeEscapeIntention method processIntention.
@Override
protected void processIntention(Editor editor, @NotNull PsiElement element) {
final SelectionModel selectionModel = editor.getSelectionModel();
if (selectionModel.hasSelection()) {
// does not check if octal escape is inside char or string literal (garbage in, garbage out)
final Document document = editor.getDocument();
final int start = selectionModel.getSelectionStart();
final int end = selectionModel.getSelectionEnd();
final String text = document.getText(new TextRange(start, end));
final int textLength = end - start;
final StringBuilder replacement = new StringBuilder(textLength);
int anchor = 0;
while (true) {
final int index = indexOfOctalEscape(text, anchor + 1);
if (index < 0) {
break;
}
replacement.append(text.substring(anchor, index));
anchor = appendUnicodeEscape(text, index, replacement);
}
replacement.append(text.substring(anchor, textLength));
document.replaceString(start, end, replacement);
} else if (element instanceof PsiLiteralExpression) {
final PsiLiteralExpression literalExpression = (PsiLiteralExpression) element;
final String text = literalExpression.getText();
final CaretModel model = editor.getCaretModel();
final int offset = model.getOffset() - literalExpression.getTextOffset();
final StringBuilder newLiteralText = new StringBuilder();
final int index1 = indexOfOctalEscape(text, offset);
final int index2 = indexOfOctalEscape(text, offset + 1);
final int escapeStart = index2 == offset ? index2 : index1;
newLiteralText.append(text.substring(0, escapeStart));
final int escapeEnd = appendUnicodeEscape(text, escapeStart, newLiteralText);
newLiteralText.append(text.substring(escapeEnd, text.length()));
PsiReplacementUtil.replaceExpression(literalExpression, newLiteralText.toString());
}
}
use of com.intellij.psi.PsiLiteralExpression in project intellij-community by JetBrains.
the class ShiftUtils method getExpBase2.
public static int getExpBase2(PsiExpression rhs) {
final PsiLiteralExpression literal = (PsiLiteralExpression) rhs;
final Object value = literal.getValue();
if (value == null) {
return 0;
}
final int intValue = ((Number) value).intValue() & 31;
int exp = 1;
for (int i = 0; i < intValue; i++) {
exp <<= 1;
}
return exp;
}
use of com.intellij.psi.PsiLiteralExpression in project intellij-community by JetBrains.
the class ShiftUtils method getLogBase2.
public static int getLogBase2(PsiExpression rhs) {
final PsiLiteralExpression literal = (PsiLiteralExpression) rhs;
final Object value = literal.getValue();
int intValue = ((Number) value).intValue();
int log = 0;
while (intValue % 2 == 0) {
intValue >>= 1;
log++;
}
return log;
}
use of com.intellij.psi.PsiLiteralExpression in project intellij-community by JetBrains.
the class ExpectedTypeBasedCompletionProvider method addCompletions.
@Override
public void addCompletions(@NotNull final CompletionParameters params, final ProcessingContext matchingContext, @NotNull final CompletionResultSet result) {
final PsiElement position = params.getPosition();
if (position.getParent() instanceof PsiLiteralExpression)
return;
addCompletions(params, result, ContainerUtil.newHashSet(JavaSmartCompletionContributor.getExpectedTypes(params)));
}
use of com.intellij.psi.PsiLiteralExpression in project intellij-community by JetBrains.
the class InplaceIntroduceConstantTest method getExpressionFromEditor.
@Nullable
@Override
protected PsiExpression getExpressionFromEditor() {
final PsiExpression expression = super.getExpressionFromEditor();
if (expression != null) {
return expression;
}
final PsiExpression expr = PsiTreeUtil.getParentOfType(getFile().findElementAt(getEditor().getCaretModel().getOffset()), PsiExpression.class);
return expr instanceof PsiLiteralExpression ? expr : null;
}
Aggregations