Skip to content

Commit

Permalink
Avoid echoing the received text back to the browser (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaste authored Feb 18, 2023
1 parent 15d92a0 commit d88e03d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
5 changes: 3 additions & 2 deletions GhostText.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ def __init__(self, settings, current_view):
def on_message(self, text):
try:
request = json.loads(text)
self._current_view.run_command('replace_content', request)
self._current_view.window().focus_view(self._current_view)
with OnSelectionModifiedListener.disabled(self._current_view):
self._current_view.run_command('replace_content', request)
self._current_view.window().focus_view(self._current_view)
except ValueError as e:
Utils.show_error(e, 'Invalid JSON')

Expand Down
19 changes: 18 additions & 1 deletion GhostTextTools/OnSelectionModifiedListener.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from contextlib import contextmanager

import sublime
from sublime_plugin import EventListener
import json
Expand All @@ -8,9 +10,14 @@ class OnSelectionModifiedListener(EventListener):
Handles content changes, each changes gets send with the given web socket server to the client.
"""
_bind_views = {}
_disabled = set()

def on_selection_modified(self, view):
if view.id() not in OnSelectionModifiedListener._bind_views:
vid = view.id()
if (
vid not in OnSelectionModifiedListener._bind_views
or vid in self._disabled
):
return

changed_text = view.substr(sublime.Region(0, view.size()))
Expand All @@ -24,6 +31,16 @@ def on_selection_modified(self, view):

OnSelectionModifiedListener._bind_views[view.id()].send_message(response)

@classmethod
@contextmanager
def disabled(cls, view):
vid = view.id()
cls._disabled.add(vid)
try:
yield
finally:
cls._disabled.remove(vid)

def on_close(self, view):
if view.id() not in OnSelectionModifiedListener._bind_views:
return
Expand Down

0 comments on commit d88e03d

Please sign in to comment.