[PATCH] D27501: clang-format-vsix: add command to format document

2016-12-15 Thread Antonio Maiorano via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL289910: clang-format-vsix: add command to format document 
(authored by amaiorano).

Changed prior to commit:
  https://reviews.llvm.org/D27501?vs=80533=81701#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D27501

Files:
  cfe/trunk/tools/clang-format-vs/.gitignore
  cfe/trunk/tools/clang-format-vs/ClangFormat/ClangFormat.vsct
  cfe/trunk/tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs
  cfe/trunk/tools/clang-format-vs/ClangFormat/PkgCmdID.cs
  cfe/trunk/tools/clang-format-vs/README.txt

Index: cfe/trunk/tools/clang-format-vs/README.txt
===
--- cfe/trunk/tools/clang-format-vs/README.txt
+++ cfe/trunk/tools/clang-format-vs/README.txt
@@ -25,3 +25,27 @@
 ClangFormat/source.extension.vsixmanifest. Once the plug-in has been built with
 CMake once, it can be built manually from the ClangFormat.sln solution in Visual
 Studio.
+
+===
+ Debugging
+===
+
+Once you've built the clang_format_vsix project from LLVM.sln at least once,
+open ClangFormat.sln in Visual Studio, then:
+
+- Make sure the "Debug" target is selected
+- Open the ClangFormat project properties
+- Select the Debug tab
+- Set "Start external program:" to where your devenv.exe is installed. Typically
+  it's "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe"
+- Set "Command line arguments" to: /rootsuffix Exp
+- You can now set breakpoints if you like
+- Press F5 to build and run with debugger
+
+If all goes well, a new instance of Visual Studio will be launched in a special
+mode where it uses the experimental hive instead of the normal configuration hive.
+By default, when you build a VSIX project in Visual Studio, it auto-registers the
+extension in the experimental hive, allowing you to test it. In the new Visual Studio
+instance, open or create a C++ solution, and you should now see the Clang Format
+entries in the Tool menu. You can test it out, and any breakpoints you set will be
+hit where you can debug as usual.
Index: cfe/trunk/tools/clang-format-vs/.gitignore
===
--- cfe/trunk/tools/clang-format-vs/.gitignore
+++ cfe/trunk/tools/clang-format-vs/.gitignore
@@ -1,5 +1,6 @@
 # Visual Studio files
 .vs/
+*.user
 /packages/
 /ClangFormat/obj/
 /ClangFormat/bin/
