Search in sources :

Example 1 with NonNls

use of org.jetbrains.annotations.NonNls in project intellij-community by JetBrains.

the class GroovyGenerateEqualsHelper method createEquals.

private PsiMethod createEquals() throws IncorrectOperationException {
    final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(myProject);
    String[] nameSuggestions = codeStyleManager.suggestVariableName(VariableKind.PARAMETER, null, null, PsiType.getJavaLangObject(myClass.getManager(), myClass.getResolveScope())).names;
    final String objectBaseName = nameSuggestions.length > 0 ? nameSuggestions[0] : BASE_OBJECT_PARAMETER_NAME;
    myParameterName = getUniqueLocalVarName(objectBaseName, myEqualsFields);
    //todo isApplicable it
    final PsiType classType = TypesUtil.createType(myClass.getQualifiedName(), myClass.getContext());
    nameSuggestions = codeStyleManager.suggestVariableName(VariableKind.LOCAL_VARIABLE, null, null, classType).names;
    String instanceBaseName = nameSuggestions.length > 0 && nameSuggestions[0].length() < 10 ? nameSuggestions[0] : BASE_OBJECT_LOCAL_NAME;
    myClassInstanceName = getUniqueLocalVarName(instanceBaseName, myEqualsFields);
    @NonNls StringBuffer buffer = new StringBuffer();
    buffer.append("boolean equals(").append(myParameterName).append(") {\n");
    addEqualsPrologue(buffer);
    if (myEqualsFields.length > 0) {
        addClassInstance(buffer);
        ArrayList<PsiField> equalsFields = new ArrayList<>();
        ContainerUtil.addAll(equalsFields, myEqualsFields);
        Collections.sort(equalsFields, EqualsFieldsComparator.INSTANCE);
        for (PsiField field : equalsFields) {
            if (!field.hasModifierProperty(PsiModifier.STATIC)) {
                final PsiType type = field.getType();
                if (type instanceof PsiArrayType) {
                    addArrayEquals(buffer, field);
                } else if (type instanceof PsiPrimitiveType) {
                    if (PsiType.DOUBLE.equals(type) || PsiType.FLOAT.equals(type)) {
                        addDoubleFieldComparison(buffer, field);
                    } else {
                        addFieldComparison(buffer, field);
                    }
                } else {
                    if (type instanceof PsiClassType) {
                        final PsiClass aClass = ((PsiClassType) type).resolve();
                        if (aClass != null && aClass.isEnum()) {
                            addFieldComparison(buffer, field);
                            continue;
                        }
                    }
                    addFieldComparison(buffer, field);
                }
            }
        }
    }
    buffer.append("\nreturn true\n}");
    GrMethod result = myFactory.createMethodFromText(buffer.toString());
    final PsiParameter parameter = result.getParameterList().getParameters()[0];
    PsiUtil.setModifierProperty(parameter, PsiModifier.FINAL, CodeStyleSettingsManager.getSettings(myProject).GENERATE_FINAL_PARAMETERS);
    try {
        result = ((GrMethod) CodeStyleManager.getInstance(myProject).reformat(result));
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
    return result;
}
Also used : NonNls(org.jetbrains.annotations.NonNls) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 2 with NonNls

use of org.jetbrains.annotations.NonNls in project intellij-community by JetBrains.

the class ForToEachIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrForStatement parentStatement = (GrForStatement) element;
    final GrForInClause clause = (GrForInClause) parentStatement.getClause();
    final GrVariable var = clause.getDeclaredVariable();
    final GrStatement body = parentStatement.getBody();
    final String bodyText;
    if (body instanceof GrBlockStatement) {
        final String text = body.getText();
        bodyText = text.substring(1, text.length() - 1);
    } else {
        bodyText = body.getText();
    }
    GrExpression collection = clause.getIteratedExpression();
    assert collection != null;
    @NonNls final String statement = "x.each{" + var.getText() + " -> " + bodyText + " }";
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(parentStatement.getProject());
    final GrMethodCallExpression eachExpression = (GrMethodCallExpression) factory.createTopElementFromText(statement);
    ((GrReferenceExpression) eachExpression.getInvokedExpression()).getQualifierExpression().replaceWithExpression(collection, true);
    parentStatement.replaceWithStatement(eachExpression);
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) NonNls(org.jetbrains.annotations.NonNls) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrForInClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause) GrForStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrForStatement) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrBlockStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 3 with NonNls

use of org.jetbrains.annotations.NonNls in project intellij-community by JetBrains.

the class IdeaCommitHandler method trackDeletedFile.

