-
-
Notifications
You must be signed in to change notification settings - Fork 9
Read & Writes
Tonic manage file writes and save files using various techniques and optimizations to provide a smooth user experience. Here are some of the ways it handles file writes:
-
Buffering: Tonic use in-memory buffers to store changes made to a file. When a user makes changes to a file, the changes are stored in the buffer instead of being immediately written to the disk. This helps minimize the number of disk I/O operations and improves performance.
-
Auto-save: Tonic implement an auto-save feature that automatically saves changes to the file at regular intervals or when specific triggers occur, such as losing focus on the editor window. This ensures that the user doesn't lose their work due to unexpected application crashes or system failures.
-
Atomic writes: When saving a file, Tonic often use atomic write operations to ensure that the file's contents are not left in an inconsistent state if the write operation fails midway. This typically involves writing the new content to a temporary file, then renaming the temporary file to replace the original file once the write operation has completed successfully.
-
File locking: To prevent multiple applications from writing to the same file simultaneously, Tonic may use file locking mechanisms to ensure that only one application can write to the file at a time. This helps maintain the integrity of the file and avoid potential data corruption.
-
Change detection: Tonic often monitor the files in the project for changes made outside the IDE. If a file is modified externally, it will prompt the user to reload the file, merge the changes, or ignore the changes.
-
Version control integration: Tonic integrates with version control systems (such as Git, Mercurial, or SVN) to manage file changes, allowing users to commit, revert, or view the history of changes made to the files in the project.
Tonic Core reads files using the file system APIs provided by the underlying operating system. When a user opens a file, the editor reads the file's contents and displays it in a new editor tab.
Here's a simplified overview of the process:
-
File open request: When a user opens a file, either by clicking on it in the file explorer or using the "Open File" menu command, Tonic receives a file open request with the file's path.
-
Reading the file: Tonic then uses Dart file system APIs (such as
fs.readAsString()
) to read the file's contents. These APIs interact with the underlying operating system to access the file and read its content. -
Handling large files: Tonic has special handling for large files to avoid performance issues. When a file is too large, it may open it in a "Large File Mode" that disables certain features like syntax highlighting and tokenization to improve performance.
-
Encoding detection: While reading the file, Tonic also detects the file's character encoding (such as UTF-8, UTF-16, etc.). This ensures that the file's content is displayed correctly in the editor. Encoding detection is done using heuristics and user-configurable settings.
-
Displaying the file: After the file's contents are read and its encoding is detected, the content is displayed in a new editor tab. The file's syntax highlighting, code folding, and other language-specific features are applied based on the detected language, as mentioned in the previous answer.
-
File system watching: VSCode also sets up file system watchers to monitor the opened file for any external changes. If the file is modified outside of VSCode, the editor will automatically update the displayed content or prompt the user to reload the file, depending on the user's settings.