@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement element = descriptor.getStartElement(); if (element.isValid()) { replaceWithAssignment(project, element); } } }
@Override public S createStub(@NotNull P psi, StubElement parentStub) { final String message = "Should not be called. Element=" + psi + "; class" + psi.getClass() + "; file=" + (psi.isValid() ? psi.getContainingFile() : "-"); throw new UnsupportedOperationException(message); }
@Nullable @Override public Icon getIcon(@NotNull PsiElement e, @Iconable.IconFlags int flags) { return e instanceof GoFile && e.isValid() && GoTestFinder.isTestFile((GoFile)e) ? GoIcons.TEST_RUN : null; } }
@Nullable public PsiReference getReference(PsiElement element) { if (element != null && element.isValid()) { for (PsiReference reference : element.getReferences()) { if (isSupportedReference(reference)) { return reference; } } } return null; }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement element = descriptor.getPsiElement(); if (element.isValid() && element instanceof GoVarDefinition) { PsiElement parent = element.getParent(); if (parent instanceof GoVarSpec) { ((GoVarSpec)parent).deleteDefinition((GoVarDefinition)element); } } } }
private static boolean hasImportUsers(@NotNull GoImportSpec spec) { //noinspection SynchronizationOnLocalVariableOrMethodParameter synchronized (spec) { List<PsiElement> list = spec.getUserData(GoReferenceBase.IMPORT_USERS); if (list != null) { for (PsiElement e : list) { if (e.isValid()) { return true; } ProgressManager.checkCanceled(); } } } return false; }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement element = descriptor.getPsiElement(); if (element.isValid() && element instanceof GoConstDefinition) { PsiElement parent = element.getParent(); if (parent instanceof GoConstSpec) { ((GoConstSpec)parent).deleteDefinition((GoConstDefinition)element); } } } }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement element = descriptor.getPsiElement(); if (element == null || !element.isValid()) return; if (element instanceof GoStatement) { element.replace(GoElementFactory.createDeferStatement(project, "func() {\n" + "\tif r := recover(); r != nil {\n" + "\t\t\n" + "\t}\n" + "\t}()}")); } } }
@Override public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement start, @NotNull PsiElement end) { if (start.isValid() && end.isValid()) { PsiElement parent = start.getParent(); if (parent != null && parent.equals(end.getParent())) { parent.getNode().removeRange(start.getNode(), end.getNode().getTreeNext()); } } } }
@Nullable @Override public final PsiElement resolve() { return myElement.isValid() ? ResolveCache.getInstance(myElement.getProject()).resolveWithCaching(this, MY_RESOLVER, false, false) : null; }
@Override public void invoke(@NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, @NotNull PsiElement endElement) { if (startElement.isValid() && startElement instanceof GoNamedElement) { ((GoNamedElement)startElement).setName("_"); if (startElement instanceof GoVarDefinition) { PsiElement parent = startElement.getParent(); if (parent instanceof GoVarSpec) { if (GoNoNewVariablesInspection.hasNonNewVariables(((GoVarSpec)parent).getVarDefinitionList())) { GoNoNewVariablesInspection.replaceWithAssignment(project, parent); } } } } }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement element = descriptor.getPsiElement(); if (element.isValid() && element instanceof GoLabelDefinition) { PsiElement parent = element.getParent(); if (parent instanceof GoLabeledStatement) { GoStatement innerStatement = ((GoLabeledStatement)parent).getStatement(); if (innerStatement != null) { parent.replace(innerStatement); } } } } }
@Nullable @Override public final PsiElement resolve() { // return resolveInner(); return myElement.isValid() ? ResolveCache.getInstance(myElement.getProject()).resolveWithCaching(this, MY_RESOLVER, false, true) : null; }
@Nullable private static Data getData(@NotNull PsiElement element) { if (!element.isValid() || !element.isWritable()) return null; GoAssignmentStatement assignment = getValidAssignmentParent(element); GoReferenceExpression selectedFieldReference = assignment != null ? getFieldReferenceExpression(element, assignment) : null; GoCompositeLit compositeLit = selectedFieldReference != null ? getStructLiteralByReference(selectedFieldReference, assignment) : null; if (compositeLit == null) return null; List<GoReferenceExpression> references = getUninitializedSingleFieldReferences(assignment, selectedFieldReference, compositeLit); return !references.isEmpty() ? new Data(assignment, compositeLit, references) : null; }
@Override public void run() { if (!importEntriesToDelete.isEmpty() || !importIdentifiersToDelete.isEmpty()) { PsiDocumentManager manager = PsiDocumentManager.getInstance(file.getProject()); Document document = manager.getDocument(file); if (document != null) { manager.commitDocument(document); } } for (PsiElement importEntry : importEntriesToDelete) { if (importEntry != null && importEntry.isValid()) { deleteImportSpec(getImportSpec(importEntry)); } } for (PsiElement identifier : importIdentifiersToDelete) { if (identifier != null && identifier.isValid()) { identifier.delete(); } } } };
@Override protected boolean crossOff(@NotNull PsiElement e) { if (!(e instanceof GoFieldDefinition) && !(e instanceof GoAnonymousFieldDefinition)) return true; GoNamedElement named = (GoNamedElement)e; PsiFile myFile = myOrigin.getContainingFile(); PsiFile file = e.getContainingFile(); if (!(myFile instanceof GoFile) || !GoPsiImplUtil.allowed(file, myFile, myModule)) return true; boolean localResolve = GoReference.isLocalResolve(myFile, file); return !e.isValid() || !(named.isPublic() || localResolve); } }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement element = descriptor.getPsiElement(); if (!(element instanceof GoConditionalExpr) || !element.isValid()) { return; } GoConditionalExpr expr = (GoConditionalExpr)element; GoStringLiteral literal = ContainerUtil.findInstance(Arrays.asList(expr.getLeft(), expr.getRight()), GoStringLiteral.class); if (literal == null || !isSingleCharLiteral(literal)) { return; } literal.replace(createExpression(project, extractSingleCharFromText(literal))); }
@Nullable public static PsiElement getContextElement(@Nullable ConfigurationContext context) { if (context == null) { return null; } PsiElement psiElement = context.getPsiLocation(); if (psiElement == null || !psiElement.isValid()) { return null; } FileIndexFacade indexFacade = FileIndexFacade.getInstance(psiElement.getProject()); PsiFileSystemItem psiFile = psiElement instanceof PsiFileSystemItem ? (PsiFileSystemItem)psiElement : psiElement.getContainingFile(); VirtualFile file = psiFile != null ? psiFile.getVirtualFile() : null; if (file != null && file.getFileType() != ScratchFileType.INSTANCE && (!indexFacade.isInContent(file) || indexFacade.isExcludedFile(file))) { return null; } return psiElement; }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement element = descriptor.getPsiElement(); if (element != null && element.isValid()) { if (element instanceof GoUnaryExpr) { GoUnaryExpr unaryExpr = (GoUnaryExpr)element; GoExpression expr = unaryExpr.getExpression(); if (unaryExpr.getBitAnd() != null && expr instanceof GoCompositeLit) { GoLiteralValue literalValue = ((GoCompositeLit)expr).getLiteralValue(); if (literalValue != null) { unaryExpr.replace(literalValue); } } } else if (element instanceof GoTypeReferenceExpression) { PsiElement parent = element.getParent(); if (parent instanceof GoCompositeLit) { GoLiteralValue literalValue = ((GoCompositeLit)parent).getLiteralValue(); if (literalValue != null) { parent.replace(literalValue); } } } } } }
@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)); } }