使用翻译错误转义字符的sublime命令

时间:2019-04-12 19:55:53

标签: python regex windows sublimetext3

我在markdown工作时使用了转义和粘贴命令:

{ "keys": ["ctrl+shift+v"], 
    "command": "escape_and_paste", 
    "args": {
        "\\": "\\\\",
        "_" : "\\_",
        "*" : "\\*"
    }
}

,实际命令定义为:

import sublime
import sublime_plugin

class EscapeAndPasteCommand(sublime_plugin.TextCommand):
    def run(self, edit, **kwargs):
        # Get the clipboard text
        originalText = textToPaste = sublime.get_clipboard() 

        # Use the arguments as a translation table
        textToPaste = textToPaste.translate(str.maketrans(kwargs))

        # Place the text back into the clipboard and paste in place
        sublime.set_clipboard(textToPaste)
        self.view.run_command("paste")

        # Restore the original text to the clipboard
        sublime.set_clipboard(originalText)

它应该做的是用反斜杠转义反斜杠,下划线和星号。但是,这样的文本会带来奇怪的后果:

"skylling\sdfds and _adst_",当应为"skylling\\sdfds and \\_adst\\_"时转义为"skylling\\sdfds and \_adst\_"。不确定原因

当我直接在Python中运行此测试时,没有出现此问题。

textToPaste = 'skylling\sdfds and _adst_'
escapay = {
    "\\": "\\\\",
    "_": "\\_",
     "*": "\\*"
 }
print(textToPaste.translate(str.maketrans(escapay)))

skylling\\sdfds and \_adst\_

这是因为我的args如何发送到我的py sublime命令,还是对translate()的工作方式有误解?运行Sublime Text 3内部版本3143。

0 个答案:

没有答案