Search in sources :

Example 1 with SuggestedNameInfo

use of com.intellij.psi.codeStyle.SuggestedNameInfo in project intellij-community by JetBrains.

the class ParameterNameExpression method calculateResult.

@Override
public Result calculateResult(ExpressionContext context) {
    PsiDocumentManager.getInstance(context.getProject()).commitDocument(context.getEditor().getDocument());
    SuggestedNameInfo info = getNameInfo(context);
    if (info == null)
        return new TextResult("p");
    String[] names = info.names;
    if (names.length > 0) {
        return new TextResult(names[0]);
    }
    return null;
}
Also used : TextResult(com.intellij.codeInsight.template.TextResult) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo)

Example 2 with SuggestedNameInfo

use of com.intellij.psi.codeStyle.SuggestedNameInfo in project intellij-community by JetBrains.

the class DictionarySuggestionProvider method getSuggestedNames.

@Override
public SuggestedNameInfo getSuggestedNames(PsiElement element, PsiElement nameSuggestionContext, Set<String> result) {
    assert result != null;
    if (!active || nameSuggestionContext == null) {
        return null;
    }
    String text = nameSuggestionContext.getText();
    if (nameSuggestionContext instanceof PsiNamedElement) {
        //noinspection ConstantConditions
        text = ((PsiNamedElement) element).getName();
    }
    if (text == null) {
        return null;
    }
    Project project = element.getProject();
    SpellCheckerManager manager = SpellCheckerManager.getInstance(project);
    manager.getSuggestions(text).stream().filter(newName -> RenameUtil.isValidName(project, element, newName)).forEach(result::add);
    return SuggestedNameInfo.NULL_INFO;
}
Also used : SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) RenameUtil(com.intellij.refactoring.rename.RenameUtil) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) Set(java.util.Set) PsiNamedElement(com.intellij.psi.PsiNamedElement) SpellCheckerManager(com.intellij.spellchecker.SpellCheckerManager) PreferrableNameSuggestionProvider(com.intellij.refactoring.rename.PreferrableNameSuggestionProvider) Project(com.intellij.openapi.project.Project) PsiNamedElement(com.intellij.psi.PsiNamedElement) SpellCheckerManager(com.intellij.spellchecker.SpellCheckerManager)

Example 3 with SuggestedNameInfo

use of com.intellij.psi.codeStyle.SuggestedNameInfo in project intellij-community by JetBrains.

the class GenerateMainAction method actionPerformed.

