use of com.intellij.lang.javascript.psi.resolve.JSSimpleTypeProcessor in project oxy-template-support-plugin by mutant-industries.
the class JavaContext method fillCompletionVariants.
@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
Module module;
PsiElement element = parameters.getOriginalFile().getViewProvider().findElementAt(parameters.getOffset() - 1, OxyTemplateInnerJs.INSTANCE);
if (element == null || element.getNode().getElementType() != JSTokenTypes.DOT && (element = element.getPrevSibling()) == null) {
return;
}
if (element.getNode().getElementType() == TokenType.WHITE_SPACE) {
element = element.getPrevSibling();
}
while (element != null && element.getNode().getElementType() != JSTokenTypes.DOT) {
element = element.getLastChild();
if (element instanceof PsiErrorElement) {
element = element.getPrevSibling();
}
}
if (element == null || element.getNode().getElementType() != JSTokenTypes.DOT || (element = element.getPrevSibling()) == null) {
return;
}
if (element.getNode().getElementType() == TokenType.WHITE_SPACE) {
element = element.getPrevSibling();
}
if (!(element instanceof JSExpression) || (module = ModuleUtilCore.findModuleForPsiElement(parameters.getOriginalFile())) == null) {
return;
}
JSSimpleTypeProcessor typeProcessor = new JSSimpleTypeProcessor();
JSTypeEvaluator.evaluateTypes((JSExpression) element, parameters.getOriginalFile().getViewProvider().getPsi(OxyTemplateInnerJs.INSTANCE), typeProcessor);
JSType type = typeProcessor.getType();
List<String> possibleJavaTypes = new LinkedList<>();
if (type != null) {
if (type instanceof JSCompositeTypeImpl) {
possibleJavaTypes.addAll(((JSCompositeTypeImpl) type).getTypes().stream().filter(jsType -> jsType instanceof JSTypeImpl).map(JSType::getTypeText).collect(Collectors.toList()));
} else if (type instanceof JSTypeImpl) {
possibleJavaTypes.add(type.getTypeText());
}
}
JavaPsiFacade facade = JavaPsiFacade.getInstance(parameters.getOriginalFile().getProject());
GlobalSearchScope scope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module);
List<PsiClass> possibleInstancesOf = new LinkedList<>();
boolean suggestionsFound = false;
PsiClass aClass;
for (String possibleType : possibleJavaTypes) {
if ((aClass = facade.findClass(possibleType, scope)) != null) {
possibleInstancesOf.add(aClass);
possibleInstancesOf.addAll(ExtenderProvider.getExtenders(aClass));
}
}
for (PsiClass psiClass : possibleInstancesOf) {
List<String> alreadySuggested = new LinkedList<>();
for (PsiMethod method : psiClass.getAllMethods()) {
if (method.getContainingClass() == null || Object.class.getName().equals(method.getContainingClass().getQualifiedName()) || method.getReturnType() == null || shippedBaseExtenderMethods.contains(method.getName()) || !method.getModifierList().hasModifierProperty(PsiModifier.PUBLIC) || InheritanceUtil.isInheritor(method.getContainingClass(), EXTENDER_INTERFACE_FQN) && !InheritanceUtil.isInheritor(method.getContainingClass(), EXTENDER_BASE_CLASS_FQN)) {
continue;
}
String insertText = method.getName();
String presentableText = method.getName();
if (insertText.matches("((^is)|(^get)|(^set))[A-Z].*")) {
insertText = presentableText = StringUtils.decapitalize(insertText.replaceFirst("(^is)|(^get)|(^set)", ""));
} else {
presentableText = method.getPresentation() == null ? presentableText + "()" : method.getPresentation().getPresentableText();
insertText += "()";
}
if (alreadySuggested.contains(insertText)) {
continue;
}
result.consume(LookupElementBuilder.create(method, insertText).withIcon(method.getIcon(0)).withTypeText(method.getReturnType().getPresentableText(), true).withTailText(" (" + psiClass.getContainingFile().getName() + ")", true).withPresentableText(presentableText).withInsertHandler(new TrailingPatternConsumer(INSERT_CONSUME)).withAutoCompletionPolicy(AutoCompletionPolicy.GIVE_CHANCE_TO_OVERWRITE));
alreadySuggested.add(insertText);
suggestionsFound = true;
}
}
if (suggestionsFound) {
result.stopHere();
}
}
use of com.intellij.lang.javascript.psi.resolve.JSSimpleTypeProcessor in project oxy-template-support-plugin by mutant-industries.
the class InnerJsTypeEvaluator method checkForEachDefinition.
// -----------------------------------------------------------------------------------------------------------------
@Nullable
private static JSType checkForEachDefinition(@NotNull final PsiElement element) {
PsiElement elementLocal = element;
if (elementLocal.getParent() instanceof JSVarStatement) {
elementLocal = elementLocal.getParent();
}
// repeat macro - var keyword is missing
if (elementLocal instanceof PsiPackage || !(elementLocal.getFirstChild() instanceof JSVariable)) {
return null;
}
PsiElement elementAt = elementLocal.getContainingFile().getViewProvider().findElementAt(elementLocal.getNode().getStartOffset(), OxyTemplate.INSTANCE);
assert elementAt != null;
MacroAttribute attribute = PsiTreeUtil.getParentOfType(elementAt, MacroAttribute.class);
if (attribute == null || !MacroIndex.REPEAT_MACRO_VARIABLE_DEFINITION.equals(attribute.getMacroParamName().getText())) {
return null;
}
MacroCall macroCall = PsiTreeUtil.getParentOfType(attribute, MacroCall.class);
assert macroCall != null;
for (MacroAttribute macroAttribute : macroCall.getMacroAttributeList()) {
if (MacroIndex.REPEAT_MACRO_LIST_DEFINITION.equals(macroAttribute.getMacroParamName().getText())) {
MacroParam macroParam;
if ((macroParam = macroAttribute.getMacroParam()) == null) {
return null;
}
PsiElement list = elementLocal.getContainingFile().getViewProvider().findElementAt(macroParam.getNode().getStartOffset() + macroParam.getTextLength() - 1, OxyTemplateInnerJs.INSTANCE);
JSReferenceExpression statement = PsiTreeUtil.getParentOfType(list, JSReferenceExpression.class);
if (statement != null) {
JSSimpleTypeProcessor typeProcessor = new JSSimpleTypeProcessor();
JSTypeEvaluator.evaluateTypes(statement, statement.getContainingFile(), typeProcessor);
if (typeProcessor.getType() instanceof JSArrayTypeImpl) {
return ((JSArrayTypeImpl) typeProcessor.getType()).getType();
}
}
}
}
return null;
}
Aggregations