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.