Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Templates: Macro Expansion to Specified Size #27

Closed
victor-fdez opened this issue Nov 20, 2015 · 8 comments
Closed

Templates: Macro Expansion to Specified Size #27

victor-fdez opened this issue Nov 20, 2015 · 8 comments

Comments

@victor-fdez
Copy link

Hi,

Is there a way to expand a macro to a specific size? Right now macros expand and much all of the characters after it like so:

Suppose NAME is 'Morgan Stanley' then the macro will expand like this ->

/*-------------------------------------------------*/
/*   name:   |NAME|         <-- COOL NAME          */

After expansion

/*-------------------------------------------------*/
/*   name:   Morgan Stanley         <-- COOL NAME          */

I though it would be really nice if a macro expansion could specify a maximum size to which it could expand and fill the non expanded space with spaces. For example above the ending comments don't line up anymore.

So is there something like the following?

Eg.

/*-------------------------------------------------------*/
/*   name:   |NAME------|         <-- COOL NAME          */

After expansion

/*--------------------------------------------------------*/
/*   name:   Morgan Stanl         <-- COOL NAME           */

Thanks,

PS: If there isn't something like that, it would be awesome if someone could give me a hint on where in the mmtemplates I could modify this to do it.

@WolfgangMehner
Copy link
Owner

I agree, this would be nice. And I will try to add this at the weekend. I can probably do it really fast, so there is no need for you to do it yourself.

Would you really cut of the text, or just let it use more space in case the inserted text is longer?

Eg.

/*-------------------------------------------------------*/
/*   name:   |NAME--------------| <-- COOL NAME          */

Would result in (as it should) [Example 1]:

/*--------------------------------------------------------*/
/*   name:   Morgan Stanley       <-- COOL NAME           */

Which is really useful and therefore should be included. But if the name is to long [Example 2],

/*--------------------------------------------------------*/
/*   name:   Really Long Name of a Dude <-- COOL NAME           */

it would actually break the format (on purpose, see below).

Compare C's sprintf. The format specifier would also completely display text, even if it is longer than 20 characters:

sprintf(buf, "%20s some further text", text);

This is a question of design philosophy. In designing all of this, my approach is that editing stuff is easier than adding new stuff. So I would rather break the format and insert the full name, seeing how this would be easy to fix. Secondly, if it breaks the format, that's easy to spot, while an incompletely inserted variable name, date, ... would be much harder to notice at a glance. You would of course still get the benefit of all the cases where it works correctly, which would hopefully be the vast majority of cases.

So I really like your suggestion, I'm just unsure about the "exception handling". What do you think?

@victor-fdez
Copy link
Author

By exceptions do you mean text expanded which would be larger than specified macro?

Were it possible, in those cases, symbols like '|' or '-' could be used after the first '|' in the macro expansion to specify whether the expanded text should be cutoff. If one of those symbols were included then it would cutoff the text else it would just proceed as normal expanding the whole text even if it pushed the rest of the line further.

For example without '-':

/*--------------------------------------------------------*/
/*   name:   |NAME----------|     <-- COOL NAME           */

Would expand to ->

/*--------------------------------------------------------*/
/*   name:   Really Long name of Dude     <-- COOL NAME           */

And with '-':

/*--------------------------------------------------------*/
/*   name:   |-NAME----------|     <-- COOL NAME          */

Would expand to ->

/*--------------------------------------------------------*/
/*   name:   Really Long name      <-- COOL NAME          */

I think one can also cutoff text with C's sprintf by adding '.20' after the '20' to make the string no longer than 20 characters, but also occupy 20 characters if less characters are specified by text.

sprintf(buf, "%20.20s some further text", text);

I don't want to push any work on you, because I don't know how difficult it would be to implement this in your code, or whether this would break syntax or other stuff. But I think this is how I would change this.

Thanks for replying 👍

PS: You are right that date, times and those types would be difficult to debug if they were cutoff

@WolfgangMehner
Copy link
Owner

Is in the works. I'll push tomorrow.

@WolfgangMehner
Copy link
Owner

I've pushed a version of the format specifiers:
1a7cfed

See the documentation:
https://github.com/WolfgangMehner/vim-plugins/blob/master/doc/templatesupport.txt
Look for section

4.1.3 Format Specifiers

@WolfgangMehner
Copy link
Owner

Your template now could look like this:

/*--------------------------------------------------------*/
/*     name: |NAME%----------|     <-- COOL NAME          */
/*  comment: <RCURSOR>                                    */
/*--------------------------------------------------------*/

@victor-fdez
Copy link
Author

Nice, I just read the documentation. I will try your changes this afternoon. The changes on the documentation look awesome 😃

@victor-fdez
Copy link
Author

Thanks! Your new changes are awesome 😄 I will keep on testing them and will let you know of anything I find. But your changes exceeded my expectations. Thank you so much

@WolfgangMehner
Copy link
Owner

Your welcome.

I plan to release the plug-ins with the new template version after Christmas. Until then the behavior may still change. So suggestions (and bug reports) are highly welcome.
That being said, I'm already quiet content with the changes. And they will help me with another extension I planned.

@WolfgangMehner WolfgangMehner changed the title Macro Expansion to Specified Size Templates: Macro Expansion to Specified Size Nov 26, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants