Sublime Forum

I got an error when develop a plugin(takes 2 positional arguments but 3 were given)

#1
import sublime
import sublime_plugin


class DocumentCommand(sublime_plugin.TextCommand):

	htmlTemplateHead = '''<!DOCTYPE HTML>
<html>
<head>
	<meta charset="utf-8">
	<title>Title of the document</title>
	<style>
	</style>
</head>
<body>'''

	htmlTemplateFoot = '''</body>\n</html>'''

	def run(self, edit):

		view = self.view

		# get current view content
		contentReg = sublime.Region(0, self.view.size())
		content = self.view.substr(contentReg);

		convertedResult = self.convert_content(content);

		self.tab = self.creat_tab(view)

		self.set_code(self.htmlTemplateHead)
		self.set_code(convertedResult)
		self.set_code(self.htmlTemplateFoot)

	def creat_tab(self, view):
		win = view.window()
		tab = win.new_file()
		tab.set_syntax_file('Packages/HTML/HTML.tmLanguage')
		return tab

	def set_code(self, code):
		tab = self.tab
		# tab.set_name('untitled.' + self.type)
		# insert codes
		tab.run_command('insert_snippet', {'contents': code})

	def convert_content(self, content):
		contentArray = content.split('\n')
		resultArray = []

		currentType = 'none'
		currentGroup = []

		for line in contentArray:
			if line.find('#') == 0 :
				result = self.wrap_content(currentType, currentGroup)
				resultArray.append(result)
				currentGroup = []
				currentType = line
			else :
				currentGroup.append(line)
			
		resutlStr = '\n'.join(resultArray)
		return resutlStr

	def wrap_content(self, type, group):
		if type.find('topic') >= 0:
			return self.wrap_topic(group)
		elif type.find('title') >= 0 :
			return self.wrap_title(group)
		elif type.find('content') >= 0 :
			return self.wrap_content(group)
		elif type.find('code') >= 0 :
			return self.wrap_code(group)
		elif type.find('img') >= 0 :
			return self.wrap_img(group)

	def wrap_topic(self, group):
		return '<div class="line"><span class="topic">' + group[0] + '</span></div>'

	def wrap_title(self, group):
		return '<div class="line"><span class="' + type.str[1:] + '">' + group[0] + '</span></div>'

	def wrap_content(self, group):
		result = '<div class="line">'
		suf = '</div>'
		
		for line in group :
			result + '<p>' + line + '</p>'

		return result + suf

	def wrap_code(self, group):
		result = '<div class="line"><pre>'
		suf = '</pre></div>'
		
		for line in group :
			result + '\n' + line

		return result + suf

	def wrap_img(self, group) :
		pre = '<div class="line"><img src="data:image/jpg;base64,'
		suf = '"></div>'
		return pre + group[0] + suf

and I get an error, when I run the plugin:

  File "C:\Users\weilu\AppData\Roaming\Sublime Text 3\Packages\HtmlDocumentHelper\HtmlDocumentHelper.py", line 27, in run
    convertedResult = self.convert_content(content);
  File "C:\Users\weilu\AppData\Roaming\Sublime Text 3\Packages\HtmlDocumentHelper\HtmlDocumentHelper.py", line 56, in convert_content
    result = self.wrap_content(currentType, currentGroup)
TypeError: wrap_content() takes 2 positional arguments but 3 were given

thanks for your help!

0 Likes

#2

It looks like Python doesn’t support this:

class DocumentCommand(sublime_plugin.TextCommand):

	def wrap_content(self, type, group):
		...
	def wrap_content(self, group):
		...

Ref: http://stackoverflow.com/questions/10202938/how-do-i-use-method-overloading-in-python

5 Likes

#4

thanks, it’s a mistake, I don’t want to use overloading.

0 Likes