Sublime Forum

Use subl.exe and plugins for git smudge, clean filters

#1

TL;DR:

  1. How do you pipe STDIN to subl.exe?
  2. How do you pipe STDOUT from subl?
  3. How do you process STDIN with a plugin?

Detail

I have written sublimeText3 plugins which process a file.

class SmudgeviewCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        # ExampleModify
        self.view.replace(edit, self.view.line(0), "Smudge")

class CleanviewCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        # ExampleModify
        self.view.replace(edit, self.view.line(0), "Clean")

I would like to use them as smudge and clean filters for git on Windows 7 x64.

I had trouble piping the content so use this bash script as an interface:

#!/bin/bash

TMPDIR=${TMPDIR:-/tmp}  # default to /tmp if TMPDIR isn't set
F=$(mktemp $TMPDIR/sublclean-XXXXXXXX.inc)
cat >| $F  # use >| instead of > if you set noclobber in bash
"C:/PORTABLE/Sublime Text Build 3114 x64/subl.exe" $F --wait --background --command smudgeview
sleep .3  # give subl a little time to open the file
cat $F
rm -f $F  # file will be deleted as soon as subl closes it

I was unable to get the plugin to run, so added this:

# Have to wait for the file to load
callback_clean_on_load = {}
callback_smudge_on_load = {}
class CleanCommand(sublime_plugin.WindowCommand):
    def run(self):
        print("Clean")
        global callback_clean_on_load
        view = self.window.active_view()
        callback_clean_on_load[view.file_name()] = lambda: self.clean(view)
    def clean(self, view):
        print("Clean Callback clean")
        view.run_command("cleanview")
        print("Clean Callback save")
        view.run_command("save")
        print("Clean Callback close")
        view.close()

class SmudgeCommand(sublime_plugin.WindowCommand):
    def run(self):
        print("Smudge")
        global callback_smudge_on_load
        view = self.window.active_view()
        callback_smudge_on_load[view.file_name()] = lambda: self.smudge(view)
    def smudge(self, view):
        print("Smudge Callback smudge")
        view.run_command("smudgeview")
        print("Smudge Callback save")
        view.run_command("save")
        print("Smudge Callback close")
        view.close()

class EventListener(sublime_plugin.EventListener):
    # Called when a file is finished loading.
    def on_load(self, view):
        global callback_clean_on_load
        if view.file_name() in callback_clean_on_load:
            callback_clean_on_load[view.file_name()]()
            del callback_clean_on_load[view.file_name()]

        global callback_smudge_on_load
        if view.file_name() in callback_smudge_on_load:
            callback_smudge_on_load[view.file_name()]()
            del callback_smudge_on_load[view.file_name()]

and changed the bash scripts to call smudge and clean instead of smudgeview and cleanview.

They seem to run and get stuck in a loop.

Any idea how to use plugins for git smudge clean filters?

0 Likes