Search in sources :

Example 11 with PsiLiteralExpression

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());
    }
}
Also used : PsiLiteralExpression(com.intellij.psi.PsiLiteralExpression) CaretModel(com.intellij.openapi.editor.CaretModel) SelectionModel(com.intellij.openapi.editor.SelectionModel) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document)

Example 12 with PsiLiteralExpression

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;
}
Also used : PsiLiteralExpression(com.intellij.psi.PsiLiteralExpression)

Example 13 with PsiLiteralExpression

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;
}
Also used : PsiLiteralExpression(com.intellij.psi.PsiLiteralExpression)

Example 14 with PsiLiteralExpression

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)));
}
Also used : PsiLiteralExpression(com.intellij.psi.PsiLiteralExpression) PsiElement(com.intellij.psi.PsiElement)

Example 15 with PsiLiteralExpression

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;
}
Also used : PsiLiteralExpression(com.intellij.psi.PsiLiteralExpression) PsiExpression(com.intellij.psi.PsiExpression) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PsiLiteralExpression (com.intellij.psi.PsiLiteralExpression)23 PsiType (com.intellij.psi.PsiType)6 PsiElement (com.intellij.psi.PsiElement)5 NonNls (org.jetbrains.annotations.NonNls)4 Nullable (org.jetbrains.annotations.Nullable)4 TextRange (com.intellij.openapi.util.TextRange)2 PsiExpression (com.intellij.psi.PsiExpression)2 I18nQuickFixHandler (com.intellij.codeInspection.i18n.I18nQuickFixHandler)1 I18nizeAction (com.intellij.codeInspection.i18n.I18nizeAction)1 PropertiesFile (com.intellij.lang.properties.psi.PropertiesFile)1 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)1 DataContext (com.intellij.openapi.actionSystem.DataContext)1 CaretModel (com.intellij.openapi.editor.CaretModel)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 SelectionModel (com.intellij.openapi.editor.SelectionModel)1 PsiBinaryExpression (com.intellij.psi.PsiBinaryExpression)1 PsiField (com.intellij.psi.PsiField)1 PsiFile (com.intellij.psi.PsiFile)1 PsiMethod (com.intellij.psi.PsiMethod)1