public void actionPerformed(AnActionEvent e) {
    final Project project = e.getData(CommonDataKeys.PROJECT);
    assert project != null;
    final Editor editor = e.getData(CommonDataKeys.EDITOR);
    assert editor != null;
    final int offset = editor.getCaretModel().getOffset();
    final PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
    PsiClass psiClass = PsiTreeUtil.getParentOfType(file.findElementAt(offset), PsiClass.class);
    assert psiClass != null;
    if (!PsiUtil.hasDefaultConstructor(psiClass)) {
        Messages.showMessageDialog(project, UIDesignerBundle.message("generate.main.no.default.constructor"), UIDesignerBundle.message("generate.main.title"), Messages.getErrorIcon());
        return;
    }
    final List<PsiFile> boundForms = FormClassIndex.findFormsBoundToClass(project, psiClass.getQualifiedName());
    final LwRootContainer rootContainer;
    try {
        rootContainer = Utils.getRootContainer(boundForms.get(0).getText(), null);
    } catch (AlienFormFileException ex) {
        Messages.showMessageDialog(project, "The form bound to the class is not a valid IntelliJ IDEA form", UIDesignerBundle.message("generate.main.title"), Messages.getErrorIcon());
        return;
    } catch (Exception ex) {
        LOG.error(ex);
        return;
    }
    if (rootContainer.getComponentCount() == 0) {
        Messages.showMessageDialog(project, UIDesignerBundle.message("generate.main.empty.form"), UIDesignerBundle.message("generate.main.title"), Messages.getErrorIcon());
        return;
    }
    String rootBinding = rootContainer.getComponent(0).getBinding();
    if (rootBinding == null || psiClass.findFieldByName(rootBinding, true) == null) {
        Messages.showMessageDialog(project, UIDesignerBundle.message("generate.main.no.root.binding"), UIDesignerBundle.message("generate.main.title"), Messages.getErrorIcon());
        return;
    }
    @NonNls final StringBuilder mainBuilder = new StringBuilder("public static void main(String[] args) { ");
    final JavaCodeStyleManager csm = JavaCodeStyleManager.getInstance(project);
    SuggestedNameInfo nameInfo = csm.suggestVariableName(VariableKind.LOCAL_VARIABLE, "frame", null, null);
    String varName = nameInfo.names[0];
    mainBuilder.append(JFrame.class.getName()).append(" ").append(varName).append("= new ").append(JFrame.class.getName());
    mainBuilder.append("(\"").append(psiClass.getName()).append("\");");
    mainBuilder.append(varName).append(".setContentPane(new ").append(psiClass.getQualifiedName()).append("().").append(rootBinding).append(");");
    mainBuilder.append(varName).append(".setDefaultCloseOperation(").append(JFrame.class.getName()).append(".EXIT_ON_CLOSE);");
    mainBuilder.append(varName).append(".pack();");
    mainBuilder.append(varName).append(".setVisible(true);");
    mainBuilder.append("}\n");
    CommandProcessor.getInstance().executeCommand(project, () -> ApplicationManager.getApplication().runWriteAction(() -> {
        try {
            PsiMethod method = JavaPsiFacade.getInstance(file.getProject()).getElementFactory().createMethodFromText(mainBuilder.toString(), file);
            List<PsiGenerationInfo<PsiMethod>> infos = Collections.singletonList(new PsiGenerationInfo<>(method));
            List<PsiGenerationInfo<PsiMethod>> resultMembers = GenerateMembersUtil.insertMembersAtOffset(file, offset, infos);
            resultMembers.get(0).positionCaret(editor, false);
        } catch (IncorrectOperationException e1) {
            LOG.error(e1);
        }
    }), null, null);
}
Also used : NonNls(org.jetbrains.annotations.NonNls) AlienFormFileException(com.intellij.uiDesigner.compiler.AlienFormFileException) LwRootContainer(com.intellij.uiDesigner.lw.LwRootContainer) IncorrectOperationException(com.intellij.util.IncorrectOperationException) AlienFormFileException(com.intellij.uiDesigner.compiler.AlienFormFileException) Project(com.intellij.openapi.project.Project) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) List(java.util.List) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Editor(com.intellij.openapi.editor.Editor) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) PsiGenerationInfo(com.intellij.codeInsight.generation.PsiGenerationInfo)

Example 4 with SuggestedNameInfo

use of com.intellij.psi.codeStyle.SuggestedNameInfo in project intellij-community by JetBrains.

the class GroovyNameSuggestionProvider method getSuggestedNames.

@Override
public SuggestedNameInfo getSuggestedNames(final PsiElement element, @Nullable PsiElement nameSuggestionContext, Set<String> result) {
    if (nameSuggestionContext == null)
        nameSuggestionContext = element;
    if (element instanceof GrVariable && nameSuggestionContext instanceof GroovyPsiElement) {
        final PsiType type = ((GrVariable) element).getTypeGroovy();
        if (type != null) {
            final String[] names = GroovyNameSuggestionUtil.suggestVariableNameByType(type, new DefaultGroovyVariableNameValidator((GroovyPsiElement) nameSuggestionContext));
            result.addAll(Arrays.asList(names));
            return new SuggestedNameInfo(names) {

                @Override
                public void nameChosen(String name) {
                    JavaStatisticsManager.incVariableNameUseCount(name, JavaCodeStyleManager.getInstance(element.getProject()).getVariableKind((GrVariable) element), ((GrVariable) element).getName(), type);
                }
            };
        }
    }
    return null;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) PsiType(com.intellij.psi.PsiType)

