Sublime Forum

View.replace() not working in build 4169

#1

I have developed a plugin to format some javascript code nested in a JSON object (itself nested in an XML file). As part of the plugin, I find all instances of “\\n” and replace with “\n”.

Upon updating Sublime to 4169 this replacement does not work. It’s working fine in 4152. See the code sample below that is causing the problem:

	found = newfile.find_all('\\\\n')
	for region in reversed(found):		
		newfile.replace(edit, region, '\n')

find_all() correctly returns a list of regions, but replace() doesn’t do any replacement.

0 Likes

#2

Is the edit object used from within the TextCommand.run method? If not, it’s not supposed to work.

1 Like

#3
1 Like

#4

Yeah, it’s in TextCommand.run. I’ve been using it for about 2 years now and it has only broken sometime between 4152 and 4169.

0 Likes

#5

Please see the linked post by kingkeith. An edit token is only valid for one single view and since 4158 ST properly enforces this.

2 Likes

#6

Is it possible then to do a search and replace in a different view? I see the other post has a method for inserting text, but I am unsure how to do a replace.

0 Likes

#7

You’ll need to do so in a separate text command so you can get an edit token for the new view.

1 Like

#8

OK. Thanks for your help. Pardon my ignorance, as I haven’t touched plugins since I wrote this 2 years ago.

If I create a new class to operate on the view in the same .py file and then do something like this:

newfile = self.view.window().new_file()
newfile.run_command('paste')

then how do I call my new class?

0 Likes

#9

You would do:

newfile.run_command('my_other_command_here')

Where the other class is a TextCommand named MyOtherCommandHereCommand (but of course, use whatever name you like).

You would take whatever logic you were using in the original command and put it into the run() method of the other command, where the edit will apply directly to that particular file, so you can use it to make the modifications you want.

1 Like

#10

Thank you so much for this. A job for tomorrow!

0 Likes

#11

I’m struggling in getting the new class to run. I have it in the same file as the original. And this is what I’m doing:

class snReplaceLineBreaksCommand(sublime_plugin.TextCommand):
	FLAGS = {
		"LITERAL": sublime.LITERAL,
		"IGNORECASE": sublime.IGNORECASE
	}

	def run(self, edit):
		print("I'm here")
		self.view.run_command('pretty_json')
		# Other replacements below here removed for brevity

then the original class looks like this:

class snGetTextCommand(sublime_plugin.TextCommand):

	FLAGS = {
		"LITERAL": sublime.LITERAL,
		"IGNORECASE": sublime.IGNORECASE
	}


	def run(self, edit):
		newfile = self.view.window().new_file()
		newfile.run_command('paste')
		newfile.run_command('snReplaceLineBreaks')

But it fails to fire the snReplaceLineBreaks command (I’ve tried calling snReplaceLineBreaksCommand too)

0 Likes

#12

The command name is snake_case of the class name, without trailing Command. So, sn_replace_line_breaks.

2 Likes

#13

And I’m in business!

Thanks to all that commented - it’s been a long time since I wrote this and my python skills a a little rusty.

0 Likes