Skip to content

Commit

Permalink
Set initial cursor position (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronnix authored Mar 18, 2024
1 parent 8ace809 commit baa52df
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
13 changes: 10 additions & 3 deletions GhostText.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,17 @@ class ReplaceContentCommand(TextCommand):
"""

def run(self, edit, **args):
self.view.replace(edit, sublime.Region(0, self.view.size()), args['text'])
# Temporarily disable conversion of tabs to spaces before
# pasting text, so that the selections stay valid
old_setting = self.view.settings().get('translate_tabs_to_spaces')
try:
self.view.settings().set('translate_tabs_to_spaces', False)
self.view.replace(edit, sublime.Region(0, self.view.size()), args['text'])
finally:
self.view.settings().set('translate_tabs_to_spaces', old_setting)

text_length = len(args['text'])
self.view.sel().clear()

if 'selections' in args and len(args['selections']) > 0:
selection = args['selections'][0]
self.view.sel().add(sublime.Region(selection['start'], selection['end']))
Expand All @@ -112,7 +119,7 @@ def on_message(self, text):
window_helper = WindowHelper()
syntax = Utils.get_syntax_by_host(request['url'])
current_view = window_helper.add_file(
request['title'] + '.' + syntax, request['text']
request['title'] + '.' + syntax, request['text'], request.get('selections')
)
OnSelectionModifiedListener.bind_view(current_view, self._web_socket_server)
self._web_socket_server.on_message(OnMessage(self._settings, current_view))
Expand Down
4 changes: 2 additions & 2 deletions GhostTextTools/WindowHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def __init__(self):
self._view_disconnected_prefix = Utils.get_view_prefix('disconnected')
self._view_connected_prefix = Utils.get_view_prefix('connected')

def add_file(self, title, text):
def add_file(self, title, text, selections):
"""
Creates a new file and adds the given text content to it.
"""
Expand All @@ -22,7 +22,7 @@ def add_file(self, title, text):
'{} {}'.format(self._view_connected_prefix, os.path.splitext(title)[0])
)
view.set_status('title', title)
view.run_command('replace_content', {'text': text})
view.run_command('replace_content', {'text': text, 'selections': selections})
view.set_scratch(True)

return view
Expand Down

0 comments on commit baa52df

Please sign in to comment.