'''
import sublime, sublime_plugin
import os, sys
import thread
import subprocess
import functools
import time
import inspect

class privatize_var(sublime_plugin.TextCommand):
    def run(self, edit):
		body = self.view.substr(sublime.Region(0, self.view.size())
		view.insert(edit, 0, 'hi\n')
		view.insert(edit, 0, 'more stuff\n')
		view.end_edit(edit)
'''
import sublime
import sublime_plugin
import urllib
import urllib2
import threading
import re


class privatize_var(sublime_plugin.TextCommand):
    def run(self, edit):
        braces = False
        sels = self.view.sel()
        for sel in sels:
            if self.view.substr(sel).find('_') != -1:
                braces = True

        if not braces:
            new_sels = []
            for sel in sels:
                new_sels.append(self.view.find(' ', sel.end()))
            sels.clear()
            for sel in new_sels:
                sels.add(sel)
            self.view.run_command("expand_selection", {"to": "brackets"})

        threads = []
        for sel in sels:
            string = self.view.substr(sel)
            thread = privatize_varApiCall(sel, string, 5)
            threads.append(thread)
            thread.start()

        self.view.sel().clear()

        edit = self.view.begin_edit('privatize_var')

        self.handle_threads(edit, threads, braces)