Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 54
ADFA-3133 Bug fix for over-eager auto-save during onPause event#1151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hal-eisen-adfa
merged 3 commits into
stage
from
ADFA-3133-Editor-writes-files-without-user-assentApr 8, 2026
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
06f1484
ADFA-3133 Bug fix for over-eager auto-save during onPause event
hal-eisen-adfa ad4d7fe
Fix 2 CodeRabbit issues: undo/redo too broad, and don't opt-out of ui…
hal-eisen-adfa 7751d32
Merge branch 'stage' into ADFA-3133-Editor-writes-files-without-user-…
hal-eisen-adfa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 32 additions & 11 deletions
43 app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -232,6 +232,10 @@ open class EditorHandlerActivity : | ||
| loadPluginTabs() | ||
| } | ||
| /** | ||
| * Persists which tabs are open (preferences only). Does **not** write project file buffers to disk; | ||
| * saving is explicit or prompted (e.g. close project). | ||
| */ | ||
| override fun onPause() { | ||
| super.onPause() | ||
| // Record timestamps for all currently open files before saving the cache | ||
| @@ -246,7 +250,6 @@ open class EditorHandlerActivity : | ||
| if (!isOpenedFilesSaved.get()) { | ||
| saveOpenedFiles() | ||
| saveOpenedPluginTabs() | ||
| saveAllAsync(notify = false) | ||
| } | ||
| } | ||
| @@ -271,26 +274,29 @@ open class EditorHandlerActivity : | ||
| invalidateOptionsMenu() | ||
| } | ||
| /** | ||
| * Reloads disk content into an open editor only when the file changed on disk since the last | ||
| * [onPause] snapshot **and** the in-memory buffer is still clean ([CodeEditorView.isModified] is | ||
| * false). A clean buffer may still have undo history after [IDEEditor.markUnmodified] / save; we | ||
| * reload anyway so external edits are not ignored. Never replaces buffers with unsaved edits. | ||
| */ | ||
| private fun checkForExternalFileChanges() { | ||
| // Get the list of files currently managed by the ViewModel | ||
| val openFiles = editorViewModel.getOpenedFiles() | ||
| if (openFiles.isEmpty() || fileTimestamps.isEmpty()) return | ||
| lifecycleScope.launch(Dispatchers.IO) { | ||
| // Check each open file | ||
| openFiles.forEach { file -> | ||
| val lastKnownTimestamp = fileTimestamps[file.absolutePath] ?: return@forEach | ||
| val currentTimestamp = file.lastModified() | ||
| // If the file on disk is newer. | ||
| if (currentTimestamp > lastKnownTimestamp) { | ||
| val newContent = runCatching { file.readText() }.getOrNull() ?: return@forEach | ||
| withContext(Dispatchers.Main) { | ||
| // If the editor for the new file exists AND has no unsaved changes... | ||
| val editorView = getEditorForFile(file) ?: return@withContext | ||
| if (editorView.isModified) return@withContext | ||
| val ideEditor = editorView.editor ?: return@withContext | ||
| editorView.editor?.setText(newContent) | ||
| ideEditor.setText(newContent) | ||
| editorView.markAsSaved() | ||
coderabbitai[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| updateTabs() | ||
| } | ||
| @@ -341,12 +347,19 @@ open class EditorHandlerActivity : | ||
| prefs.getString(PREF_KEY_OPEN_FILES_CACHE, null) | ||
| } ?: return@launch | ||
| if (editorViewModel.getOpenedFileCount() > 0) { | ||
| // Returning to an in-memory session (e.g. after onPause/onStop). Replaying the | ||
| // snapshot would be redundant and could interfere with dirty buffers and undo. | ||
| withContext(Dispatchers.IO) { prefs.putString(PREF_KEY_OPEN_FILES_CACHE, null) } | ||
| return@launch | ||
| } | ||
| val cache = withContext(Dispatchers.Default) { | ||
| Gson().fromJson(jsonCache, OpenedFilesCache::class.java) | ||
| } | ||
| onReadOpenedFilesCache(cache) | ||
| // Clear the preference so it's only loaded once on startup | ||
| // Clear the preference so it's only loaded once per cold restore | ||
| withContext(Dispatchers.IO) { prefs.putString(PREF_KEY_OPEN_FILES_CACHE, null) } | ||
| } catch (err: Throwable) { | ||
| log.error("Failed to reopen recently opened files", err) | ||
| @@ -747,6 +760,11 @@ open class EditorHandlerActivity : | ||
| override fun onConfigurationChanged(newConfig: Configuration) { | ||
| super.onConfigurationChanged(newConfig) | ||
| val safeContent = contentOrNull ?: return | ||
| for (i in 0 until safeContent.editorContainer.childCount) { | ||
| (safeContent.editorContainer.getChildAt(i) as? CodeEditorView)?.reapplyEditorDisplayPreferences() | ||
| } | ||
| getCurrentEditor()?.editor?.apply { | ||
| doOnNextLayout { | ||
| cursor?.let { c -> ensurePositionVisible(c.leftLine, c.leftColumn, true) } | ||
| @@ -1069,17 +1087,20 @@ open class EditorHandlerActivity : | ||
| nameBuilder.addPath(it, it.path) | ||
| } | ||
| for (index in 0 until content.tabs.tabCount) { | ||
| val file = files.getOrNull(index) ?: continue | ||
| for (tabPos in 0 until content.tabs.tabCount) { | ||
| if (isPluginTab(tabPos)) continue | ||
| val fileIndex = getFileIndexForTabPosition(tabPos) | ||
| if (fileIndex < 0) continue | ||
| val file = files.getOrNull(fileIndex) ?: continue | ||
| val count = dupliCount[file.name] ?: 0 | ||
| val isModified = getEditorAtIndex(index)?.isModified ?: false | ||
| val isModified = getEditorAtIndex(fileIndex)?.isModified ?: false | ||
| var name = if (count > 1) nameBuilder.getShortPath(file) else file.name | ||
| if (isModified) { | ||
| name = "*$name" | ||
| } | ||
| names[index] = name to FileExtension.Factory.forFile(file, file.isDirectory).icon | ||
| names[tabPos] = name to FileExtension.Factory.forFile(file, file.isDirectory).icon | ||
| } | ||
| withContext(Dispatchers.Main) { | ||
42 changes: 36 additions & 6 deletions
42 app/src/main/java/com/itsaky/androidide/fragments/RecyclerViewFragment.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.