/// <summary> /// Runs clang-format on the current selection /// </summary> private void FormatSelection(OptionPageGrid options) { IWpfTextView view = Vsix.GetCurrentView(); if (view == null) { // We're not in a text view. return; } string text = view.TextBuffer.CurrentSnapshot.GetText(); int start = view.Selection.Start.Position.GetContainingLine().Start.Position; int end = view.Selection.End.Position.GetContainingLine().End.Position; int length = end - start; // clang-format doesn't support formatting a range that starts at the end // of the file. if (start >= text.Length && text.Length > 0) { start = text.Length - 1; } string path = Vsix.GetDocumentParent(view); string filePath = Vsix.GetDocumentPath(view); RunClangFormatAndApplyReplacements(text, start, length, path, filePath, options, view); }
/// <summary> /// Runs clang-format on the current selection /// </summary> private void FormatSelection(OptionPageGrid options) { IWpfTextView view = Vsix.GetCurrentView(); if (view == null) { // We're not in a text view. return; } string text = view.TextBuffer.CurrentSnapshot.GetText(); int start = view.Selection.Start.Position.GetContainingLine().Start.Position; int end = view.Selection.End.Position.GetContainingLine().End.Position; if (start >= end) { return; } // convert the utf16 index to multi bytes index start = enc.GetByteCount(text.ToCharArray(), 0, start); end = enc.GetByteCount(text.ToCharArray(), 0, end); int length = end - start; if (length <= 0) { return; } // clang-format doesn't support formatting a range that starts at the end // of the file. if (start >= text.Length && text.Length > 0) { start = text.Length - 1; } string path = Vsix.GetDocumentParent(view); string filePath = Vsix.GetDocumentPath(view); byte[] buffer = enc.GetBytes(text); RunClangFormatAndApplyReplacements(buffer, start, length, path, filePath, options, view); }
/// <summary> /// Runs clang-format on the current document /// </summary> private void FormatDocument(OptionPageGrid options) { FormatView(Vsix.GetCurrentView(), options); }