I don’t know about whether it is bad practice or not, but you can write a plugin to do this: (split selection by scope - text (HTML) or source (PHP) and then invoke the toggle_comment
command.)
- Tools menu -> Developer -> New Plugin
- Select all and replace with the following:
import sublime
import sublime_plugin
class ToggleCommentSourceTextCommand(sublime_plugin.TextCommand):
def run(self, edit):
original_sel = [sel for sel in self.view.sel()]
regions = list()
for sel in self.view.sel():
if sel.empty():
sel = self.view.line(sel)
region_start = None
for pos in range(sel.begin(), sel.end() + 1):
if region_start is None or self.view.match_selector(pos, 'source') != self.view.match_selector(region_start, 'source') or self.view.match_selector(pos, 'punctuation.section.embedded'):
if region_start is not None:
regions.append(sublime.Region(region_start, pos - (1 if self.view.match_selector(pos, 'punctuation.section.embedded.end') else 0))) # the minus one works around a `toggle_comment` command bug
region_start = None
if not self.view.match_selector(pos, 'punctuation.section.embedded'):
region_start = pos
print(pos, region_start)
if region_start != pos:
regions.append(sublime.Region(region_start, pos))
self.view.sel().clear()
self.view.sel().add_all(regions)
self.view.run_command('toggle_comment', { 'block': True })
self.view.sel().clear()
self.view.sel().add_all(original_sel)
- Save it as
toggle_comment_cag8f.py
in the folder ST recommends (Packages/User/
)
- Create a keybinding to use the
toggle_comment_source_text
command (optionally replace the default block comment key combination)
- Optionally update the Edit -> Comment menu entry to use this command
Note that due to a bug in the toggle_comment
command, it requires whitespace between the end of the PHP code and the ?>
- you can see this if you manually select everything between the <?php
and ?>
and try to use the toggle_comment
command with arguments block
true
- nothing happens.