0
0

More than 1 year has passed since last update.

Sublime Text、簡易的な誤字脱字検知プラグイン

Last updated at Posted at 2022-02-01

表題通り。

5fa9763e790826f2fbfc8f981de7568f[1].png

こんな感じ。誤字脱字にハイライトを表示するシンプルなプラグイン。変換候補とかも出そうと思ったけれど、ハイライトだけで十分なので、ハイライトだけ。

ツール>デベロッパー>ニュープラグイン

でテンプレートを出し、それが保存されるPackagesディレクトリに「word_caveat」ディレクトリを作り、その中に色々と作っていく。

プラグイン本体。

WordCaveatCommand.py
import sublime
import sublime_plugin


def settings_template(self, add_flag):
    settings = sublime.load_settings('WordCaveat.sublime-settings')
    words = settings.get('words')
    for region in self.view.sel():
        sel_string = self.view.substr(region)
        if len(sel_string) > 0:
            if add_flag:
                words += [sel_string]
            else:
                if sel_string in words:
                    words.remove(sel_string)

    settings.set("words", words)
    sublime.save_settings("WordCaveat.sublime-settings")


class WordCaveatCommand(sublime_plugin.EventListener):
    def on_modified(self, view):
        settings = sublime.load_settings('WordCaveat.sublime-settings')
        words = settings.get('words')
        regions = []
        for word in words:
            regions += view.find_all(word)

        view.add_regions("WordCaveat", regions, "region.redish",
                         "dot",
                         sublime.DRAW_NO_FILL)


class WordCaveatAddCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        settings_template(self, True)


class WordCaveatRemoveCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        settings_template(self, False)


WordCaveatCommandでセッティングから単語リストを読み出し、文章が書き込まれるたびに、そこにハイライトをつける。

WordCaveatAddCommandで新たな単語を追加する。

WordCaveatRemoveCommandで選択した単語をリストから削除する。

Context.sublime-menu
[
      { "command": "word_caveat_add","caption":"ハイライト単語追加"},
      { "command": "word_caveat_remove","caption":"選択ハイライト単語削除"},
]

右クリックに単語追加用と削除用のメニューを追加する。

Main.sublime-menu
[
    {
        "mnemonic": "n",
        "caption": "Preferences",
        "id": "preferences",
        "children": [
            {
                "mnemonic": "P",
                "caption": "Package Settings",
                "id": "package-settings",
                "children": [
                    {
                        "caption": "Word Caveat",
                        "children": [
                            {
                                "caption": "Settings – Default",
                                "args": {
                                    "file": "${packages}/word_caveat/WordCaveat.sublime-settings"
                                },
                                "command": "open_file"
                            },
                            {
                                "caption": "Settings – User",
                                "args": {
                                    "file": "${packages}/User/WordCaveat.sublime-settings"
                                },
                                "command": "open_file"
                            },
                            {
                                "caption": "-"
                            }
                        ]
                    }
                ]
            }
        ]
    }
]

設定のパッケージセッティングにユーザーセッティングとパッケージセッティングファイルへのリンクを作る。

WordCaveat.sublime-settings
{
    "words": [
        "あんた",
    ]
}

プラグインのデフォルト設定ファイルを作る。

これで、誤字脱字の部分を右クリックし、単語を追加すると、以後、単語が出るとハイライトしてくれるようになる。やったぜ!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0