List of usage examples for com.intellij.openapi.actionSystem IdeActions ACTION_CODE_COMPLETION
String ACTION_CODE_COMPLETION
To view the source code for com.intellij.openapi.actionSystem IdeActions ACTION_CODE_COMPLETION.
Click Source Link
From source file:org.intellij.erlang.completion.ErlangCompletionContributor.java
License:Apache License
public ErlangCompletionContributor() { extend(CompletionType.BASIC, psiElement().inFile(instanceOf(ErlangFile.class)), new CompletionProvider<CompletionParameters>() { @Override//from w ww.ja v a2 s. co m protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) { PsiElement position = parameters.getPosition(); PsiFile file = position.getContainingFile(); if (ErlangParserUtil.isApplicationConfigFileType(file)) return; boolean inConsole = ErlangParserUtil.isConsole(file); PsiElement parent = position.getParent(); if (parent instanceof ErlangAtom) { // This assignment makes the code below work the same way as it did before ErlangAtom stopped being a leaf element. parent = parent.getParent(); } PsiElement grandPa = parent.getParent(); PsiElement originalPosition = parameters.getOriginalPosition(); PsiElement originalParent = originalPosition != null ? originalPosition.getParent() : null; if (originalParent instanceof ErlangIncludeString && originalPosition instanceof LeafPsiElement && ErlangTypes.ERL_STRING == ((LeafPsiElement) originalPosition).getElementType()) { TextRange range = new TextRange( ((LeafPsiElement) originalPosition).getStartOffset() + 1, parameters.getOffset()); String includeText = range.getLength() >= 0 ? range.substring(file.getText()) : ""; if (grandPa instanceof ErlangInclude) { result.addAllElements(getModulePathLookupElements(file, includeText)); } else if (grandPa instanceof ErlangIncludeLib) { result.addAllElements(getLibPathLookupElements(file, includeText)); } } if (originalParent instanceof ErlangStringLiteral || originalPosition instanceof PsiComment) return; if (grandPa instanceof ErlangType) { result.addAllElements(getTypeLookupElements(file, true, false)); } if (originalParent instanceof ErlangRecordExpression || prevIsRadix(originalPosition) || prevIsRadix(grandPa)) { result.addAllElements(getRecordLookupElements(file)); } else if (grandPa instanceof ErlangExportFunction && file instanceof ErlangFile) { result.addAllElements( createFunctionLookupElements(((ErlangFile) file).getFunctions(), true)); } else { ErlangColonQualifiedExpression colonQualified = PsiTreeUtil.getParentOfType(position, ErlangColonQualifiedExpression.class); if (colonQualified != null && (PsiTreeUtil.getParentOfType(position, ErlangClauseBody.class) != null || inConsole)) { ErlangQAtom moduleAtom = getQAtom(colonQualified); result.addAllElements(getFunctionLookupElements(file, false, moduleAtom)); // when completing at my_module:<caret>, ... the cursor is actually located at comma and not at the colon qualified expression ErlangColonQualifiedExpression originalColonQExpr = PsiTreeUtil .getParentOfType(originalPosition, ErlangColonQualifiedExpression.class); String moduleName = moduleAtom != null ? getName(moduleAtom) : null; String prefix = originalColonQExpr != null ? StringUtil.first(originalColonQExpr.getText(), parameters.getOffset() - originalColonQExpr.getTextOffset(), false) : moduleName != null ? moduleName + ":" : null; (StringUtil.isEmpty(prefix) ? result : result.withPrefixMatcher( result.getPrefixMatcher().cloneWithPrefix(prefix))).addAllElements( getAllExportedFunctionsWithModuleLookupElements( file.getProject(), false, moduleName)); } else if (grandPa instanceof ErlangRecordField || grandPa instanceof ErlangRecordTuple) { Pair<List<ErlangTypedExpr>, List<ErlangQAtom>> recordFields = getRecordFields( grandPa); final boolean withoutEq = is(grandPa.getFirstChild(), ErlangTypes.ERL_DOT); result.addAllElements(ContainerUtil.map(recordFields.first, e -> createFieldLookupElement(e.getProject(), e.getName(), withoutEq))); result.addAllElements(ContainerUtil.map(recordFields.second, a -> createFieldLookupElement(a.getProject(), a.getText(), withoutEq))); return; } else if (grandPa instanceof ErlangMacros) { return; } else if (PsiTreeUtil.getParentOfType(position, ErlangExport.class) == null) { //noinspection unchecked boolean inside = PsiTreeUtil.getParentOfType(position, ErlangClauseBody.class, ErlangFunTypeSigs.class, ErlangTypeRef.class) != null; //noinspection unchecked boolean insideImport = PsiTreeUtil.getParentOfType(position, ErlangImportDirective.class, ErlangImportFunctions.class) instanceof ErlangImportDirective; boolean insideBehaviour = PsiTreeUtil.getParentOfType(position, ErlangBehaviour.class) != null; if (inside || inConsole && !isDot(position) || insideImport) { boolean withColon = !insideImport && null == PsiTreeUtil .getParentOfType(position, ErlangFunctionCallExpression.class, false); suggestModules(result, position, withColon); } else if (insideBehaviour) { suggestBehaviours(result, position); } } if (colonQualified == null && grandPa instanceof ErlangExpression && (inFunction(position) || inConsole || PsiTreeUtil.getParentOfType(position, ErlangTypedRecordFields.class) != null)) { result.addAllElements(getFunctionLookupElements(file, false, null)); result.addAllElements(getAllExportedFunctionsWithModuleLookupElements( file.getProject(), false, null)); } int invocationCount = parameters.getInvocationCount(); boolean moduleScope = invocationCount > 0 && invocationCount % 2 == 0; boolean moduleWithDeps = invocationCount > 0 && invocationCount % 3 == 0; if (moduleScope || moduleWithDeps) { Project project = file.getProject(); Module module = ModuleUtilCore.findModuleForPsiElement(position); GlobalSearchScope scope = module != null && moduleScope ? GlobalSearchScope.moduleScope(module) : ProjectScope.getProjectScope(project); Collection<String> names = ErlangAtomIndex.getNames(project, scope); for (String name : names) { result.addElement(PrioritizedLookupElement.withPriority( LookupElementBuilder.create(name).withLookupString(name.toLowerCase()) .withIcon(ErlangIcons.ATOM), ATOM_PRIORITY)); } } String shortcut = getActionShortcut(IdeActions.ACTION_CODE_COMPLETION); if (invocationCount == 1 && new Random().nextBoolean()) { result.addLookupAdvertisement("Press " + shortcut + " to activate atom completion from application scope"); } if (moduleScope) { result.addLookupAdvertisement( "Press " + shortcut + " to activate atom completion from project scope"); } } // fun foo/n if (!(parent instanceof ErlangRecordExpression) && grandPa instanceof ErlangFunctionWithArityVariables) { result.addAllElements(ErlangPsiImplUtil.getFunctionLookupElements(file, true, null)); } } private boolean isDot(@NotNull PsiElement position) { PsiElement dot = PsiTreeUtil.prevVisibleLeaf(position); return dot != null && dot.getNode().getElementType() == ErlangTypes.ERL_DOT; } }); extend(CompletionType.SMART, psiElement().inside(true, psiElement(ErlangArgumentList.class)), new CompletionProvider<CompletionParameters>() { @Override protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) { PsiElement position = parameters.getPosition(); Set<ErlangExpressionType> expectedTypes = ErlangCompletionUtil .expectedArgumentTypes(position); if (expectedTypes.isEmpty()) return; List<LookupElement> functionLookupElements = getFunctionLookupElements( position.getContainingFile(), false, null); for (LookupElement lookupElement : functionLookupElements) { ErlangFunction function = ObjectUtils.tryCast(lookupElement.getPsiElement(), ErlangFunction.class); ErlangExpressionType type = function != null ? ErlangExpressionType.calculateFunctionType(function) : null; if (type != null && ErlangCompletionUtil.containsType(expectedTypes, type)) { result.addElement(lookupElement); } } } }); }
From source file:org.intellij.erlang.ErlangCompletionContributor.java
License:Apache License
public ErlangCompletionContributor() { extend(CompletionType.BASIC, psiElement().inFile(instanceOf(ErlangFileImpl.class)), new CompletionProvider<CompletionParameters>() { @Override/*from w ww . j av a 2 s .co m*/ protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) { PsiElement position = parameters.getPosition(); PsiFile file = position.getContainingFile(); if (ErlangParserUtil.isApplicationConfigFileType(file)) return; boolean inConsole = ErlangParserUtil.isConsole(file); PsiElement parent = position.getParent().getParent(); PsiElement originalPosition = parameters.getOriginalPosition(); PsiElement originalParent = originalPosition != null ? originalPosition.getParent() : null; if (originalParent instanceof ErlangIncludeString && originalPosition instanceof LeafPsiElement && ErlangTypes.ERL_STRING == ((LeafPsiElement) originalPosition).getElementType()) { TextRange range = new TextRange( ((LeafPsiElement) originalPosition).getStartOffset() + 1, parameters.getOffset()); String includeText = range.getLength() >= 0 ? range.substring(file.getText()) : ""; if (parent instanceof ErlangInclude) { result.addAllElements(getModulePathLookupElements(file, includeText)); } else if (parent instanceof ErlangIncludeLib) { result.addAllElements(getLibPathLookupElements(file, includeText)); } } if (originalParent instanceof ErlangStringLiteral || originalPosition instanceof PsiComment) return; if (parent instanceof ErlangType) { result.addAllElements(ErlangPsiImplUtil.getTypeLookupElements(file, true, false)); } if (originalParent instanceof ErlangRecordExpression || prevIsRadix(originalPosition) || prevIsRadix(parent)) { result.addAllElements(ErlangPsiImplUtil.getRecordLookupElements(file)); } else if (originalParent instanceof ErlangExportFunctions && file instanceof ErlangFile) { result.addAllElements(ErlangPsiImplUtil .createFunctionLookupElements(((ErlangFile) file).getFunctions(), true)); } else { ErlangColonQualifiedExpression colonQualified = PsiTreeUtil.getParentOfType(position, ErlangColonQualifiedExpression.class); if (colonQualified != null && (PsiTreeUtil.getParentOfType(position, ErlangClauseBody.class) != null || inConsole)) { ErlangQAtom qAtom = ErlangPsiImplUtil.getQAtom(colonQualified); result.addAllElements( ErlangPsiImplUtil.getFunctionLookupElements(file, false, qAtom)); } else if (parent instanceof ErlangRecordField || parent instanceof ErlangRecordFields) { Pair<List<ErlangTypedExpr>, List<ErlangQAtom>> recordFields = ErlangPsiImplUtil .getRecordFields(parent); final boolean withoutEq = is(parent.getFirstChild(), ErlangTypes.ERL_DOT); result.addAllElements(ContainerUtil.map(recordFields.first, new Function<ErlangTypedExpr, LookupElement>() { @Override public LookupElement fun(ErlangTypedExpr a) { return createFieldLookupElement(a.getName(), withoutEq); } })); result.addAllElements(ContainerUtil.map(recordFields.second, new Function<ErlangQAtom, LookupElement>() { @Override public LookupElement fun(ErlangQAtom a) { return createFieldLookupElement(a.getText(), withoutEq); } })); return; } else if (parent instanceof ErlangMacros) { return; } else if (PsiTreeUtil.getParentOfType(position, ErlangExport.class) == null) { for (String keyword : suggestKeywords(position)) { result.addElement(createKeywordLookupElement(keyword)); } int invocationCount = parameters.getInvocationCount(); boolean moduleCompletion = invocationCount > 0 && invocationCount % 2 == 0; //noinspection unchecked boolean inside = PsiTreeUtil.getParentOfType(position, ErlangClauseBody.class, ErlangFunTypeSigs.class, ErlangTypeRef.class) != null; if ((inside || inConsole) && moduleCompletion) { suggestModules(result, position, true); } else { //noinspection unchecked if (PsiTreeUtil.getParentOfType(position, ErlangImportDirective.class, ErlangImportFunctions.class) instanceof ErlangImportDirective) { suggestModules(result, position, false); } else { String shortcut = getActionShortcut(IdeActions.ACTION_CODE_COMPLETION); CompletionService.getCompletionService().setAdvertisementText( shortcut + " to activate module name completion"); } } } if (colonQualified == null && parent instanceof ErlangExpression && (ErlangPsiImplUtil.inFunction(position) || inConsole)) { result.addAllElements( ErlangPsiImplUtil.getFunctionLookupElements(file, false, null)); } } } }); }