void ShowInconsistentWarning(bool preferUnixNewLines) { if (editor.Document == null) return; // editor was disposed windows = new RadioButton { IsChecked = !preferUnixNewLines, Content = ResourceService.GetString("Dialog.Options.IDEOptions.LoadSaveOptions.WindowsRadioButton"), Margin = new Thickness(0, 0, 8, 0) }; unix = new RadioButton { IsChecked = preferUnixNewLines, Content = ResourceService.GetString("Dialog.Options.IDEOptions.LoadSaveOptions.UnixRadioButton") }; normalizeButton = new Button { Content = ResourceService.GetString("AddIns.AvalonEdit.InconsistentNewlines.Normalize") }; cancelButton = new Button { Content = ResourceService.GetString("Global.CancelButtonText") }; var content = new StackPanel { Children = { new TextBlock { Text = ResourceService.GetString("AddIns.AvalonEdit.InconsistentNewlines.Description"), TextWrapping = TextWrapping.WrapWithOverflow }, windows, unix, new StackPanel { Margin = new Thickness(0, 2, 0, 0), Orientation = Orientation.Horizontal, Children = { normalizeButton, cancelButton } } } }; groupBox = editor.GetService<IEditorUIService>().CreateOverlayUIElement(content); groupBox.Title = ResourceService.GetString("AddIns.AvalonEdit.InconsistentNewlines.Header"); var featureUse = SD.AnalyticsMonitor.TrackFeature(typeof(NewLineConsistencyCheck)); EventHandler removeWarning = null; removeWarning = delegate { groupBox.Remove(); editor.PrimaryTextEditor.TextArea.Focus(); editor.LoadedFileContent -= removeWarning; featureUse.EndTracking(); }; editor.LoadedFileContent += removeWarning; cancelButton.Click += delegate { SD.AnalyticsMonitor.TrackFeature(typeof(NewLineConsistencyCheck), "cancelButton"); removeWarning(null, null); }; normalizeButton.Click += delegate { SD.AnalyticsMonitor.TrackFeature(typeof(NewLineConsistencyCheck), "normalizeButton"); removeWarning(null, null); TextDocument document = editor.Document; string newNewLine = (unix.IsChecked == true) ? "\n" : "\r\n"; using (document.RunUpdate()) { for (int i = 1; i <= document.LineCount; i++) { // re-fetch DocumentLine for every iteration because we're modifying the newlines so that DocumentLines might get re-created DocumentLine line = document.GetLineByNumber(i); if (line.DelimiterLength > 0) { int endOffset = line.EndOffset; if (document.GetText(endOffset, line.DelimiterLength) != newNewLine) document.Replace(endOffset, line.DelimiterLength, newNewLine); } } } }; }
void AttachToCodeEditor() { if (editor.Document == null) return; // editor was disposed var content = new StackPanel { Children = { new TextBlock { Text = "Use Up/Down to move to another location.\r\n" + "Press Enter to select the location.\r\n" + "Press Esc to cancel this operation." } } }; groupBox = editor.GetService<IEditorUIService>().CreateOverlayUIElement(content); groupBox.Title = operation; }
void ShowInconsistentWarning(bool preferUnixNewLines) { if (editor.Document == null) { return; // editor was disposed } windows = new RadioButton { IsChecked = !preferUnixNewLines, Content = ResourceService.GetString("Dialog.Options.IDEOptions.LoadSaveOptions.WindowsRadioButton"), Margin = new Thickness(0, 0, 8, 0) }; unix = new RadioButton { IsChecked = preferUnixNewLines, Content = ResourceService.GetString("Dialog.Options.IDEOptions.LoadSaveOptions.UnixRadioButton") }; normalizeButton = new Button { Content = ResourceService.GetString("AddIns.AvalonEdit.InconsistentNewlines.Normalize") }; cancelButton = new Button { Content = ResourceService.GetString("Global.CancelButtonText") }; var content = new StackPanel { Children = { new TextBlock { Text = ResourceService.GetString("AddIns.AvalonEdit.InconsistentNewlines.Description"), TextWrapping = TextWrapping.WrapWithOverflow }, windows, unix, new StackPanel { Margin = new Thickness(0, 2, 0, 0), Orientation = Orientation.Horizontal, Children = { normalizeButton, cancelButton } } } }; groupBox = editor.GetService <IEditorUIService>().CreateOverlayUIElement(content); groupBox.Title = ResourceService.GetString("AddIns.AvalonEdit.InconsistentNewlines.Header"); var featureUse = SD.AnalyticsMonitor.TrackFeature(typeof(NewLineConsistencyCheck)); EventHandler removeWarning = null; removeWarning = delegate { groupBox.Remove(); editor.PrimaryTextEditor.TextArea.Focus(); editor.LoadedFileContent -= removeWarning; featureUse.EndTracking(); }; editor.LoadedFileContent += removeWarning; cancelButton.Click += delegate { SD.AnalyticsMonitor.TrackFeature(typeof(NewLineConsistencyCheck), "cancelButton"); removeWarning(null, null); }; normalizeButton.Click += delegate { SD.AnalyticsMonitor.TrackFeature(typeof(NewLineConsistencyCheck), "normalizeButton"); removeWarning(null, null); TextDocument document = editor.Document; string newNewLine = (unix.IsChecked == true) ? "\n" : "\r\n"; using (document.RunUpdate()) { for (int i = 1; i <= document.LineCount; i++) { // re-fetch DocumentLine for every iteration because we're modifying the newlines so that DocumentLines might get re-created DocumentLine line = document.GetLineByNumber(i); if (line.DelimiterLength > 0) { int endOffset = line.EndOffset; if (document.GetText(endOffset, line.DelimiterLength) != newNewLine) { document.Replace(endOffset, line.DelimiterLength, newNewLine); } } } } }; }