@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()]); }
@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]); }
@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); }
@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()]); }
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); }