Sublime Forum

show_popup help for displaying the data

#1

Hi,

I need more understanding on the view.show_popup() with the parameter sublime.COOPERATE_WITH_AUTO_COMPLETE. my autocomplete has a tuple. how can i associate the value to be visible in the popup with the autocomplete?

Below is my code

def on_query_completions(self, view, prefix, locations):
completion_set=[]
tooltiptext = None
self.scope_name = sublime.windows()[0].active_view().scope_name(sublime.windows()[0].active_view().sel()[0].begin())
#print("IntelliSense debug: o_q_c scope_name = ", scope_name)
if view.match_selector(locations[0], “source.yaml”):

        currentposition = view.sel()[0].begin()
        currentline = view.full_line(currentposition)
        text = view.substr(currentline)
        
        completion_set = self.get_autocomplete_list(prefix, self.scope_name,text)

view.show_popup(“The funds in the account that one is able to withdraw, transfer or use. It excludes any pending deposits.”, sublime.COOPERATE_WITH_AUTO_COMPLETE)

        return (completion_set,sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS) 

my completion_set has the thrid value in the tuple which i need to associate with the show_popup. can someone please help me with that? Please treat this as urgent as i have the deliverable for today.

Snapshot of the tuple created. The third parameter is tooltip text. it may or may not be available. if not available it would be blank.

[(‘accountActivityToDate : type : Date’, ‘accountActivityToDate: \n\ttype: Date’, ‘’),
(‘accountAdvertisementsId : type : Description’, ‘accountAdvertisementsId: \n\ttype: Description’, ‘’),
(‘accountAvailableBalance : type : Money’, ‘accountAvailableBalance: \n\ttype: Money’, ‘The funds in the account that one is able to withdraw, transfer or use. It excludes any pending deposits.’),
(‘accountAvailableCashAdvanceBalance : type : Money’, ‘accountAvailableCashAdvanceBalance: \n\ttype: Money’, ‘’)]

1 Like

#2

@FichteFoll Could you please be able to help me with this?

0 Likes

#3

I don’t know what is exactly not working in your code but I see you are using show_popup instead of show_popup_menu

This is a simple example with show_popup_menu working:

class SomeCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        items = ['Text', 'Another']
        self.view.show_popup_menu(items, self.on_done)

    def on_done(self, result):
        print(result)
0 Likes

#4

Hi @gepd I am trying to use view.show_popup which was introduced in the 3070 build. I understand it is not in the API reference but this is used for tooltip and not for menu. I need help for the tooltip.

0 Likes

#5
class exampleCommand(sublime_plugin.TextCommand):

    def run(self, edit):
        self.view.show_popup(
            'The Text other line', max_width=100, on_hide=self.done)

    def done(self):
        print("finished")

As I tested, show_popup only accept a text string, so you can’t use a tuple, you should convert the tuple to a string, but it doesn’t have many sense to me, this function isn’t clickeable its only to show information. If it is what you want the example above should help.

0 Likes

#6

Take a look at this like. I have examples of using links and HTML when using the popup functionality. It’s very basic but it should help you.

Show_popup example

0 Likes

#7

Hello @huot25, I am able to get the popup with static content along side the autocomplete. but my content being dynamic, for each value within the autocomplete, I am unable to pass the dynamic value as I am unable to get text on scrolling the autocomplete.

I am trying with key bindings but have not got much success

0 Likes

#8

@huot25 I was looking at your color picker. when would on_navigate be fired?

0 Likes

#9

I haven’t had time to look into this yet, but I take it you want the minihtml popup to update based on what is currently selected/hovered in the completion popup?

0 Likes

#10

yes, is there any github link for the minihtml popup

0 Likes

#11

No, there is no documentation on it.

I’m afraid it is not possible at this moment to find out what the user selected in the completions popup, so you can not update the minihtml popup (or “tooltip popup”, I guess that’s a better name) depending on what the user selected. An addition to the API is required.

0 Likes

#12

As mentioned here show_popup()


How to add html to popup ?

0 Likes

#13

show_popup("<body id=\"plugin-name\">the text</body>")

0 Likes

#14

