Sublime Forum

[Question][Solved] Pass self variables between classes in Sublime Text plugins

#1

1. Summary

I don’t understand, how I can pass variables between classes, if I write Sublime Text 3 plugins.

2. Example

Example part of my Sublime Text plugin:

class FirstSashaCommand(sublime_plugin.TextCommand):


    def run(self, edit):

        # Get selection text
        select = self.view.sel()
        selection_region = select[0]
        selection_text = self.view.substr(selection_region)
        print(selection_text)

        # Any actions…

        # Replace selection text
        self.view.replace(
            edit, selection_region, any_variable)


class SecondSashaCommand(sublime_plugin.TextCommand):


    def run(self, edit):

        # Get selection text
        select = self.view.sel()
        selection_region = select[0]
        selection_text = self.view.substr(selection_region)
        print(selection_text)

        # Another actions…

	# Replace selection text
        self.view.replace(
            edit, selection_region, another_variable)

# Similar code blocks

This code working.

3. Problem

I have duplicate code. I have repeatly variables selection_region and selection_text in each block with same values.

select = self.view.sel()
selection_region = select[0]
selection_text = self.view.substr(selection_region)
print(selection_text)

How correctly should I act to get rid of duplicate code?

Thanks.

0 Likes

#2

You can put it into a global function:


import sublime_plugin

def duplicated_code(self):
    select = self.view.sel()
    selection_region = select[0]

    selection_text = self.view.substr(selection_region)
    print(selection_text)

    return selection_region


class FirstSashaCommand(sublime_plugin.TextCommand):

    def run(self, edit):

        # Get selection text
        selection_region = duplicated_code(self)

        # Any actions…

        # Replace selection text
        self.view.replace(
            edit, selection_region, any_variable)


class SecondSashaCommand(sublime_plugin.TextCommand):

    def run(self, edit):

        # Get selection text
        selection_region = duplicated_code(self)

        # Another actions…

        # Replace selection text
        self.view.replace(
            edit, selection_region, another_variable)

# Similar code blocks
0 Likes

#3

Also inheritance. So here is a very simple example using your code:

class FirstSashaCommand(sublime_plugin.TextCommand):

    def get_selection(self)
        # Get selection text
        select = self.view.sel()
        selection_region = select[0]
        return self.view.substr(selection_region), selection_region

    def replace_selection(self, edit, selection_region, any_variable):
        self.view.replace(
            edit, selection_region, any_variable)

    def run(self, edit):

        selection_text, selection_region = self.get_selection()
        print(selection_text)

        # Any actions…

        # Replace selection text
        self.replace_selection(edit, selection_region, any_variable)


class SecondSashaCommand(FirstSashaCommand):

    def run(self, edit):

        selection_text, selection_region = self.get_selection()
        print(selection_text)

        # Another actions…

        # Replace selection text
        self.replace_selection(edit, selection_region, any_variable)
3 Likes

#4

Thanks, it works!

class SecondSashaCommand(FirstSashaCommand):polymorphism it turns out, is possible for Sublime Text plugins.

0 Likes