Esempio n. 1
0
    def on_inspected(self, result):
        imp_module = None

        if self.view.is_dirty():
            # Use the buffer's contents
            pyresult = json.loads(result).get('result')
            if pyresult is not None:
                if Logging.is_log_level(Logging.LOG_DEBUG):
                    pprint.pprint(pyresult, width=80)
                modinfo = pyresult.get('module')
                if modinfo is not None:
                    imp_module = HsDevResultParse.parse_module(modinfo)
        else:
            # Otherwise, use the actual file
            imp_module = Utils.head_of(
                hsdev.client.module(file=self.current_file_name))

        if imp_module is not None:
            imports = sorted(imp_module.imports, key=lambda i: i.position.line)
            after = [i for i in imports if i.module > self.module_name]

            insert_line = 0
            insert_gap = False

            if len(after) > 0:
                # Insert before after[0]
                insert_line = after[0].position.line - 1
            elif len(imports) > 0:
                # Insert after all imports
                insert_line = imports[-1].position.line
            elif len(imp_module.declarations) > 0:
                # Insert before first declaration
                insert_line = min([
                    d.position.line for d in imp_module.declarations.values()
                ]) - 1
                insert_gap = True
            else:
                # Try to add the import just after the "where" of the module declaration
                contents = self.view.substr(sublime.Region(
                    0, self.view.size()))
                mod_decl = re.search('module.*where', contents, re.MULTILINE)
                if mod_decl is not None:
                    insert_line = mod_decl.end()
                    insert_gap = True
                else:
                    # Punt! Insert at the end of the file
                    insert_line = self.view.rowcol(self.view.size())[0]

            insert_text = 'import {0}\n'.format(
                self.module_name) + ('\n' if insert_gap else '')

            point = self.view.text_point(insert_line, 0)
            self.view.insert(self.edit, point, insert_text)

            Common.show_status_message(
                'Import {0} added'.format(self.module_name), True)
Esempio n. 2
0
    def contents_to_module(self, contents):
        imp_module = None
        hsinspect_proc = ProcHelper.exec_with_wrapper(self.exec_with, self.install_dir, ['hsinspect'])
        if hsinspect_proc.process is not None:
            exit_code, result, _ = hsinspect_proc.wait(input_str=contents)
            if exit_code == 0:
                pyresult = json.loads(result).get('result')
                if pyresult is not None:
                    if Logging.is_log_level(Logging.LOG_DEBUG):
                        pprint.pprint(pyresult, width=127)
                    modinfo = pyresult.get('module')
                    if modinfo is not None:
                        imp_module = ResultParse.parse_module(modinfo)

        return imp_module
Esempio n. 3
0
    def contents_to_module(self, contents):
        imp_module = None
        hsinspect_proc = ProcHelper.exec_with_wrapper(self.exec_with, self.install_dir, ['hsinspect'])
        if hsinspect_proc.process is not None:
            exit_code, result, _ = hsinspect_proc.wait(input_str=contents)
            if exit_code == 0:
                pyresult = json.loads(result).get('result')
                if pyresult is not None:
                    if Logging.is_log_level(Logging.LOG_DEBUG):
                        pprint.pprint(pyresult, width=127)
                    modinfo = pyresult.get('module')
                    if modinfo is not None:
                        imp_module = ResultParse.parse_module(modinfo)

        return imp_module