예제 #1
0
    def apply_change(self, change):
        """Apply a change to the document."""
        text = change["text"]
        change_range = change.get("range")

        text_bytes = text.encode(Document.encoding, "replace")

        if not change_range:
            # The whole file has changed
            raise AssertionError
            # if len(text_bytes) < thin.Files_Map.Get_Buffer_Length(self._fe):
            #    xxxx_replace
            # else:
            #    xxxx_free
            #    xxxx_allocate
            # return

        start_line = change_range["start"]["line"]
        start_col = change_range["start"]["character"]
        end_line = change_range["end"]["line"]
        end_col = change_range["end"]["character"]

        status = files_map_editor.Replace_Text(
            self._fe,
            start_line + 1,
            start_col,
            end_line + 1,
            end_col,
            ctypes.c_char_p(text_bytes),
            len(text_bytes),
        )
        if status:
            return

        # Failed to replace text.
        # Increase size
        self.gap_size *= 2
        fileid = files_map.Get_File_Name(self._fe)
        dirid = files_map.Get_Directory_Name(self._fe)
        buf_len = files_map.Get_File_Length(
            self._fe) + len(text_bytes) + self.gap_size
        files_map.Discard_Source_File(self._fe)
        new_sfe = files_map.Reserve_Source_File(dirid, fileid, buf_len)
        files_map_editor.Copy_Source_File(new_sfe, self._fe)
        files_map.Free_Source_File(self._fe)
        self._fe = new_sfe
        status = files_map_editor.Replace_Text(
            self._fe,
            start_line + 1,
            start_col,
            end_line + 1,
            end_col,
            ctypes.c_char_p(text_bytes),
            len(text_bytes),
        )
        assert status
예제 #2
0
파일: document.py 프로젝트: FisherG1/ghdl-1
 def load(source, dirname, filename):
     # Write text to file buffer.
     src_bytes = source.encode(Document.encoding, "replace")
     src_len = len(src_bytes)
     buf_len = src_len + Document.initial_gap_size
     fileid = name_table.Get_Identifier(filename.encode('utf-8'))
     if os.path.isabs(filename):
         dirid = name_table.Null_Identifier
     else:
         dirid = name_table.Get_Identifier(dirname.encode('utf-8'))
     sfe = files_map.Reserve_Source_File(dirid, fileid, buf_len)
     files_map_editor.Fill_Text(sfe, ctypes.c_char_p(src_bytes), src_len)
     return sfe