Skip to content
Snippets Groups Projects
Commit 54c6c5d7 authored by Daniel Jasper's avatar Daniel Jasper
Browse files

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
parent 8284fd6a
No related branches found
No related tags found
No related merge requests found
...@@ -202,9 +202,10 @@ namespace LLVM.ClangFormat ...@@ -202,9 +202,10 @@ namespace LLVM.ClangFormat
if (start >= text.Length && text.Length > 0) if (start >= text.Length && text.Length > 0)
start = text.Length - 1; start = text.Length - 1;
string path = GetDocumentParent(view); string path = GetDocumentParent(view);
string filePath = GetDocumentPath(view);
try 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(); var edit = view.TextBuffer.CreateEdit();
foreach (XElement replacement in root.Descendants("replacement")) foreach (XElement replacement in root.Descendants("replacement"))
{ {
...@@ -237,7 +238,7 @@ namespace LLVM.ClangFormat ...@@ -237,7 +238,7 @@ namespace LLVM.ClangFormat
/// ///
/// Formats the text range starting at offset of the given length. /// Formats the text range starting at offset of the given length.
/// </summary> /// </summary>
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( string vsixPath = Path.GetDirectoryName(
typeof(ClangFormatPackage).Assembly.Location); typeof(ClangFormatPackage).Assembly.Location);
...@@ -257,6 +258,8 @@ namespace LLVM.ClangFormat ...@@ -257,6 +258,8 @@ namespace LLVM.ClangFormat
if (GetSortIncludes()) if (GetSortIncludes())
process.StartInfo.Arguments += " -sort-includes "; process.StartInfo.Arguments += " -sort-includes ";
string assumeFilename = GetAssumeFilename(); string assumeFilename = GetAssumeFilename();
if (string.IsNullOrEmpty(assumeFilename))
assumeFilename = filePath;
if (!string.IsNullOrEmpty(assumeFilename)) if (!string.IsNullOrEmpty(assumeFilename))
process.StartInfo.Arguments += " -assume-filename \"" + assumeFilename + "\""; process.StartInfo.Arguments += " -assume-filename \"" + assumeFilename + "\"";
process.StartInfo.CreateNoWindow = true; process.StartInfo.CreateNoWindow = true;
...@@ -355,5 +358,15 @@ namespace LLVM.ClangFormat ...@@ -355,5 +358,15 @@ namespace LLVM.ClangFormat
} }
return null; return null;
} }
private string GetDocumentPath(IWpfTextView view)
{
ITextDocument document;
if (view.TextBuffer.Properties.TryGetProperty(typeof(ITextDocument), out document))
{
return document.FilePath;
}
return null;
}
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment