Search in sources :

Example 1 with DartExpression

use of com.jetbrains.lang.dart.psi.DartExpression in project intellij-plugins by JetBrains.

the class DartNameSuggesterUtil method getSuggestedNames.

public static Collection<String> getSuggestedNames(final DartExpression expression, @Nullable Collection<String> additionalUsedNames) {
    Collection<String> candidates = new LinkedHashSet<>();
    String text = expression.getText();
    if (expression instanceof DartReference) {
        DartClass dartClass = ((DartReference) expression).resolveDartClass().getDartClass();
        String dartClassName = dartClass == null ? null : dartClass.getName();
        if (dartClassName != null && !dartClassName.equals(StringUtil.decapitalize(dartClassName))) {
            candidates.add(StringUtil.decapitalize(dartClassName));
        }
    }
    if (expression instanceof DartCallExpression) {
        final DartExpression callee = ((DartCallExpression) expression).getExpression();
        text = callee.getText();
    }
    if (text != null) {
        candidates.addAll(generateNames(text));
    }
    final Set<String> usedNames = DartRefactoringUtil.collectUsedNames(expression);
    if (additionalUsedNames != null && !additionalUsedNames.isEmpty()) {
        usedNames.addAll(additionalUsedNames);
    }
    final Collection<String> result = new ArrayList<>();
    for (String candidate : candidates) {
        int index = 0;
        String suffix = "";
        while (usedNames.contains(candidate + suffix)) {
            suffix = Integer.toString(++index);
        }
        result.add(candidate + suffix);
    }
    if (result.isEmpty()) {
        // never empty
        result.add("o");
    }
    return result;
}
Also used : DartClass(com.jetbrains.lang.dart.psi.DartClass) DartReference(com.jetbrains.lang.dart.psi.DartReference) DartCallExpression(com.jetbrains.lang.dart.psi.DartCallExpression) DartExpression(com.jetbrains.lang.dart.psi.DartExpression)

Example 2 with DartExpression

use of com.jetbrains.lang.dart.psi.DartExpression in project intellij-plugins by JetBrains.

the class DartWithExpressionSurrounder method surroundElements.

@Nullable
public TextRange surroundElements(@NotNull Project project, @NotNull Editor editor, @NotNull PsiElement[] elements) throws IncorrectOperationException {
    PsiElement source = elements[0];
    final DartExpression parenthExprNode = getSurroundedNode(source);
    if (parenthExprNode == null) {
        throw new IncorrectOperationException("Can't create expression for: " + source.getText());
    }
    final PsiElement replace = source.replace(parenthExprNode);
    final int endOffset = replace.getTextRange().getEndOffset();
    return TextRange.create(endOffset, endOffset);
}
Also used : IncorrectOperationException(com.intellij.util.IncorrectOperationException) PsiElement(com.intellij.psi.PsiElement) DartExpression(com.jetbrains.lang.dart.psi.DartExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with DartExpression

use of com.jetbrains.lang.dart.psi.DartExpression in project intellij-plugins by JetBrains.

the class DartIntroduceTestBase method buildSuggestions.

protected Collection<String> buildSuggestions(Class<? extends DartExpression> parentClass) {
    myFixture.configureByFile(getTestName(false) + getFileExtension());
    DartExpression expr = PsiTreeUtil.getParentOfType(myFixture.getFile().findElementAt(myFixture.getEditor().getCaretModel().getOffset()), parentClass);
    return DartNameSuggesterUtil.getSuggestedNames(expr);
}
Also used : DartExpression(com.jetbrains.lang.dart.psi.DartExpression)

Example 4 with DartExpression

use of com.jetbrains.lang.dart.psi.DartExpression in project intellij-plugins by JetBrains.

the class DartServerExtractLocalVariableDialog method perform.

public void perform() {
    final SelectionModel selectionModel = editor.getSelectionModel();
    final int offset = selectionModel.getSelectionStart();
    final int length = selectionModel.getSelectionEnd() - offset;
    // create refactoring
    createRefactoring(offset, length);
    if (refactoring == null) {
        return;
    }
    // prepare expressions
    final java.util.List<DartExpression> expressions;
    {
        final int[] offsets = refactoring.getCoveringExpressionOffsets();
        final int[] lengths = refactoring.getCoveringExpressionLengths();
        expressions = getDartExpressions(offsets, lengths);
        if (expressions == null) {
            return;
        }
    }
    // select the expression to extract
    if (expressions.size() == 1 || ApplicationManager.getApplication().isUnitTestMode()) {
        performOnExpression(expressions.get(0));
    } else if (expressions.size() > 1) {
        IntroduceTargetChooser.showChooser(editor, expressions, new Pass<DartExpression>() {

            @Override
            public void pass(DartExpression expression) {
                performOnExpression(expression);
            }
        }, PsiElement::getText);
    }
}
Also used : Pass(com.intellij.openapi.util.Pass) SelectionModel(com.intellij.openapi.editor.SelectionModel) DartExpression(com.jetbrains.lang.dart.psi.DartExpression)

Example 5 with DartExpression

use of com.jetbrains.lang.dart.psi.DartExpression in project intellij-plugins by JetBrains.

the class DartServerExtractLocalVariableDialog method getDartExpressions.

@Nullable
private List<DartExpression> getDartExpressions(int[] offsets, int[] lengths) {
    final List<DartExpression> expressions = Lists.newArrayList();
    for (int i = 0; i < offsets.length; i++) {
        final DartExpression expression = findExpressionWithRange(offsets[i], lengths[i]);
        if (expression == null) {
            return null;
        }
        expressions.add(expression);
    }
    return expressions;
}
Also used : DartExpression(com.jetbrains.lang.dart.psi.DartExpression) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

DartExpression (com.jetbrains.lang.dart.psi.DartExpression)6 Nullable (org.jetbrains.annotations.Nullable)3 SelectionModel (com.intellij.openapi.editor.SelectionModel)1 Pass (com.intellij.openapi.util.Pass)1 PsiElement (com.intellij.psi.PsiElement)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 DartArgumentList (com.jetbrains.lang.dart.psi.DartArgumentList)1 DartArguments (com.jetbrains.lang.dart.psi.DartArguments)1 DartCallExpression (com.jetbrains.lang.dart.psi.DartCallExpression)1 DartClass (com.jetbrains.lang.dart.psi.DartClass)1 DartReference (com.jetbrains.lang.dart.psi.DartReference)1