Search in sources :

Example 61 with PsiElement

use of com.intellij.psi.PsiElement in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoFunctionVariadicParameterInspection method checkResult.

private static void checkResult(@NotNull GoSignature o, @NotNull ProblemsHolder holder) {
    GoResult result = o.getResult();
    if (result == null)
        return;
    GoParameters parameters = result.getParameters();
    if (parameters == null)
        return;
    for (GoParameterDeclaration declaration : parameters.getParameterDeclarationList()) {
        PsiElement dot = declaration.getTripleDot();
        if (dot != null) {
            holder.registerProblem(dot, "Cannot use <code>...</code> in output argument list", ProblemHighlightType.GENERIC_ERROR_OR_WARNING, DELETE_QUICK_FIX);
        }
    }
}
Also used : PsiElement(com.intellij.psi.PsiElement)

Example 62 with PsiElement

use of com.intellij.psi.PsiElement in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoInvalidPackageImportInspection method checkFile.

@Override
protected void checkFile(@NotNull GoFile file, @NotNull ProblemsHolder problemsHolder) {
    Module module = ModuleUtilCore.findModuleForPsiElement(file);
    VirtualFile sdkHome = GoSdkUtil.getSdkSrcDir(file.getProject(), module);
    boolean supportsVendoring = GoVendoringUtil.isVendoringEnabled(module);
    String sdkVersion = GoSdkService.getInstance(file.getProject()).getSdkVersion(module);
    boolean supportsInternalPackages = GoVendoringUtil.supportsInternalPackages(sdkVersion);
    boolean supportsInternalPackagesInSdk = sdkHome != null && GoVendoringUtil.supportsSdkInternalPackages(sdkVersion);
    for (GoImportSpec importSpec : file.getImports()) {
        if (importSpec.isCImport()) {
            PsiElement dot = importSpec.getDot();
            if (dot != null) {
                problemsHolder.registerProblem(importSpec, "Cannot rename import `C`", new GoDeleteImportSpecAlias(), new GoDeleteImportQuickFix());
            }
            PsiElement identifier = importSpec.getIdentifier();
            if (identifier != null) {
                problemsHolder.registerProblem(importSpec, "Cannot import 'builtin' package", new GoDeleteImportSpecAlias(), new GoDeleteImportQuickFix());
            }
            continue;
        }
        PsiDirectory resolve = importSpec.getImportString().resolve();
        if (resolve != null) {
            if (GoPackageUtil.isBuiltinPackage(resolve)) {
                problemsHolder.registerProblem(importSpec, "Cannot import 'builtin' package", new GoDeleteImportQuickFix());
            }
            Collection<String> packagesInDirectory = GoPackageUtil.getAllPackagesInDirectory(resolve, module, true);
            if (packagesInDirectory.isEmpty()) {
                problemsHolder.registerProblem(importSpec, "'" + resolve.getVirtualFile().getPath() + "' has no buildable Go source files", new GoDeleteImportQuickFix());
                continue;
            }
            if (!GoTestFinder.isTestFile(file) && packagesInDirectory.size() == 1 && packagesInDirectory.contains(GoConstants.MAIN)) {
                problemsHolder.registerProblem(importSpec, "'" + importSpec.getPath() + "' is a program, not an importable package", new GoDeleteImportQuickFix());
                continue;
            }
            if (packagesInDirectory.size() > 1) {
                problemsHolder.registerProblem(importSpec, "Found several packages [" + StringUtil.join(packagesInDirectory, ", ") + "] in '" + resolve.getVirtualFile().getPath() + "'", new GoDeleteImportQuickFix());
                continue;
            }
            VirtualFile contextFile = file.getVirtualFile();
            VirtualFile resolvedFile = resolve.getVirtualFile();
            boolean resolvedToSdk = sdkHome != null && VfsUtilCore.isAncestor(sdkHome, resolvedFile, false);
            boolean validateInternal = supportsInternalPackages || supportsInternalPackagesInSdk && resolvedToSdk;
            if (supportsVendoring || validateInternal || resolvedToSdk) {
                Set<VirtualFile> sourceRoots = GoSdkUtil.getSourcesPathsToLookup(file.getProject(), module);
                for (PsiReference reference : importSpec.getImportString().getReferences()) {
                    if (reference instanceof GoImportReference) {
                        String canonicalText = reference.getCanonicalText();
                        if (resolvedToSdk && GoConstants.TESTDATA_NAME.equals(canonicalText)) {
                            problemsHolder.registerProblem(importSpec, "Use of testdata package from SDK is not allowed", new GoDeleteImportQuickFix());
                            break;
                        } else if (validateInternal && GoConstants.INTERNAL.equals(canonicalText)) {
                            if (GoSdkUtil.isUnreachableInternalPackage(resolvedFile, contextFile, sourceRoots)) {
                                problemsHolder.registerProblem(importSpec, "Use of internal package is not allowed", new GoDeleteImportQuickFix());
                                break;
                            }
                        } else if (supportsVendoring && GoConstants.VENDOR.equals(canonicalText)) {
                            if (GoSdkUtil.isUnreachableVendoredPackage(resolvedFile, contextFile, sourceRoots)) {
                                problemsHolder.registerProblem(importSpec, "Use of vendored package is not allowed", new GoDeleteImportQuickFix(), GoDisableVendoringInModuleQuickFix.create(module));
                                break;
                            } else {
                                String vendoredImportPath = GoSdkUtil.getImportPath(resolve, true);
                                if (vendoredImportPath != null) {
                                    problemsHolder.registerProblem(importSpec, "Must be imported as '" + vendoredImportPath + "'", new GoReplaceImportPath(vendoredImportPath), new GoDeleteImportQuickFix(), GoDisableVendoringInModuleQuickFix.create(module));
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        } else {
            for (PsiReference reference : importSpec.getImportString().getReferences()) {
                if (reference instanceof GoImportReference) {
                    if (((GoImportReference) reference).getFileReferenceSet().isAbsolutePathReference()) {
                        problemsHolder.registerProblem(importSpec, "Cannot import absolute path", new GoDeleteImportQuickFix());
                        break;
                    }
                }
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GoDeleteImportQuickFix(com.goide.quickfix.GoDeleteImportQuickFix) GoImportSpec(com.goide.psi.GoImportSpec) PsiReference(com.intellij.psi.PsiReference) PsiDirectory(com.intellij.psi.PsiDirectory) Module(com.intellij.openapi.module.Module) PsiElement(com.intellij.psi.PsiElement) GoImportReference(com.goide.psi.impl.imports.GoImportReference)

Example 63 with PsiElement

use of com.intellij.psi.PsiElement in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoStructLiteralCompletion method allowedVariants.

@NotNull
static Variants allowedVariants(@Nullable GoReferenceExpression structFieldReference) {
    GoValue value = parent(structFieldReference, GoValue.class);
    GoElement element = parent(value, GoElement.class);
    if (element != null && element.getKey() != null) {
        return Variants.NONE;
    }
    GoType type = GoPsiImplUtil.getLiteralType(element, true);
    if (!(type instanceof GoStructType)) {
        return Variants.NONE;
    }
    boolean hasValueInitializers = false;
    boolean hasFieldValueInitializers = false;
    GoLiteralValue literalValue = parent(element, GoLiteralValue.class);
    List<GoElement> fieldInitializers = literalValue != null ? literalValue.getElementList() : Collections.emptyList();
    for (GoElement initializer : fieldInitializers) {
        if (initializer == element) {
            continue;
        }
        PsiElement colon = initializer.getColon();
        hasFieldValueInitializers |= colon != null;
        hasValueInitializers |= colon == null;
    }
    return hasFieldValueInitializers && !hasValueInitializers ? Variants.FIELD_NAME_ONLY : !hasFieldValueInitializers && hasValueInitializers ? Variants.VALUE_ONLY : Variants.BOTH;
}
Also used : PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 64 with PsiElement

use of com.intellij.psi.PsiElement in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoStructLiteralCompletion method alreadyAssignedFields.

@NotNull
static Set<String> alreadyAssignedFields(@Nullable GoLiteralValue literal) {
    if (literal == null) {
        return Collections.emptySet();
    }
    return ContainerUtil.map2SetNotNull(literal.getElementList(), element -> {
        GoKey key = element.getKey();
        GoFieldName fieldName = key != null ? key.getFieldName() : null;
        PsiElement identifier = fieldName != null ? fieldName.getIdentifier() : null;
        return identifier != null ? identifier.getText() : null;
    });
}
Also used : PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 65 with PsiElement

use of com.intellij.psi.PsiElement in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoExcludePathLookupActionProvider method fillActions.

@Override
public void fillActions(LookupElement element, Lookup lookup, Consumer<LookupElementAction> consumer) {
    PsiElement psiElement = element.getPsiElement();
    PsiFile file = psiElement != null && psiElement.isValid() ? psiElement.getContainingFile() : null;
    String importPath = file instanceof GoFile ? ((GoFile) file).getImportPath(false) : null;
    if (importPath != null) {
        Project project = psiElement.getProject();
        for (String path : getPaths(importPath)) {
            consumer.consume(new ExcludePathAction(project, path));
        }
        consumer.consume(new EditExcludedAction(project));
    }
}
Also used : GoFile(com.goide.psi.GoFile) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiElement (com.intellij.psi.PsiElement)3198 Nullable (org.jetbrains.annotations.Nullable)493 PsiFile (com.intellij.psi.PsiFile)474 NotNull (org.jetbrains.annotations.NotNull)442 TextRange (com.intellij.openapi.util.TextRange)239 PsiReference (com.intellij.psi.PsiReference)227 Project (com.intellij.openapi.project.Project)222 VirtualFile (com.intellij.openapi.vfs.VirtualFile)210 ArrayList (java.util.ArrayList)195 ASTNode (com.intellij.lang.ASTNode)142 XmlTag (com.intellij.psi.xml.XmlTag)134 PsiClass (com.intellij.psi.PsiClass)115 Editor (com.intellij.openapi.editor.Editor)112 Document (com.intellij.openapi.editor.Document)109 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)85 PsiDirectory (com.intellij.psi.PsiDirectory)80 IElementType (com.intellij.psi.tree.IElementType)78 Module (com.intellij.openapi.module.Module)77 PsiMethod (com.intellij.psi.PsiMethod)73 UsageInfo (com.intellij.usageView.UsageInfo)70