def run(self):
sublime.run_command('new_window')
new_window = sublime.active_window()
new_window.run_command('set_layout', {
'cols': [0.0, 0.5, 1.0],
'rows': [0.0, 1.0],
'cells': [[0, 0, 1, 1], [1, 0, 2, 1]]
})
new_window.focus_group(0)
edit_view = new_window.new_file()
edit_view_settings = edit_view.settings()
edit_view_settings.set('minihtml_preview_edit_view', True)
edit_view.set_scratch(True)
edit_view.set_syntax_file('Packages/HTML/HTML.sublime-syntax')
# Unfortunately Sublime indents on 'insert'
edit_view.settings().set('auto_indent', False)
edit_view.run_command('insert', {'characters': template})
edit_view.settings().set('auto_indent', True)
new_window.focus_group(1)
output_view = new_window.new_file()
output_view.set_scratch(True)
edit_view_settings.set('minihtml_preview_output_view_id',
output_view.id())
new_window.focus_group(0)
python类run_command()的实例源码
def run(self):
self.window = sublime.active_window()
for view in self.window.views():
self.view = view
if not self.will_closing_discard(view):
continue
self.last_focused_view = self.window.active_view()
self.window.focus_view(view)
self.show_discard_prompt()
return # wait for input, then start over
self.window.run_command('close_window')
def run(self):
for win in sublime.windows():
self.window = win
for view in self.window.views():
self.view = view
if not self.will_closing_discard(view):
continue
self.last_focused_view = self.window.active_view()
self.window.focus_view(view)
self.show_discard_prompt()
return # wait for input, then start over
sublime.run_command('exit')
#
# Rest of the commands
#
def ensure_reload():
"""
Ensure all modules reload to initialize plugin successfully.
"""
start_upgrade_msg = "Do not close the Sublime Text. Upgrading {}".format(
PACKAGE_NAME
)
finish_upgrade_msg = "{} upgrade finished.".format(PACKAGE_NAME)
active_view = sublime.active_window().active_view()
active_view.set_status("afi_status", start_upgrade_msg)
def erase_status():
active_view.erase_status("afi_status")
def reload():
sublime.run_command("afi_reload")
active_view.set_status("afi_status", finish_upgrade_msg)
sublime.set_timeout(erase_status, 2000)
sublime.set_timeout_async(reload, 5000)
def show_html(md_view, preview):
global windows_phantom_set
html = markdown2html(get_view_content(md_view), os.path.dirname(md_view.file_name()), md_view.settings().get('color_scheme'))
phantom_set = windows_phantom_set.setdefault(preview.window().id(),
sublime.PhantomSet(preview, 'markdown_live_preview'))
phantom_set.update([sublime.Phantom(sublime.Region(0), html, sublime.LAYOUT_BLOCK,
lambda href: sublime.run_command('open_url', {'url': href}))])
# lambda href: sublime.run_command('open_url', {'url': href})
# get the "ratio" of the markdown view's position.
# 0 < y < 1
y = md_view.text_to_layout(md_view.sel()[0].begin())[1] / md_view.layout_extent()[1]
# set the vector (position) for the preview
vector = [0, y * preview.layout_extent()[1]]
# remove half of the viewport_extent.y to center it on the screen (verticaly)
vector[1] -= preview.viewport_extent()[1] / 2
# make sure the minimum is 0
vector[1] = 0 if vector[1] < 0 else vector[1]
# the hide the first line
vector[1] += preview.line_height()
preview.set_viewport_position(vector, animate=False)
def typescript_update_structure(view, force):
return
project.assert_initialisation_finished()
def async_react(members, filename, sender_view_id):
## members is the already json-decoded tss.js answer
Debug('structure', 'STRUCTURE async_react for %s in start view %s, now view %s'
% (filename, view.id(), sublime.active_window().active_view().id()) )
if sublime.active_window().active_view().id() != sender_view_id or view.id() != sender_view_id:
Debug('structure', 'STRUCTURE async_react canceled because of view change')
return
view.run_command('typescript_outline_view_set_text', {"members": members} )
if T3SVIEWS.OUTLINE.is_active() and (force or not T3SVIEWS.OUTLINE.is_current_ts(view)):
Debug('structure', 'STRUCTURE for %s in view %s, active view is %s'
% (view.file_name(), view.id(), sublime.active_window().active_view().id()))
TSS.structure(view.file_name(), view.id(), async_react)
# OPEN and WRITE TEXT TO OUTLINE VIEW
def run(self, edit, characters):
project = get_or_create_project_and_add_view(self.view)
if project:
filename = self.view.file_name()
if not project.get_setting('activate_build_system'):
Debug('notify', "Build system is disabled.")
return
project.assert_initialisation_finished()
self.window = sublime.active_window()
if characters != False:
self.window.run_command('save')
project.compile_once(self.window, filename)
def on_request(self, request):
if len(sublime.windows()) == 0 or self.new_window_on_connect:
sublime.run_command('new_window')
if len(self.window_command_on_connect) > 0:
sublime.active_window().run_command(self.window_command_on_connect)
web_socket_server_thread = WebSocketServerThread(self._settings)
web_socket_server_thread.start()
while not web_socket_server_thread.get_server().get_running():
sleep(0.1)
port = web_socket_server_thread.get_server().get_port()
Utils.show_status('Connection opened')
return Response(json.dumps({"WebSocketPort": port, "ProtocolVersion": 1}),
"200 OK",
{'Content-Type': 'application/json'})
def _create_project(self):
os.makedirs(self.project_dir, exist_ok=True)
if not os.path.exists(self.project_dir):
sublime.error_message("Could not create directory %s" % self.project_dir)
os.makedirs(os.path.join(self.project_dir, "src"), exist_ok=True)
with open(os.path.join(self.project_dir, "CMakeLists.txt"), "w") as f:
f.write(CMAKELISTS_FILE.format(self.project_name, self.type))
with open(os.path.join(self.project_dir, "src", "main." + self.suffix), "w") as f:
if self.type == "C":
f.write(CFILE)
else:
f.write(CXXFILE)
with open(os.path.join(self.project_dir, "src", "CMakeLists.txt"), "w") as f:
f.write(CMAKELISTS_SRC_FILE.format(self.suffix))
project_file = os.path.join(self.project_dir, self.project_name + ".sublime-project")
with open(project_file, "w") as f:
f.write(PROJECTFILE)
if sublime.ok_cancel_dialog('Select the file %s in "%s" in the upcoming prompt...' % (self.project_name + ".sublime-project", self.project_dir)):
sublime.run_command("prompt_open_project_or_workspace")
def on_close(self, view):
"""Clean up the visualization.
"""
if 'text.sublundo.tree' not in view.scope_name(0):
return
w = sublime.active_window()
single = not w.views_in_group(0) or not w.views_in_group(1)
if w.num_groups() == 2 and single:
sublime.set_timeout(lambda: w.set_layout(
{
"cols": [0.0, 1.0],
"rows": [0.0, 1.0],
"cells": [[0, 0, 1, 1]]
}
), 300)
w.run_command('hide_panel', {'panel': 'output.sublundo'})
w.destroy_output_panel('output.sublundo')
for v in w.views():
v.erase_regions('sublundo')
def open_settings_window(self):
sublime.run_command("new_window")
settings_window = sublime.active_window()
left_right_group = {
"cols": [0.0, 0.5, 1.0],
"rows": [0.0, 1.0],
"cells": [[0, 0, 1, 1], [1, 0, 2, 1]]
}
settings_window.set_layout(left_right_group) # the left group is focused by default
settings_window.run_command("open_file", { "file": "${packages}/SpotifyWeb/" + self.__settings_file_name})
settings_window.focus_group(1) # focus the right group
settings_window.run_command("open_file", { "file": "${packages}/User/" + self.__settings_file_name})
def follow_link(self):
topic = self.extract_topic()
if topic is not None:
return sublime.run_command("hyper_help", {"topic": topic})
log("Cannot follow link; no link found under the cursor")
def on_text_command(self, view, command, args):
"""
Listen for double clicks in help files and, if they occur over links,
follow the link instead of selecting the text.
"""
if command == "drag_select" and args.get("by", None) == "words":
event = args["event"]
point = view.window_to_text((event["x"], event["y"]))
if view.match_selector(point, "text.hyperhelp meta.link"):
view.window().run_command("hyper_help_navigate", {"nav": "follow_link"})
return ("noop")
return None
def run(self, edit, event):
base_path, input_path = self.get_path(event)
abspath = computer_friendly(os.path.join(base_path, input_path))
sublime.run_command('fm_creater', {'abspath': abspath,
'input_path': input_path})
def transform_aliases(self, string):
"""Transform aliases using the settings and the default variables
It's recursive, so you can use aliases *in* your aliases' values
"""
def has_unescaped_dollar(string):
start = 0
while True:
index = string.find('$', start)
if index < 0:
return False
elif string[index-1] == '\\':
start = index + 1
else:
return True
string = string.replace('$$', '\\$')
vars = self.window.extract_variables()
vars.update(get_settings().get('aliases'))
inifinite_loop_counter = 0
while has_unescaped_dollar(string):
inifinite_loop_counter += 1
if inifinite_loop_counter > 100:
sublime.error_message("Infinite loop: you better check your "
"aliases, they're calling each other "
"over and over again.")
if get_settings().get('open_help_on_alias_infinite_loop',
True) is True:
sublime.run_command('open_url', {
'url': 'https://github.com/math2001/ '
'FileManager/wiki/Aliases '
'#watch-out-for-infinite-loops'
})
return string
string = sublime.expand_variables(string, vars)
return string
def on_post_save(self, view):
"""Reload FileManager
To use this, you need to have this plugin:
https://github.com/math2001/sublime-plugin-reloader"""
if not (os.path.dirname(__file__) in view.file_name() and
view.file_name().endswith('.py')):
return
sublime.run_command('reload_plugin', {
'main': __file__,
'folders': ["FMcommands"],
'scripts': ["input_for_path", "sublimefunctions",
"pathhelper"],
})
def on_load(self, view):
settings = view.settings()
snippet = settings.get('fm_insert_snippet_on_load', None)
if snippet:
view.run_command('insert_snippet', {'contents': snippet})
settings.erase('fm_insert_snippet_on_load')
if get_settings().get('save_after_creating'):
view.run_command('save')
if settings.get('fm_reveal_in_sidebar'):
view.window().run_command('reveal_in_side_bar')
def on_text_command(self, view, command, args):
if command not in ['undo', 'unindent'] or view.name() != 'FileManager::input-for-path':
return
settings = view.settings()
if command == 'unindent':
index = settings.get('completions_index')
settings.set('go_backwards', True)
view.run_command('insert', {'characters': '\t'})
return
# command_history: (command, args, times)
first = view.command_history(0)
if first[0] != 'fm_edit_replace' or first[2] != 1:
return
second = view.command_history(-1)
if ((second[0] != 'reindent') and
not (second[0] == 'insert' and second[1] == {'characters': '\t'})):
return
settings.set('ran_undo', True)
view.run_command('undo')
index = settings.get('completions_index')
if index == 0 or index is None:
settings.erase('completions')
settings.erase('completions_index')
else:
settings.set('completions_index', index - 1)
def destroy_view(self):
sublime.active_window().focus_view(self.view)
sublime.active_window().run_command("close")
self.view = None
self.closed = True
def do_add_line(self, line):
self.view.run_command("gdb_view_add_line", {"line": line, "doScroll": self.doScroll})
def do_fold_all(self, data):
self.view.run_command("fold_all")
def do_clear(self, data):
self.view.run_command("gdb_view_clear")
def do_scroll(self, data):
self.view.run_command("goto_line", {"line": data + 1})
def run(self):
global gdb_shutting_down
gdb_shutting_down = True
wait_until_stopped()
run_cmd("-gdb-exit", True)
# if gdb_server_process:
# gdb_server_process.terminate()
sublime.active_window().run_command("hide_panel", {"cancel": True})
def run(self, edit):
if gdb_variables_view.is_open() and self.view.id() == gdb_variables_view.get_view().id():
self.view.run_command("gdb_edit_variable")
else:
self.view.run_command("gdb_edit_register")
def force_close_view(view):
view.set_scratch(True)
win = view.window()
win.focus_view(view)
win.run_command('close')
def replace_view_text(view, new_text):
view.run_command('no_dialogs_replace_helper', {'new_text': new_text})
def run(self, _):
global next_completion
next_completion = True
self.view.run_command(settings.get('no_dialogs_right_arrow_default_command'), settings.get('no_dialogs_right_arrow_default_args'))
def resave(self):
mkdirp(self.view.file_name())
self.view.run_command('save')
def reopen_from_new_path(self):
force_close_view(self.view)
self.window.open_file(self.path)
self.window.run_command('hide_panel')