From c10bc95e543ca6101e1ef6b50f2c52bc906de743 Mon Sep 17 00:00:00 2001 From: Mathieu PATUREL Date: Sat, 16 Nov 2019 12:44:41 +1100 Subject: [PATCH] remove utils.py file; use a setting file for delay between updates utils.py contained two functions. One which wasn't used, and another, get_settings() which I moved to MarkdownLivePreview.py Add an entry to the command palette to edit the settings --- MarkdownLivePreview.py | 61 ++++++++++++++++------------ MarkdownLivePreview.sublime-commands | 8 ++++ MarkdownLivePreview.sublime-settings | 4 ++ live-testing/test.md | 10 +---- utils.py | 27 ------------ 5 files changed, 47 insertions(+), 63 deletions(-) create mode 100644 MarkdownLivePreview.sublime-settings delete mode 100644 utils.py diff --git a/MarkdownLivePreview.py b/MarkdownLivePreview.py index 848511b..f5ebb04 100644 --- a/MarkdownLivePreview.py +++ b/MarkdownLivePreview.py @@ -1,3 +1,12 @@ +""" +Terminology +original_view: the view in the regular editor, without it's own window +markdown_view: the markdown view, in the special window +preview_view: the preview view, in the special window +original_window: the regular window +preview_window: the window with the markdown file and the preview +""" + import os.path import sublime import sublime_plugin @@ -5,44 +14,22 @@ import sublime_plugin from functools import partial from .markdown2html import markdown2html -from .utils import * MARKDOWN_VIEW_INFOS = "markdown_view_infos" PREVIEW_VIEW_INFOS = "preview_view_infos" -# FIXME: put this as a setting for the user to choose? -DELAY = 100 # ms - - -def get_resource(resource): - path = "Packages/MarkdownLivePreview/resources/" + resource - abs_path = os.path.join(sublime.packages_path(), "..", path) - if os.path.isfile(abs_path): - with open(abs_path, "r") as fp: - return fp.read() - return sublime.load_resource(path) - +SETTING_DELAY_BETWEEN_UPDATES = "delay_between_updates" resources = {} def plugin_loaded(): + global DELAY resources["base64_404_image"] = get_resource("404.base64") resources["base64_loading_image"] = get_resource("loading.base64") resources["stylesheet"] = get_resource("stylesheet.css") - - -# try to reload the resources if we save this file -try: - plugin_loaded() -except OSError: - pass - -# Terminology -# original_view: the view in the regular editor, without it's own window -# markdown_view: the markdown view, in the special window -# preview_view: the preview view, in the special window -# original_window: the regular window -# preview_window: the window with the markdown file and the preview + # FIXME: how could we make this setting update without restarting sublime text + # and not loading it every update as well + DELAY = get_settings().get(SETTING_DELAY_BETWEEN_UPDATES) class MdlpInsertCommand(sublime_plugin.TextCommand): @@ -237,3 +224,23 @@ class MarkdownLivePreviewListener(sublime_plugin.EventListener): ) ] ) + + +def get_settings(): + return sublime.load_settings("MarkdownLivePreview.sublime-settings") + + +def get_resource(resource): + path = "Packages/MarkdownLivePreview/resources/" + resource + abs_path = os.path.join(sublime.packages_path(), "..", path) + if os.path.isfile(abs_path): + with open(abs_path, "r") as fp: + return fp.read() + return sublime.load_resource(path) + + +# try to reload the resources if we save this file +try: + plugin_loaded() +except OSError: + pass diff --git a/MarkdownLivePreview.sublime-commands b/MarkdownLivePreview.sublime-commands index cdca348..f670328 100644 --- a/MarkdownLivePreview.sublime-commands +++ b/MarkdownLivePreview.sublime-commands @@ -3,5 +3,13 @@ { "caption": "MarkdownLivePreview: Open Preview", "command": "open_markdown_preview" + }, + { + "caption": "MarkdownLivePreview: Open Settings", + "command": "edit_settings", "args": + { + "base_file": "${packages}/MarkdownLivePreview/MarkdownLivePreview.sublime-settings", + "default": "{\n\t$0\n}\n" + }, } ] \ No newline at end of file diff --git a/MarkdownLivePreview.sublime-settings b/MarkdownLivePreview.sublime-settings new file mode 100644 index 0000000..9715073 --- /dev/null +++ b/MarkdownLivePreview.sublime-settings @@ -0,0 +1,4 @@ +{ + // minimum number of milliseconds to wait before updating the preview again + "delay_between_updates": 100 +} \ No newline at end of file diff --git a/live-testing/test.md b/live-testing/test.md index 2185d2b..2be7c65 100644 --- a/live-testing/test.md +++ b/live-testing/test.md @@ -1,9 +1 @@ -``` -hello world -``` - -``` -second part -``` - -hello world \ No newline at end of file +# hello world diff --git a/utils.py b/utils.py deleted file mode 100644 index c89ab72..0000000 --- a/utils.py +++ /dev/null @@ -1,27 +0,0 @@ -# import sublime -import time - - -def get_settings(): - return sublime.get_settings("MarkdownLivePreview.sublime-settings") - - -def min_time_between_call(timeout, on_block=lambda *args, **kwargs: None): - """ Enforces a timeout between each call to the function - timeout is in seconds - """ - last_call = 0 - - def outer(func): - def wrapper(*args, **kwargs): - nonlocal last_call - - if time.time() - last_call < timeout: - time.sleep(timeout - (time.time() - last_call)) - - last_call = time.time() - return func(*args, **kwargs) - - return wrapper - - return outer