"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
if (start >= text.Length && text.Length > 0)\r
start = text.Length - 1;\r
string path = GetDocumentParent(view);\r
+ string filePath = GetDocumentPath(view);\r
try\r
{\r
- var root = XElement.Parse(RunClangFormat(text, start, length, path));\r
+ var root = XElement.Parse(RunClangFormat(text, start, length, path, filePath));\r
var edit = view.TextBuffer.CreateEdit();\r
foreach (XElement replacement in root.Descendants("replacement"))\r
{\r
/// \r
/// Formats the text range starting at offset of the given length.\r
/// </summary>\r
- private string RunClangFormat(string text, int offset, int length, string path)\r
+ private string RunClangFormat(string text, int offset, int length, string path, string filePath)\r
{\r
string vsixPath = Path.GetDirectoryName(\r
typeof(ClangFormatPackage).Assembly.Location);\r
if (GetSortIncludes())\r
process.StartInfo.Arguments += " -sort-includes ";\r
string assumeFilename = GetAssumeFilename();\r
+ if (string.IsNullOrEmpty(assumeFilename))\r
+ assumeFilename = filePath;\r
if (!string.IsNullOrEmpty(assumeFilename))\r
process.StartInfo.Arguments += " -assume-filename \"" + assumeFilename + "\"";\r
process.StartInfo.CreateNoWindow = true;\r
}\r
return null;\r
}\r
+\r
+ private string GetDocumentPath(IWpfTextView view)\r
+ {\r
+ ITextDocument document;\r
+ if (view.TextBuffer.Properties.TryGetProperty(typeof(ITextDocument), out document))\r
+ {\r
+ return document.FilePath;\r
+ }\r
+ return null;\r
+ }\r
}\r
}\r