Sublime Forum

[FIXED] Wrap paragraph at XX characters, too greedy

#1

[size=150]
*** Please see my 11/13 post for a fix to this issue ***
[/size]

When wrapping a paragraph of text to xx characters, it appears that the command is a bit too greedy and ends up wrapping all surrounding content until it hits a newline either above or below the selection.

<html>
<head>
  <title>Wrapping Bug</title>
</head>
<body>
  <p class="does_not_wrap_properly">
    Select lines 7-10, Edit -> Wrap paragraph at 120 chars. All of the indenting for
    surrounding content changes. Lorem ipsum dolor sit amet, consectetur
    adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna
    aliqua.
  </p>
  <p class="wraps_properly">

    Select lines 14-17, Edit -> Wrap at paragraph 120 chars. This time with the newline
    before and after the paragraph, it indents properly and does not change
    surrounding content. Lorem ipsum dolor sit amet, consectetur adipisicing
    elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

  </p>
</body>
</html>

Here are my user preferences:

{
 "tab_size": 2,
 "translate_tabs_to_spaces": true,
 "ensure_newline_at_eof_on_save": true
}

Is there a file that contains this command or macro that I can override until a bug fix is in, or is it implemented in compiled code?

0 Likes

#2

This bug also appears in build 2112. Changed build from 2109 -> 2112 in title.

0 Likes

#3

This bug also appears in build 2113. Changed build from 2112 -> 2113 in title.

0 Likes

#4

This is working as designed: paragraphs are defined to text bounded by blank lines.

Wrap paragraph is implemented via a plugin, in Default/paragraph.py

0 Likes

#5

I understand your point about paragraphs, however, it is different than expected behavior and real-world usage:


vs


All editors I’ve used wrap any text that is highlighted to the current level of indentation, regardless of leading and trailing blank lines. When writing HTML, it is not a common practice to put a blank line before and after the content between

tags. The wrap feature in its current state unfortunately isn’t very usable for the most common use cases.

Perhaps this can be changed to “Wrap text at XX characters” and apply only to the lines selected?

0 Likes

#6

Is there a better way to report bugs/issues? It seems that topics posted to this forum slowly drift away and are forgotten. I still see the current behavior of this command as a bug because it is not usable in the most common scenarios. It should wrap all text in the selection, regardless of whether it is separated by whitespace or not. I like this editor and want to be your customer. Please correct this basic functionality and you’ll sell another license.

0 Likes

#7

I went ahead and created a workaround for this issue. The “wrap paragraph” command now behaves exactly like TextMate.

# Place this code in Packages/User/paragraph.py

class WrapLinesCommand(WrapLinesCommand):
    def run(self, edit, width=0):
        if width == 0 and self.view.settings().get("wrap_width"):
            try:
                width = int(self.view.settings().get("wrap_width"))
            except TypeError:
                pass

        if width == 0 and self.view.settings().get("rulers"):
            # try and guess the wrap width from the ruler, if any
            try:
                width = int(self.view.settings().get("rulers")[0])
            except ValueError:
                pass
            except TypeError:
                pass

        if width == 0:
            width = 78

        # Make sure tabs are handled as per the current buffer
        tab_width = 8
        if self.view.settings().get("tab_size"):
            try:
                tab_width = int(self.view.settings().get("tab_size"))
            except TypeError:
                pass

        if tab_width == 0:
            tab_width == 8

        for s in self.view.sel():
            wrapper = textwrap.TextWrapper()
            wrapper.expand_tabs = False
            wrapper.width = width
            prefix = self.extract_prefix(s)
            if prefix:
                wrapper.initial_indent = prefix
                wrapper.subsequent_indent = prefix
                wrapper.width -= self.width_in_spaces(prefix, tab_width)

            if wrapper.width < 0:
                continue

            txt = self.view.substr(s)
            last_char = u"\n" if txt-1] == u"\n" else ""
            txt = re.sub('\s{2,}', ' ', txt.strip())
            if prefix:
                txt = txt.replace(prefix, u"")

            txt = string.expandtabs(txt, tab_width)

            txt = wrapper.fill(txt) + last_char
            self.view.replace(edit, s, txt)
0 Likes