Пример #1
0
  @Nullable
  private KtNamedDeclaration[] getVariables(Expression[] params, ExpressionContext context) {
    if (params.length != 0) return null;

    Project project = context.getProject();
    PsiDocumentManager.getInstance(project).commitAllDocuments();

    PsiFile psiFile =
        PsiDocumentManager.getInstance(project).getPsiFile(context.getEditor().getDocument());
    if (!(psiFile instanceof KtFile)) return null;

    KtExpression contextExpression = findContextExpression(psiFile, context.getStartOffset());
    if (contextExpression == null) return null;

    ResolutionFacade resolutionFacade = ResolutionUtils.getResolutionFacade(contextExpression);

    BindingContext bindingContext =
        resolutionFacade.analyze(contextExpression, BodyResolveMode.FULL);
    LexicalScope scope =
        ScopeUtils.getResolutionScope(contextExpression, bindingContext, resolutionFacade);

    IterableTypesDetector detector =
        resolutionFacade.getIdeService(IterableTypesDetection.class).createDetector(scope);

    DataFlowInfo dataFlowInfo =
        BindingContextUtilsKt.getDataFlowInfo(bindingContext, contextExpression);

    List<VariableDescriptor> filteredDescriptors = new ArrayList<VariableDescriptor>();
    for (DeclarationDescriptor declarationDescriptor : getAllVariables(scope)) {
      if (declarationDescriptor instanceof VariableDescriptor) {
        VariableDescriptor variableDescriptor = (VariableDescriptor) declarationDescriptor;

        if (variableDescriptor.getExtensionReceiverParameter() != null
            && ExtensionUtils.substituteExtensionIfCallableWithImplicitReceiver(
                    variableDescriptor, scope, bindingContext, dataFlowInfo)
                .isEmpty()) {
          continue;
        }

        if (isSuitable(variableDescriptor, project, detector)) {
          filteredDescriptors.add(variableDescriptor);
        }
      }
    }

    List<KtNamedDeclaration> declarations = new ArrayList<KtNamedDeclaration>();
    for (DeclarationDescriptor declarationDescriptor : filteredDescriptors) {
      PsiElement declaration =
          DescriptorToSourceUtils.descriptorToDeclaration(declarationDescriptor);
      assert declaration == null || declaration instanceof PsiNamedElement;

      if (declaration instanceof KtProperty || declaration instanceof KtParameter) {
        declarations.add((KtNamedDeclaration) declaration);
      }
    }

    return declarations.toArray(new KtNamedDeclaration[declarations.size()]);
  }
Пример #2
0
  @Override
  public Result calculateResult(@NotNull Expression[] params, ExpressionContext context) {
    Editor editor = context.getEditor();
    if (editor != null) {
      AnonymousTemplateEditingListener.registerListener(editor, context.getProject());
    }

    PsiNamedElement[] vars = getSupertypes(params, context);
    if (vars == null || vars.length == 0) return null;
    return new JetPsiElementResult(vars[0]);
  }
Пример #3
0
  @Override
  public Result calculateResult(
      @NotNull Expression[] expressions, ExpressionContext expressionContext) {
    PsiFile file =
        PsiDocumentManager.getInstance(expressionContext.getProject())
            .getPsiFile(expressionContext.getEditor().getDocument());

    if (file instanceof LuaPsiFile) return new TextResult(file.getName());

    return null;
  }
 public Result calculateResult(ExpressionContext context) {
   TemplateState templateState = TemplateManagerImpl.getTemplateState(context.getEditor());
   final TextResult insertedValue =
       templateState != null
           ? templateState.getVariableValue(InplaceRefactoring.PRIMARY_VARIABLE_NAME)
           : null;
   if (insertedValue != null) {
     if (!insertedValue.getText().isEmpty()) return insertedValue;
   }
   return new TextResult(myName);
 }
Пример #5
0
  @Nullable
  private static PsiNamedElement[] getSupertypes(Expression[] params, ExpressionContext context) {
    if (params.length != 0) return null;

    Project project = context.getProject();
    PsiDocumentManager.getInstance(project).commitAllDocuments();

    PsiFile psiFile =
        PsiDocumentManager.getInstance(project).getPsiFile(context.getEditor().getDocument());
    if (!(psiFile instanceof JetFile)) return null;

    JetExpression expression =
        PsiTreeUtil.getParentOfType(
            psiFile.findElementAt(context.getStartOffset()), JetExpression.class);
    if (expression == null) return null;

    BindingContext bc = ResolutionUtils.analyze(expression, BodyResolveMode.FULL);
    JetScope scope = bc.get(BindingContext.RESOLUTION_SCOPE, expression);
    if (scope == null) return null;

    List<PsiNamedElement> result = new ArrayList<PsiNamedElement>();

    for (DeclarationDescriptor descriptor :
        scope.getDescriptors(
            DescriptorKindFilter.NON_SINGLETON_CLASSIFIERS,
            JetScope.Companion.getALL_NAME_FILTER())) {
      if (!(descriptor instanceof ClassDescriptor)) continue;
      ClassDescriptor classDescriptor = (ClassDescriptor) descriptor;
      if (!classDescriptor.getModality().isOverridable()) continue;
      ClassKind kind = classDescriptor.getKind();
      if (kind == ClassKind.INTERFACE || kind == ClassKind.CLASS) {
        PsiElement declaration = DescriptorToSourceUtils.descriptorToDeclaration(descriptor);
        if (declaration != null) {
          result.add((PsiNamedElement) declaration);
        }
      }
    }

    return result.toArray(new PsiNamedElement[result.size()]);
  }