private void trackDeletedFile(@NotNull ProgressEvent event) {
    @NonNls final String filePath = "file://" + event.getFile().getAbsolutePath().replace(File.separatorChar, '/');
    VirtualFile virtualFile = ApplicationManager.getApplication().runReadAction((Computable<VirtualFile>) () -> VirtualFileManager.getInstance().findFileByUrl(filePath));
    if (virtualFile != null) {
        myDeletedFiles.add(virtualFile);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) NonNls(org.jetbrains.annotations.NonNls)

Example 4 with NonNls

use of org.jetbrains.annotations.NonNls in project intellij-community by JetBrains.

the class DomRootInvocationHandler method setEmptyXmlTag.

@Override
protected XmlTag setEmptyXmlTag() {
    final XmlTag[] result = new XmlTag[] { null };
    getManager().runChange(() -> {
        try {
            final String namespace = getXmlElementNamespace();
            @NonNls final String nsDecl = StringUtil.isEmpty(namespace) ? "" : " xmlns=\"" + namespace + "\"";
            final XmlFile xmlFile = getFile();
            final XmlTag tag = XmlElementFactory.getInstance(xmlFile.getProject()).createTagFromText("<" + getXmlElementName() + nsDecl + "/>");
            result[0] = ((XmlDocument) xmlFile.getDocument().replace(((XmlFile) tag.getContainingFile()).getDocument())).getRootTag();
        } catch (IncorrectOperationException e) {
            LOG.error(e);
        }
    });
    return result[0];
}
Also used : NonNls(org.jetbrains.annotations.NonNls) XmlFile(com.intellij.psi.xml.XmlFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException) XmlTag(com.intellij.psi.xml.XmlTag)

Example 5 with NonNls

use of org.jetbrains.annotations.NonNls in project intellij-community by JetBrains.

the class XmlUtil method getDefaultNamespaces.

@Nullable
@NonNls
public static String[][] getDefaultNamespaces(final XmlDocument document) {
    final XmlFile file = getContainingFile(document);
    final XmlTag tag = document.getRootTag();
    if (tag == null)
        return null;
    @NotNull final XmlFileNSInfoProvider[] nsProviders = Extensions.getExtensions(XmlFileNSInfoProvider.EP_NAME);
    if (file != null) {
        NextProvider: for (XmlFileNSInfoProvider nsProvider : nsProviders) {
            final String[][] pairs = nsProvider.getDefaultNamespaces(file);
            if (pairs != null && pairs.length > 0) {
                for (final String[] nsMapping : pairs) {
                    if (nsMapping == null || nsMapping.length != 2 || nsMapping[0] == null || nsMapping[1] == null) {
                        LOG.debug("NSInfoProvider " + nsProvider + " gave wrong info about " + file.getVirtualFile());
                        continue NextProvider;
                    }
                }
                return pairs;
            }
        }
    }
    String namespace = getDtdUri(document);
    if (namespace != null) {
        boolean overrideNamespaceFromDocType = false;
        if (file != null) {
            for (XmlFileNSInfoProvider provider : nsProviders) {
                try {
                    if (provider.overrideNamespaceFromDocType(file)) {
                        overrideNamespaceFromDocType = true;
                        break;
                    }
                } catch (AbstractMethodError ignored) {
                }
            }
        }
        if (!overrideNamespaceFromDocType)
            return new String[][] { new String[] { "", namespace } };
    }
    if ("taglib".equals(tag.getName())) {
        return new String[][] { new String[] { "", TAGLIB_1_2_URI } };
    }
    if (file != null) {
        final Language language = file.getLanguage();
        if (language.isKindOf(HTMLLanguage.INSTANCE) || language == XHTMLLanguage.INSTANCE) {
            return new String[][] { new String[] { "", XHTML_URI } };
        }
    }
    return null;
}
Also used : Language(com.intellij.lang.Language) XMLLanguage(com.intellij.lang.xml.XMLLanguage) XHTMLLanguage(com.intellij.lang.xhtml.XHTMLLanguage) HTMLLanguage(com.intellij.lang.html.HTMLLanguage) NotNull(org.jetbrains.annotations.NotNull) NonNls(org.jetbrains.annotations.NonNls) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

NonNls (org.jetbrains.annotations.NonNls)294 Project (com.intellij.openapi.project.Project)43 NotNull (org.jetbrains.annotations.NotNull)36 Nullable (org.jetbrains.annotations.Nullable)35 VirtualFile (com.intellij.openapi.vfs.VirtualFile)34 PsiElement (com.intellij.psi.PsiElement)32 File (java.io.File)28 IncorrectOperationException (com.intellij.util.IncorrectOperationException)26 TextRange (com.intellij.openapi.util.TextRange)18 PsiFile (com.intellij.psi.PsiFile)18 CodeStyleManager (com.intellij.psi.codeStyle.CodeStyleManager)17 Element (org.jdom.Element)16 Document (com.intellij.openapi.editor.Document)15 IElementType (com.intellij.psi.tree.IElementType)14 IOException (java.io.IOException)14 XmlTag (com.intellij.psi.xml.XmlTag)12 List (java.util.List)12 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)11 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)11 ArrayList (java.util.ArrayList)11