Sublime Forum

In EventListener command,when I call view.run_command missing error

#1

my code like this, when I try pass the via {"text":currStr,"region":line} to DofunnyCommand method run()

class LookingCammand(sublime_plugin.EventListener):
     def on_modified(self,view):
         view.run_command("dofunny",{"text":currStr,"region":line})

class DofunnyCommand(sublime_plugin.TextCommand):
    def run(self,edit):
         self.view.replace(edit,region,"hello world!")

it told me error,like this, how I shoud do
Traceback (most recent call last):
File “C:\Program Files\Sublime Text 3\sublime_plugin.py”, line 389, in run_callback
expr()
File “C:\Program Files\Sublime Text 3\sublime_plugin.py”, line 488, in
run_callback(‘on_modified’, callback, lambda: callback.on_modified(v))
File “C:\Users\khh\AppData\Roaming\Sublime Text 3\Packages\ldb\DoFunny.py”, line 47, in on_modified
view.run_command(“dofunny”,{“text”:currStr,“region”:line})
File “C:\Program Files\Sublime Text 3\sublime.py”, line 830, in run_command
sublime_api.view_run_command(self.view_id, cmd, args)
TypeError: Value required

0 Likes

#2

Could you give us the full code? Because this could never work (line isn’t defined, neither is region)

0 Likes

#3
import sublime
import sublime_plugin
import re
import os



def getCurrLine(view):
    #获取当前行区域
    mark = view.sel()[0]
    line = view.line(mark.a)
    return line

def getExtIsAllow(view):
    file_name = os.path.basename(view.file_name())
    ext = file_name[-3:]
    return ext.upper()


class LookingCammand(sublime_plugin.EventListener):

    def on_modified(self,view):
        #判断文件后缀是否允许
        if getExtIsAllow(view)  == "PHP":
            action = view.command_history(0, True)[0]
            if action == "left_delete" or action == "right_delete":
                return
            #do not change text again if undo requested
            action = view.command_history(1, True)[0]
            if action == "dofunny":
                return
            #获取当前行的输出,并检索字符串
            line = getCurrLine(view)
            currStr = view.substr(line)
            view.run_command("dofunny",{"text":currStr,"region":line})


class DofunnyCommand(sublime_plugin.TextCommand):
    def run(self,edit,args):
        self.view.replace(edit,args.region,args.text)
0 Likes

#4

Oh, yes, I know, I had the same problem: you can only pass arguments to commands that are writable in JSON.

So, it’s

  • list
  • dict (called object in JSON)
  • numbers
  • string

If it’s not one of those, it’s not gonna work. So, you can convert your region to a list: [line.begin, line.end], and, in your command, transform the list into a region: sublime.Region(args.line[0], args.line[1]).

BTW, I don’t understand how the args thing in you DoFunnyCommand could works…

0 Likes

#5

If you have any trouble with python, ask, I’ll be happy to help you out :slightly_smiling:

1 Like

#6

you are very nice! I will try slove. thank u

0 Likes