zufuliu / notepad4

Notepad4 (Notepad2⨯2, Notepad2++) is a light-weight Scintilla based text editor for Windows with syntax highlighting, code folding, auto-completion and API list for many programming languages and documents, bundled with file browser plugin matepath.
Other
2.6k stars 184 forks source link

Error displaying text when files are automatically updated #686

Closed DeepTulip closed 1 year ago

DeepTulip commented 1 year ago

When a file is modified by another process, notepad2 automatically updates the contents, which can lose the first few characters. If you edit the text at this point, these characters will be completely lost.

20230718013900 ↓↓↓ 20230718013915


WIN 10 64 FILE UTF-8

Notepad2 (64-bit) 4.23.06 r4862 (d53367e0) Compiled on Jun 18 2023 with Visual C++ 19.36.32534.0, Scintilla 5.3.5. Encoding: UTF-8 BOM, GBK Scheme: .ahk, AutoHotkey Script System: 10.0.19045 x64

zufuliu commented 1 year ago

It just load the file content, if the file is saved in another process, Notepad2 should reload it again with the updated content.

DeepTulip commented 1 year ago

Yes, Notepad2 just loads the file, and the real content of the file is:

test(t) {
    MsgBox "test"
}

But reloading it will not show the few characters on the front end, which becomes this:

t(t) {
    MsgBox "test"
}

If the text is modified at this point, the currently displayed characters will overwrite the real file, and thus the file becomes:

t(t) {
    MsgBox "test"
}

This is a long-standing bug. But since Notepad2 is not my main editor, I just use it for a quick look at the text. But when I started using it more often and configured the formatting tool (which formats files from the background), the problem became serious and many files were corrupted.

1

zufuliu commented 1 year ago

Looks like subsequent changes not detected (file not reloaded), please check "Detect file change by polling instead of notification" on "File Change Notification" dialog (we may need a better description for the option).

DeepTulip commented 1 year ago

This is what happens when the "Detect file change by polling instead of notification" option is enabled.

zufuliu commented 1 year ago

This is what happens when the "Detect file change by polling instead of notification" option is enabled.

Oh, what happens after turn off it?

DeepTulip commented 1 year ago

It's the same when it's closed. My understanding of this option is that it is used to discover file changes. In the GIF, Notepad2 has apparently found the changes and re-read the file (it already shows the formatted text), so it shouldn't be a problem with this option.

DeepTulip commented 1 year ago

By the way, the same formatting tool (modifying the file in the background), the same file (test.ahk), both EmEditor and vs code were able to display the changed file immediately and correctly.

zufuliu commented 1 year ago

Hi @DeepTulip, can you share how to setup your background formatting tool? I can try to reproduce the problem.

DeepTulip commented 1 year ago

Use AutoHotkey.exe to load this code:

    try
        FileDelete path := A_Temp "/test.ahk"

    n2 := "D:\Notepad2\Notepad2.exe"

    FileAppend 'test(t){`nMsgBox "test"`n}`n', path, "utf-8"
    Sleep 200
    Run(n2 " " path)
    Sleep 700

    fo := FileOpen(path, "rw"), fo.Encoding := "UTF-8"
    fo.Length := 0, fo.Pos := 0, fo.Write('test(t){`n`tMsgBox "test"`n}`n')
    fo.Close()
    MsgBox "end"

https://github.com/AutoHotkey/AutoHotkey/releases

zufuliu commented 1 year ago

It works on second run without FileDelete.

zufuliu commented 1 year ago

Fixed by e632ff20096b54f0e6b8bec73cd83d14bb3d639d (tes is incorrectly treated as BOM on reloading), please download and test latest builds from https://github.com/zufuliu/notepad2/actions.

DeepTulip commented 1 year ago

This is just as I suspected. Thank you very much.