Example #1
0
def complete(choices, options = {}):
    
    if '2' not in textmate.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'] = textmate.current_word(characters, "left")

    command = [textmate.DIALOG, "popup", "--returnChoice"]
    if "initial_filter" in options and options['initial_filter']:
        command.append("--alreadyTyped %s" % textmate.sh_escape(options["initial_filter"]))
    if "static_prefix" in options and options['static_prefix']:
        command.append("--staticPrefix %s" % textmate.sh_escape(options["static_prefix"]))
    if "extra_chars" in options and options['extra_chars']:
        command.append("--additionalWordCharacters %s" % textmate.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:
        textmate.exit_show_tool_tip('ERROR: %s' % e)
    finally:
        f.close()
Example #2
0
def main():
    # TODO: Determine if this is necessary. Can we still provide basic completion in a 'standalone' file?
    if textmate.PROJECT_DIRECTORY is None:
        textmate.exit_show_tool_tip('No completions.')

    source = sys.stdin.read()

    #from rope.contrib import autoimport
    project = Project(textmate.PROJECT_DIRECTORY)
    #autoimport = autoimport.AutoImport(project)
    resource = project.get_resource(textmate.FILEPATH.replace(textmate.PROJECT_DIRECTORY, '')[1:])
    #project.validate(self.project_path)
    caret_index = source.find(textmate.CURRENT_LINE) + textmate.LINE_INDEX

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

    proposals = codeassist.code_assist(project, source, caret_index, resource)
    
    try:
        if len(proposals) == 0:
            raise 'no proposals found'
    except:
        textmate.exit_show_tool_tip("No completions.")
    
    if len(proposals) == 1:
        textmate.exit_insert_text(proposals[0].name.replace(current_word, '', 1))
    else:
        proposals = codeassist.sorted_proposals(proposals)
        #autoimport.generate_cache()
        #autoimport.generate_modules_cache(modules)
        #project.pycore.analyze_module(resource)
        names = [proposal.name for proposal in proposals]
        #if self.starting.strip() and '.' not in self.expression:
        #        import_assists = self.autoimport.import_assist(self.starting)
        #        names.extend(x[0] + ' : ' + x[1] for x in import_assists)

        #plist = "{ menuItems=(%s);}"
        ui.complete(names, {'initial_filter': current_word, 'extra_chars': "_"})