Sublime Forum

Boost Library issue mismatch with function view.add_regions

#1

Hey everyone,
I am new to plugin development and trying to validate some details. It’s a learning project. My whole idea to mark out all invalid data as per my validation. Here is my code

import sublime, sublime_plugin
import logging
import os
import urllib
import urllib2
import json
 
class ExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
    	user = os.environ.get('USER')
    	log_file = "/Users/$USER/Desktop/fuck.txt".replace("$USER",str(user))
    	logging.basicConfig(filename=log_file,level=logging.DEBUG)
        region = sublime.Region(0, self.view.size())
        logging.debug('Beautifying Entire File')
        rawcode = self.view.substr(region)
        str_rawcode = str(rawcode)
        headers = {'content-type': "application/json"}
        request = urllib2.Request("http://localhost:8080/validation/client-spec/",str_rawcode,headers)
        response = urllib2.urlopen(request)
        html = response.read()
        logging.info(" respose info ::")
        logging.info(html)
        json_data = json.loads(html)
        for key,value in json_data.iteritems():
            logging.info("key: {key} | value: {value}".format(key=key, value=value))
            if value=="invalid":
                content = self.view.substr(sublime.Region(0, self.view.size()))
                begin = content.find(key)
                if begin == -1:
                    return
                end = begin + len(key)
                target_region = sublime.Region(begin, end)
                self.view.sel().clear()
                self.view.add_regions('sublimelinter-annotations.error-gutter-marks',target_region,scope='sublimelinter.mark.annotations.error', icon='dot', flags=sublime.INHIBIT_WORD_COMPLETIONS)

When I execute command view.run_command('example') I get following error. Please help me out. I am using sublime 2.

File "./learn.py", line 34, in run
    self.view.add_regions('sublimelinter-annotations.error-gutter-marks',target_region,scope='sublimelinter.mark.annotations.error', icon='dot', flags=sublime.INHIBIT_WORD_COMPLETIONS)
Boost.Python.ArgumentError: Python argument types in
    View.add_regions(View, Edit, str, Region)
did not match C++ signature:
    add_regions(SP<TextBufferView>, std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >, boost::python::list, std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >, std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >)
    add_regions(SP<TextBufferView>, std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >, boost::python::list, std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >, int)
    add_regions(SP<TextBufferView>, std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >, boost::python::list, std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >)
    add_regions(SP<TextBufferView>, std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >, boost::python::list, std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >, std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >, int)
0 Likes

#2

The regions should be a list, i.e. self.view.add_regions('sublimelinter-...', [target_region], ...). You may also consider to switch to ST3, because it has a better and more error resistant API.

2 Likes

#3

Getting to build plugin for Sublime Text? Great!! You’ll see, it’ll save you so much time. Speaking about time, you should bind a shortcut to your command, instead of typing view.run_command('example') every time.

A few notes:

Paste your python code like this

 ```python
 print('Like this, you get the coloration syntaxic')
 ```

 ```php
 echo 'same for php, for example';
 ```

Python

Instead of using str.find (content.find for example), you should use the in and not in operators, because you don’t need the index.

In python, the convention is two put spaces before and after operators:

# yes
if nb % 2 == 1:
    print('nb is an odd number')

# no
if nb%2==1:
    print('nb is an odd number')

:+1:

0 Likes

#4

Thanks @r-stein it worked. :slightly_smiling:

0 Likes