Invalid sublime indentation for PHP files

I am using a special label for indenting in Sublime Text specified in this post. Indenting multiple lines in Sublime Text

But this does not work if there is an odd number of spaces. Like my tab size, it is set to 2 spaces, and when there are 3 spaces at the beginning of any line, this does not indent the remaining code.

eg:

<?php function test_indent() { if (condition) { echo "here"; } else { echo "else"; } } 

And when I indent with the custom label specified in the above post, this is:

 { "keys": ["ctrl+shift+f"], "command": "reindent", "args": {"single_line": false} } 

for me it looks like:

 function test_indent() { if (condition) { echo "here"; } else { echo "else"; } } 

What do I need to do to indent correctly?

+5
source share
1 answer

While playing with this, I found that if you select completely unindent first, then reindent will work as expected. This seems to be a bug in the reindent , which occurs when the indent is not a multiple of the size of the "tab" that is set.

Therefore, we can work around this error by changing your shortcut to completely deselect and then delay it again.

From the Tools menu, select Developer β†’ New Plugin...

Select all and replace it with the following:

 import sublime import sublime_plugin import re class UnindentAndReindentCommand(sublime_plugin.TextCommand): def run(self, edit): while any([sel for sel in self.view.sel() if re.search('^[ \t]+', self.view.substr(sel.cover(self.view.line(sel.begin()))), re.MULTILINE)]): self.view.run_command('unindent') self.view.run_command('reindent', { 'single_line': False }) 

Save it in the place that ST offers, something like fix_reindent.py - the file extension is important, but there is no base name.

Then change the key binding to use the new unindent_and_reindent command we just created:

 { "keys": ["ctrl+shift+f"], "command": "unindent_and_reindent" } 

And now it will correctly rewrite your code.

+4
source

Source: https://habr.com/ru/post/1264836/


All Articles