2012-03-14 84 views
0

我想創建一個Sublime Text 2 Snippet,它填充變量I前面和後面用空格鍵入的空格。在文本之前和之後用特殊字符填充排隊

它應該是這樣的:

===========================my_filename.js=========================== 

的文件名應該是居中所以之前和之後文本空格數必須匹配。 另外我需要這一行的整個列寬保持不變。因此,當我添加2個字符時,每邊的空格數減1。

我覺得這個樣本是:

spacesLeft = roundDown((columnWidth/2) - (textSize/2)) 
spacesRight = roundUp((columnWidth/2) - (textSize/2)) 

但由於只有正則表達式是在崇高的片段可我不看我能,來完成這項任務。

Vintagmode能以任何方式幫助我嗎?

感謝您的幫助!

回答

1

因爲片段本質上是靜態的,所以在這種情況下它們將無法幫助您。但是,您可以創建一個插件來相對簡單地完成此操作。 Sublime Text爲其插件使用python。您可以通過轉到工具>新插件來創建一個。 ST2的API可以找到here,並且非常令人印象深刻。從本質上講,你要存儲使用

sel_reg = self.view.sel()[0] # the current selection 'Region' 
sel = self.view.substr(sel_reg) # the text within the 'Region' 

當前的選擇(你的變量),然後生成=的

signs = '' 
each_side = len(80 - len(sel))/2 # 80 is the standard window length, 
            # change this to what you want. 
            # Not sure about how to make it dynamic. 
for x in xrange(each_side): 
    signs += '=' 

然後用替換當前行(self.view.line(sel))。

+0

'sel =「」+ re.search(「[^ /] + $」,self.view.file_name())。group(0)+「」'做了工作:) – Handfeger 2012-03-15 09:43:09

相關問題