Last active
August 31, 2024 13:30
-
-
Save pierresh/f5822e7e038b4ac1d79c8e185dee23c5 to your computer and use it in GitHub Desktop.
SublimeText: replace "this" with "$this->" when typing in PHP files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sublime | |
import sublime_plugin | |
class PhpThisToThisArrowCommand(sublime_plugin.TextCommand): | |
def run(self, edit): | |
# Get the current cursor position | |
for region in self.view.sel(): | |
if region.empty(): | |
# Get the position of the cursor | |
pos = region.begin() | |
# Ensure we do not attempt to create a negative region | |
if pos < 5: | |
continue | |
# Check if "this." is before the cursor | |
word_region = sublime.Region(pos - 5, pos) | |
word = self.view.substr(word_region) | |
if word == "\tthis": | |
# Replace "[tab]this" with "[tab]$this->" | |
self.view.replace(edit, word_region, "\t$this->") | |
elif word == " this": | |
# Replace "[space]this." with "[space]$this->" | |
self.view.replace(edit, word_region, " $this->") | |
class PhpThisToThisArrowListener(sublime_plugin.EventListener): | |
def on_modified_async(self, view): | |
# Check if the current file is a PHP file | |
if view.is_valid() and len(view.sel()) > 0 and view.match_selector(view.sel()[0].begin(), "source.php"): | |
view.run_command("php_this_to_this_arrow") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To be put in ~/Library/Application Support/Sublime Text/Packages/User