Sublime Forum

Noobie question here

#1

Hello,

I haven’t worked with python or c++ before and i’ve run into an error I can’t figure out in an html entities conversion plugin i’m trying to write.

here’s the error:

Traceback (most recent call last):
  File "./sublime_plugin.py", line 255, in run_
  File "./unicodeToEntities.py", line 19, in run
    self.view.replace(region, txt)
Boost.Python.ArgumentError: Python argument types in
    View.replace(View, Region, unicode)
did not match C++ signature:
    replace(SP<TextBufferView>, SP<Edit>, SelectionRegion, std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >)

and here’s my code:

[code]import sublime, sublime_plugin

from htmlentitydefs import codepoint2name

class UnicodeToEntitiesCommand(sublime_plugin.TextCommand):
def run(self, edit):
htmlentities = list()
for region in self.view.sel():
if not region.empty():
s = self.view.substr(region)
for c in s:
if ord© < 128:
htmlentities.append©
else:
htmlentities.append(’&%s;’ % codepoint2name[ord©])

	txt = ''.join(htmlentities)
	print txt, "txt"
	self.view.replace(region, txt)

[/code]

Could any kind person help me out with this?

Thanks.

0 Likes

#2

[quote=“deafmetal”]

[code]import sublime, sublime_plugin

from htmlentitydefs import codepoint2name

class UnicodeToEntitiesCommand(sublime_plugin.TextCommand):
def run(self, edit):
htmlentities = list()
for region in self.view.sel():
if not region.empty():
s = self.view.substr(region)
for c in s:
if ord© < 128:
htmlentities.append©
else:
htmlentities.append(’&%s;’ % codepoint2name[ord©])

	txt = ''.join(htmlentities)
	print txt, "txt"
	self.view.replace(region, txt)

[/code]

Could any kind person help me out with this?

Thanks.[/quote]

In SublimeText 2 you have to pass the edit object to the replace method, like the error message tell you:

self.view.replace(edit, region, txt)

And it’s probably not safe to use the region variable outside the for loop, you better use a new local variable.
By the way your region variable reference the last selected region, is it what you want to do or is this an indentation error ?

Good luck.

0 Likes

#3

Thanks bizoo, that was exactly what i was looking for. Also yes, that was a mistake only using the last selected region.

Here is the revised plugin code:

[code]import sublime, sublime_plugin

from htmlentitydefs import codepoint2name

class UnicodeToEntitiesCommand(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
htmlentities = list()
if not region.empty():
s = self.view.substr(region)
for c in s:
if ord© < 128:
htmlentities.append©
else:
htmlentities.append(’&%s;’ % codepoint2name[ord©])

			txt = u''.join(htmlentities)
			self.view.replace(edit, region, txt)

[/code]

0 Likes