2014-04-29 94 views
2

我想this plugin從崇高文本2轉換爲崇高的文本3,但我一直運行到這個錯誤崇高的文本3插件,去掉引號,

File "/Users/macintoshhd/Library/Application Support/Sublime Text 3/Packages/magiclessquotesjon.py", line 17, in on_pre_save 
    edit = view.begin_edit() 
TypeError: begin_edit() missing 2 required positional arguments: 'edit_token' and 'cmd' 

我知道,API版本之間和改變,因此是porting guide,但我仍然不確定如何獲得正確的TextCommand或如何使用它。

插件代碼在這裏:

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

__author__ = "Daryl Tucker" 

import sublime, sublime_plugin 

class RemoveMagicFromMagic(sublime_plugin.EventListener): 
    def on_pre_save(self, view): 
     replacements = [ 
      [u'[’‘`]{1}',u'\''], 
      [u'[「」]{1}',u'"'], 
      [u'[…]{1}',u'...'], 
      [u'[—]{1}',u'---'], 
      [u'[–]{1}',u'--'], 
      [u'[•]{1}',u'*'], 
      [u' & ',u' & '], 
     ] 
     edit = view.begin_edit() 
     for replacement in replacements: 
      x = view.find_all(replacement[0]) 
      for position in x: 
       view.replace(edit, position, replacement[1]) 
     view.end_edit(edit) 

回答

6

能得到它與一個額外的回調工作..

class RemoveSmartQuotesCommand(sublime_plugin.TextCommand): 
    def run(self, edit, user_input=None): 
     self.edit = edit 
     replacements = [ 
      [u'[’‘`]{1}',u'\''], 
      [u'[「」]{1}',u'"'], 
      [u'[…]{1}',u'...'], 
      [u'[—]{1}',u'---'], 
      [u'[–]{1}',u'--'], 
      [u'[•]{1}',u'*'], 
      [u' & ',u' & '], 
     ] 
     for replacement in replacements: 
      x = self.view.find_all(replacement[0]) 
      for position in x: 
       self.view.replace(edit, position, replacement[1]) 

class RemoveSmartQuotesWhenSaving(sublime_plugin.EventListener): 
    def on_pre_save(self, view): 
     view.run_command('remove_smart_quotes') 
+0

供您參考:我遇到了一個類似的問題:收到了極好的__ [解決方案](https://forum.sublimetext.com/t/solved-issue-using-edit-objects-recursively/18400)__在Sublime Text論壇上,我現在非常喜歡使用'edit'的標準方法對象。這是線程中提到的解決方案的__ [工作版本](https://gist.github.com/Enteleform/8b46387a5e0d9341fc88)__。它的實現如下:'edit = Edit(view)'&'edit.replace(region,text)' – Enteleform