Add auto-complete support for macros of structures

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Add auto-complete support for macros of structures

Post by Mistrel »

I cannot use auto-completion with macros of structures. I'm transitioning to using macros more widely in my projects and this is creating a problem.

Code: Select all

Structure SomeStruct
  Field.i
EndStructure

Macro MACRO_STRUCT
  SomeStruct
EndMacro

Define a.SomeStruct
Define b.MACRO_STRUCT

;/ Auto-completes OK
a\Field

;/ Does not auto-complete
b\

Procedure SomeProc(a.SomeStruct, b.MACRO_STRUCT)
  ;/ Auto-completes OK
  a\Field
  
  ;/ Does not auto-complete
  b\
EndProcedure
#NULL
Addict
Addict
Posts: 1497
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Add auto-complete support for macros of structures

Post by #NULL »

you might know, but freak explained here why this is not working currently: http://www.purebasic.fr/blog/?p=417
but you could trick the IDE if you really need to.

Code: Select all

Structure SomeStruct
  Field.i
EndStructure

CompilerIf 0
  Structure MACRO_STRUCT
    Field.i
  EndStructure
CompilerEndIf

Macro MACRO_STRUCT
  SomeStruct
EndMacro

Define a.SomeStruct
Define b.MACRO_STRUCT

;/ Auto-completes OK
a\Field = 123

;/ Does not auto-complete
b\Field = 234

Procedure SomeProc(*a.SomeStruct, *b.MACRO_STRUCT)
  ;/ Auto-completes OK
  *a\Field = 456
 
  ;/ Does not auto-complete
  *b\Field = 678
EndProcedure
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Add auto-complete support for macros of structures

Post by Mistrel »

Thanks for the work-around.

This adds a LOT of duplicate definitions and bloat though.
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Add auto-complete support for macros of structures

Post by freak »

I don't really understand the specific use case here. Why do you need a macro in this case?

If you just want to define an alias for the structure, just use the "Extends" keyword:

Code: Select all

Structure SomeStruct
  Field.i
EndStructure

Structure MACRO_STRUCT Extends SomeStruct: EndStructure

Define a.SomeStruct
Define b.MACRO_STRUCT

;/ Auto-completes OK
a\Field

;/ Does not auto-complete
b\

Procedure SomeProc(a.SomeStruct, b.MACRO_STRUCT)
  ;/ Auto-completes OK
  a\Field
 
  ;/ Does not auto-complete
  b\
EndProcedure
The IDE can resolve this properly. No macro required.
quidquid Latine dictum sit altum videtur
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Add auto-complete support for macros of structures

Post by Mistrel »

I use macros like this by convention in other languages like C and C++ and it's convenient to keep this consistent when updating code back and forth. It's also convenient to see what macros are available for various types in the Procedures tab in the IDE.

Would it be possible to support my particular use case? :(
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Add auto-complete support for macros of structures

Post by Mistrel »

I looked at using the a structure with the Extends keyword but would still rather use macros even with the added code bloat. The added use of structures causes a mess in the debugger by creating variations of the same structure when viewing variables where they should be the same type. It also screws with my convention in the debugger as well.

For example, I use all-caps only for macros so seeing it as a structure is hiding the actual structure it's supposed to be using which makes things harder to read. I have to go back and figure out what is being extended.

I hope this makes sense.
#NULL
Addict
Addict
Posts: 1497
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Add auto-complete support for macros of structures

Post by #NULL »

probably not useful in your case, but funnily autocompletion as well as debugger tooltips are working if a macro is used in the structure definition.

Code: Select all

Macro MACRO_STRUCT
  SomeStruct
EndMacro

Structure MACRO_STRUCT
  Field.i
EndStructure

Define a.SomeStruct
Define b.MACRO_STRUCT

a\Field = 123
b\Field = 234

Procedure SomeProc(*a.SomeStruct, *b.MACRO_STRUCT)
  *a\Field = 456
  *b\Field = 678
  ProcedureReturn 
EndProcedure

SomeProc(a, b)
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Add auto-complete support for macros of structures

Post by Mistrel »

That is horrible. But I can see that it does indeed work and may be the least painful way of implementing it with regards to code bloat and not having to maintain duplicate structures.

Now that I've seen it I don't know if I can ever un-see it..
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Add auto-complete support for macros of structures

Post by freak »

You can also combine #NULLs first suggestion with mine to get this:

Code: Select all

Structure SomeStruct
  Field.i
EndStructure

; This is just for IDE support
CompilerIf 0
  Structure MACRO_STRUCT Extends SomeStruct
  EndStructure
CompilerEndIf

Macro MACRO_STRUCT
  SomeStruct
EndMacro

; ...
This way you at least don't have to keep multiple structure definitions in sync.

The thing is that macro expansion simply does not fit into the way the whole AutoComplete feature currently works (see the blog post mentioned above). I see no way to add it so that it really works in all circumstance without doing pretty much an actual compilation of the code. A feature that works only in simple circumstances will look like a buggy feature and cause more frustration than help. So I don't think macro expansion will be possible in AutoComplete any time soon.
quidquid Latine dictum sit altum videtur
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Add auto-complete support for macros of structures

Post by Mistrel »

That's probably the best compromise as it keeps the layout I want with structs and macros.

Thanks for the tips.
Post Reply