fix(editor): content modification status is not updated when editor.setText() is used (fixes #878)

This commit is contained in:
Akash Yadav
2023-03-19 22:48:17 +05:30
parent 9f31276a5c
commit 80cb8bf3d2
4 changed files with 13 additions and 12 deletions
Binary file not shown.
@@ -251,9 +251,6 @@ open class EditorHandlerActivity : ProjectHandlerActivity(), IEditorHandler {
log.info("Opening file at index:", position, "file: ", file)
val editor = CodeEditorView(this, file, selection!!)
editor.editor.subscribeEvent(ContentChangeEvent::class.java) { event, _ ->
onEditorContentChanged(event)
}
editor.layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
binding.editorContainer.addView(editor)
@@ -365,11 +362,9 @@ open class EditorHandlerActivity : ProjectHandlerActivity(), IEditorHandler {
}
}
private fun onEditorContentChanged(event: ContentChangeEvent) {
if (event.action != ContentChangeEvent.ACTION_SET_NEW_TEXT) {
viewModel.setFilesModified(true)
invalidateOptionsMenu()
}
private fun onEditorContentChanged() {
viewModel.setFilesModified(true)
invalidateOptionsMenu()
}
override fun areFilesModified(): Boolean {
@@ -542,6 +537,9 @@ open class EditorHandlerActivity : ProjectHandlerActivity(), IEditorHandler {
@Subscribe(threadMode = ThreadMode.MAIN)
fun onDocumentChange(event: DocumentChangeEvent) {
// update content modification status
onEditorContentChanged()
val index = findIndexOfEditorByFile(event.file.toFile())
if (index == -1) {
return
@@ -118,6 +118,12 @@ public class CodeEditorView extends LinearLayout {
final var editor = getEditor();
editor.post(() -> {
editor.setText(contents, createEditorArgs());
// editor.setText(...) sets the modified flag to true
// but in this case, file is read from disk and hence the contents are not modified at all
// so the flag must be changed to unmodified
// TODO: Find a better way to check content modification status
editor.markUnmodified();
postRead();
selection.validate();
@@ -722,10 +722,7 @@ public void updateFile(File file) {
* @param event The content change event.
*/
private void handleContentChange(ContentChangeEvent event, Unsubscribe unsubscribe) {
if (event.getAction() != ContentChangeEvent.ACTION_SET_NEW_TEXT) {
isModified = true;
}
isModified = true;
if (getFile() == null) {
return;
}