Index: cfe/trunk/tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs
===
--- cfe/trunk/tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs
+++ cfe/trunk/tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs
@@ -180,41 +180,86 @@
 var commandService = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
 if (commandService != null)
 {
-var menuCommandID = new CommandID(GuidList.guidClangFormatCmdSet, (int)PkgCmdIDList.cmdidClangFormat);
-var menuItem = new MenuCommand(MenuItemCallback, menuCommandID);
-commandService.AddCommand(menuItem);
+{
+var menuCommandID = new CommandID(GuidList.guidClangFormatCmdSet, (int)PkgCmdIDList.cmdidClangFormatSelection);
+var menuItem = new MenuCommand(MenuItemCallback, menuCommandID);
+commandService.AddCommand(menuItem);
+}
+
+{
+var menuCommandID = new CommandID(GuidList.guidClangFormatCmdSet, (int)PkgCmdIDList.cmdidClangFormatDocument);
+var menuItem = new MenuCommand(MenuItemCallback, menuCommandID);
+commandService.AddCommand(menuItem);
+}
 }
 }
 #endregion
 
 private void MenuItemCallback(object sender, EventArgs args)
 {
+var mc = sender as System.ComponentModel.Design.MenuCommand;
+if (mc == null)
+return;
+
+switch (mc.CommandID.ID)
+{
+case (int)PkgCmdIDList.cmdidClangFormatSelection:
+FormatSelection();
+break;
+
+case (int)PkgCmdIDList.cmdidClangFormatDocument:
+FormatDocument();
+break;
+}
+}
+
+/// 
+/// Runs clang-format on the current selection
+/// 
+private void FormatSelection()
+{
 IWpfTextView view = GetCurrentView();
 if (view == null)
 // We're not in a text view.
 return;
 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;
+
 // 

[PATCH] D27501: clang-format-vsix: add command to format document

2016-12-12 Thread Antonio Maiorano via Phabricator via cfe-commits
amaiorano added inline comments.



Comment at: tools/clang-format-vs/ClangFormat/ClangFormat.vsct:76
+
+  Clang Format Document
+

klimek wrote:
> amaiorano wrote:
> > klimek wrote:
> > > hans wrote:
> > > > amaiorano wrote:
> > > > > hans wrote:
> > > > > > I think File would be better than Document when referring to source 
> > > > > > code.
> > > > > > 
> > > > > > But it seems a little annoying to need two menu alternatives. Could 
> > > > > > we make the regular "Clang Format" option just format the whole 
> > > > > > file if there is currently no selection, or would that be confusing?
> > > > > The reason I chose "Document" is that it mimics the existing 
> > > > > functionality in Visual Studio:
> > > > > {F2661117}
> > > > > 
> > > > > As you can see, "Ctrl+K, Ctrl+F" is bound to format selection by 
> > > > > default; which I assumed is the reason "Ctrl+R, Ctrl+F" was used for 
> > > > > Clang Format (Selection). So along the same lines, I bound "Ctrl + R, 
> > > > > Ctrl + D" to Clang Format Document.
> > > > > 
> > > > > I don't really have a problem with having multiple menu options, 
> > > > > although we could have both of them underneath a "Clang Format" top 
> > > > > menu, with "Format Selection" and "Format Document" as sub menu items.
> > > > > 
> > > > > As for annoyance in menus, I think most developers would reach for 
> > > > > keyboard shortcuts in this case anyway. Furthermore, the next feature 
> > > > > I want to add, building on top of this one, is allowing the user to 
> > > > > enable "Format on Save", which would make this even easier to use.
> > > > I see, I didn't realize they call it documents. And if they also have 
> > > > separate commands for formating selection and formating the whole file, 
> > > > maybe that makes sense for us too.
> > > > 
> > > > Right, I also imagine folks would use this from the keyboard, but there 
> > > > too it's annoying to have two shortcuts for the same thing. But again, 
> > > > if that's how it's generally done in VS...
> > > > 
> > > > I'd like to hear from Manuel on this patch too, though.
> > > Clang-format currently formats the current line / statement if there is 
> > > no selection, and that's how most people I know use it.
> > > Formatting the whole file by default is not what clang-format is 
> > > optimized for, and highly disruptive (eclipse does that, and it's 
> > > annoying).
> > > I'm more wondering why we need a setting for this at all - isn't 
> > > ctrl-a,ctrl-k,ctrl-f equivalent to formatting the whole file?
> > @klimek My own experience, which also seems to match those of others 
> > online, is that clang-format is a fantastic tool for making sure your 
> > code-base follows a strict formatting standard. What many people do is 
> > install Git pre-stage hooks, for instance, to run clang-format across 
> > entire source files, to ensure they're formatted properly. So even if it's 
> > not optimized for that, it works very well :)
> > 
> > Using clang-format to format only the current line doesn't guarantee that 
> > everything will be formatted accordingly. My own experience is that it 
> > sometimes formats the line differently than if you format the entire 
> > document. Or perhaps you end up writing a few lines of code, and formatting 
> > current line doesn't pick up all the lines you've modified. As for 
> > ctrl-a,ctrl-r-ctrl-f, the problem with it is that you lose your cursor 
> > position, as ctrl-a will place it at the end of the file.
> > 
> > You also mention that formatting the entire file is "highly disruptive" 
> > like in eclipse. I'm not sure how eclipse handles that, but so far VS seems 
> > to do a pretty good job. It manages to maintain breakpoint positions quite 
> > well, it manages to keep the cursor in the same place, and it manages to 
> > keep the relative location of the code where the cursor is at visible on 
> > screen. I suspect part of the reason it works well in VS is because 
> > Microsoft made sure it worked for their own format document feature 
> > (ctrl-k,ctrl-d).
> > 
> > As I mentioned earlier, this is a stepping stone towards adding a feature 
> > to "format on save", for which I'd like to offer the ability to choose 
> > "current line/selection" and "document". [[ 
> > https://github.com/Elders/VSE-FormatDocumentOnSave | Here's an example ]] 
> > of an extension that offers this feature, but for VS's built-in formatting.
> Note: with "highly disruptive" I meant only if the default behavior without 
> any selection is to format the whole file, as that basically kills the 
> workflow; that was what Hans suggested.
> 
> I'm not opposed to having an extra key binding if enough folks want it and 
> there is enough benefit - the argument to not move your cursor is a good 
> point.
Thanks! Now I just need to get my svn submit working. Chris Latner created my 
account, etc. but when I tried to submit another accepted change, it 

[PATCH] D27501: clang-format-vsix: add command to format document

2016-12-12 Thread Manuel Klimek via Phabricator via cfe-commits
klimek accepted this revision.
klimek added a comment.
This revision is now accepted and ready to land.

LG. I also think it makes the code nicer by breaking out the right functions. 
Thanks!




Comment at: tools/clang-format-vs/ClangFormat/ClangFormat.vsct:76
+
+  Clang Format Document
+

amaiorano wrote:
> klimek wrote:
> > hans wrote:
> > > amaiorano wrote:
> > > > hans wrote:
> > > > > I think File would be better than Document when referring to source 
> > > > > code.
> > > > > 
> > > > > But it seems a little annoying to need two menu alternatives. Could 
> > > > > we make the regular "Clang Format" option just format the whole file 
> > > > > if there is currently no selection, or would that be confusing?
> > > > The reason I chose "Document" is that it mimics the existing 
> > > > functionality in Visual Studio:
> > > > {F2661117}
> > > > 
> > > > As you can see, "Ctrl+K, Ctrl+F" is bound to format selection by 
> > > > default; which I assumed is the reason "Ctrl+R, Ctrl+F" was used for 
> > > > Clang Format (Selection). So along the same lines, I bound "Ctrl + R, 
> > > > Ctrl + D" to Clang Format Document.
> > > > 
> > > > I don't really have a problem with having multiple menu options, 
> > > > although we could have both of them underneath a "Clang Format" top 
> > > > menu, with "Format Selection" and "Format Document" as sub menu items.
> > > > 
> > > > As for annoyance in menus, I think most developers would reach for 
> > > > keyboard shortcuts in this case anyway. Furthermore, the next feature I 
> > > > want to add, building on top of this one, is allowing the user to 
> > > > enable "Format on Save", which would make this even easier to use.
> > > I see, I didn't realize they call it documents. And if they also have 
> > > separate commands for formating selection and formating the whole file, 
> > > maybe that makes sense for us too.
> > > 
> > > Right, I also imagine folks would use this from the keyboard, but there 
> > > too it's annoying to have two shortcuts for the same thing. But again, if 
> > > that's how it's generally done in VS...
> > > 
> > > I'd like to hear from Manuel on this patch too, though.
> > Clang-format currently formats the current line / statement if there is no 
> > selection, and that's how most people I know use it.
> > Formatting the whole file by default is not what clang-format is optimized 
> > for, and highly disruptive (eclipse does that, and it's annoying).
> > I'm more wondering why we need a setting for this at all - isn't 
> > ctrl-a,ctrl-k,ctrl-f equivalent to formatting the whole file?
> @klimek My own experience, which also seems to match those of others online, 
> is that clang-format is a fantastic tool for making sure your code-base 
> follows a strict formatting standard. What many people do is install Git 
> pre-stage hooks, for instance, to run clang-format across entire source 
> files, to ensure they're formatted properly. So even if it's not optimized 
> for that, it works very well :)
> 
> Using clang-format to format only the current line doesn't guarantee that 
> everything will be formatted accordingly. My own experience is that it 
> sometimes formats the line differently than if you format the entire 
> document. Or perhaps you end up writing a few lines of code, and formatting 
> current line doesn't pick up all the lines you've modified. As for 
> ctrl-a,ctrl-r-ctrl-f, the problem with it is that you lose your cursor 
> position, as ctrl-a will place it at the end of the file.
> 
> You also mention that formatting the entire file is "highly disruptive" like 
> in eclipse. I'm not sure how eclipse handles that, but so far VS seems to do 
> a pretty good job. It manages to maintain breakpoint positions quite well, it 
> manages to keep the cursor in the same place, and it manages to keep the 
> relative location of the code where the cursor is at visible on screen. I 
> suspect part of the reason it works well in VS is because Microsoft made sure 
> it worked for their own format document feature (ctrl-k,ctrl-d).
> 
> As I mentioned earlier, this is a stepping stone towards adding a feature to 
> "format on save", for which I'd like to offer the ability to choose "current 
> line/selection" and "document". [[ 
> https://github.com/Elders/VSE-FormatDocumentOnSave | Here's an example ]] of 
> an extension that offers this feature, but for VS's built-in formatting.
Note: with "highly disruptive" I meant only if the default behavior without any 
selection is to format the whole file, as that basically kills the workflow; 
that was what Hans suggested.

I'm not opposed to having an extra key binding if enough folks want it and 
there is enough benefit - the argument to not move your cursor is a good point.


https://reviews.llvm.org/D27501



___
cfe-commits mailing list
cfe-commits@lists.llvm.org

[PATCH] D27501: clang-format-vsix: add command to format document

2016-12-09 Thread Antonio Maiorano via Phabricator via cfe-commits
amaiorano added a comment.

(NOTE, I forgot to click Submit on the reply to @klimek a couple days ago!)




Comment at: tools/clang-format-vs/ClangFormat/ClangFormat.vsct:76
+
+  Clang Format Document
+

klimek wrote:
> hans wrote:
> > amaiorano wrote:
> > > hans wrote:
> > > > I think File would be better than Document when referring to source 
> > > > code.
> > > > 
> > > > But it seems a little annoying to need two menu alternatives. Could we 
> > > > make the regular "Clang Format" option just format the whole file if 
> > > > there is currently no selection, or would that be confusing?
> > > The reason I chose "Document" is that it mimics the existing 
> > > functionality in Visual Studio:
> > > {F2661117}
> > > 
> > > As you can see, "Ctrl+K, Ctrl+F" is bound to format selection by default; 
> > > which I assumed is the reason "Ctrl+R, Ctrl+F" was used for Clang Format 
> > > (Selection). So along the same lines, I bound "Ctrl + R, Ctrl + D" to 
> > > Clang Format Document.
> > > 
> > > I don't really have a problem with having multiple menu options, although 
> > > we could have both of them underneath a "Clang Format" top menu, with 
> > > "Format Selection" and "Format Document" as sub menu items.
> > > 
> > > As for annoyance in menus, I think most developers would reach for 
> > > keyboard shortcuts in this case anyway. Furthermore, the next feature I 
> > > want to add, building on top of this one, is allowing the user to enable 
> > > "Format on Save", which would make this even easier to use.
> > I see, I didn't realize they call it documents. And if they also have 
> > separate commands for formating selection and formating the whole file, 
> > maybe that makes sense for us too.
> > 
> > Right, I also imagine folks would use this from the keyboard, but there too 
> > it's annoying to have two shortcuts for the same thing. But again, if 
> > that's how it's generally done in VS...
> > 
> > I'd like to hear from Manuel on this patch too, though.
> Clang-format currently formats the current line / statement if there is no 
> selection, and that's how most people I know use it.
> Formatting the whole file by default is not what clang-format is optimized 
> for, and highly disruptive (eclipse does that, and it's annoying).
> I'm more wondering why we need a setting for this at all - isn't 
> ctrl-a,ctrl-k,ctrl-f equivalent to formatting the whole file?
@klimek My own experience, which also seems to match those of others online, is 
that clang-format is a fantastic tool for making sure your code-base follows a 
strict formatting standard. What many people do is install Git pre-stage hooks, 
for instance, to run clang-format across entire source files, to ensure they're 
formatted properly. So even if it's not optimized for that, it works very well 
:)

Using clang-format to format only the current line doesn't guarantee that 
everything will be formatted accordingly. My own experience is that it 
sometimes formats the line differently than if you format the entire document. 
Or perhaps you end up writing a few lines of code, and formatting current line 
doesn't pick up all the lines you've modified. As for ctrl-a,ctrl-r-ctrl-f, the 
problem with it is that you lose your cursor position, as ctrl-a will place it 
at the end of the file.

You also mention that formatting the entire file is "highly disruptive" like in 
eclipse. I'm not sure how eclipse handles that, but so far VS seems to do a 
pretty good job. It manages to maintain breakpoint positions quite well, it 
manages to keep the cursor in the same place, and it manages to keep the 
relative location of the code where the cursor is at visible on screen. I 
suspect part of the reason it works well in VS is because Microsoft made sure 
it worked for their own format document feature (ctrl-k,ctrl-d).

As I mentioned earlier, this is a stepping stone towards adding a feature to 
"format on save", for which I'd like to offer the ability to choose "current 
line/selection" and "document". [[ 
https://github.com/Elders/VSE-FormatDocumentOnSave | Here's an example ]] of an 
extension that offers this feature, but for VS's built-in formatting.


https://reviews.llvm.org/D27501



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


[PATCH] D27501: clang-format-vsix: add command to format document

2016-12-08 Thread Manuel Klimek via Phabricator via cfe-commits
klimek added inline comments.



Comment at: tools/clang-format-vs/ClangFormat/ClangFormat.vsct:76
+
+  Clang Format Document
+

hans wrote:
> amaiorano wrote:
> > hans wrote:
> > > I think File would be better than Document when referring to source code.
> > > 
> > > But it seems a little annoying to need two menu alternatives. Could we 
> > > make the regular "Clang Format" option just format the whole file if 
> > > there is currently no selection, or would that be confusing?
> > The reason I chose "Document" is that it mimics the existing functionality 
> > in Visual Studio:
> > {F2661117}
> > 
> > As you can see, "Ctrl+K, Ctrl+F" is bound to format selection by default; 
> > which I assumed is the reason "Ctrl+R, Ctrl+F" was used for Clang Format 
> > (Selection). So along the same lines, I bound "Ctrl + R, Ctrl + D" to Clang 
> > Format Document.
> > 
> > I don't really have a problem with having multiple menu options, although 
> > we could have both of them underneath a "Clang Format" top menu, with 
> > "Format Selection" and "Format Document" as sub menu items.
> > 
> > As for annoyance in menus, I think most developers would reach for keyboard 
> > shortcuts in this case anyway. Furthermore, the next feature I want to add, 
> > building on top of this one, is allowing the user to enable "Format on 
> > Save", which would make this even easier to use.
> I see, I didn't realize they call it documents. And if they also have 
> separate commands for formating selection and formating the whole file, maybe 
> that makes sense for us too.
> 
> Right, I also imagine folks would use this from the keyboard, but there too 
> it's annoying to have two shortcuts for the same thing. But again, if that's 
> how it's generally done in VS...
> 
> I'd like to hear from Manuel on this patch too, though.
Clang-format currently formats the current line / statement if there is no 
selection, and that's how most people I know use it.
Formatting the whole file by default is not what clang-format is optimized for, 
and highly disruptive (eclipse does that, and it's annoying).
I'm more wondering why we need a setting for this at all - isn't 
ctrl-a,ctrl-k,ctrl-f equivalent to formatting the whole file?


https://reviews.llvm.org/D27501



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


[PATCH] D27501: clang-format-vsix: add command to format document

2016-12-07 Thread Hans Wennborg via Phabricator via cfe-commits
hans added inline comments.



Comment at: tools/clang-format-vs/ClangFormat/ClangFormat.vsct:76
+
+  Clang Format Document
+

amaiorano wrote:
> hans wrote:
> > I think File would be better than Document when referring to source code.
> > 
> > But it seems a little annoying to need two menu alternatives. Could we make 
> > the regular "Clang Format" option just format the whole file if there is 
> > currently no selection, or would that be confusing?
> The reason I chose "Document" is that it mimics the existing functionality in 
> Visual Studio:
> {F2661117}
> 
> As you can see, "Ctrl+K, Ctrl+F" is bound to format selection by default; 
> which I assumed is the reason "Ctrl+R, Ctrl+F" was used for Clang Format 
> (Selection). So along the same lines, I bound "Ctrl + R, Ctrl + D" to Clang 
> Format Document.
> 
> I don't really have a problem with having multiple menu options, although we 
> could have both of them underneath a "Clang Format" top menu, with "Format 
> Selection" and "Format Document" as sub menu items.
> 
> As for annoyance in menus, I think most developers would reach for keyboard 
> shortcuts in this case anyway. Furthermore, the next feature I want to add, 
> building on top of this one, is allowing the user to enable "Format on Save", 
> which would make this even easier to use.
I see, I didn't realize they call it documents. And if they also have separate 
commands for formating selection and formating the whole file, maybe that makes 
sense for us too.

Right, I also imagine folks would use this from the keyboard, but there too 
it's annoying to have two shortcuts for the same thing. But again, if that's 
how it's generally done in VS...

I'd like to hear from Manuel on this patch too, though.


https://reviews.llvm.org/D27501



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


[PATCH] D27501: clang-format-vsix: add command to format document

2016-12-07 Thread Antonio Maiorano via Phabricator via cfe-commits
amaiorano added inline comments.



Comment at: tools/clang-format-vs/ClangFormat/ClangFormat.vsct:76
+
+  Clang Format Document
+

hans wrote:
> I think File would be better than Document when referring to source code.
> 
> But it seems a little annoying to need two menu alternatives. Could we make 
> the regular "Clang Format" option just format the whole file if there is 
> currently no selection, or would that be confusing?
The reason I chose "Document" is that it mimics the existing functionality in 
Visual Studio:
{F2661117}

As you can see, "Ctrl+K, Ctrl+F" is bound to format selection by default; which 
I assumed is the reason "Ctrl+R, Ctrl+F" was used for Clang Format (Selection). 
So along the same lines, I bound "Ctrl + R, Ctrl + D" to Clang Format Document.

I don't really have a problem with having multiple menu options, although we 
could have both of them underneath a "Clang Format" top menu, with "Format 
Selection" and "Format Document" as sub menu items.

As for annoyance in menus, I think most developers would reach for keyboard 
shortcuts in this case anyway. Furthermore, the next feature I want to add, 
building on top of this one, is allowing the user to enable "Format on Save", 
which would make this even easier to use.


https://reviews.llvm.org/D27501



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


[PATCH] D27501: clang-format-vsix: add command to format document

2016-12-07 Thread Hans Wennborg via Phabricator via cfe-commits
hans added inline comments.



Comment at: tools/clang-format-vs/ClangFormat/ClangFormat.vsct:76
+
+  Clang Format Document
+

I think File would be better than Document when referring to source code.

But it seems a little annoying to need two menu alternatives. Could we make the 
regular "Clang Format" option just format the whole file if there is currently 
no selection, or would that be confusing?


https://reviews.llvm.org/D27501



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


[PATCH] D27501: clang-format-vsix: add command to format document

2016-12-06 Thread Antonio Maiorano via Phabricator via cfe-commits
amaiorano updated this revision to Diff 80533.
amaiorano added a comment.

My first patch accidentally included the changes from 
https://reviews.llvm.org/D27440


https://reviews.llvm.org/D27501

Files:
  tools/clang-format-vs/.gitignore
  tools/clang-format-vs/ClangFormat/ClangFormat.vsct
  tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs
  tools/clang-format-vs/ClangFormat/PkgCmdID.cs
  tools/clang-format-vs/README.txt

Index: tools/clang-format-vs/README.txt
===
--- tools/clang-format-vs/README.txt
+++ tools/clang-format-vs/README.txt
@@ -25,3 +25,27 @@
 ClangFormat/source.extension.vsixmanifest. Once the plug-in has been built with
 CMake once, it can be built manually from the ClangFormat.sln solution in Visual
 Studio.
+
+===
+ Debugging
+===
+
+Once you've built the clang_format_vsix project from LLVM.sln at least once,
+open ClangFormat.sln in Visual Studio, then:
+
+- Make sure the "Debug" target is selected
+- Open the ClangFormat project properties
+- Select the Debug tab
+- Set "Start external program:" to where your devenv.exe is installed. Typically
+  it's "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe"
+- Set "Command line arguments" to: /rootsuffix Exp
+- You can now set breakpoints if you like
+- Press F5 to build and run with debugger
+
+If all goes well, a new instance of Visual Studio will be launched in a special
+mode where it uses the experimental hive instead of the normal configuration hive.
+By default, when you build a VSIX project in Visual Studio, it auto-registers the
+extension in the experimental hive, allowing you to test it. In the new Visual Studio
+instance, open or create a C++ solution, and you should now see the Clang Format
+entries in the Tool menu. You can test it out, and any breakpoints you set will be
+hit where you can debug as usual.
Index: tools/clang-format-vs/ClangFormat/PkgCmdID.cs
===
--- tools/clang-format-vs/ClangFormat/PkgCmdID.cs
+++ tools/clang-format-vs/ClangFormat/PkgCmdID.cs
@@ -2,6 +2,7 @@
 {
 static class PkgCmdIDList
 {
-public const uint cmdidClangFormat = 0x100;
+public const uint cmdidClangFormatSelection = 0x100;
+public const uint cmdidClangFormatDocument = 0x101;
 };
 }
\ No newline at end of file
Index: tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs
===
--- tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs
+++ tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs
@@ -180,41 +180,86 @@
 var commandService = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
 if (commandService != null)
 {
-var menuCommandID = new CommandID(GuidList.guidClangFormatCmdSet, (int)PkgCmdIDList.cmdidClangFormat);
-var menuItem = new MenuCommand(MenuItemCallback, menuCommandID);
-commandService.AddCommand(menuItem);
+{
+var menuCommandID = new CommandID(GuidList.guidClangFormatCmdSet, (int)PkgCmdIDList.cmdidClangFormatSelection);
+var menuItem = new MenuCommand(MenuItemCallback, menuCommandID);
+commandService.AddCommand(menuItem);
+}
+
+{
+var menuCommandID = new CommandID(GuidList.guidClangFormatCmdSet, (int)PkgCmdIDList.cmdidClangFormatDocument);
+var menuItem = new MenuCommand(MenuItemCallback, menuCommandID);
+commandService.AddCommand(menuItem);
+}
 }
 }
 #endregion
 
 private void MenuItemCallback(object sender, EventArgs args)
 {
+var mc = sender as System.ComponentModel.Design.MenuCommand;
+if (mc == null)
+return;
+
+switch (mc.CommandID.ID)
+{
+case (int)PkgCmdIDList.cmdidClangFormatSelection:
+FormatSelection();
+break;
+
+case (int)PkgCmdIDList.cmdidClangFormatDocument:
+FormatDocument();
+break;
+}
+}
+
+/// 
+/// Runs clang-format on the current selection
+/// 
+private void FormatSelection()
+{
 IWpfTextView view = GetCurrentView();
 if (view == null)
 // We're not in a text view.
 return;
 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 

[PATCH] D27501: clang-format-vsix: add command to format document

2016-12-06 Thread Antonio Maiorano via Phabricator via cfe-commits
amaiorano created this revision.
amaiorano added reviewers: hans, zturner, klimek.
amaiorano added a subscriber: cfe-commits.

Bound to Ctrl+R, Ctrl+D by default. Also added section on how to debug the 
extension to the Readme.


https://reviews.llvm.org/D27501

Files:
  tools/clang-format-vs/.gitignore
  tools/clang-format-vs/ClangFormat/ClangFormat.vsct
  tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs
  tools/clang-format-vs/ClangFormat/PkgCmdID.cs
  tools/clang-format-vs/README.txt

Index: tools/clang-format-vs/README.txt
===
--- tools/clang-format-vs/README.txt
+++ tools/clang-format-vs/README.txt
@@ -25,3 +25,27 @@
 ClangFormat/source.extension.vsixmanifest. Once the plug-in has been built with
 CMake once, it can be built manually from the ClangFormat.sln solution in Visual
 Studio.
+
+===
+ Debugging
+===
+
+Once you've built the clang_format_vsix project from LLVM.sln at least once,
+open ClangFormat.sln in Visual Studio, then:
+
+- Make sure the "Debug" target is selected
+- Open the ClangFormat project properties
+- Select the Debug tab
+- Set "Start external program:" to where your devenv.exe is installed. Typically
+  it's "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe"
+- Set "Command line arguments" to: /rootsuffix Exp
+- You can now set breakpoints if you like
+- Press F5 to build and run with debugger
+
+If all goes well, a new instance of Visual Studio will be launched in a special
+mode where it uses the experimental hive instead of the normal configuration hive.
+By default, when you build a VSIX project in Visual Studio, it auto-registers the
+extension in the experimental hive, allowing you to test it. In the new Visual Studio
+instance, open or create a C++ solution, and you should now see the Clang Format
+entries in the Tool menu. You can test it out, and any breakpoints you set will be
+hit where you can debug as usual.
Index: tools/clang-format-vs/ClangFormat/PkgCmdID.cs
===
--- tools/clang-format-vs/ClangFormat/PkgCmdID.cs
+++ tools/clang-format-vs/ClangFormat/PkgCmdID.cs
@@ -2,6 +2,7 @@
 {
 static class PkgCmdIDList
 {
-public const uint cmdidClangFormat = 0x100;
+public const uint cmdidClangFormatSelection = 0x100;
+public const uint cmdidClangFormatDocument = 0x101;
 };
 }
\ No newline at end of file
Index: tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs
===
--- tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs
+++ tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs
@@ -180,41 +180,86 @@
 var commandService = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
 if (commandService != null)
 {
-var menuCommandID = new CommandID(GuidList.guidClangFormatCmdSet, (int)PkgCmdIDList.cmdidClangFormat);
-var menuItem = new MenuCommand(MenuItemCallback, menuCommandID);
-commandService.AddCommand(menuItem);
+{
+var menuCommandID = new CommandID(GuidList.guidClangFormatCmdSet, (int)PkgCmdIDList.cmdidClangFormatSelection);
+var menuItem = new MenuCommand(MenuItemCallback, menuCommandID);
+commandService.AddCommand(menuItem);
+}
+
+{
+var menuCommandID = new CommandID(GuidList.guidClangFormatCmdSet, (int)PkgCmdIDList.cmdidClangFormatDocument);
+var menuItem = new MenuCommand(MenuItemCallback, menuCommandID);
+commandService.AddCommand(menuItem);
+}
 }
 }
 #endregion
 
 private void MenuItemCallback(object sender, EventArgs args)
 {
+var mc = sender as System.ComponentModel.Design.MenuCommand;
+if (mc == null)
+return;
+
+switch (mc.CommandID.ID)
+{
+case (int)PkgCmdIDList.cmdidClangFormatSelection:
+FormatSelection();
+break;
+
+case (int)PkgCmdIDList.cmdidClangFormatDocument:
+FormatDocument();
+break;
+}
+}
+
+/// 
+/// Runs clang-format on the current selection
+/// 
+private void FormatSelection()
+{
 IWpfTextView view = GetCurrentView();
 if (view == null)
 // We're not in a text view.
 return;
 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;
+