Search in sources :

Example 1 with ScratchRootType

use of com.intellij.ide.scratch.ScratchRootType in project flutter-intellij by flutter.

the class FlutterErrorReportSubmitter method submitAsync.

@SuppressWarnings("deprecation")
@Override
public void submitAsync(@NotNull IdeaLoggingEvent[] events, @Nullable String additionalInfo, @NotNull Component parentComponent, @NotNull Consumer<SubmittedReportInfo> consumer) {
    if (events.length == 0) {
        consumer.consume(new SubmittedReportInfo(null, null, SubmittedReportInfo.SubmissionStatus.FAILED));
        return;
    }
    final DataContext dataContext = DataManager.getInstance().getDataContext(parentComponent);
    Project project = PlatformDataKeys.PROJECT.getData(dataContext);
    if (project == null) {
        project = ProjectManager.getInstance().getDefaultProject();
    }
    final StringBuilder builder = new StringBuilder();
    builder.append("Please file this bug report at https://github.com/flutter/flutter-intellij/issues/new.\n");
    builder.append("\n");
    builder.append("---\n");
    builder.append("\n");
    builder.append("## What happened\n");
    builder.append("\n");
    if (additionalInfo != null) {
        builder.append(additionalInfo.trim()).append("\n");
    } else {
        builder.append("(please describe what you were doing when this exception occurred)\n");
    }
    builder.append("\n");
    builder.append("## Version information\n");
    builder.append("\n");
    // IntelliJ version
    final ApplicationInfo info = ApplicationInfo.getInstance();
    builder.append(info.getVersionName()).append(" `").append(info.getFullVersion()).append("`");
    final PluginId pid = FlutterUtils.getPluginId();
    final IdeaPluginDescriptor flutterPlugin = PluginManager.getPlugin(pid);
    // noinspection ConstantConditions
    builder.append(" • Flutter plugin `").append(pid.getIdString()).append(' ').append(flutterPlugin.getVersion()).append("`");
    final IdeaPluginDescriptor dartPlugin = PluginManager.getPlugin(PluginId.getId("Dart"));
    if (dartPlugin != null) {
        // noinspection ConstantConditions
        builder.append(" • Dart plugin `").append(dartPlugin.getVersion()).append("`");
    }
    builder.append("\n\n");
    final FlutterSdk sdk = FlutterSdk.getFlutterSdk(project);
    if (sdk == null) {
        builder.append("No Flutter sdk configured.\n");
    } else {
        final String flutterVersion = getFlutterVersion(sdk);
        if (flutterVersion != null) {
            builder.append(flutterVersion.trim()).append("\n");
        } else {
            builder.append("Error getting Flutter sdk information.\n");
        }
    }
    builder.append("\n");
    for (IdeaLoggingEvent event : events) {
        builder.append("## Exception\n");
        builder.append("\n");
        builder.append(event.getMessage()).append("\n");
        builder.append("\n");
        if (event.getThrowable() != null) {
            builder.append("```\n");
            builder.append(event.getThrowableText().trim()).append("\n");
            builder.append("```\n");
            builder.append("\n");
            FlutterInitializer.getAnalytics().sendException(event.getThrowable(), false);
        }
    }
    final String text = builder.toString().trim() + "\n";
    // Create scratch file.
    final ScratchRootType scratchRoot = ScratchRootType.getInstance();
    final VirtualFile file = scratchRoot.createScratchFile(project, "bug-report.md", Language.ANY, text);
    if (file != null) {
        // Open the file.
        new OpenFileDescriptor(project, file).navigate(true);
    } else {
        consumer.consume(new SubmittedReportInfo(null, null, SubmittedReportInfo.SubmissionStatus.FAILED));
    }
    consumer.consume(new SubmittedReportInfo(null, "", SubmittedReportInfo.SubmissionStatus.NEW_ISSUE));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ApplicationInfo(com.intellij.openapi.application.ApplicationInfo) IdeaPluginDescriptor(com.intellij.ide.plugins.IdeaPluginDescriptor) PluginId(com.intellij.openapi.extensions.PluginId) IdeaLoggingEvent(com.intellij.openapi.diagnostic.IdeaLoggingEvent) Project(com.intellij.openapi.project.Project) DataContext(com.intellij.openapi.actionSystem.DataContext) FlutterSdk(io.flutter.sdk.FlutterSdk) ScratchRootType(com.intellij.ide.scratch.ScratchRootType) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) SubmittedReportInfo(com.intellij.openapi.diagnostic.SubmittedReportInfo)

Example 2 with ScratchRootType

use of com.intellij.ide.scratch.ScratchRootType in project flutter-intellij by flutter.

the class FlutterErrorReportSubmitter method submit.

// @Override
public boolean submit(@NotNull IdeaLoggingEvent[] events, @Nullable String additionalInfo, @NotNull Component parentComponent, @NotNull Consumer<? super SubmittedReportInfo> consumer) {
    if (events.length == 0) {
        // Don't remove the cast until a later version of Android Studio.
        fail(((Consumer<SubmittedReportInfo>) consumer));
        return false;
    }
    String stackTrace = null;
    String errorMessage = null;
    for (IdeaLoggingEvent event : events) {
        String stackTraceText = event.getThrowableText();
        if (stackTraceText.startsWith(COMPLETION_EXCEPTION_PREFIX)) {
            stackTraceText = stackTraceText.substring(COMPLETION_EXCEPTION_PREFIX.length());
            if (stackTraceText.startsWith(DaemonApi.FLUTTER_ERROR_PREFIX)) {
                final String message = stackTraceText.substring(DaemonApi.FLUTTER_ERROR_PREFIX.length());
                final int start = message.indexOf(": ") + 2;
                if (start == 0)
                    continue;
                int end = message.indexOf('\n');
                if (end < 0)
                    end = message.length();
                final String error = message.substring(start, end);
                stackTrace = message.substring(end + 1);
                for (String err : KNOWN_ERRORS) {
                    if (error.contains(err)) {
                        if (end != message.length()) {
                            // Dart stack trace included so extract it and set the issue target to the Flutter repo.
                            errorMessage = err;
                            final int endOfDartStack = stackTrace.indexOf("\\n\"\n");
                            if (endOfDartStack > 0) {
                                // Get only the part between quotes. If the format is wrong just use the whole thing.
                                stackTrace = stackTrace.substring(1, endOfDartStack);
                            }
                            break;
                        }
                    }
                }
            }
        }
    }
    final DataContext dataContext = DataManager.getInstance().getDataContext(parentComponent);
    final Project project = PROJECT.getData(dataContext);
    if (project == null) {
        // Don't remove the cast until a later version of Android Studio.
        fail(((Consumer<SubmittedReportInfo>) consumer));
        return false;
    }
    final StringBuilder builder = new StringBuilder();
    builder.append("Please file this bug report at ");
    builder.append("https://github.com/flutter/flutter-intellij/issues/new");
    builder.append(".\n");
    builder.append("\n");
    builder.append("---\n");
    builder.append("\n");
    builder.append("## What happened\n");
    builder.append("\n");
    if (additionalInfo != null) {
        builder.append(additionalInfo.trim()).append("\n");
    } else {
        builder.append("(please describe what you were doing when this exception occurred)\n");
    }
    builder.append("\n");
    builder.append("## Version information\n");
    builder.append("\n");
    // IntelliJ version
    final ApplicationInfo info = ApplicationInfo.getInstance();
    builder.append(info.getVersionName()).append(" `").append(info.getFullVersion()).append("`");
    final PluginId pid = FlutterUtils.getPluginId();
    final IdeaPluginDescriptor flutterPlugin = PluginManagerCore.getPlugin(pid);
    // noinspection ConstantConditions
    builder.append(" • Flutter plugin `").append(pid.getIdString()).append(' ').append(flutterPlugin.getVersion()).append("`");
    final IdeaPluginDescriptor dartPlugin = PluginManagerCore.getPlugin(PluginId.getId("Dart"));
    if (dartPlugin != null) {
        builder.append(" • Dart plugin `").append(dartPlugin.getVersion()).append("`");
    }
    builder.append("\n\n");
    final FlutterSdk sdk = FlutterSdk.getFlutterSdk(project);
    if (sdk == null) {
        builder.append("No Flutter sdk configured.\n");
    } else {
        final String flutterVersion = getFlutterVersion(sdk);
        if (flutterVersion != null) {
            builder.append(flutterVersion.trim()).append("\n");
        } else {
            builder.append("Error getting Flutter sdk information.\n");
        }
    }
    builder.append("\n");
    if (stackTrace == null) {
        for (IdeaLoggingEvent event : events) {
            builder.append("## Exception\n");
            builder.append("\n");
            builder.append(event.getMessage()).append("\n");
            builder.append("\n");
            if (event.getThrowable() != null) {
                builder.append("```\n");
                builder.append(event.getThrowableText().trim()).append("\n");
                builder.append("```\n");
                builder.append("\n");
            }
        }
    } else {
        builder.append("## Exception\n");
        builder.append("\n");
        builder.append(errorMessage).append("\n");
        builder.append("\n");
        builder.append("```\n");
        builder.append(stackTrace.replaceAll("\\\\n", "\n")).append("\n");
        builder.append("```\n");
        builder.append("\n");
    }
    for (IdeaLoggingEvent event : events) {
        FlutterInitializer.getAnalytics().sendException(event.getThrowableText(), false);
    }
    final String text = builder.toString().trim() + "\n";
    // Create scratch file.
    final ScratchRootType scratchRoot = ScratchRootType.getInstance();
    final VirtualFile file = scratchRoot.createScratchFile(project, "bug-report.md", PlainTextLanguage.INSTANCE, text);
    if (file == null) {
        // Don't remove the cast until a later version of Android Studio.
        fail(((Consumer<SubmittedReportInfo>) consumer));
        return false;
    }
    // Open the file.
    new OpenFileDescriptor(project, file).navigate(true);
    consumer.consume(new SubmittedReportInfo(null, "", SubmittedReportInfo.SubmissionStatus.NEW_ISSUE));
    return true;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ApplicationInfo(com.intellij.openapi.application.ApplicationInfo) IdeaPluginDescriptor(com.intellij.ide.plugins.IdeaPluginDescriptor) PluginId(com.intellij.openapi.extensions.PluginId) IdeaLoggingEvent(com.intellij.openapi.diagnostic.IdeaLoggingEvent) Project(com.intellij.openapi.project.Project) DataContext(com.intellij.openapi.actionSystem.DataContext) Consumer(com.intellij.util.Consumer) FlutterSdk(io.flutter.sdk.FlutterSdk) ScratchRootType(com.intellij.ide.scratch.ScratchRootType) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) SubmittedReportInfo(com.intellij.openapi.diagnostic.SubmittedReportInfo)

Aggregations

IdeaPluginDescriptor (com.intellij.ide.plugins.IdeaPluginDescriptor)2 ScratchRootType (com.intellij.ide.scratch.ScratchRootType)2 DataContext (com.intellij.openapi.actionSystem.DataContext)2 ApplicationInfo (com.intellij.openapi.application.ApplicationInfo)2 IdeaLoggingEvent (com.intellij.openapi.diagnostic.IdeaLoggingEvent)2 SubmittedReportInfo (com.intellij.openapi.diagnostic.SubmittedReportInfo)2 PluginId (com.intellij.openapi.extensions.PluginId)2 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)2 Project (com.intellij.openapi.project.Project)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 FlutterSdk (io.flutter.sdk.FlutterSdk)2 Consumer (com.intellij.util.Consumer)1