Sublime Forum

How to easily comment out lines that are a mix of HTML and PHP?

#21

@kingkeith, care to revive this old issue we were discussing? If you recall, I was trying to add a custom plugin that you wrote, but was having issues. I finally had a chance to revisit this. I tracked down the issue. When I execute the keyboard shortcut assigned to the plugin, the console output is:

Traceback (most recent call last):
  File "C:\Program Files\Sublime Text 3\sublime_plugin.py", line 812, in run_
    return self.run(edit, **args)
TypeError: run() got an unexpected keyword argument 'block'

Thoughts on that? Seems to be an issue in sublime_plugin.py?

0 Likes

#22

it’s telling you that the command you are trying to run doesn’t accept an argument called block, probably you will want to edit def run(self, edit): to def run(self, edit, block=True): to get it working.
And self.view.run_command('toggle_comment', { 'block': True }) to self.view.run_command('toggle_comment', { 'block': block }) to make use of the arg value

0 Likes

#23

OK thanks for that. With those edits, there are now no console errors when I execute the keybind. But now, when I execute the keybind on sample code:

<span class="item"><?php echo "test"; ?></span>

the PHP portion of of the line remains uncommented (screenshots). At least, the color of the text remains as uncommented. Furthermore, the on-screen rendering of this code changes from test to // test Thoughts on that? Which of this, if any, is expected?

0 Likes

#24

this plugin snippet still makes use of the built in toggle comment functionality, which doesn’t know that the line comment for PHP shouldn’t begin at the start of the line, but inside the start of the PHP block, so all of it is expected - its only really supported to use block comments here. Maybe it would be possible to improve the plugin I posted to handle this, anybody feel free to tinker and try

0 Likes

#25

OK I understand that this plugin is intended for use in blocks, not single lines. But I’m still not quite understanding exactly how to use this plugin. As an example, in the below code, what exactly would I highlight before executing the plugin?

<div class="col-7">
  <h2><?php the_title(); ?></h2>
  <?php echo ale_trim_excerpt(12); ?>
    <?php if(ale_get_meta('propertytype')){ ?>
      <span class="item typeap"><?php echo esc_attr(ale_get_meta('propertytype')); ?></span>
    <?php } ?>
    <?php if(ale_get_meta('propertysq')){ ?>
      <span class="item area"><?php echo esc_attr(ale_get_meta('propertysq')); ?></span>
    <?php } ?>
  <?php if(function_exists('the_ratings')) {
    echo '<span class="rating">'.__('Rating', 'aletheme').':</span>';
      the_ratings();
    } ?>
</div>
0 Likes

#26

it seems to work fine for me just selecting everything in one go. I commented out the last two lines though, as the selection doesn’t end up quite right at the end otherwise. I guess can replace with the following to select everything that was previously selected:

    region = self.view.sel()[0].cover(self.view.sel()[-1])
    self.view.sel().clear()
    self.view.sel().add_all([region])

0 Likes

#27

Hey sorry for the late reply. OK I tried selecting all, then executing the keyboard shortcut, but I did not achieve your results. See the below screenshot for my results. Yes, I see the same selection error you mentioned. But the real problem is that the commenting on the <?php lines doesn’t occur. There are no errors in the console. Thoughts on that?

0 Likes