Esempio n. 1
0
    def fix_name(self, args):
        curr_file = vim_helper.get_current_file_path()
        modified = os.path.getmtime(curr_file)
        changed = os.path.getctime(curr_file)
        head_lines = self.vim.current.buffer[:100]
        head_string = '\n'.join(head_lines)

        def get_basename_ext(path):
            filename = os.path.basename(path)
            return os.path.splitext(filename)

        def rename_function(name):
            new_head = self.function_name_pattern.sub(
                r"\1{}\3".format(name), head_string).split('\n')
            for i, line in enumerate(new_head):
                if line != head_lines[i]:
                    self.vim.current.buffer[i] = line

        basename, ext = get_basename_ext(curr_file)

        if len(args) > 0:
            new_name, new_ext = get_basename_ext(args[0])
            new_ext = new_ext if len(new_ext) > 0 else ext
            rename_function(new_name)
            self.vim.command("Rename {}{}".format(new_name, new_ext))
            return

        if vim_helper.is_current_buffer_modified() or changed != modified:
            match = self.function_name_pattern.search(head_string)
            if match is None:
                return
            function_name = match.group(2)
            self.vim.command("Rename {}{}".format(function_name, ext))
        else:
            rename_function(basename)
Esempio n. 2
0
    def refresh_buffer(self):
        path = vim_helper.get_current_file_path()
        last_written = self.buffer_state[path]['last_written']
        if time.time() - last_written < 1 or not os.path.isfile(path):
            return

        modified = os.stat(path).st_mtime

        if modified > last_written + 8:
            row_col = vim_helper.get_cursor()
            vim_helper.edit_file(path)
            vim_helper.set_cursor(row_col)
Esempio n. 3
0
    def matlab_write_function_files(self):
        options = vim_helper.get_options()
        if 'split' in options:
            group_name = options['split'][0]
        else:
            return

        dir_path = os.path.join(
            os.path.dirname(vim_helper.get_current_file_path()), group_name)
        if not os.path.exists(dir_path):
            os.mkdir(dir_path)

        existing_filenames = [
            name for name in os.listdir(dir_path) if name.endswith('.m')
        ]

        function_blocks = vim_helper.get_function_blocks()
        new_filenames = [name + '.m' for name in function_blocks.keys()]

        unused_filenames = list(set(existing_filenames) - set(new_filenames))
        for name in unused_filenames:
            try:
                os.remove(os.path.join(dir_path, name))
            except:
                pass

        added_filenames = list(set(new_filenames) - set(existing_filenames))
        for name in added_filenames:
            content = function_blocks[os.path.splitext(name)[0]].strip()
            with open(os.path.join(dir_path, name), 'w') as f:
                f.write(content)

        common_filenames = set.intersection(
            *[set(existing_filenames),
              set(new_filenames)])
        for name in common_filenames:
            content = function_blocks[os.path.splitext(name)[0]].strip()
            code_hash = hashlib.md5(content).hexdigest()
            with open(os.path.join(dir_path, name), 'r+') as f:
                file_hash = hashlib.md5(f.read().strip()).hexdigest()
                f.seek(0)
                if code_hash != file_hash:
                    f.write(content)
                    f.truncate()
Esempio n. 4
0
    def matlab_write_function_files(self):
        options = vim_helper.get_options()
        if 'split' in options:
            group_name = options['split'][0]
        else:
            return

        dir_path = os.path.join(
            os.path.dirname(vim_helper.get_current_file_path()), group_name)
        if not os.path.exists(dir_path):
            os.mkdir(dir_path)

        existing_filenames = [name for name in os.listdir(dir_path) if
                              name.endswith('.m')]

        function_blocks = vim_helper.get_function_blocks()
        new_filenames = [name + '.m' for name in function_blocks.keys()]

        unused_filenames = list(set(existing_filenames) - set(new_filenames))
        for name in unused_filenames:
            try:
                os.remove(os.path.join(dir_path, name))
            except:
                pass

        added_filenames = list(set(new_filenames) - set(existing_filenames))
        for name in added_filenames:
            content = function_blocks[os.path.splitext(name)[0]].strip()
            with open(os.path.join(dir_path, name), 'w') as f:
                f.write(content)

        common_filenames = set.intersection(
            *[set(existing_filenames), set(new_filenames)])
        for name in common_filenames:
            content = function_blocks[os.path.splitext(name)[0]].strip()
            code_hash = hashlib.md5(content).hexdigest()
            with open(os.path.join(dir_path, name), 'r+') as f:
                file_hash = hashlib.md5(f.read().strip()).hexdigest()
                f.seek(0)
                if code_hash != file_hash:
                    f.write(content)
                    f.truncate()
Esempio n. 5
0
    def buf_write(self):
        path = vim_helper.get_current_file_path()
        self.buffer_state[path]['last_written'] = time.time()

        self.matlab_write_function_files()
Esempio n. 6
0
 def buf_delete(self):
     path = vim_helper.get_current_file_path()
     self.buffer_state.pop(path, None)
Esempio n. 7
0
 def matlab_cli_open_in_matlab_editor(self):
     if self.cli_controller is None:
         self.activate_cli()
     path = vim_helper.get_current_file_path()
     self.cli_controller.open_in_matlab_editor(path)
Esempio n. 8
0
 def set_last_written(self):
     path = vim_helper.get_current_file_path()
     self.buffer_state[path]['last_written'] = time.time()
Esempio n. 9
0
 def set_last_written(self):
     path = vim_helper.get_current_file_path()
     self.buffer_state[path]['last_written'] = time.time()