BeautifyRuby icon indicating copy to clipboard operation
BeautifyRuby copied to clipboard

Turn off beautify for regions?

Open Quincunx271 opened this issue 10 years ago • 1 comments

Is there a way for BeautifyRuby to skip beautifying sections of code? I made a workaround that I put in beautify_ruby.py:

def extract_no_beautify(text):
  lines = text.splitlines()
  no_beautify = []
  noformat_start = -1
  for index, line in enumerate(lines):
    if line.lstrip().startswith("#") and "@noformat start" in line.lower():
      noformat_start = index + 1
    elif line.lstrip().startswith("#") and "@noformat end" in line.lower():
      no_beautify.append([noformat_start, index])
  no_beautify = ['\n'.join(lines[start:end]) for start, end in no_beautify]
  return no_beautify

def insert_no_beautify(text, no_beautify):
  lines = text.splitlines()
  no_beautify_regions = []
  noformat_start = -1
  for index, line in enumerate(lines):
    if line.lstrip().startswith("#") and "@noformat start" in line.lower():
      noformat_start = index + 1
    elif line.lstrip().startswith("#") and "@noformat end" in line.lower():
      no_beautify_regions.append([noformat_start, index])
  for index, region in enumerate(no_beautify_regions):
    lines[region[0]:region[1]] = [no_beautify[index]]
  return '\n'.join(lines)

And here:

def beautify_buffer(self, edit):
  buffer_region = sublime.Region(0, self.view.size())
  buffer_text = self.view.substr(buffer_region)
  no_beautify = extract_no_beautify(buffer_text)
  if buffer_text == "":
    return
  self.save_viewport_state()
  beautified_buffer = self.pipe(self.cmd(), buffer_text)
  fix_lines = beautified_buffer.replace(os.linesep,'\n')
  fix_lines = insert_no_beautify(fix_lines, no_beautify)
  self.check_valid_output(fix_lines)
  self.view.replace(edit, buffer_region, fix_lines)
  self.reset_viewport_state()

Quincunx271 avatar Jul 22 '15 17:07 Quincunx271

I have not tried the code above but I would love to have it added as a feature. For example some strings inside %w() are causing the indentation to get messed up.

roycetech avatar Apr 17 '16 03:04 roycetech