サブライムラッププラス

サブライムラッププラス

テキストが現在折り返されているすべてのポイントに自動的に改行を挿入する方法はありますか? この操作の後、行は折り返されませんが、見た目は同じになるはずです。

答え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

数年後、このような用途の既成パッケージ (プラグイン) が登場しました。これらは、(ウィンドウに表示されている現在の折り返しと一致するように) 要求を完全に満たすものではないかもしれませんが、設定で折り返す列を設定できます。

サブライムラッププラス

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 a Package
  4. 入力を開始して選択しますsublime-wrap-text

使用法

  1. 問題のテキストを選択します。
  2. command+alt+q(Mac OS X) またはalt+q(Windows)を押します。

詳しい使用方法や設定方法については、GitHub ページを参照してください。

デモ

前に

ここに画像の説明を入力してください

(テキスト全体をハイライトして、Alt+Q を押しました)

ここに画像の説明を入力してください

もう一つの類似パッケージは崇高なラップステートメント

GitHub ページ

私自身はまだ試していませんが、よろしければ試してみてください。

答え3

コメントとして追加するにはアカウントが若すぎる
が、2021年のアップデートを追加したいMarredCheeseの素晴らしい2017年の回答上記にあります。

MarredCheese が言及した最初のパッケージをインストールするには:

GitHub: https://github.com/ehuss/Sublime-Wrap-Plus
崇高なパッケージ名: ラッププラス

ステップ1。まだ動作します。元の投稿から貼り付けます:

  1. サブライムを開く

ステップ2。まだ動作します。元の投稿から貼り付けます:

  1. commandshiftp(Mac OS X) またはctrlshiftp(Windows)を押してを開きCommand Palette、「インストール」と入力して、 のオプションを選択しますInstall Package Control

ステップ3。若干不正確です。
構文が変更された場合、2021 年に機能するものは次のとおりです。

   ステップ3. (オリジナル):

  • を開きCommand Palette again、もう一度「インストール」と入力して、オプションを選択しますInstall a Package

   ステップ 3. (2021 年版に改訂):

  • 代わりに、Install a Package次のいずれかを入力します。
    • Install Packageまたは
    • Package Control: Install Package
    • 注: 「パッケージ コントロール: 高度なインストール パッケージ」は便利です
      が、このレシピには必要ありません。

ステップ4。動作しなくなりました。

   ステップ 4. (オリジナル):

  • 入力を開始して選択しますsublime-wrap-text

   ステップ 4. (2021 年版に改訂):

MarredCheese が回答の下部で言及した 2 番目のパッケージをインストールするには:

GitHub: https://github.com/shagabutdinov/sublime-wrap-statement
Sublime パッケージ名:ラップステートメント

前のセクションと同じ手順に従いますが、ステップ3。WrapStatement代わりに(スペースなしで)入力して
ください。(https://github.com/shagabutdinov/sublime-wrap-statementInstall Packageドロップダウンのこのエントリの詳細で

答え4

現時点では、この機能は Sublime Text 2 の設定には含まれていないようです (Default/Preferences.sublime-settings で確認できます)。"line_padding_bottom": 4すべての行の読みやすさを向上させるために、(各行の下のピクセル数が 4 の場合) のような設定オプションを使用することは可能ですが、行が折り返されているかどうかに応じて異なる行パディングを選択的に適用することはできません。

機能リクエストを送信することもできますSublime Text 2のフォーラム実装が合理的であれば、私もこの機能を高く評価すると思います。

関連情報