Example 5 with SuggestedNameInfo

use of com.intellij.psi.codeStyle.SuggestedNameInfo in project intellij-community by JetBrains.

the class MyLookupExpression method initLookupItems.

private static LookupElement[] initLookupItems(LinkedHashSet<String> names, PsiNamedElement elementToRename, PsiElement nameSuggestionContext, final boolean shouldSelectAll) {
    if (names == null) {
        names = new LinkedHashSet<>();
        for (NameSuggestionProvider provider : Extensions.getExtensions(NameSuggestionProvider.EP_NAME)) {
            final SuggestedNameInfo suggestedNameInfo = provider.getSuggestedNames(elementToRename, nameSuggestionContext, names);
            if (suggestedNameInfo != null && provider instanceof PreferrableNameSuggestionProvider && !((PreferrableNameSuggestionProvider) provider).shouldCheckOthers()) {
                break;
            }
        }
    }
    final LookupElement[] lookupElements = new LookupElement[names.size()];
    final Iterator<String> iterator = names.iterator();
    for (int i = 0; i < lookupElements.length; i++) {
        final String suggestion = iterator.next();
        lookupElements[i] = LookupElementBuilder.create(suggestion).withInsertHandler(new InsertHandler<LookupElement>() {

            @Override
            public void handleInsert(InsertionContext context, LookupElement item) {
                if (shouldSelectAll)
                    return;
                final Editor topLevelEditor = InjectedLanguageUtil.getTopLevelEditor(context.getEditor());
                final TemplateState templateState = TemplateManagerImpl.getTemplateState(topLevelEditor);
                if (templateState != null) {
                    final TextRange range = templateState.getCurrentVariableRange();
                    if (range != null) {
                        topLevelEditor.getDocument().replaceString(range.getStartOffset(), range.getEndOffset(), suggestion);
                    }
                }
            }
        });
    }
    return lookupElements;
}
Also used : PreferrableNameSuggestionProvider(com.intellij.refactoring.rename.PreferrableNameSuggestionProvider) NameSuggestionProvider(com.intellij.refactoring.rename.NameSuggestionProvider) TextRange(com.intellij.openapi.util.TextRange) PreferrableNameSuggestionProvider(com.intellij.refactoring.rename.PreferrableNameSuggestionProvider) LookupElement(com.intellij.codeInsight.lookup.LookupElement) InsertionContext(com.intellij.codeInsight.completion.InsertionContext) TemplateState(com.intellij.codeInsight.template.impl.TemplateState) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) InsertHandler(com.intellij.codeInsight.completion.InsertHandler) Editor(com.intellij.openapi.editor.Editor)

Aggregations

SuggestedNameInfo (com.intellij.psi.codeStyle.SuggestedNameInfo)40 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)22 VariableKind (com.intellij.psi.codeStyle.VariableKind)11 Nullable (org.jetbrains.annotations.Nullable)7 Project (com.intellij.openapi.project.Project)6 NotNull (org.jetbrains.annotations.NotNull)6 ArrayList (java.util.ArrayList)4 LinkedHashSet (java.util.LinkedHashSet)4 NonNls (org.jetbrains.annotations.NonNls)4 LookupElement (com.intellij.codeInsight.lookup.LookupElement)3 Editor (com.intellij.openapi.editor.Editor)3 PsiType (com.intellij.psi.PsiType)3 PreferrableNameSuggestionProvider (com.intellij.refactoring.rename.PreferrableNameSuggestionProvider)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 List (java.util.List)3 ApplicationManager (com.intellij.openapi.application.ApplicationManager)2 Logger (com.intellij.openapi.diagnostic.Logger)2 com.intellij.psi (com.intellij.psi)2 THashSet (gnu.trove.THashSet)2 JavaPsiEquivalenceUtil (com.intellij.codeInsight.JavaPsiEquivalenceUtil)1