Search in sources :

Example 1 with Configuration

use of com.android.tools.lint.client.api.Configuration in project android by JetBrains.

the class SuppressLintIntentionAction method invoke.

@Override
public void invoke(@NotNull Project project, @Nullable Editor editor, @NotNull PsiFile file) throws IncorrectOperationException {
    if (file instanceof XmlFile) {
        final XmlTag element = PsiTreeUtil.getParentOfType(myElement, XmlTag.class);
        if (element == null) {
            return;
        }
        String lintId = getLintId(myId);
        addSuppressAttribute(project, (XmlFile) file, element, lintId);
    } else if (file instanceof PsiJavaFile) {
        final PsiModifierListOwner container = PsiTreeUtil.getParentOfType(myElement, PsiModifierListOwner.class);
        if (container == null) {
            return;
        }
        final PsiModifierList modifierList = container.getModifierList();
        if (modifierList != null) {
            String lintId = getLintId(myId);
            addSuppressAnnotation(project, container, container, lintId);
        }
    } else if (file instanceof GroovyFile) {
        Document document = PsiDocumentManager.getInstance(myElement.getProject()).getDocument(file);
        if (document != null) {
            int offset = myElement.getTextOffset();
            int line = document.getLineNumber(offset);
            int lineStart = document.getLineStartOffset(line);
            if (lineStart > 0) {
                int prevLineStart = document.getLineStartOffset(line - 1);
                int prevLineEnd = document.getLineEndOffset(line - 1);
                String prevLine = document.getText(new TextRange(prevLineStart, prevLineEnd));
                int index = prevLine.indexOf(NO_INSPECTION_PREFIX);
                if (index != -1) {
                    document.insertString(prevLineStart + index + NO_INSPECTION_PREFIX.length(), getLintId(myId) + ",");
                    return;
                }
            }
            String linePrefix = document.getText(new TextRange(lineStart, offset));
            int nonSpace = 0;
            for (; nonSpace < linePrefix.length(); nonSpace++) {
                if (!Character.isWhitespace(linePrefix.charAt(nonSpace))) {
                    break;
                }
            }
            ApplicationManager.getApplication().assertWriteAccessAllowed();
            document.insertString(lineStart + nonSpace, NO_INSPECTION_PREFIX + getLintId(myId) + "\n" + linePrefix.substring(0, nonSpace));
        }
    } else if (file instanceof PsiBinaryFile) {
        VirtualFile virtualFile = file.getVirtualFile();
        if (virtualFile != null) {
            File binaryFile = VfsUtilCore.virtualToIoFile(virtualFile);
            // Can't suppress lint checks inside a binary file (typically an icon): use
            // the lint XML facility instead
            Module module = ModuleUtilCore.findModuleForPsiElement(file);
            if (module != null) {
                //LintIdeRequest
                File dir = LintIdeProject.getLintProjectDirectory(module, AndroidFacet.getInstance(module));
                if (dir != null) {
                    LintIdeClient client = new LintIdeClient(project);
                    com.android.tools.lint.detector.api.Project lintProject = client.getProject(dir, dir);
                    Configuration configuration = client.getConfiguration(lintProject, null);
                    Issue issue = getIssue();
                    if (issue != null) {
                        configuration.ignore(issue, binaryFile);
                    }
                }
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Issue(com.android.tools.lint.detector.api.Issue) XmlFile(com.intellij.psi.xml.XmlFile) Configuration(com.android.tools.lint.client.api.Configuration) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) Project(com.intellij.openapi.project.Project) Module(com.intellij.openapi.module.Module) XmlFile(com.intellij.psi.xml.XmlFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) File(java.io.File) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

Configuration (com.android.tools.lint.client.api.Configuration)1 Issue (com.android.tools.lint.detector.api.Issue)1 Document (com.intellij.openapi.editor.Document)1 Module (com.intellij.openapi.module.Module)1 Project (com.intellij.openapi.project.Project)1 TextRange (com.intellij.openapi.util.TextRange)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 XmlFile (com.intellij.psi.xml.XmlFile)1 XmlTag (com.intellij.psi.xml.XmlTag)1 File (java.io.File)1 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)1