From: Daniel Jasper Date: Wed, 10 Feb 2016 12:42:58 +0000 (+0000) Subject: clang-format sort include use the source file name to determine the X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=54c6c5d79a1bde926f7d03e48f28c8b26b9abf2b;p=clang clang-format sort include use the source file name to determine the "main include" that will be the 1st include (category 0). Because the clang-format visual studio extension does not pass the file name and use the standard input, sort include cannot find a "main include": Testing fix on llvm\tools\clang\lib\Format\Format.cpp: Original file: #include "clang/Format/Format.h" ... #include "clang/Basic/SourceManager.h" #include "clang/Lex/Lexer.h" Without fix, selecting the includes and running visual studio clang-format: ... #include "clang/Basic/SourceManager.h" #include "clang/Format/Format.h" #include "clang/Lex/Lexer.h" With fix, selecting the includes and running visual studio clang-format: #include "clang/Format/Format.h" ... #include "clang/Basic/SourceManager.h" #include "clang/Lex/Lexer.h" Test 2 with main header not at the start: Original file: ... #include "clang/Format/Format.h" #include "clang/Basic/SourceManager.h" #include "clang/Lex/Lexer.h" Without fix, selecting the includes and running visual studio clang-format: ... #include "clang/Basic/SourceManager.h" #include "clang/Format/Format.h" #include "clang/Lex/Lexer.h" With fix, selecting the includes and running visual studio clang-format: #include "clang/Format/Format.h" ... #include "clang/Basic/SourceManager.h" #include "clang/Lex/Lexer.h" Patch by Jean-Philippe Dufraigne, thank you. Review: http://reviews.llvm.org/D16524 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@260378 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs b/tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs index df872b2e21..6af2fd177f 100644 --- a/tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs +++ b/tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs @@ -202,9 +202,10 @@ namespace LLVM.ClangFormat if (start >= text.Length && text.Length > 0) start = text.Length - 1; string path = GetDocumentParent(view); + string filePath = GetDocumentPath(view); try { - var root = XElement.Parse(RunClangFormat(text, start, length, path)); + var root = XElement.Parse(RunClangFormat(text, start, length, path, filePath)); var edit = view.TextBuffer.CreateEdit(); foreach (XElement replacement in root.Descendants("replacement")) { @@ -237,7 +238,7 @@ namespace LLVM.ClangFormat /// /// Formats the text range starting at offset of the given length. /// - private string RunClangFormat(string text, int offset, int length, string path) + private string RunClangFormat(string text, int offset, int length, string path, string filePath) { string vsixPath = Path.GetDirectoryName( typeof(ClangFormatPackage).Assembly.Location); @@ -257,6 +258,8 @@ namespace LLVM.ClangFormat if (GetSortIncludes()) process.StartInfo.Arguments += " -sort-includes "; string assumeFilename = GetAssumeFilename(); + if (string.IsNullOrEmpty(assumeFilename)) + assumeFilename = filePath; if (!string.IsNullOrEmpty(assumeFilename)) process.StartInfo.Arguments += " -assume-filename \"" + assumeFilename + "\""; process.StartInfo.CreateNoWindow = true; @@ -355,5 +358,15 @@ namespace LLVM.ClangFormat } return null; } + + private string GetDocumentPath(IWpfTextView view) + { + ITextDocument document; + if (view.TextBuffer.Properties.TryGetProperty(typeof(ITextDocument), out document)) + { + return document.FilePath; + } + return null; + } } }