Пример #6
0
  public LookupElement[] calculateLookupItems(ExpressionContext context) {
    if (myItems.size() <= 1) return null;
    PsiDocumentManager.getInstance(context.getProject()).commitAllDocuments();

    List<LookupElement> result = new ArrayList<LookupElement>(myItems.size());
    for (final SmartTypePointer item : myItems) {
      final PsiType type = item.getType();
      if (type != null) {
        result.add(PsiTypeLookupItem.createLookupItem(type, null));
      }
    }
    return result.toArray(new LookupElement[result.size()]);
  }
  private void invokeCompletion(final ExpressionContext context) {
    final Project project = context.getProject();
    final Editor editor = context.getEditor();

    final PsiFile psiFile = editor != null ? PsiUtilBase.getPsiFileInEditor(editor, project) : null;
    Runnable runnable =
        () -> {
          if (project.isDisposed()
              || editor == null
              || editor.isDisposed()
              || psiFile == null
              || !psiFile.isValid()) return;

          // it's invokeLater, so another completion could have started
          if (CompletionServiceImpl.getCompletionService().getCurrentCompletion() != null) return;

          CommandProcessor.getInstance()
              .executeCommand(
                  project,
                  () -> {
                    // if we're in some completion's insert handler, make sure our new completion
                    // isn't treated as the second invocation
                    CompletionServiceImpl.setCompletionPhase(CompletionPhase.NoCompletion);

                    invokeCompletionHandler(project, editor);
                    Lookup lookup = LookupManager.getInstance(project).getActiveLookup();

                    if (lookup != null) {
                      lookup.addLookupListener(
                          new MyLookupListener(context, myCheckCompletionChar));
                    }
                  },
                  "",
                  null);
        };
    ApplicationManager.getApplication().invokeLater(runnable);
  }
  @Override
  public Result calculateResult(@NotNull Expression[] params, ExpressionContext context) {
    if (params.length != 1) return null;
    final Result result = params[0].calculateResult(context);
    if (result == null) return null;

    Project project = context.getProject();

    PsiExpression expr = MacroUtil.resultToPsiExpression(result, context);
    if (expr == null) return null;
    PsiType type = expr.getType();

    if (type instanceof PsiArrayType) {
      return new PsiTypeResult(((PsiArrayType) type).getComponentType(), project);
    }

    if (type instanceof PsiClassType) {
      PsiClassType.ClassResolveResult resolveResult = ((PsiClassType) type).resolveGenerics();
      PsiClass aClass = resolveResult.getElement();

      if (aClass != null) {
        PsiClass iterableClass =
            JavaPsiFacade.getInstance(project)
                .findClass("java.lang.Iterable", aClass.getResolveScope());
        if (iterableClass != null) {
          PsiSubstitutor substitutor =
              TypeConversionUtil.getClassSubstitutor(
                  iterableClass, aClass, resolveResult.getSubstitutor());
          if (substitutor != null) {
            PsiType parameterType = substitutor.substitute(iterableClass.getTypeParameters()[0]);
            if (parameterType instanceof PsiCapturedWildcardType) {
              parameterType = ((PsiCapturedWildcardType) parameterType).getWildcard();
            }
            if (parameterType != null) {
              if (parameterType instanceof PsiWildcardType) {
                if (((PsiWildcardType) parameterType).isExtends()) {
                  return new PsiTypeResult(((PsiWildcardType) parameterType).getBound(), project);
                } else return null;
              }
              return new PsiTypeResult(parameterType, project);
            }
          }
        }
      }
    }

    return null;
  }
Пример #9
0
  public Result calculateResult(ExpressionContext context) {
    final Project project = context.getProject();
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    if (myItems.isEmpty()) return null;

    final PsiType type = myItems.iterator().next().getType();
    return type == null
        ? null
        : new PsiTypeResult(type, project) {
          @Override
          public void handleRecalc(
              PsiFile psiFile, Document document, int segmentStart, int segmentEnd) {
            if (myItems.size() <= 1)
              super.handleRecalc(psiFile, document, segmentStart, segmentEnd);
          }
        };
  }
    @Override
    public void itemSelected(LookupEvent event) {
      LookupElement item = event.getItem();
      if (item == null) return;

      char c = event.getCompletionChar();
      if (myCheckCompletionChar && !LookupEvent.isSpecialCompletionChar(c)) {
        return;
      }

      final Project project = myContext.getProject();
      if (project == null) {
        return;
      }

      Runnable runnable =
          () ->
              new WriteCommandAction(project) {
                @Override
                protected void run(@NotNull com.intellij.openapi.application.Result result)
                    throws Throwable {
                  Editor editor = myContext.getEditor();
                  if (editor != null) {
                    TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
                    if (templateState != null) {
                      templateState.considerNextTabOnLookupItemSelected(item);
                    }
                  }
                }
              }.execute();
      if (ApplicationManager.getApplication().isUnitTestMode()) {
        runnable.run();
      } else {
        ApplicationManager.getApplication()
            .invokeLater(runnable, ModalityState.current(), project.getDisposed());
      }
    }