Click here to Skip to main content
Click here to Skip to main content

Template Completion in the VC++ editor

By , 17 Feb 2003
 

Introduction

The TemplateCompletion macro provides a way to expand short letter sequences (templates) to larger text blocks while you are typing text. My aim is to make this process (template completion) as simple and fast as possible.

The idea is to link simple keystroke (backslash for example) with the macro which reads the word to left of insertion point and replaces it with corresponding string. If macro does not know the word it inserts character pressed by user (backslash). This allows user (you) to type character in text.

Installation and testing

  • Unzip TemplateCompletion.zip file into "F:\Program Files\Microsoft Visual Studio\Common\MSDev98\Macros" folder (use the appropriate folder name according to the configuration of your computer).

  • In Visual Studio open "Tools|Customize" dialog.

  • Select "Add-ins and Macro Files" tab, turn on "TemplateCompletion" item in list.

  • Select "Keyboard" tab, set "Category" to "Macros" and "Editor" to "Text", link CompleteTemplateBackSlash command with "\" keystroke (backslash), link ToggleTemplateCompletion command with "Ctrl+\" keystroke.

  • Close "Customize" dialog.

  • Open any C or C++ source file and type in foi\ sequence, it will be expanded to
    for (i = 0; i < ; ++i) {
    }
  • Type in "Hello world\n" sequence. No expansion, backslash is inserted in text.

Configuration

You can temporarily toggle on/off template completion. The ToggleTemplateCompletion command provides this service.

You can change keystroke which invokes completion. The possible choices are "\" (backslash), "`" (back quote), "|" (bar or "Shift+\"), " " (space). My first choice was space, but then I changed my mind and now use backslash. You can also add your own CompleteTemplate* subroutine to the file TemplateCompletion.dsm to support any keystroke you want.

You can change the list of the templates. The templates are added by calls to AddTemplate subroutine. Take a look to "Module initialization" section. You can freely change set of templates. But keep the templates distinct. If two or more templates are the same, the first template will be used in expansion. The AddTemplate subroutine does not warn about duplicated templates.

And you can freely change the code of macro itself.

Additionally I want to bring your attention to "escape sequences" in template expansion. You have some control over text layout and caret position. The examples are in TemplateCompletion.dsm file.

Issues

When you undo template completion in VC editor, you need to execute several "undo" commands to remove expanded text (this is also true for "redo" command). I hope this is minor inconvenience. This behaviour also takes place when macro does not do template completion at all (when you type in, for example, backslash in text). If you try to undo your text typing, the first "undo" command deletes text after backslash, second deletes backslash itself and third deletes text before backslash.

In some configurations VC editor "paste" command breaks after installation of this macro. In that case go to http://support.microsoft.com/directory/article.asp?ID=kb;en-us;Q195009 page and apply its recommendations to msdev.exe. This helped me to make "paste" command to work. I hope this will help you also.

Contacts

Feel free to e-mail me with any comments, suggestions etc.

History

26 June 2002 - updated download

18 Feb 2003 - updated download

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Stanislav Volodarskiy
Web Developer
Russian Federation Russian Federation
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionTemplate Completion in VS .net 2003?memberJoerg Reuter11 Apr '05 - 0:43 
Hi,
can anyone give me a hint on how to use this macro in Visual Studio .net 2003? I can't get it to work, it seems this version does not support *.dsm files :-/
Alternatively, is there a similar macro/add-in for VS 2003? I really like the concept this one uses ...
 
Thanks,
Joerg
AnswerRe: Template Completion in VS .net 2003?sussAnonymous12 Apr '05 - 4:13 
Hello!
Unfortunately, it is impossible to port this macro to VS.net directly. I have idea to port macro when I will change my old VC 6.0 to VC.net. As an alternative, try Visual Assistant.
 
Sincerely, Stanislav Volodarskiy
GeneralPossible extensionsmemberRichard Grenfell15 May '03 - 15:04 
This is very slick, and a lack of this ability has been a major thorn towards my not using VC's editor as my primary. However, I am heavily trained towards using the space bar for my character expansion and find it hard to switch. As such I have always used Ctrl+Space to allow me to force just a space character into place. I didn't see an easy way to assign the space character to the Ctrl+Space keystroke, so I wrote an extension macro to this set, and thought others might find it useful who work the same way I do. (Yes, it's trivial, but maybe I'll save someone 5 minutes.)
 

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' InsertSpace()
'
' implements template completion
' NOTE: subroutine depends on and affects active document of DevStudio,
' link it with any keystroke to invoke template completion
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
sub InsertSpace()
' DESCRIPTION: Link it with any shortcut to invoked insertion of a space.
ActiveDocument.Selection = " "
end sub

 
I also wanted auto-brace expansion without having to hit the spacebar first. This is one I know some folks like. The template completion code here also makes this trivial.
 

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' CompleteAutoBrace()
'
' Implements auto-brace template expansion.
' NOTE: subroutine depends on and affects active document of DevStudio,
' link it with any keystroke to invoke template completion
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
sub CompleteAutoBrace()
' DESCRIPTION: Link it with any shortcut to invoke insertion of a brace set.
ActiveDocument.Selection = "{"
CompleteTemplateOrInsertChar ActiveDocument, ""
end sub

 
One of the nice things about the later, is if you use the toggle completion command, this turns off as well.
 
Richard Grenfell
GeneralRe: Possible extensionsmemberStanislav Volodarskiy15 May '03 - 19:47 
Thank you, Richard.
 
I'll include these macroses to the next release. The second macro is surprise for me. I did not think about expansion without any special keystroke. Good idea for any kind of braces.
 
Stanislav Volodarskiy

GeneralGood, work too with AZERTY keyboardmemberJean-Michel LE FOL19 Jun '02 - 21:07 
I associated the macro with AltGr+8 which is the keys for backslash.
GeneralGREAT - but a minor problem...memberMichael Hansen19 Jun '02 - 20:51 
This is THE one feature I have been missing the most in the Visual Studio editor, so thank you very much!   Smile | :)
 
I have one problem though: template expansion do not take place when the position is at the start of a line, except for the very first line a document.
 
The problem seems to lie in the SelectWord subroutine.   Here's a revised SelectWord which expands at the beginning of lines also:
 
private sub SelectWord(doc)
     dim col   ' current column
 
     doc.Selection.WordLeft dsExtend
     col = doc.Selection.CurrentColumn
     doc.Selection.CharLeft dsExtend
 
     if doc.Selection.CurrentColumn < col then
          if left(doc.Selection, 1) <> "#" then
               doc.Selection.CharRight dsExtend
          end if
     end if
 
            ' Special handlling for word selection at start of lines:
     if (doc.Selection.CurrentColumn = 1) and (col = 1) then
          if asc(left(doc.Selection, 1)) = 13 then
               doc.Selection.CharRight dsExtend
          end if
     end if
 
end sub
 
Since I am not much of a VS-Macro programmer, there may very well be a simpler way to achieve this... if there is, please let me know.
 
Cheers
Michael.

 
MBH

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 18 Feb 2003
Article Copyright 2002 by Stanislav Volodarskiy
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid