Coder Social home page Coder Social logo

Comments (3)

brian-vogogo avatar brian-vogogo commented on June 24, 2024

+1

from beautifyruby.

andersennl avatar andersennl commented on June 24, 2024

+1

from beautifyruby.

Quincunx271 avatar Quincunx271 commented on June 24, 2024

+1.

As a temporary workaround, open up beautify_ruby.py and replace the beautify_buffer function with this:

  def beautify_buffer(self, edit):
    sel = self.view.sel()
    if all(selection.empty() for selection in sel): # if there is no selection, 
      sel = [sublime.Region(0, self.view.size())]   # beautify the entire file
    for buffer_region in sel:
      # buffer_region = sublime.Region(0, self.view.size())
      buffer_text = self.view.substr(buffer_region)
      if buffer_text == "":
        return
      self.save_viewport_state()
      beautified_buffer = self.pipe(self.cmd(), buffer_text)
      fix_lines = beautified_buffer.replace(os.linesep,'\n')
      self.check_valid_output(fix_lines)
      self.view.replace(edit, buffer_region, fix_lines)
      self.reset_viewport_state()

This beautifies just the selections, or the entire file if there is no selection. However, BeautifyRuby treats it as if the selection is all there is in the file, so indentation is stripped down to the base indentation. To fix this, we could use python's textwrap module.

import textwrap
# skip a bunch of lines
  def beautify_buffer(self, edit):
    sel = self.view.sel()
    if all(selection.empty() for selection in sel):
      sel = [sublime.Region(0, self.view.size())]
    for buffer_region in sel:
      # buffer_region = sublime.Region(0, self.view.size())
      buffer_text = self.view.substr(buffer_region)
      indentation = len(buffer_text) - len(buffer_text.lstrip()) # this line
      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 = textwrap.indent(fix_lines, ' ' * indentation) # and this one
      self.check_valid_output(fix_lines)
      self.view.replace(edit, buffer_region, fix_lines)
      self.reset_viewport_state()

from beautifyruby.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.