def execute(self):
        if tm.PROJECT_DIRECTORY is None:
            tm.exit_show_tool_tip('No imports found.')

        autoimport = self.get_auto_import()

        current_word = tm.current_word(r"[a-zA-Z_]*", 'both')

        if not current_word:
            tm.exit_show_tool_tip('No imports found.')

        proposals = autoimport.import_assist(current_word)
        result = None

        try:
            if len(proposals) == 0:
                raise 'no proposals found'
        except:
            tm.exit_show_tool_tip("No imports found.")

        if len(proposals) == 1:
            result = 'from %s import %s' % (proposals[0][1], proposals[0][0])
        else:
            names = []
            for name, module in proposals:
                names.append(('%s (%s)' % (name, module),
                    'from %s import %s' % (module, name)))

            result = tm.ui.menu(names)

        if result:
            self._insert_import(result, current_word)
        else:
            tm.exit_discard()
Exemplo n.º 2
0
def complete(choices, options = {}):
    
    if '2' not in tm.DIALOG:
        raise 'Dialog2 not found.'
    
    if 'initial_filter' not in options:
        characters = 'a-zA-Z0-9'
        if 'extra_chars' in options:
            characters += re.escape(options['extra_chars'])

        options['initial_filter'] = tm.current_word(characters, "left")

    command = [tm.DIALOG, "popup", "--returnChoice"]
    if "initial_filter" in options and options['initial_filter']:
        command.append("--alreadyTyped %s" % tm.sh_escape(options["initial_filter"]))
    if "static_prefix" in options and options['static_prefix']:
        command.append("--staticPrefix %s" % tm.sh_escape(options["static_prefix"]))
    if "extra_chars" in options and options['extra_chars']:
        command.append("--additionalWordCharacters %s" % tm.sh_escape(options['extra_chars']))
    if "case_insensitive" in options and options['case_insensitive']:
        command.append("--caseInsensitive")

    def formalize(choice):
        try:
            choice['display']
            return choice
        except (KeyError, IndexError, TypeError):
            return {'display': choice}

    choices = [formalize(choice) for choice in choices]
    
    plist = {'suggestions': choices}

    try:
        f = tempfile.NamedTemporaryFile()
        plistlib.writePlist(plist, f)
        f.seek(0)
        
        command = ' '.join(command).strip()
        
        process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
        process.stdin.write(f.read())
        process.stdin.close()
        f.close()

    except Exception as e:
        tm.exit_show_tool_tip('ERROR: %s' % e)
    finally:
        f.close()
    def _complete_import(self):
        autoimport = self.get_auto_import()

        current_word = tm.current_word(r"[a-zA-Z_]*", 'both')

        proposals = autoimport.import_assist(current_word)

        names = []
        name_re = re.compile(r'.*import\s+.*')
        for name, module in proposals:
            if name_re.match(tm.CURRENT_LINE):
                names.append(name)
            else:
                names.append(module)

        tm.ui.complete(names, {'initial_filter': current_word,
            'extra_chars': "_"})
    def _complete(self):
        caret_index = self.source.find(tm.CURRENT_LINE) + tm.LINE_INDEX
        project = self.get_project()
        resource = project.get_resource(tm.FILEPATH.replace(tm.PROJECT_DIRECTORY, '')[1:])


        current_word = tm.current_word(r"[a-zA-Z_]*", 'both')
        proposals = codeassist.code_assist(project, self.source, caret_index, resource)

        try:
            if len(proposals) == 0:
                raise 'no proposals found'
        except:
            tm.exit_show_tool_tip("No completions.")

        if len(proposals) == 1:
            tm.exit_insert_text(proposals[0].name.replace(current_word, '', 1))
        else:
            proposals = codeassist.sorted_proposals(proposals)
            names = [proposal.name for proposal in proposals]
            tm.ui.complete(names, {'initial_filter': current_word, 'extra_chars': "_"})