Author Topic: Auto select correct member access operator? (C/C++)  (Read 5229 times)

Pete_M

  • Community Member
  • Posts: 9
  • Hero Points: 0
Auto select correct member access operator? (C/C++)
« on: January 17, 2014, 01:06:31 PM »
Is it possible for SlickEdit to insert the correct member access operator as I type a '.' and if so, can some kind soul point me at the right option? Thanks!


Graeme

  • Senior Community Member
  • Posts: 2821
  • Hero Points: 347
Re: Auto select correct member access operator? (C/C++)
« Reply #1 on: January 18, 2014, 12:35:11 AM »
You might be able to write a macro to do it.  What is your definition of member access operator?

Pete_M

  • Community Member
  • Posts: 9
  • Hero Points: 0
Re: Auto select correct member access operator? (C/C++)
« Reply #2 on: January 18, 2014, 11:21:28 AM »
Either "." or "->" depending on the symbol definition. E.g. given struct foo { int bar; } f, *b; then typing…
"f." would insert "f." i.e. no change but…
"b." would change the '.' for "->" & result in "b->" being inserted.
Visual assist has this feature (have to use it at work) so I've become very used to it (read semi-dependent!) ;)
Thanks!


Graeme

  • Senior Community Member
  • Posts: 2821
  • Hero Points: 347
Re: Auto select correct member access operator? (C/C++)
« Reply #3 on: January 18, 2014, 11:58:03 AM »
Here's a macro (attached) that's not exactly what you asked for  - xlist_symbols.  It borrows some code from the list_symbols (bound to alt+dot for me) function to work out which operator to use but in doing so, it's forced to call AutoCompleteUpdateInfo() which has the effect of showing a list of member symbols as if you typed alt+dot.  Also, if neither . or -> are valid, it tries .* and ->* too.

The code does not insert a "." character unless it thinks it's inserting a member access operator  so you can't bind the macro to the "." key but you can bind it to alt+dot  - or whatever key you have bound to list_symbols.

If you really want to bind the macro to "." you would have to change it slightly so that if you type 123.  - it actually inserts a decimal point - or if you're in a comment or string.

If you want to try it, open xlistsym.e in the editor and load it using the load-module command in the macro menu.
« Last Edit: January 18, 2014, 02:28:33 PM by Graeme »

Pete_M

  • Community Member
  • Posts: 9
  • Hero Points: 0
Re: Auto select correct member access operator? (C/C++)
« Reply #4 on: January 18, 2014, 12:26:28 PM »
Thanks Graeme, I had a quick go but it doesn't seem to work for me. I loaded the module & I'm testing it on my above example. I type 'b' then call your xlist-symbols macro from the command line immediate after but it doesn't seem to recognise the symbol as being a pointer & instead lists local symbols followed by all symbols outside the scope of foo. Am I calling the macro correctly? I'll have a poke around your code when I have more time (I've have no experience of SlickEdit macros) but it seems like it could be doable - so thanks again for the pointer.

Graeme

  • Senior Community Member
  • Posts: 2821
  • Hero Points: 347
Re: Auto select correct member access operator? (C/C++)
« Reply #5 on: January 18, 2014, 12:32:37 PM »
Does it work for you on the following code?  If I put the cursor after the vp and call xlist_symbols, it inserts an arrow and shows a list.

Code: [Select]
struct xy1 {
   int v1;
};

int main (int argc, char *argv[])
{
   xy1 * vp;
   xy1 v2;
   vp
}

Pete_M

  • Community Member
  • Posts: 9
  • Hero Points: 0
Re: Auto select correct member access operator? (C/C++)
« Reply #6 on: January 18, 2014, 12:53:37 PM »
Ah, yes, but bring the struct into main & I get "..*" inserted.
I was trying it on this before (run macro after b)...

Code: [Select]
int main (int argc, char *argv[])
{
struct foo { int bar; } f, *b;
b
}

I guess the parser doesn't like that particular declaration style?

Graeme

  • Senior Community Member
  • Posts: 2821
  • Hero Points: 347
Re: Auto select correct member access operator? (C/C++)
« Reply #7 on: January 18, 2014, 02:28:05 PM »
ok, I've made a few changes.  See if it works now if you feel like it :)

_delete_text didn't do what I thought and the code didn't handle hard tabs correctly.  I've taken out .* and ->* as they seemed not to work correctly.

Pete_M

  • Community Member
  • Posts: 9
  • Hero Points: 0
Re: Auto select correct member access operator? (C/C++)
« Reply #8 on: January 18, 2014, 02:45:13 PM »
Hey, nice one Graeme, it seems to be working pretty well now. I had some null handle errors (iirc) but I can't seem to replicate them now, maybe some cache state got flushed. I'll test it more thoroughly over the next day or so & let you know. Thanks very much!  :D