private void Analyze(bool silent) { var watch = new DiagnosticTimer(); var bufferId = NotepadPPGateway.GetCurrentBufferId(); var textLength = PluginBase.CurrentScintillaGateway.GetTextLength(); var text = PluginBase.CurrentScintillaGateway.GetTextRange(0, Math.Min(100000, textLength)); watch.Checkpoint("GetText"); var csvSettings = CsvAnalyzer.Analyze(text); if (csvSettings.Separator == '\0' && csvSettings.FieldWidths == null) { if (silent) { return; } var askUserDialog = new ParseSettings(csvSettings); this.UiThread(() => askUserDialog.ShowDialog()); if (askUserDialog.DialogResult != DialogResult.OK) { return; } csvSettings = askUserDialog.Settings; } watch.Checkpoint("Analyze"); using (var sr = ScintillaStreams.StreamAllText()) { this.Parse(csvSettings, watch, sr, bufferId); } }
private void OnMenuClickCreateNewCSV(object sender, EventArgs e) { if (!(this.dataGrid.DataSource is DataTable table)) { MessageBox.Show("No results available to convert"); return; } var initialSettings = this._lastGenerateSettings ?? new CsvSettings(Main.Settings.DefaultSeparator.Unescape(), Main.Settings.DefaultQuoteChar, '\0', true); var settingsDialog = new ParseSettings(initialSettings) { btnReparse = { Text = "&Ok" }, MainLabel = { Text = "How should the CSV be generated?" }, hasHeaderCheckbox = { ThreeState = false, Text = "Create header row" }, txbCommentChar = { Visible = false }, CommentCharLabel = { Visible = false }, }; if (settingsDialog.ShowDialog() == DialogResult.Cancel) { return; } var settings = settingsDialog.Settings; var watch = new DiagnosticTimer(); try { // Create new tab for results Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_MENUCOMMAND, 0, NppMenuCmd.IDM_FILE_NEW); watch.Checkpoint("New document created"); var headerLookup = Main.Settings.UseOriginalColumnHeadersOnGeneratedCsv && table.ExtendedProperties.ContainsKey("bufferId") && table.ExtendedProperties["bufferId"] is IntPtr bufferId ? Main.DataStorage.GetUnsafeColumnMaps(bufferId) : null; using (var stream = new BlockingStream(10)) { var producer = stream.StartProducer(s => settings.GenerateToStream(this.dataGrid.DataSource as DataTable, s, headerLookup)); var consumer = stream.StartConsumer(s => PluginBase.CurrentScintillaGateway.AddText(s)); Task.WaitAll(producer, consumer); } } catch (Exception ex) { Trace.TraceInformation("CSV gen: Exception: " + ex.GetType().Name + " - " + ex.Message); } this._lastGenerateSettings = settings; Trace.TraceInformation(watch.LastCheckpoint("CSV Done")); }