Sublime Forum

Change language = lost focus = annoying behavior

#1

Hi,
I use two languages and often switch between them. Also I like to use next options

“trim_automatic_white_space”: true,
“trim_trailing_white_space_on_save”: true

In current version of Sublime Text 3103 and ubuntu 14.04 switch language action triggered lost focus actions and this and two options lead to annoying behavior.

For example, I have tab stops at the beginning of line, start typing, seeing what I start typing with wrong language, press backspace to delete symbols, switch between languages and Sublime Text lost focus, it is triggering save action what triggering trim trailing white space and my cursor go to the beginning of line.

Can this behavior be fixed?

0 Likes

#2

set "save_on_focus_lost": false, or switch language before backspacing?

0 Likes

#3

I had a similar issue with the viewport moving on my custom on_pre_save listener.

The viewport would shift after running expand_tabs or unexpand_tabs, so I added some code to retain the original viewport position.

 

In the following script, I also included some code to retain original selections & focus:

import sublime, sublime_plugin

class EventListener ( sublime_plugin.EventListener ):

	def on_pre_save ( self, view ):

		window = view.window()
		x, y   = view.viewport_position()

		selections      = view.sel()
		savedSelections = []

		for selection in selections:
			savedSelections.append ( selection )

		view.selection.clear()

		convert_spaces_to_tabs = view.settings ().get ( "convert_spaces_to_tabs_on_save" )
		indent_using_spaces    = view.settings ().get ( "translate_tabs_to_spaces" )

		if  convert_spaces_to_tabs == 1 \
		and indent_using_spaces    == 1:
			view.run_command ( "expand_tabs" )
		else:
			view.run_command ( "unexpand_tabs" )

		window.focus_view ( view )
		view.set_viewport_position ( [ 0, y ], animate = False )
		view.selection.add_all ( savedSelections )

 
The only problem is, I couldn’t figure out how to get it to activate only after a syntax change.

I tried [on|post]_window_command, & [on|post]_text_command but none of them were executed after:
set_file_type {"syntax": "Packages/_____/_____.sublime-syntax"}

Any ideas @kingkeith ?

0 Likes

#4
import sublime, sublime_plugin

class FicoListener(sublime_plugin.EventListener):
    def on_post_text_command(self, view, cmd, args):
        print(cmd, args)

works for me and prints: set_file_type {'syntax': 'Packages/___/___.sublime-syntax'}

it’s another place where the official API reference is wrong, unfortunately. If the community docs don’t correct it, then we need to log an issue.

1 Like

#5

That’s exactly what I tried before…

I think I might have mistaken the print for console output :sweat_smile:

1 Like

#6

Actually, probably a combination of my previously mentioned error & the fact that on_post_text_command is incorrectly listed as post_text_command @ the official API… :unamused:

1 Like

#7

@vova

 

This plugin will intercept any syntax-changing commands, and run the included script in their place.

The benefit is that you can control the order of commands and [ save | restore ] things like cursor & viewport position.

 

You may need to modify it slightly, as I don’t entirely understand the issue you’re running into.

 

import sublime, sublime_plugin

class EventListener ( sublime_plugin.EventListener ):

	def on_text_command ( self, view, cmd, args ):

		if  cmd == "set_file_type" \
		and "syntax" in args:

			window = view.window()
			x, y   = view.viewport_position()

			view.run_command ( cmd, args )

			selections      = view.sel()
			savedSelections = []

			for selection in selections:
				savedSelections.append ( selection )

			view.selection.clear()

			convert_spaces_to_tabs = view.settings ().get ( "convert_spaces_to_tabs_on_save" )
			indent_using_spaces    = view.settings ().get ( "translate_tabs_to_spaces" )

			if  convert_spaces_to_tabs == 1 \
			and indent_using_spaces    == 1:
				view.run_command ( "expand_tabs" )
			else:
				view.run_command ( "unexpand_tabs" )

			window.focus_view ( view )
			view.set_viewport_position ( [ 0, y ], animate = False )
			view.selection.add_all ( savedSelections )

			return ( "___null_command___", "___null_args___" )

 
( thanks @kingkeith for pointing out my dumb oversight :grin: )

1 Like

#8

also see this thread for other ways to detect syntax changes to a view

0 Likes