Sublime Forum

Toggle the minimap only with large files

#1

I want to see the minimap only when a file has more than 100 lines, is possible to achieve this?

0 Likes

#2

This works I think:

import sublime_plugin
import sublime

class MinimapHider(sublime_plugin.ViewEventListener):

    def on_modified(self):
        region = sublime.Region(0, self.view.size())
        count = len(self.view.split_by_newlines(region))
        self.view.window().set_minimap_visible(count > 100)

    on_new = on_activated = on_clone = on_modified

save as MinimapHider.py in your User directory of your Packages folder.

6 Likes

#3

Thanks! It works perfectly

0 Likes

#4

Just as an aside to the above code, a potential enhancement to collecting the number of lines in the buffer is:

count = self.view.rowcol(self.view.size())[0] + 1

Instead of splitting the buffer into lines and then counting the result, this uses the rowcol method on view to convert a point to a row and column offset. Passing in the size of the buffer gives you the row and column of the last character in the buffer.

The first element in the returned tuple is the row, and we add 1 because the row count is 0 based and not 1 based.

3 Likes

#5

I tried your solution and it works fine

0 Likes

#6

Alright, let’s make some final tweaks. Having a ViewEventListener is redundant, because we’re not storing data per view. The on_clone and on_new methods are also redundant because the on_activated method is sufficient to check wether we should display the minimap upon switching or opening views. With @OdatNurd’s suggestion, the import sublime line is also redundant. Final version:

import sublime_plugin

class MinimapHider(sublime_plugin.EventListener):

    def on_modified(self, view):
        count = view.rowcol(view.size())[0] + 1
        view.window().set_minimap_visible(count > 100)

    on_activated = on_modified
3 Likes

#7

Whit this code when I do cmd + shift + p the minimap hides

0 Likes

#8

Huh, it looks like using ViewEventListener keeps it enabled. I guess we are storing state per-view :slight_smile: somehow. For the record:

import sublime_plugin

class MinimapHider(sublime_plugin.ViewEventListener):

    def on_modified(self):
        count = self.view.rowcol(self.view.size())[0] + 1
        self.view.window().set_minimap_visible(count > 100)

    on_activated = on_modified
1 Like

#9

Nice! Now works perfectly ^^

0 Likes

#10

I think this is because View encompasses more than just the edit area for files; text inputs are also views, which includes the console input area and the quick panel/goto anything text entry field as well. Those all have less than 100 lines, so while they have the focus it’s as if a file with a single line is the current one.

Trivially, a change such as the following stops it from happening:

    def on_modified(self, view):
        if not view.settings().get("is_widget", False):
            count = view.rowcol(view.size())[0] + 1
            view.window().set_minimap_visible(count > 100)

However, I think using ViewEventListener is a much cleaner solution. :+1: As mentioned in this forum post, the is_widget setting is potentially a bit flakey:

2 Likes

#11

Can I show minimap always when there is more than one pane open?

0 Likes