sponsor Vim development Vim logo Vim Book Ad

Message Formatter : An plugin to ease typing of repetitive of code

 script karma  Rating 1/1, Downloaded by 2468  Comments, bugs, improvements  Vim wiki

created by
Salman Halim
 
script type
utility
 
description
This plugin allows the simplification of repetitive code fragments, allowing
much faster entry; for example, this text
        getset  List<String>  names

gets converted to
        public List<String> getNames()
        {
                return m_names;
        }

        public void setNames( List<String> val )
        {
                m_names = val;
        }

More importantly, it figures out when you're defining a boolean variable and
replaces the "get" with "is"; thus,
        getset  boolean  old enough

becomes
        public boolean isOldEnough()
        {
                return m_oldEnough;
        }

        public void setOldEnough( boolean val )
        {
                m_oldEnough = val;
        }

Observe how "old enough" was automatically camel-cased to "oldEnough", and,
when used with "get" or "is", was also capitalized. The same variable can be
used over and over again, each time with (if desired) different formatting
directives; for example, this template pattern
        My name is {john::f_fName} and my full name is {{fName} smith::uq_fullName}.

gets expanded to
        My name is John and my full name is "JOHN SMITH".

Observe, also, how the "fullName" directive recursively contained and expanded
the fName directive.

In the "getset" example, the "get" is defined as:

Addlocaltemplate get public {::type} {eval '{type}' ==? 'boolean' ? 'is' : 'get'::get}{::cf_property}()\n{\nreturn m_{c_property};\n}

The "set" is defined as:

Addlocaltemplate set public void set{::cf_property}( {::type} val )\n{\nm_{c_property} = val;\n}

And the "getset" is defined simply as:

Addlocaltemplate getset {tem get::getTem}\n\n{tem set::setTem}

This allows "get" and "set" to be used independently (for read-only or write-only properties, for example) and doesn't require duplication of code to define a separate "getset".
 
install details
Source the Vimball archive.
 

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
MessageFormatter.vba 9.0 2011-08-21 7.0 Salman Halim Added a new mapping: <Plug>PlaceInlineTemplateForLine; allows for template expansion on the current line even if the template is somewhere in the middle of the line.
MessageFormatter.vba 8.5 2011-08-17 7.0 Salman Halim New directive (<LS>) to go along with <CR>, <SW>. See :help MessageFormatter_LS.

Added a command: ApplySameTemplateToMultipleLines

Added a mapping: <Plug>PlaceTemplatesForRange
MessageFormatter.vba 8.0 2011-06-06 7.3 Salman Halim New special value: "iab" to expand insert-mode abbreviations inline; see :help MessageFormatter_iab.

New mapping: <Plug>InsertModeCompletion (// by default) that will complete templates during insert-mode.
MessageFormatter.vba 7.5 2011-05-12 7.0 Salman Halim New special value: "tem" to include other template definitions inline; see :help MessageFormatter_tem.
MessageFormatter.vba 7.0 2011-05-09 7.3 Salman Halim New option: g:MessageFormatter_moveArgumentsToStart; if 1 (the default), input arguments are moved to the front for easier expansion. See :help
g:MessageFormatter_moveArgumentsToStart for details.

More new options: g:MessageFormatter_highlightDirectives and g:MessageFormatter_highlightDirectivesLink; control the highlighting of directives in text to
make them stand out.

New command: Setcolordirectives: toggles the directive highlighting feature.
MessageFormatter.vba 6.5 2011-05-07 7.3 Salman Halim New modifier:

o: if the value is non-empty, prepends a ", " (a comma and a space); otherwise, leaves it empty
P: Just like 'p', but ignores empty strings
Q: Just like 'q', but ignores empty strings
MessageFormatter.vba 6.0 2011-05-05 7.3 Salman Halim Added an option: g:MessageFormatter_autoAddJumpToEnd; if this is 1 (the default) and if a parameter contains !jump! directives, another !jump! is added to the end to allow the user to quickly continue typing beyond the template. See :help g:MessageFormatter_autoAddJumpToEnd.
MessageFormatter.vba 5.5 2011-05-04 7.3 Salman Halim Fixed bug in default processing.

Improved help and added examples to help (:help MessageFormatter_Examples).
MessageFormatter.vba 5.0 2011-05-03 7.3 Salman Halim Added a default value mechanism: if a template variable is defined like this:

{def John::firstName}

then, during expansion, if an empty value is passed in for firstName, "John" will be used instead. This value can be recursive and may contain other
parameters, as before. (Including other "def" expansions.)

Also, parameters with default values may be left out to have their default value used; see :help MessageFormatter_def for more details.
MessageFormatter.vba 4.5 2011-05-02 7.3 Salman Halim Bug fixes, mostly, though added one more option:

g:MessageFormatter_formatCurrentLineAsFallback (default 1): if attempting to format a template via the <Plug>FormatCurrentTemplate mapping when not actually
in a template, will fall back to formatting just the current line as an ad-hoc template if this is 1. If 0, will give an error message instead.
MessageFormatter.vba 4.0 2011-04-30 7.3 Salman Halim Added a proper help file; no actual functionality changes.
MessageFormatter.vba 4.0 2011-04-29 7.3 Salman Halim Templates can be expanded inline while typing, allowing for macro expansion.
MessageFormatter.zip 3.5 2011-04-20 7.0 Salman Halim Added a command version of FormatContainedMessage called Formatcontainedmessage that passes everything on the command-line as-is to the function and echoes
the result.

Added a new default value type:

If the default value for for a parameter (passed to FormatContainedMessage) is "ask", it defaults to asking the user (via an input). If the value is anything
followed by "ask", it will use that as the default value for the input.
MessageFormatter.zip 3.0 2011-04-19 7.0 Salman Halim Fairly big changes. RELIES UPON MY GetVar.vim (http://vim.sourceforge.net/scripts/script.php?script_id=353) script now.

New formatting parameter:

n: non-displayed value. The return value is suppressed--useful for adding a value to the cache to be used later.

New function:

FormatContainedMessage: Works like MessageFormatter#FormatMessage except that it's always recursive and that its original text string can contain default
values for the parameters (so the second parameter is optional); for example,

Some new commands, also.
MessageFormatter.vim 2.0 2011-04-12 7.0 Salman Halim Added new formatting modifier:

e: Escapes out quotation marks (") and backslashes, leaving the value suitable for placing in quotes. For example, {e_fName} where fName is Jo\nhn results in
Jo\\nhn.

If an expansion parameter starts with "eval ", the rest of the value is evaluated and the return value used as the actual parameter value. If recursion is on,
that value may contain further parameters.

Example:

echo MessageFormatter#FormatMessage('public static final {type} {C_variable} = {value};', {'type':'eval input("Type for {variable}: ", "String")', 'variable':'eval input("Variable name: ")', 'value':'eval input("Value: ", "\"{C_variable}\"")'}, 1)

Bear in mind that 'type' and 'value' both use the parameter 'variable'. If 'variable' were to refer to either of these, you'd have circular recursion. There
is no check in place for that; you'd just end up with a stack overflow.

Note, also, that the expression is evaluated only once. After that, its value is stored on the cache--this allows eval parameters to refer to other eval
parameters (only useful if recursion is on).
MessageFormatter.vim 1.5 2011-04-11 7.0 Salman Halim Added a cache so repeated expansions of the same variable can be looked up rather than computed (potentially much faster, especially when recursion is on).
MessageFormatter.vim 1.0 2011-04-11 7.2 Salman Halim Initial upload
ip used for rating: 3.239.83.89

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Vim at Github