Sublime Forum

ST3 plugin doesn't work in ST4?

#1

The following Sublime Text 3 plugin overrides the default detect_indentation mechanism by a more straightforward approach: the values of translate_tabs_to_spaces and tab_size are set depending to the file type of exention.

// stupid_indent.py

import os.path
import fnmatch
import sublime
import sublime_plugin

class StupidIndent(sublime_plugin.EventListener):
  def on_load(self, view):
    basename = os.path.basename(view.file_name());
    settings = view.settings()

    for entry in sublime.load_settings('Stupid Indent.sublime-settings').get('configuration'):
      for pattern in entry.get('patterns', []):
        if fnmatch.fnmatch(basename, pattern):
          settings.set('tab_size', entry.get('tab_size', settings.get('tab_size')))
          settings.set('translate_tabs_to_spaces', entry.get('translate_tabs_to_spaces', settings.get('translate_tabs_to_spaces')))
          return
// Stupid Indent.sublime-settings

{
  "configuration":
  [
    {
      "patterns": ["*.html", "*.js", "*.css"],
      "tab_size": 2,
      "translate_tabs_to_spaces": true
    },
    {
      "patterns": ["*.md", "*.markdown"],
      "tab_size": 4,
      "translate_tabs_to_spaces": true
    },
    {
      "patterns": ["Makefile"],
      "tab_size": 4,
      "translate_tabs_to_spaces": false
    }
  ]
}

I put them both in $packages/Stupid Indent, but it doesn’t seem to work. I also tried to move the .sublime-settings file to $packages/User, but this didn’t help.

Why is that? Is there an error on my side or the plugin code should be updated to work in Sublime Text 4?

0 Likes

#2

Most likely because core indentation detection is performed asynchronously and thus starts after your listener and thus overriding its results.

The listener should also set detect_indentation false.

That being said, it appears you are looking for something like EditorConfig

1 Like

#3

The listener should also set detect_indentation false .

I have "detect_indentation": false in the Preferences file, but this doesn’t seem to help.

0 Likes

#5

Shouldn’t on_load check the file has finished loading before doing any work?

1 Like