Search in sources :

Example 1 with DartReference

use of com.jetbrains.lang.dart.psi.DartReference 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 DartReference

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

the class DartServerFindUsagesHandler method getUsagePsiElement.

@Nullable
public static PsiElement getUsagePsiElement(@NotNull final PsiFile psiFile, @NotNull final TextRange textRange) {
    // try to find DartReference matching textRange. If not possible then return the topmost element matching textRange.
    // If neither found then return minimal element that includes the textRange.
    PsiElement element = psiFile.findElementAt(textRange.getStartOffset());
    if (element == null)
        return null;
    boolean rangeOk = element.getTextRange().contains(textRange);
    if (rangeOk && element instanceof DartReference)
        return element;
    TextRange previousRange = element.getTextRange();
    PsiElement parent;
    while ((parent = element.getParent()) != null) {
        final TextRange parentRange = parent.getTextRange();
        if (rangeOk) {
            if (!parentRange.equals(previousRange)) {
                // range became bigger, return previous that matched better
                return element;
            }
            if (parent instanceof DartReference) {
                return parent;
            } else {
                previousRange = parentRange;
                element = parent;
            }
        } else {
            rangeOk = parent.getTextRange().contains(textRange);
            if (rangeOk && parent instanceof DartReference) {
                return parent;
            } else {
                previousRange = parentRange;
                element = parent;
            }
        }
    }
    return null;
}
Also used : DartReference(com.jetbrains.lang.dart.psi.DartReference) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with DartReference

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

the class DartTypeHierarchyProvider method getTarget.

@Nullable
@Override
public DartClass getTarget(@NotNull final DataContext dataContext) {
    final Project project = CommonDataKeys.PROJECT.getData(dataContext);
    final Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
    if (project == null || editor == null)
        return null;
    final PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
    final PsiElement psiElement = file == null ? null : file.findElementAt(editor.getCaretModel().getOffset());
    final DartReference dartReference = PsiTreeUtil.getParentOfType(psiElement, DartReference.class);
    if (dartReference != null) {
        return dartReference.resolveDartClass().getDartClass();
    }
    return PsiTreeUtil.getParentOfType(psiElement, DartClass.class);
}
Also used : Project(com.intellij.openapi.project.Project) DartReference(com.jetbrains.lang.dart.psi.DartReference) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

DartReference (com.jetbrains.lang.dart.psi.DartReference)3 PsiElement (com.intellij.psi.PsiElement)2 Nullable (org.jetbrains.annotations.Nullable)2 Editor (com.intellij.openapi.editor.Editor)1 Project (com.intellij.openapi.project.Project)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiFile (com.intellij.psi.PsiFile)1 DartCallExpression (com.jetbrains.lang.dart.psi.DartCallExpression)1 DartClass (com.jetbrains.lang.dart.psi.DartClass)1 DartExpression (com.jetbrains.lang.dart.psi.DartExpression)1