Skip to content

Instantly share code, notes, and snippets.

@shisashi
Last active December 19, 2017 12:47
Show Gist options
  • Save shisashi/d31a241e73ac1f3d5c0413a0b288db23 to your computer and use it in GitHub Desktop.
Save shisashi/d31a241e73ac1f3d5c0413a0b288db23 to your computer and use it in GitHub Desktop.
view から、行コメントの行を除いてクリップボードにコピーするプラグイン。keymap に copy-without-comment を登録して使ってね
import sublime, sublime_plugin
import io
# コメントを除いてコピーするやつ
# comment.py から持ってきた
def build_comment_data(view, pt):
shell_vars = view.meta_info("shellVariables", pt)
if not shell_vars:
return None
# transform the list of dicts into a single dict
all_vars = {}
for v in shell_vars:
if 'name' in v and 'value' in v:
all_vars[v['name']] = v['value']
# transform the dict into a single array of valid comments
suffixes = [""] + ["_" + str(i) for i in range(1, 10)]
for suffix in suffixes:
start = all_vars.setdefault("TM_COMMENT_START" + suffix)
end = all_vars.setdefault("TM_COMMENT_END" + suffix)
disable_indent = all_vars.setdefault("TM_COMMENT_DISABLE_INDENT" + suffix)
if start and not end:
return start.strip()
return None
class CopyWithoutCommentCommand(sublime_plugin.TextCommand):
def run(self, edit):
# 一応、行ごとにコメントにする方法が違うかもしれないけど、気にしない方向で
line_comment_start = build_comment_data(self.view, 0)
with io.StringIO() as buf:
for line_reg in self.view.lines(sublime.Region(0, self.view.size())):
line_str = self.view.substr(line_reg)
if line_comment_start:
pos = line_str.find(line_comment_start)
if pos == -1:
# コメントが無いならそのまま
buf.write(line_str)
buf.write('\n')
else:
# コメントがあれば、そこを削ってから、rstrip して、空行じゃなければコピー対象に追加
line_str = line_str[:pos]
line_str = line_str.rstrip()
if line_str:
buf.write(line_str)
buf.write('\n')
else:
# コメント書式が無いならそのまま
buf.write(line_str)
buf.write('\n')
self.view.window().status_message("copy without comment Done!")
sublime.set_clipboard(buf.getvalue())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment