使用 sublime text 3 删除 U+200B 零宽度空格字符 [英] delete U+200B zero-width space characters using sublime text 3

查看:106
本文介绍了使用 sublime text 3 删除 U+200B 零宽度空格字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在使用 sublime text 3 时制作 U+200B 字符或删除它们.我找到了 http://pastebin.com/ehWxNfMe 但我不知道如何使用它

How can I make U+200B character or delete them in using sublime text 3. I found http://pastebin.com/ehWxNfMe but I am not sure how to use it

推荐答案

以下将在 Sublime Text 2 和 3 中工作.但是,由于后面讨论的一些问题,它有可能在编辑大文件时阻塞程序,和/或在慢速计算机上.使用异步方法的 Sublime Text 3 特定版本位于底部.

The following will work in Sublime Text 2 and 3. However, due to some issues discussed later, it has the potential to block the program when editing large files, and/or on slow computers. A Sublime Text 3-specific version using an asynchronous method is at the bottom.

在 Sublime 中打开一个新文件,并将其语法设置为 Python.将以下内容粘贴到其中:

Open a new file in Sublime, and set its syntax to Python. Paste the following into it:

import sublime_plugin

class ShowZeroWidthSpace(sublime_plugin.EventListener):
    def on_modified(self, view):
        spaces = []
        p = 0
        while True:
            s = view.find(u'\u200b', p + 1)
            if not s:
                break
            spaces.append(s)
            p = s.a
 
        if spaces:
            view.add_regions("zero-width", spaces, "invalid")
        else:
            view.erase_regions("zero-width")


将文件保存在 Packages/User 目录中为 show_zero_width_space.py,它应该立即开始工作.基本上它所做的是每次修改当前视图时,它会逐个字符地搜索它以查找零宽度空格字符 U+200B.如果找到,则将该位置添加到列表中并继续查找,直到到达文件末尾.然后,如果找到任何字符,它们会根据主题中的 invalid 范围突出显示.突出显示后,可以选择和删除它们.


Save the file in your Packages/User directory as show_zero_width_space.py, and it should start working immediately. Basically what it does is every time the current view is modified, it searches through it character by character looking for the zero-width space character, U+200B. If it finds one, it adds the location to a list and keeps looking until it reaches the end of the file. Then, if any characters were found, they are highlighted according to the invalid scope in your theme. Once highlighted, they can be selected and deleted.

由于此插件在每次修改视图时都会运行(例如,在每次击键后),因此它有可能真正降低 Sublime 的速度,尤其是对于大文件.因此,如果您正在处理您认为干净的文件,只需将插件重命名为 show_zero_width_space.py.old 并确保删除任何名为 show_zero_width_space.pyc 的文件,然后它不会被激活.有关 Sublime Text 3 特定的解决方法,请参见下文.

Since this plugin runs every single time the view is modified (after every keystroke, for example), it has the potential to really slow down Sublime, especially for large files. Therefore, if you are working on files you know are clean, simply rename the plugin to show_zero_width_space.py.old and make sure you delete any file named show_zero_width_space.pyc, and it won't be active. For a Sublime Text 3-specific workaround, see below.

EventListener 的 Sublime Text 3 API 现在包含一个 on_modified_async() 方法,与所有 ST3 函数现在都是线程安全的事实保持一致.这以非阻塞方式异步运行代码,因此 Sublime 不应该被它减慢.新插件如下:

The Sublime Text 3 API for EventListener now includes an on_modified_async() method, in keeping with the fact that all ST3 functions are now thread-safe. This runs the code asynchronously, in a non-blocking manner, so Sublime shouldn't be slowed down by it. The new plugin is as follows:

import sublime_plugin

class ShowZeroWidthSpace(sublime_plugin.EventListener):
    def on_modified_async(self, view):
        spaces = []
        p = 0
        while True:
            s = view.find('\u200b', p + 1)
            if not s:
                break
            spaces.append(s)
            p = s.a
 
        if spaces:
            view.add_regions("zero-width", spaces, "invalid")
        else:
            view.erase_regions("zero-width")

这篇关于使用 sublime text 3 删除 U+200B 零宽度空格字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