Can we provide clickable buttons in html ?
Can we show data from list into ordered list in html ?

0 Likes

#15

At the moment, only a subset of html features are supported by show_popup() and phantoms.
Here is the reference : minihtml
You cannot provide <button> tags but <a> tags are supported so you can define the call back function to be executed by your plugin.
Yes, <li>, <ol>, <ul> are supported.

0 Likes

#16

I would be great @UltraInstinct05 if you please provide me some examples of show_popup().
I have written a few function which returns a
dict = {'id':"f1bj3f5j6g77","name": "abcdefgh", ..............}
I want to display these names in ordered/unordered lists. and when I will click on the name I should be able to get this id and pass it to other function

0 Likes

#17

You can create unordered lists with <a href="dest"> links. The hrefs value (e.g. dest) is passed to the on_navigate() callback function. You are free to do anything with it then.

What you need is a generator function which converts your dict into valid html and pass it to the contents argument.

It’s not a very basic example, but the Default/symbol.py does something like that. It contains an EventListener at the bottom of the file which shows the “Goto Definition” popup with an unordered list of all available definitions of a variable.

You can open it via View Package File command from Command Palette.

2 Likes

#19

Are you referring to this ?

class ShowDefinitions(sublime_plugin.EventListener):
    def on_hover(self, view, point, hover_zone):
        if not view.settings().get('show_definitions'):
            return

        if hover_zone != sublime.HOVER_TEXT:
            return

        def score(scopes):
            return view.score_selector(point, scopes)

        # Limit where we show the hover popup
        if score('text.html') and not score('text.html source'):
            is_class = score('meta.attribute-with-value.class')
            is_id = score('meta.attribute-with-value.id')
            if not is_class and not is_id:
                return
        else:
            if not score('source'):
                return
            if score('comment'):
                return
            # Only show definitions in a string if there is interpolated source
            if score('string') and not score('string source'):
                return

        symbol, locations = symbol_at_point(view, point)
        locations = filter_current_symbol(view, point, symbol, locations)
        ref_locations = lookup_references(view.window(), symbol)
        ref_locations = filter_current_symbol(view, point, symbol, ref_locations)
        if not locations and not ref_locations:
            return

        links = []
        for l in locations:
            links.append('<a href="%s">%s</a>' % (
                location_href(l), format_location(l)))
        links = '<br>'.join(links)
        plural = 's' if len(locations) > 1 else ''

        ref_links = []
        for l in ref_locations:
            ref_links.append('<a href="%s">%s</a>' % (
                location_href(l), format_location(l)))
        ref_plural = 's' if len(ref_links) != 1 else ''
        ref_links = '<br>'.join(ref_links)

        def on_navigate(href):
            view.window().open_file(
                href,
                sublime.ENCODED_POSITION | sublime.FORCE_GROUP)

        if len(locations) > 0:
            def_section = """
                <h1>Definition%s:</h1>
                <p>%s</p>
            """ % (plural, links)
        else:
            def_section = ""

        if len(ref_locations) > 0:
            ref_section = """
                <h1>Reference%s:</h1>
                <p>%s</p>
            """ % (ref_plural, ref_links)
            if len(def_section) != 0:
                ref_section = "<br>" + ref_section
        else:
            ref_section = ""

        body = """
            <body id=show-definitions>
                <style>
                    body {
                        font-family: system;
                    }
                    h1 {
                        font-size: 1.1rem;
                        font-weight: bold;
                        margin: 0 0 0.25em 0;
                    }
                    p {
                        font-size: 1.05rem;
                        margin: 0;
                    }
                </style>
                %s
                %s
            </body>
        """ % (def_section, ref_section)

        view.show_popup(
            body,
            flags=sublime.HIDE_ON_MOUSE_MOVE_AWAY,
            location=point,
            on_navigate=on_navigate,
            max_width=1024)
0 Likes

#20

Yes, I do. The event listener handles the on_hover() event. It generates html code with all definitions found for the symbol under the curser and finally creates a popup. It also shows the <a href... creation and usage of the on_navigate() callback.

I know it is not a real minimal example, but one which is available by default implemented with builtin features only.

1 Like