Sublime Forum

Hide/show 'assert' lines in python

#1

I use a fair number of ‘assert’ lines in my python code to help catch bugs, but there are times when the assertions get in the way of overall succinctness and program readability.

I was wondering if there is any way one one might create some kind of toggle to show/hide lines that start with ‘assert’, to have access to the ‘succinct’ version of the code when wanted.

0 Likes

#2

You can use the fold and unfold API methods.

0 Likes

#3

I see that’s a good idea…

Alas, constructing the ‘region’ to fold by aggregating all (non-commented) ‘assert’ lines up to and including the newlines terminating the previous line is above my patience/skill level :joy_cat:

0 Likes

#4

This does everything you need (from the console) and should get you started for writing your own plugin:

view.fold([sublime.Region(reg.a - 1, reg.b) for reg in [view.line(r) for r in view.find_by_selector("keyword.other.assert.python - comment")]])
2 Likes

#5

Thanks! For reference, I got it to work by adding

class HideAssertionsCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        view = self.view
        view.fold([sublime.Region(reg.a - 1, reg.b) for reg in [view.line(r) for r in view.find_by_selector("keyword.other.assert.python - comment")]])


class ShowAssertionsCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        view = self.view
        view.unfold([sublime.Region(reg.a - 1, reg.b) for reg in [view.line(r) for r in view.find_by_selector("keyword.other.assert.python - comment")]])

to

~/Library/Application Support/Sublime Text 3/Packages/User/my_own_stuff.py

and by adding

{ "keys": ["alt+f"], "command": "hide_assertions" },
{ "keys": ["shift+alt+f"], "command": "show_assertions" },

to my user key bindings.

Just one thing: I’d rather have a “toggle” functionality where the same key binding (e.g., alt+f in my case) is used to hide/show the assertions, depending on the state, instead of two different key bindings. Would this be possible?

0 Likes

#6

Check if ALL regions is folded, if not call Hide otherwise call Show:

all_folded = all((view.is_folded(sublime.Region(reg.a - 1, reg.b)) for reg in (view.line(r) for r in view.find_by_selector("keyword.other.assert.python - comment"))))
1 Like

#7

(Could you please unwrap these lines? They are horrible to read and the only reason I made it so long was to fit it in one statement for the console.)

0 Likes

#8

Wow! It works! :tada:

But how did you know is_folded was in the API? It doesn’t show up in the API link provided above. :ambulance:

Anyway, for reference again, here is my final ‘toggle’ function:

class ToggleAssertionsCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        view = self.view
        all_folded = all((view.is_folded(sublime.Region(reg.a - 1, reg.b)) for reg in (view.line(r) for r in view.find_by_selector("keyword.other.assert.python - comment"))))
        if all_folded:
            view.unfold([sublime.Region(reg.a - 1, reg.b) for reg in [view.line(r) for r in view.find_by_selector("keyword.other.assert.python - comment")]])
        else:
            view.fold([sublime.Region(reg.a - 1, reg.b) for reg in [view.line(r) for r in view.find_by_selector("keyword.other.assert.python - comment")]])

(Sorry for the long lines, hopefully it’s still copy-pasteable.)

0 Likes

#9

But how did you know is_folded was in the API?

This might help.

2 Likes

#10

Also, I did some more reformatting/refactoring. Have not tested this.

class ToggleAssertionsCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        view = self.view

        comment_lines = [
            sublime.Region(region.begin() - 1, region.end())
            for region in [
                view.line(r)
                for r in view.find_by_selector("keyword.other.assert.python - comment")
            ]
        ];

        if all(view.is_folded(r) for r in comment_lines):
            view.unfold(comment_lines)
        else:
            view.fold(comment_lines)
1 Like

#11

Use the wonderful Python’s introspection mechanisms.
To see available methods of an object in the ST3 console:

>>> dir(view)
['__bool__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'add_phantom', 'add_regions', 'assign_syntax', 'begin_edit', 'buffer_id', 'change_count', 'classify', 'close', 'command_history', 'em_width', 'encoding', 'end_edit', 'erase', 'erase_phantom_by_id', 'erase_phantoms', 'erase_regions', 'erase_status', 'expand_by_class', 'extract_completions', 'extract_scope', 'extract_tokens_with_scopes', 'file_name', 'find', 'find_all', 'find_all_results', 'find_all_results_with_text', 'find_by_class', 'find_by_selector', 'fold', 'folded_regions', 'full_line', 'get_regions', 'get_status', 'get_symbols', 'has_non_empty_selection_region', 'hide_popup', 'id', 'indentation_level', 'indented_region', 'indexed_references', 'indexed_symbols', 'insert', 'is_auto_complete_visible', 'is_dirty', 'is_folded', 'is_in_edit', 'is_loading', 'is_popup_visible', 'is_primary', 'is_read_only', 'is_scratch', 'is_valid', 'layout_extent', 'layout_to_text', 'layout_to_window', 'line', 'line_endings', 'line_height', 'lines', 'match_selector', 'meta_info', 'name', 'overwrite_status', 'query_phantom', 'query_phantoms', 'replace', 'retarget', 'rowcol', 'run_command', 'scope_name', 'score_selector', 'sel', 'selection', 'set_encoding', 'set_line_endings', 'set_name', 'set_overwrite_status', 'set_read_only', 'set_scratch', 'set_status', 'set_syntax_file', 'set_viewport_position', 'settings', 'settings_object', 'show', 'show_at_center', 'show_popup', 'show_popup_menu', 'size', 'split_by_newlines', 'style', 'style_for_scope', 'substr', 'symbols', 'text_point', 'text_to_layout', 'text_to_window', 'unfold', 'update_popup', 'view_id', 'viewport_extent', 'viewport_position', 'visible_region', 'window', 'window_to_layout', 'window_to_text', 'word']
1 Like