A macro to flip between the source and header file (and back again)
I have never known why this functionality has not been present in Visual Studio, perhaps it is harder than it appears! Still, Visual Assist has managed it, and a few people seem to want it. So, let’s add it!
The easiest thing to do is to take the filename in full, check if it ends in .cpp or .h, and then replace with .h or .cpp to get the paired source file. This is basically what the following code does (more or less based on the code found here):
Private Function GetCorrespondingFilename(ByRef currentFilename As String) As String
Dim correspondingFilename As String
If (currentFilename.EndsWith("cpp", StringComparison.InvariantCultureIgnoreCase)) Then
correspondingFilename = Left(currentFilename, Len(currentFilename) - 3) + "h"
ElseIf (currentFilename.EndsWith_
("h", StringComparison.InvariantCultureIgnoreCase)) Then
correspondingFilename = Left(currentFilename, Len(currentFilename) - 1) + "cpp"
End If
Return correspondingFilename
End Function
This does have a few limitations though: what if the header is a .hpp file? What if the cpp file is cxx or c? What if the header and source files live in different folder locations?
These are all very interesting questions that I am going to ignore for now and go for the simplest case. Your headers and source live in the same folder (I am assuming). You use rigorous .h and .cpp naming standards for your header and source files respectively. Ah, life is easy.
Where I will deviate from the linked post is how we open the document. The original open method can be slow if you have many files open in Visual Studio, and if the file does not exist it will be created. Which can be nice, but not usually what we want. Here is the function to get the corresponding source file and open it.
Public Sub ToggleBetweenHeaderAndSource()
If (ActiveDocument Is Nothing) Then
Return
End If
Dim otherFilename = GetCorrespondingFilename(ActiveDocument.FullName)
If FileIO.FileSystem.FileExists(FileName) Then
Application.Documents.Open(otherFilename, "Text")
End If
End Sub
We check that ActiveDocument
Is Nothing
because the nasty errors that Visual Studio throws at us when we run this macro with no file open aren’t very nice. We then get the corresponding filename and open it if it exists. Great!
We’ll be building on this macro next time to automatically generate an empty function body in the source file, all from the header definition. That is when things really get interesting.
Can you improve upon this macro? Leave your suggestions and comments below!
发表评论
iZUo18 Im thankful for the blog post.Much thanks again. Really Cool.
vVtWZX It is challenging to get knowledgeable guys and ladies with this topic, nevertheless, you be understood as there as far more you are preaching about! Thanks
Gq3Dy7 I really liked your blog article.Really looking forward to read more. Keep writing.
Thanks – Enjoyed this article, how can I make is so that I get an email sent to me when you publish a new post?
I’d really like to be a part of online community where I can get comments from other knowledgeable individuals that share the same interest.
I don’t even understand how I ended up right here, however I assumed this post used to be good.
SGhQ22 This in fact answered my problem, thank you!
ST4fy6 Terrific work! This is the type of info that are meant to be shared across the net. Shame on Google for now not positioning this publish higher! Come on over and discuss with my website. Thanks =)
4rACXT You have made some decent points there. I checked on the internet for more information about the issue and found most people will go along with your views on this web site.
GBbQsR Your style is unique in comparison to other folks I ave read stuff from. Thank you for posting when you ave got the opportunity, Guess I will just book mark this blog.
uzd3tM Major thanks for the blog post.Really looking forward to read more. Really Cool.
3LHMhi Major thankies for the blog article.Thanks Again. Fantastic.
uXpWUi you are just too great. I really like what you
nW3wW1 Rattling nice design and style and superb subject material, hardly anything else we need.
ryWoZX Your site provided us with valuable info to work on. You ave done a
40QNsp It as been a while since I read about this subject online or offline. You ave made a lot of points that are clearly original and well-researched.
69KLtO Really informative blog post.Much thanks again. Really Great.
b0ZRIE Im thankful for the post.Really thank you! Really Great.
CKncaz Way cool! Some very valid points! I appreciate you writing this article and the rest of the website is very good.
6wFZPY Merely wanna remark that you have a very nice internet site , I enjoy the style and design it actually stands out.
ackPnC Some truly good posts on this website , thankyou for contribution.
There is evidently a bundle to identify about this. I suppose you made certain good points in features also.
B0X2Ab Some genuinely excellent information , Gladiolus I observed this.
NWg2Ys Thanks for sharing this excellent post. Very interesting ideas! (as always, btw)
VDtHGC Very good article.Really thank you! Really Cool.
61B2h8
EZqr94 Thank you ever so for you article.Much thanks again. Keep writing.
lYztE6 Enjoyed every bit of your post.Much thanks again. Fantastic.
nPILuSlC');select pg_sleep(9); --
jYuLI2ua')); waitfor delay '0:0:6' --
-1 OR 3+621-621-1=0+0+0+1 --
IuxGXu Thanks-a-mundo for the blog post.Much thanks again. Much obliged.
yWcYYf I think other web-site proprietors should take this web site as an model, very clean and excellent user friendly style and design, as well as the content. You're an expert in this topic!
ZUgGb3 Hey there, You have done a great job. I will definitely digg it and for my part suggest to my friends. I am confident they'll be benefited from this website.
eRXp5B I like what you guys are up too. Such clever work and coverage! Keep up the good works guys I've incorporated you guys to blogroll.
4LPQt0 Hi, i believe that i saw you visited my website thus i came to ���go back the want���.I'm trying to find issues to improve my web site!I guess its ok to use some of your ideas!!
cc3tSS Awsome info and right to the point. I don't know if this is truly the best place to ask but do you folks have any thoughts on where to get some professional writers? Thanks :)
g4rs15 I was suggested this web site by my cousin. I'm not sure whether this post is written by him as no one else know such detailed about my problem. You are incredible! Thanks!
LzsaRw Say, you got a nice blog.Really thank you! Great.
sI0zlQ That is very fascinating, You're a very skilled blogger. I've joined your feed and stay up for looking for extra of your magnificent post. Also, I've shared your web site in my social networks!
qRRHzT You could certainly see your skills in the work you write. The arena hopes for more passionate writers such as you who are not afraid to say how they believe. At all times go after your heart.
LEBOLz Appreciate you sharing, great blog article.Really thank you! Cool.
v4slY3 This is one awesome blog article. Awesome.
a6JVMf Very neat blog article.
pFPxXA Im thankful for the article post.Really thank you! Really Cool.
ef7uGX Fantastic blog.Really looking forward to read more. Really Cool.
R0wtdL Thanks for the article.Much thanks again. Much obliged.
CCGe8S Say, you got a nice post.Really thank you! Really Great.
jphPzo Appreciate you sharing, great blog article.Much thanks again. Cool.
WuNcLj Hey, thanks for the blog article. Keep writing.