Sublime-Wrap-Plus

Sublime-Wrap-Plus

是否有一種自動方法可以在文字目前換行的每個點插入換行符號?執行此操作後,不應再換行,但視覺上應看起來相同。

答案1

為此創建一個插件。選擇工具 » 新插件...並輸入以下腳本:

import sublime, sublime_plugin

class WrapLinesExCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        wrap_column = 0

        if self.view.settings().get('word_wrap') == False:
            # wrapping is disabled, do nothing
            return

        if self.view.settings().get('wrap_width') == 0:
            # compute wrap column from viewport width
            wrap_column = int(self.view.viewport_extent()[0] / self.view.em_width())
        else:
            wrap_column = self.view.settings().get('wrap_width')

        e = self.view.begin_edit()
        rewrap(self.view, e, wrap_column)
        self.view.end_edit(e)

def rewrap(v, e, column):
    # 0-indexed current line
    current_line_no = 0

    # RHS expression is line count, can change whenever we create a new one
    while current_line_no < v.rowcol(v.size())[0] + 1:
        # where current line drawing starts
        current_line_coords = v.text_to_layout(v.text_point(current_line_no, 0))

        # rightmost character drawn in current viewport
        textpos = v.layout_to_text((v.em_width() * (column), current_line_coords[1]))

        # physical line boundaries as absolute text positions
        current_line = v.line(textpos)

        if textpos < current_line.b:
            # the current line spans multiple rows, so insert a newline at the wrap column

            textpos = v.layout_to_text((v.em_width() * (column), current_line_coords[1]))
            next_line_indent = v.text_to_layout(textpos+1)[0]

            # TODO why -1?
            next_line_indent_chars = int(next_line_indent/(v.em_width()))-1
            # determine how to indent the following line based on how wide the wrapping indents and what the current tab/spaces settings are
            if v.settings().get('translate_tabs_to_spaces') and v.settings().get('use_tab_stops'):
                next_line_indent_chars = next_line_indent_chars / v.settings().get('tab_size')
                next_line_indent_string = '\t' * next_line_indent_chars
            else:
                next_line_indent_string = ' ' * next_line_indent_chars

            # insert newline and spacing at wrap column (sublime hides actual line endings from editor, therefore it's always LF)
            v.insert(e, textpos, '\n' + next_line_indent_string)
        else:
            # only continue to the next line if we didn't edit the current line
            current_line_no = current_line_no + 1

將例如儲存wrap_lines_ex_command.py在預設 ( User) 目錄中。

若要從功能表列存取此功能,請選擇瀏覽套餐...選單項,導航到該User資料夾,然後Main.sublime-menu按照中所述進行編輯(如有必要,則建立它)這個答案所以它包含如下文字:

[
    {
        "id": "edit",
        "children":
        [
            {"id": "wrap"},
            {"command": "wrap_lines_ex", "caption": "Wrap All Lines"}
        ]
    }
]

截圖

前:

之前的截圖

後:

之後的截圖

當然,在這種情況下,由於註釋也被包裝,程式碼將不再起作用。但這是根據問題設計的行為。

答案2

幾年後,針對這種事情已經有現成的軟體包(插件)了。他們可能無法完全滿足您的要求(以匹配視窗中顯示的當前換行),但您可以在其首選項中設定要換行的列。

Sublime-Wrap-Plus

GitHub 頁面

安裝

  1. 打開Sublime Text 2 or 3
  2. command-shift-p(Mac OS X) 或ctrl-shift-p(Windows) 打開Command Palette,鍵入“安裝”,然後選擇選項Install Package Control
  3. 再次打開Command Palette,再次輸入“install”,然後選擇選項Install a Package
  4. 開始輸入,然後選擇sublime-wrap-text

用法

  1. 選擇有問題的文字。
  2. command+alt+q(Mac OS X) 或alt+q(Windows)。

請參閱 GitHub 頁面,以了解更多使用細微差別以及如何設定首選項。

示範

在此輸入影像描述

(我只是突出顯示所有文字並按 alt+q)

在此輸入影像描述

另一個類似的包是Sublime 換行語句

GitHub 頁面

我自己沒有嘗試過這個,但是如果你願意的話可以嘗試。

答案3

帳戶太年輕,無法添加評論
,但想添加 2021 年更新MarredCheese 的 2017 年優秀答案上面找到的。

要安裝 MarredCheese 提到的第一個包:

GitHub: https://github.com/ehuss/Sublime-Wrap-Plus
Sublime 套件名稱: 包裹加

步驟1。仍然有效,從原始帖子粘貼:

  1. 打開崇高

第2步。仍然有效,從原始帖子粘貼:

  1. commandshiftp(Mac OS X) 或ctrlshiftp(Windows) 打開Command Palette,鍵入“安裝”,然後選擇選項Install Package Control

步驟 3.有點不準確。
如果語法發生變化,這裡的語法將在 2021 年生效:

   步驟 3.(原):

  • 打開Command Palette again,再次輸入“install”,然後選擇選項Install a Package

   步驟 3.(2021 年修訂):

  • 而不是鍵入Install a Package以下任一內容:
    • Install Package或者
    • Package Control: Install Package
    • 注意:「套件控制:進階安裝套件」很有用,
      但對於本節來說不是必需的。

步驟4。不再有效。

   步驟 4.(原):

  • 開始輸入,然後選擇sublime-wrap-text

   步驟 4.(2021 年修訂):

要安裝其答案底部提到的第二個軟體包 MarredCheese:

GitHub: https://github.com/shagabutdinov/sublime-wrap-statement
崇高包名稱:換行語句

請按照上一節中的相同步驟進行操作,但是對於步驟 3.改為鍵入WrapStatement(無空格)。
(你會看見https://github.com/shagabutdinov/sublime-wrap-statement在下拉清單中此條目的詳細資訊中Install Package

答案4

目前,該功能似乎並未包含在 Sublime Text 2 的首選項中(您可以在 Default/Preferences.sublime-settings 中親自查看)。可以使用諸如"line_padding_bottom": 4(其中 4 是每行下方所需的像素數)之類的配置選項來確保所有行的閱讀清晰度,但不可能根據行是否換行有選擇地應用不同的行填充。

您可能希望提交功能請求在 Sublime Text 2 的論壇上。我知道如果實現合理的話,我也會很欣賞這個功能。

相關內容