Search in sources :

Example 1 with ScannableFile

use of org.infernus.idea.checkstyle.checker.ScannableFile in project checkstyle-idea by jshiell.

the class CheckStyleInspection method inspectFile.

@Nullable
public List<Problem> inspectFile(@NotNull final PsiFile psiFile, @Nullable final Module module, @NotNull final InspectionManager manager) {
    LOG.debug("Inspection has been invoked.");
    final CheckStylePlugin plugin = plugin(manager.getProject());
    ConfigurationLocation configurationLocation = null;
    final List<ScannableFile> scannableFiles = new ArrayList<>();
    try {
        configurationLocation = plugin.getConfigurationLocation(module, null);
        if (configurationLocation == null || configurationLocation.isBlacklisted()) {
            return NO_PROBLEMS_FOUND;
        }
        scannableFiles.addAll(ScannableFile.createAndValidate(singletonList(psiFile), plugin, module));
        return checkerFactory(psiFile.getProject()).checker(module, configurationLocation).map(checker -> checker.scan(scannableFiles, plugin.configurationManager().getCurrent().isSuppressErrors())).map(results -> results.get(psiFile)).map(this::dropIgnoredProblems).orElse(NO_PROBLEMS_FOUND);
    } catch (ProcessCanceledException | AssertionError e) {
        LOG.debug("Process cancelled when scanning: " + psiFile.getName());
        return NO_PROBLEMS_FOUND;
    } catch (CheckStylePluginParseException e) {
        LOG.debug("Parse exception caught when scanning: " + psiFile.getName(), e);
        return NO_PROBLEMS_FOUND;
    } catch (Throwable e) {
        handlePluginException(e, psiFile, plugin, configurationLocation, manager.getProject());
        return NO_PROBLEMS_FOUND;
    } finally {
        scannableFiles.forEach(ScannableFile::deleteIfRequired);
    }
}
Also used : ScannableFile(org.infernus.idea.checkstyle.checker.ScannableFile) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) SeverityLevel(org.infernus.idea.checkstyle.csapi.SeverityLevel) ModuleUtil(com.intellij.openapi.module.ModuleUtil) InspectionManager(com.intellij.codeInspection.InspectionManager) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) Problem(org.infernus.idea.checkstyle.checker.Problem) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) Logger(com.intellij.openapi.diagnostic.Logger) Module(com.intellij.openapi.module.Module) CheckStyleBundle.message(org.infernus.idea.checkstyle.CheckStyleBundle.message) Notifications.showException(org.infernus.idea.checkstyle.util.Notifications.showException) CheckerFactory(org.infernus.idea.checkstyle.checker.CheckerFactory) Optional.ofNullable(java.util.Optional.ofNullable) IOException(java.io.IOException) ConfigurationLocation(org.infernus.idea.checkstyle.model.ConfigurationLocation) FileNotFoundException(java.io.FileNotFoundException) CheckStylePluginParseException(org.infernus.idea.checkstyle.exception.CheckStylePluginParseException) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ServiceManager(com.intellij.openapi.components.ServiceManager) Notifications.showWarning(org.infernus.idea.checkstyle.util.Notifications.showWarning) CheckStyleInspectionPanel(org.infernus.idea.checkstyle.ui.CheckStyleInspectionPanel) LocalInspectionTool(com.intellij.codeInspection.LocalInspectionTool) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) Async.asyncResultOf(org.infernus.idea.checkstyle.util.Async.asyncResultOf) javax.swing(javax.swing) CheckStylePluginParseException(org.infernus.idea.checkstyle.exception.CheckStylePluginParseException) ConfigurationLocation(org.infernus.idea.checkstyle.model.ConfigurationLocation) ArrayList(java.util.ArrayList) ScannableFile(org.infernus.idea.checkstyle.checker.ScannableFile) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) Optional.ofNullable(java.util.Optional.ofNullable) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with ScannableFile

use of org.infernus.idea.checkstyle.checker.ScannableFile in project checkstyle-idea by jshiell.

the class CheckStyleInspection method inspectFile.

private List<Problem> inspectFile(@NotNull final PsiFile psiFile, @NotNull final List<ScannableFile> scannableFiles, @Nullable final Module module, @NotNull final InspectionManager manager) {
    LOG.debug("Inspection has been invoked for " + psiFile.getName());
    ConfigurationLocation configurationLocation = null;
    try {
        configurationLocation = configurationLocationSource(manager.getProject()).getConfigurationLocation(module, null);
        if (configurationLocation == null || configurationLocation.isBlocked()) {
            return NO_PROBLEMS_FOUND;
        }
        return checkerFactory(psiFile.getProject()).checker(module, configurationLocation).map(checker -> checker.scan(scannableFiles, configurationManager(psiFile.getProject()).getCurrent().isSuppressErrors())).map(results -> results.get(psiFile)).map(this::dropIgnoredProblems).orElse(NO_PROBLEMS_FOUND);
    } catch (ProcessCanceledException | AssertionError e) {
        LOG.debug("Process cancelled when scanning: " + psiFile.getName());
        return NO_PROBLEMS_FOUND;
    } catch (CheckStylePluginParseException e) {
        LOG.debug("Parse exception caught when scanning: " + psiFile.getName(), e);
        return NO_PROBLEMS_FOUND;
    } catch (Throwable e) {
        handlePluginException(e, psiFile, configurationLocation, manager.getProject());
        return NO_PROBLEMS_FOUND;
    } finally {
        scannableFiles.forEach(ScannableFile::deleteIfRequired);
    }
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) ScannableFile(org.infernus.idea.checkstyle.checker.ScannableFile) TreeSet(java.util.TreeSet) Collections.singletonList(java.util.Collections.singletonList) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) SeverityLevel(org.infernus.idea.checkstyle.csapi.SeverityLevel) ModuleUtil(com.intellij.openapi.module.ModuleUtil) InspectionManager(com.intellij.codeInspection.InspectionManager) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) Problem(org.infernus.idea.checkstyle.checker.Problem) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) Logger(com.intellij.openapi.diagnostic.Logger) Module(com.intellij.openapi.module.Module) CheckStyleBundle.message(org.infernus.idea.checkstyle.CheckStyleBundle.message) ConfigurationLocationSource(org.infernus.idea.checkstyle.config.ConfigurationLocationSource) Notifications.showException(org.infernus.idea.checkstyle.util.Notifications.showException) CheckerFactory(org.infernus.idea.checkstyle.checker.CheckerFactory) Optional.ofNullable(java.util.Optional.ofNullable) IOException(java.io.IOException) ConfigurationLocation(org.infernus.idea.checkstyle.model.ConfigurationLocation) FileNotFoundException(java.io.FileNotFoundException) CheckStylePluginParseException(org.infernus.idea.checkstyle.exception.CheckStylePluginParseException) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ServiceManager(com.intellij.openapi.components.ServiceManager) Notifications.showWarning(org.infernus.idea.checkstyle.util.Notifications.showWarning) CheckStyleInspectionPanel(org.infernus.idea.checkstyle.ui.CheckStyleInspectionPanel) LocalInspectionTool(com.intellij.codeInspection.LocalInspectionTool) PluginConfigurationManager(org.infernus.idea.checkstyle.config.PluginConfigurationManager) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) Async.asyncResultOf(org.infernus.idea.checkstyle.util.Async.asyncResultOf) javax.swing(javax.swing) CheckStylePluginParseException(org.infernus.idea.checkstyle.exception.CheckStylePluginParseException) ConfigurationLocation(org.infernus.idea.checkstyle.model.ConfigurationLocation) ScannableFile(org.infernus.idea.checkstyle.checker.ScannableFile) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 3 with ScannableFile

use of org.infernus.idea.checkstyle.checker.ScannableFile in project checkstyle-idea by jshiell.

the class VersionMixExceptionTest method runChecker.

private void runChecker(@NotNull final CheckStyleChecker checker) throws URISyntaxException {
    final File sourceFile = new File(getClass().getResource("SourceFile.java").toURI());
    final ScannableFile file1 = mock(ScannableFile.class);
    when(file1.getFile()).thenReturn(sourceFile);
    final List<ScannableFile> filesToScan = Collections.singletonList(file1);
    final CheckstyleActions csInstance = csService.getCheckstyleInstance();
    // 
    csInstance.scan(// 
    checker.getCheckerWithConfig4UnitTest(), // 
    filesToScan, // 
    false, // 
    2, Optional.of(sourceFile.getParent()));
}
Also used : CheckstyleActions(org.infernus.idea.checkstyle.csapi.CheckstyleActions) ScannableFile(org.infernus.idea.checkstyle.checker.ScannableFile) ScannableFile(org.infernus.idea.checkstyle.checker.ScannableFile) File(java.io.File)

Example 4 with ScannableFile

use of org.infernus.idea.checkstyle.checker.ScannableFile in project checkstyle-idea by jshiell.

the class ServiceLayerBasicTest method runChecker.

private void runChecker(@NotNull final CheckStyleChecker checker) throws URISyntaxException {
    final File sourceFile = new File(getClass().getResource("SourceFile.java").toURI());
    final ScannableFile file1 = mock(ScannableFile.class);
    when(file1.getFile()).thenReturn(sourceFile);
    final List<ScannableFile> filesToScan = Collections.singletonList(file1);
    final CheckstyleActions csInstance = checkstyleProjectService.getCheckstyleInstance();
    // 
    csInstance.scan(// 
    checker.getCheckerWithConfig4UnitTest(), // 
    filesToScan, // 
    false, // 
    2, Optional.of(sourceFile.getParent()));
}
Also used : CheckstyleActions(org.infernus.idea.checkstyle.csapi.CheckstyleActions) ScannableFile(org.infernus.idea.checkstyle.checker.ScannableFile) ScannableFile(org.infernus.idea.checkstyle.checker.ScannableFile) File(java.io.File)

Example 5 with ScannableFile

use of org.infernus.idea.checkstyle.checker.ScannableFile in project checkstyle-idea by jshiell.

the class CheckStyleInspection method checkFile.

@Override
public ProblemDescriptor[] checkFile(@NotNull final PsiFile psiFile, @NotNull final InspectionManager manager, final boolean isOnTheFly) {
    if (InjectedLanguageManager.getInstance(manager.getProject()).isInjectedFragment(psiFile)) {
        LOG.debug("Ignoring file as it is an injected fragment: " + psiFile);
        return noProblemsFound(manager);
    }
    final Module module = moduleOf(psiFile);
    List<ScannableFile> scannableFiles = ScannableFile.createAndValidate(singletonList(psiFile), manager.getProject(), module);
    if (scannableFiles.isEmpty()) {
        LOG.debug("Inspection has been cancelled as file is not scannable: " + psiFile.getName());
        return noProblemsFound(manager);
    }
    try {
        return asProblemDescriptors(asyncResultOf(() -> {
            try {
                return inspectFile(psiFile, scannableFiles, module, manager);
            } finally {
                scannableFiles.forEach(ScannableFile::deleteIfRequired);
            }
        }, NO_PROBLEMS_FOUND, FIVE_SECONDS), manager, isOnTheFly);
    } catch (ProcessCanceledException | AssertionError e) {
        LOG.debug("Inspection cancelled when scanning: " + psiFile.getName());
        return noProblemsFound(manager);
    } catch (Throwable e) {
        LOG.warn("CheckStyle threw an exception when inspecting: " + psiFile.getName(), e);
        showException(manager.getProject(), e);
        return noProblemsFound(manager);
    }
}
Also used : ScannableFile(org.infernus.idea.checkstyle.checker.ScannableFile) Module(com.intellij.openapi.module.Module) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Aggregations

ScannableFile (org.infernus.idea.checkstyle.checker.ScannableFile)5 Module (com.intellij.openapi.module.Module)3 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)3 InspectionManager (com.intellij.codeInspection.InspectionManager)2 LocalInspectionTool (com.intellij.codeInspection.LocalInspectionTool)2 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)2 ServiceManager (com.intellij.openapi.components.ServiceManager)2 Logger (com.intellij.openapi.diagnostic.Logger)2 ModuleUtil (com.intellij.openapi.module.ModuleUtil)2 Project (com.intellij.openapi.project.Project)2 PsiFile (com.intellij.psi.PsiFile)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 Collections (java.util.Collections)2 Collections.singletonList (java.util.Collections.singletonList)2 List (java.util.List)2 Optional.ofNullable (java.util.Optional.ofNullable)2 Collectors.toList (java.util.stream.Collectors.toList)2 javax.swing (javax.swing)2