[PATCH] D70633: clang-format-vs : Fix Unicode formatting

2019-11-27 Thread Hans Wennborg via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rGe20a1e486e14: clang-format-vs : Fix Unicode formatting 
(authored by hans).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D70633/new/

https://reviews.llvm.org/D70633

Files:
  clang/tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs

Index: clang/tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs
===
--- clang/tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs
+++ clang/tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs
@@ -24,6 +24,7 @@
 using System.Runtime.InteropServices;
 using System.Xml.Linq;
 using System.Linq;
+using System.Text;
 
 namespace LLVM.ClangFormat
 {
@@ -292,8 +293,7 @@
 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)
@@ -301,7 +301,7 @@
 string path = Vsix.GetDocumentParent(view);
 string filePath = Vsix.GetDocumentPath(view);
 
-RunClangFormatAndApplyReplacements(text, start, length, path, filePath, options, view);
+RunClangFormatAndApplyReplacements(text, start, end, path, filePath, options, view);
 }
 
 /// 
@@ -336,11 +336,11 @@
 RunClangFormatAndApplyReplacements(text, 0, text.Length, path, filePath, options, view);
 }
 
-private void RunClangFormatAndApplyReplacements(string text, int offset, int length, string path, string filePath, OptionPageGrid options, IWpfTextView view)
+private void RunClangFormatAndApplyReplacements(string text, int start, int end, string path, string filePath, OptionPageGrid options, IWpfTextView view)
 {
 try
 {
-string replacements = RunClangFormat(text, offset, length, path, filePath, options);
+string replacements = RunClangFormat(text, start, end, path, filePath, options);
 ApplyClangFormatReplacements(replacements, view);
 }
 catch (Exception e)
@@ -363,9 +363,9 @@
 /// 
 /// Runs the given text through clang-format and returns the replacements as XML.
 /// 
-/// Formats the text range starting at offset of the given length.
+/// Formats the text in range start and end.
 /// 
-private static string RunClangFormat(string text, int offset, int length, string path, string filePath, OptionPageGrid options)
+private static string RunClangFormat(string text, int start, int end, string path, string filePath, OptionPageGrid options)
 {
 string vsixPath = Path.GetDirectoryName(
 typeof(ClangFormatPackage).Assembly.Location);
@@ -373,6 +373,9 @@
 System.Diagnostics.Process process = new System.Diagnostics.Process();
 process.StartInfo.UseShellExecute = false;
 process.StartInfo.FileName = vsixPath + "\\clang-format.exe";
+char[] chars = text.ToCharArray();
+int offset = Encoding.UTF8.GetByteCount(chars, 0, start);
+int length = Encoding.UTF8.GetByteCount(chars, 0, end) - offset;
 // Poor man's escaping - this will not work when quotes are already escaped
 // in the input (but we don't need more).
 string style = options.Style.Replace("\"", "\\\"");
@@ -413,10 +416,11 @@
 // 2. We write everything to the standard output - this cannot block, as clang-format
 //reads the full standard input before analyzing it without writing anything to the
 //standard output.
-process.StandardInput.Write(text);
+StreamWriter utf8Writer = new StreamWriter(process.StandardInput.BaseStream, new UTF8Encoding(false));
+utf8Writer.Write(text);
 // 3. We notify clang-format that the input is done - after this point clang-format
 //will start analyzing the input and eventually write the output.
-process.StandardInput.Close();
+utf8Writer.Close();
 // 4. We must read clang-format's output before waiting for it to exit; clang-format
 //will close the channel by exiting.
 string output = process.StandardOutput.ReadToEnd();
@@ -440,13 +444,18 @@
 if (replacements.Length == 0)
 return;
 
+string text = view.TextBuffer.CurrentSnapshot.GetText();
+byte[] bytes = Encoding.UTF8.GetBytes(text);
+
 var root = 

[PATCH] D70633: clang-format-vs : Fix Unicode formatting

2019-11-27 Thread Hans Wennborg via Phabricator via cfe-commits
hans accepted this revision.
hans added a comment.
This revision is now accepted and ready to land.

>> empty2fill: Do you have an example input that I could use to hit the 
>> "Specified argument was out of the range of valid values." error?
> 
> F10867463: Main.cpp 
> 
> Encoded UTF-8(with BOM) for the Korean language.

I applied your patch locally and it seems to work :-) I'll commit it for you.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D70633/new/

https://reviews.llvm.org/D70633



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D70633: clang-format-vs : Fix Unicode formatting

2019-11-25 Thread empty2fill via Phabricator via cfe-commits
empty2fill added a comment.

In D70633#1758372 , @hans wrote:

> klimek, What do you think?
>
> empty2fill: Do you have an example input that I could use to hit the 
> "Specified argument was out of the range of valid values." error?


F10867396: Main.cpp 

Encoded Windows code page 949 for the Korean language.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D70633/new/

https://reviews.llvm.org/D70633



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D70633: clang-format-vs : Fix Unicode formatting

2019-11-25 Thread Manuel Klimek via Phabricator via cfe-commits
klimek added a comment.

generally makes sense


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D70633/new/

https://reviews.llvm.org/D70633



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D70633: clang-format-vs : Fix Unicode formatting

2019-11-25 Thread Hans Wennborg via Phabricator via cfe-commits
hans added a reviewer: klimek.
hans added a comment.

klimek, What do you think?

empty2fill: Do you have an example input that I could use to hit the "Specified 
argument was out of the range of valid values." error?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D70633/new/

https://reviews.llvm.org/D70633



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits