2016-09-21 72 views

回答

1

Sublime構建系統有一個名爲target的選項,該選項指定要調用以執行構建的WindowCommand。默認情況下,這是內部exec命令。您可以創建自己的命令,將檢查的家當的文件,並使用該解釋或一些默認的除外。

例如(警告:我不是在Python超級精通所以這可能是相當難看):

import sublime, sublime_plugin 

class ShebangerCommand(sublime_plugin.WindowCommand): 
    def parseShebang (self, filename): 
     with open(filename, 'r') as handle: 
      shebang = handle.readline().strip().split (' ', 1)[0] 
     if shebang.startswith ("#!"): 
      return shebang[2:] 
     return None 

    def createExecDict(self, sourceDict): 
     current_file = self.window.active_view().file_name() 
     args = dict (sourceDict) 

     interpreter = args.pop ("interpreter_default", "python") 
     exec_args = args.pop ("interpreter_args", ["-u"]) 
     shebang = self.parseShebang (current_file) 

     args["shell_cmd"] = "{} {} \"{}\"".format (shebang or interpreter, 
                " ".join (exec_args), 
                current_file) 

     return args 

    def run(self, **kwargs): 
     self.window.run_command ("exec", self.createExecDict (kwargs)) 

你會在Packages/User保存這是一個Python文件(例如shebanger.py)。

這將創建一個名爲shebanger的新命令,它收集它所給出的參數,檢查構建觸發的窗口的當前活動視圖中的文件,以查看第一行是否是shebang,然後綜合參數所需exec命令並運行它。

由於默認的python編譯系統假定它正在構建當前文件並將-u作爲參數傳遞,因此該命令也複製該內容。但是請注意,這個代碼是不是100%正確的,因爲在家當行任何參數將被忽略,但你的總體思路。

在使用中,您將修改默認Python.sublime-build文件看起來像這樣:

{ 
    // WindowCommand to execute for this build 
    "target": "shebanger", 

    // Use this when there is no shebang 
    "interpreter_default": "python", 

    // Args to pass to the interpreter 
    "interpreter_args": ["-u"], 

    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)", 
    "selector": "source.python", 

    "env": {"PYTHONIOENCODING": "utf-8"}, 

    "variants": 
    [ 
     { 
      "name": "Syntax Check", 
      "interpreter_args": ["-m py_compile"], 
     } 
    ] 
} 

注意的是,在變型,我們覆蓋的解釋變量是什麼;如果需要,也可以覆蓋那裏的默認解釋器。

+0

這看起來相當檢測版本有希望 - 我讓你知道如何去。謝謝! –

0

如果認爲使用標準.sublime-build文件執行此操作的唯一方法是將您的文件傳遞給另一個腳本,然後解析shebang並將其傳遞到正確的Python版本。

或者,你可以指定build variants,但你必須手動選擇所需構建變量。

0

我python.sublime建造

{ 
    "cmd": ["py", "-u", "$file"], 
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)", 
    "selector": "source.python", 
    "shell":true 
} 

在windows我用PY發射器根據家當