[PATCH] D16524: [clang-format-vs] Fix sort include main include: Use current path for the '-assume-filename'
Jean-Philippe Dufraigne via cfe-commits
cfe-commits at lists.llvm.org
Sun Jan 24 12:02:57 PST 2016
jeanphilippeD created this revision.
jeanphilippeD added a reviewer: djasper.
jeanphilippeD added a subscriber: cfe-commits.
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"
Caveat: My setup is not ideal for test (Visual studio 2015 only), and I am not familiar with visual studio extension development, but I think the changes are straight forward enough.
I've tested this fix building it and running it on Visual Studio 2015, but I do not think there would be any compatibility issue since I am using API that are already used in the GetDocumentParent function.
http://reviews.llvm.org/D16524
Files:
tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs
Index: tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs
===================================================================
--- tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs
+++ tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs
@@ -202,9 +202,10 @@
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 @@
///
/// Formats the text range starting at offset of the given length.
/// </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(
typeof(ClangFormatPackage).Assembly.Location);
@@ -257,6 +258,8 @@
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 @@
}
return null;
}
+
+ private string GetDocumentPath(IWpfTextView view)
+ {
+ ITextDocument document;
+ if (view.TextBuffer.Properties.TryGetProperty(typeof(ITextDocument), out document))
+ {
+ return document.FilePath;
+ }
+ return null;
+ }
}
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D16524.45830.patch
Type: text/x-patch
Size: 2236 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160124/a6ebe6a4/attachment.bin>
More information about the cfe-commits
mailing list