/// <summary>
/// Applies the clang-format replacements (xml) to the current view
/// </summary>
private static void ApplyClangFormatReplacements(string replacements, IWpfTextView view)
{
// clang-format returns no replacements if input text is empty
if (replacements.Length == 0)
{
return;
}
var root = XElement.Parse(replacements);
var edit = view.TextBuffer.CreateEdit();
int last_offset_utf8 = 0;
int last_offset_utf16 = 0;
foreach (XElement replacement in root.Descendants("replacement"))
{
int offset_utf8 = int.Parse(replacement.Attribute("offset").Value);
int length_utf8 = int.Parse(replacement.Attribute("length").Value);
// convert the multi bytes index to utf16 index
// assume that offsets is in order.
int offset_utf16 = enc.GetCharCount(buffer, last_offset_utf8, offset_utf8 - last_offset_utf8) + last_offset_utf16;
int length_utf16 = enc.GetCharCount(buffer, offset_utf8, length_utf8);
edit.Replace(offset_utf16, length_utf16, replacement.Value);
last_offset_utf8 = offset_utf8;
last_offset_utf16 = offset_utf16;
}
edit.Apply();
view.Selection.Clear();
}