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;
}
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;
}
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);
}
Aggregations