1 Star 0 Fork 0

zxxLang/ZxxSublime

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Commands.py 2.03 KB
一键复制 编辑 原始数据 按行查看 历史
achun 提交于 2018-12-06 17:01 +08:00 . update
import re
import sublime
import sublime_plugin
import unicodedata
def visual_width(view, pt, tabsize):
width = 0
while pt>0 and view.rowcol(pt)[1]>0:
pt -= 1
c = view.substr(pt)
if ord(c) < 255:
width += 1
else:
c = unicodedata.east_asian_width(c)
width += c in ("F", "W") and 2 or 1
return width
class ColAlignCommand(sublime_plugin.TextCommand):
def run(self, edit, follow="\n; ,([{:"):
view = self.view
origin = []
maxwidth = 0
offset = 0
tabSize = view.settings().get('tab_size')
regions = view.sel()
if len(regions) == 1:
region = regions[0]
pos = region.begin()
end = region.end()
col = view.rowcol(pos)[1]
line = view.line(pos - 1)
if pos == end and re.match(r'^( )+;-{2,}$', view.substr(line)):
view.insert(edit, pos, "-" * (54 - col))
else:
view.insert(edit, pos, " "*(tabSize - col % tabSize))
return
for region in regions:
lines = view.lines(region)
if len(lines)!=1:
view.run_command("indent")
return
pos = region.begin()
while view.substr(pos) == " ": pos+=1
while pos > 0:
if view.substr(pos - 1) in follow:
break
pos -= 1
width = visual_width(view, pos, tabSize)
origin.append((pos, width))
if maxwidth < width:
maxwidth = width + width % 2
origin.sort()
count = 0
for (pos, width) in origin:
width = maxwidth - width
if width>0:
view.insert(edit, pos+offset, " " * width)
count+=1
offset+=width
if count == 0:
for (pos, width) in origin:
if(width != -1):
view.insert(edit, pos+offset, " ")
offset+=1
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ZxxLang/ZxxSublime.git
git@gitee.com:ZxxLang/ZxxSublime.git
ZxxLang
ZxxSublime
ZxxSublime
master

搜索帮助