Is there a way to teach Android Studio how to display a C++ struct when hovering over the variable?

Viewed 102

As a practical example, say I have a struct that looks like this:

struct MyFoo {
    std::string mValue;
    bool hasValue;
    MyFoo() : hasValue(false) {}
    MyFoo(std::string value) : mValue(value), hasValue(true) {}
};

If I hover over an instance of this struct, Android Studio displays + ☰ {MyFoo}. I can expand this, and with a little drilling down, see what the contents are. (I should note that this is an oversimplified example; my real-world application is a much more complicated struct, and seeing the value is not very easy).

Is there a way to somehow get it to display something like + ☰ {MyFoo} <none> or + ☰ {MyFoo} "Hello, world" instead?

I can see Android Studio doing this with some of the STL types, which suggests that somewhere there's knowledge on how to do this.

Bonus questions: How about XCode?

1 Answers

As far as I know, Android Studio uses LLDB for debugging native code.

You can use LLDB commands to add custom formatting for showing these variables.

See: https://lldb.llvm.org/use/variable.html

The easiest way to do fancy pretty-printing is a Python script inside LLDB (see the LLDB docs link for more info, I based this example off it)

(lldb) type summary add -P MyFoo
Enter your Python command(s). Type 'DONE' to end.
def function (valobj,internal_dict,options):
   mval = valobj.GetChildMemberWithName('mValue')
   hasval = valobj.GetChildMemberWithName('hasValue')
   if hasval.GetValueAsUnsigned(0) == 0:
     return '<none>'
   else:
     return mval.GetSummary()
   DONE

Dealing with std::string is a real can of worms, but you can find some potential solutions on this question.

Related