private void Trim() { TextDocumentHandler handler = new TextDocumentHandler(this.dte); if (handler.HasNonEmptySelection) { Options options = this.package.Options; bool execute = true; if (!options.OnlyShowTrimDialogWhenShiftIsPressed || Utilities.IsShiftPressed) { TrimDialog dialog = new TrimDialog(); execute = dialog.Execute(options); } if (execute && (options.TrimStart || options.TrimEnd)) { string text = handler.SelectedText; TextLines lines = new TextLines(text); lines.Trim(options.TrimStart, options.TrimEnd); string trimmedText = lines.ToString(); handler.SetSelectedTextIfUnchanged(trimmedText, "Trim"); } } }
private void SortLines() { TextDocumentHandler handler = new TextDocumentHandler(this.dte); if (handler.HasNonEmptySelection) { Options options = this.package.Options; SortDialog dialog = new SortDialog(); if (dialog.Execute(options)) { StringComparison comparison; if (options.SortCompareByOrdinal) { comparison = options.SortCaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase; } else { comparison = options.SortCaseSensitive ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase; } // Now sort the lines and put them back as the selection string text = handler.SelectedText; TextLines lines = new TextLines(text); lines.Sort(comparison, options.SortAscending, options.SortIgnoreWhitespace, options.SortIgnorePunctuation, options.SortEliminateDuplicates); string sortedText = lines.ToString(); handler.SetSelectedTextIfUnchanged(sortedText, "Sort Lines"); } } }
private static void UpdateSelectedText(TextDocumentHandler handler, string commandName, Action <TextLines> updateSelection) { string text = handler.SelectedText; TextLines lines = new TextLines(text); updateSelection(lines); handler.SetSelectedTextIfUnchanged(lines.ToString(), commandName); }
private static void UpdateSelectedText(TextDocumentHandler handler, string commandName, Action <TextLines> updateSelection) { ThreadHelper.ThrowIfNotOnUIThread(); string text = handler.SelectedText; TextLines lines = new(text); updateSelection(lines); handler.SetSelectedTextIfUnchanged(lines.ToString(), commandName); }
private void StreamText() { TextDocumentHandler handler = new TextDocumentHandler(this.dte); if (handler.HasNonEmptySelection) { string text = handler.SelectedText; TextLines lines = new TextLines(text); string streamedText = lines.Stream(); handler.SetSelectedTextIfUnchanged(streamedText, "Stream Text"); } }
private void CheckSpelling() { TextDocumentHandler handler = new TextDocumentHandler(this.dte); if (handler.HasNonEmptySelection) { try { // Launch Word. Word._Application wordApp = new Word.Application(); // Add a document. Word._Document wordDoc = wordApp.Documents.Add(); // Clear current contents. Word.Range range = wordApp.Selection.Range; range.WholeStory(); range.Delete(); range = null; // Add the text the user selected. wordApp.Selection.Text = handler.SelectedText; // Show it wordApp.Visible = true; wordApp.Activate(); wordDoc.Activate(); // Check spelling wordDoc.CheckSpelling(); // Get the edited text back wordApp.Selection.WholeStory(); string newText = wordApp.Selection.Text; // Word always adds an extra CR, so strip that off. // Also it converts all LFs to CRs, so change // that back. if (!string.IsNullOrEmpty(newText)) { if (newText.EndsWith("\r")) { newText = newText.Substring(0, newText.Length - 1); } newText = newText.Replace("\r", "\r\n"); } handler.SetSelectedTextIfUnchanged(newText, "Check Spelling"); // Tell the doc and Word to go away. object saveChanges = false; wordDoc.Close(ref saveChanges); wordApp.Visible = false; wordApp.Quit(); } catch (COMException ex) { // If we get REGDB_E_CLASSNOTREG, then Word probably isn't installed. const uint REGDB_E_CLASSNOTREG = 0x80040154; if (unchecked ((uint)ex.ErrorCode) == REGDB_E_CLASSNOTREG) { this.package.ShowMessageBox( "Microsoft Word is required in order to check spelling, but it isn't available.\r\n\r\nDetails:\r\n" + ex.Message, true); } else { throw; } } } }