Skip to content

Turn off beautify for regions? #95

@Quincunx271

Description

@Quincunx271

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()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions