Sublime Forum

How to insert text on view with no indentation?

#1

I am inserting this text on the view:

text: Packages list:

  1: A File Icon
Sublime file-specific icons for improved visual grepping
v3.2.1; github.com/ihodev/a-file-icon

  2: Add Folder To Project
Repository for the AddFolderToProject Sublime Plugin
v1.1.1; github.com/DavidGerva/AddFolderToProject-SublimePlugin

  3: Advanced CSV
No description provided
unknown version

  4: AdvancedNewFile
File creation plugin for Sublime Text 2 and Sublime Text 3.
v1.7.0; github.com/skuroda/Sublime-AdvancedNewFile
...

However sublime text keeps inserting (and indenting) it as:

Packages list:

  1: A File Icon
  Sublime file-specific icons for improved visual grepping
  v3.2.1; github.com/ihodev/a-file-icon

    2: Add Folder To Project
    Repository for the AddFolderToProject Sublime Plugin
    v1.1.1; github.com/DavidGerva/AddFolderToProject-SublimePlugin

      3: Advanced CSV
      No description provided
      unknown version

        4: AdvancedNewFile
        File creation plugin for Sublime Text 2 and Sublime Text 3.
        v1.7.0; github.com/skuroda/Sublime-AdvancedNewFile
        ...

This is where sublime keeps messing with everthing:

            new_view.run_command("insert", {"characters": "Packages list:\n\n" + package_string})
            print( "text: " + "Packages list:\n\n" + package_string )

The one which goes to my console is correctly displayed, the one which goes to the view is messed up.
I cannot use view.insert() because I cannot access it on the thread. I tried to pass is as attribute, but did no worked.

Can insert text be fixed on new_view.run_command("insert"?

This is my code:

import textwrap
import threading
import os

import sublime
import sublime_plugin

from .. import text
from ..show_quick_panel import show_quick_panel
from ..package_manager import PackageManager
from .existing_packages_command import ExistingPackagesCommand


print( "Reloading Package Control\package_control\commands\list_packages_command.py" )

class ListPackagesOnViewCommand(sublime_plugin.WindowCommand):

    """
    A command that shows a list of all installed packages in a new view
    """

    def run(self):
        ListPackagesThread(self.window, on_view=True).start()


class ListPackagesThread(threading.Thread, ExistingPackagesCommand):

    """
    A thread to prevent the listing of existing packages from freezing the UI
    """

    def __init__(self, window, filter_function=None, on_view=False):
        """
        :param window:
            An instance of :class:`sublime.Window` that represents the Sublime
            Text window to show the list of installed packages in.

        :param filter_function:
            A callable to filter packages for display. This function gets
            called for each package in the list with a three-element list
            as returned by :meth:`ExistingPackagesCommand.make_package_list`:
              0 - package name
              1 - package description
              2 - [action] installed version; package url
            If the function returns a true value, the package is listed,
            otherwise it is discarded. If `None`, no filtering is performed.
        """

        self.window = window
        self.on_view = on_view
        self.filter_function = filter_function
        self.manager = PackageManager()
        threading.Thread.__init__(self)

    def run(self):
        self.package_list = self.make_package_list()
        if self.filter_function:
            self.package_list = list(filter(self.filter_function, self.package_list))

        def show_no_packages():
            sublime.message_dialog(text.format(
                u'''
                Package Control

                There are no packages to list
                '''
            ))

        def show_panel():
            if not self.package_list:
                show_no_packages()
                return
            show_quick_panel(self.window, self.package_list, self.on_done)

        def show_view():
            if not self.package_list:
                show_no_packages()
                return

            new_view = sublime.active_window().new_file()
            package_string = ""
            package_count = 0

            new_view.set_scratch(True)
            new_view.set_name("Packages List")
            new_view.set_syntax_file("Packages/Text/Plain text.tmLanguage")

            for package in self.package_list:
                package_count += 1;
                text = ""
                text += "%3d: %s\n" % ( package_count, package[0] )
                text += textwrap.fill(package[1], 80)
                text = textwrap.dedent(text)

                # print( "text: " + text )

                package_string += text + "\n" + package[2] + "\n\n"

            new_view.run_command("insert", {"characters": "Packages list:\n\n" + package_string})
            print( "text: " + "Packages list:\n\n" + package_string )

            new_view.sel().clear()
            initial_region = sublime.Region(0,0)
            new_view.sel().add(initial_region)
            new_view.show_at_center(initial_region)

        if self.on_view:
            sublime.set_timeout(show_view, 10)
        else:
            sublime.set_timeout(show_panel, 10)

    def on_done(self, picked):
        """
        Quick panel user selection handler - opens the homepage for any
        selected package in the user's browser

        :param picked:
            An integer of the 0-based package name index from the presented
            list. -1 means the user cancelled.
        """

        if picked == -1:
            return
        package_name = self.package_list[picked][0]

        def open_dir():
            package_dir = self.manager.get_package_dir(package_name)
            package_file = None
            if not os.path.exists(package_dir):
                package_dir = self.manager.settings['installed_packages_path']
                package_file = package_name + '.sublime-package'
                if not os.path.exists(os.path.join(package_dir, package_file)):
                    package_file = None

            open_dir_file = {'dir': package_dir}
            if package_file is not None:
                open_dir_file['file'] = package_file

            self.window.run_command('open_dir', open_dir_file)
        sublime.set_timeout(open_dir, 10)
0 Likes

#2

try using the append command instead of insert

2 Likes

#3

Thanks, it worked.

            new_view.run_command("append", {"characters": "Packages list:\n\n" + package_string})
            print( "text: " + "Packages list:\n\n" + package_string )

I also just found this other thread:

  1. View.insert changes indentation of inserted text

They said to disable the view auto_indent settings, before inserting. It also worked:

            new_view.settings().set('auto_indent', False)
            new_view.run_command("insert", {"characters": "Packages list:\n\n" + package_string})
            print( "text: " + "Packages list:\n\n" + package_string )
            new_view.settings().erase('auto_indent')
1 Like