一次选择Sublime Text中所有拼写错误的单词


5

有没有办法,当激活spellcheckingF6然后注意到一堆红色下划线的错别字,选择所有拼写错误的单词?

显然我们不能用简单的Ctrl+ 做到这一点D。我们的想法是选择所有这些,然后复制到单独的文件来检查它们或简单地一次删除所有文件。

Answers:


5

您可以轻松创建自定义插件来执行此操作:

  1. Tools菜单 - > Developer- > New Plugin...(基于Dev Build 3111的说明。对于稳定通道3103,它只是Tools- > New Plugin...
  2. 用以下内容替换文件的内容:
import sublime, sublime_plugin

class SelectAllSpellingErrorsCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        regions = []
        while True:
            self.view.run_command('next_misspelling')
            if self.view.sel()[0] not in regions:
                regions.append(self.view.sel()[0])
            else:
                break
        self.view.sel().clear()
        self.view.sel().add_all(regions)
  1. 将其保存在默认目录(将是您的Packages/User文件夹)中spelling.py
  2. 创建自定义键绑定以运行命令 select_all_spelling_errors

1
基思,这真是太棒了。完全是它的意思!
Da Rossa 2016年

仅供参考,对于任何想要创建和使用此插件而无需使用密钥绑定的人,您只需view.run_command('select_all_spelling_errors')从控制台运行即可。
Fabien Snauwaert

1

我使用该算法在一个文件中执行它,在一个文件中,在一万个拼写错误的单词中,在1.500.000行/单词内。这是个人资料结果:

         1.555.756 function calls in 9.196,582 seconds ~2,5 hours

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.077    0.077 9196.582 9196.582 <string>:1(<module>)
        1    6.087    6.087 9196.505 9196.505 select_all_spelling_errors.py:23(findRegions)
   245644    1.042    0.000    1.042    0.000 sublime.py:550(__init__)
   163763    2.559    0.000    2.559    0.000 sublime.py:557(__str__)
   245644    1.357    0.000   15.908    0.000 sublime.py:638(__getitem__)
        1    0.000    0.000    0.000    0.000 sublime.py:659(clear)
    81881    0.611    0.000   56.255    0.001 sublime.py:662(add)
        1    0.385    0.385   56.640   56.640 sublime.py:668(add_all)
    81882    0.884    0.000 9114.968    0.111 sublime.py:829(run_command)
   245646    0.344    0.000    0.344    0.000 sublime.py:832(sel)
        1    0.001    0.001 9196.582 9196.582 {built-in method exec}
    81881    0.071    0.000    0.071    0.000 {built-in method isinstance}
    81882 9114.084    0.111 9114.084    0.111 {built-in method view_run_command}
    81881   55.573    0.001   55.573    0.001 {built-in method view_selection_add_region}
        1    0.000    0.000    0.000    0.000 {built-in method view_selection_clear}
   245644   13.509    0.000   14.551    0.000 {built-in method view_selection_get}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        1    0.000    0.000    0.000    0.000 {method 'values' of 'dict' objects}

在那里,我们可能会看到几乎一直吃的罪魁祸首:

81882 9114.084    0.111 9114.084    0.111 {built-in method view_run_command}

这是使用的源代码:

import sublime
import sublime_plugin

import cProfile


class SelectAllSpellingErrorsCommand(sublime_plugin.TextCommand):

    def run(self, edit):

        cProfile.runctx( 'findRegions(self, edit)', globals(), locals() )

def findRegions(self, edit):

    regionsList = []

    while True:

        self.view.run_command('next_misspelling')

        if self.view.sel()[0] not in regionsList:

            regionsList.append( self.view.sel()[0] )

        else:

            break

    self.view.sel().clear()
    self.view.sel().add_all( regionsList )
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.