oleg-shilo / cs-script.npp

CS-Script (C# Intellisense) plugin for Notepad++ (x86/x64)
MIT License
248 stars 52 forks source link

corrupted the code while saving #18

Closed RexProg closed 6 years ago

RexProg commented 6 years ago

hi oleg-shilo sometimes when I run the code or save the code(by pressing ctrl+s), the code file gets corrupted like the video below:

ezgif com-video-to-gif

oleg-shilo commented 6 years ago

Hi there,

I haven't noticed anything out of order on the video. All I see is just scrolling up and down.

However I have noticed some strange N++ on-save artifacts by myself. On saving a non printable character can appear at the position of the caret. The rest of the post is based on the assumption that it is the same problem that you are having.

It happens very occasionally (~ once a week) and I cannot even reproduce so it to investigate if CS-Script is even involved in it. The only document content activity the plugin may do on saving is the document formatting. The formatting functionality on its own has never caused any problems. Though, if you want completely remove CS-Script from the picture uncheck "Format on save" in the config dialog and trigger formatting manually.

I have also noticed that when N++ goes in this mode (injects a character on saving) it does it every time you save the doc. Almost nothing can get the editor out of this mode, though switching the tabs usually helps.

I have also noticed that the since I have moved to N++ x64 the problem did not occurred to me at all (may be a coincidence).

Anyway, in the absence of any test-case to investigate I am interpreting it as Scintilla rendering artifact.

kraemera commented 6 years ago

Hi @oleg-shilo , I am commenting on this issue, because I also have one C# file which got corrupted when saving. Actually some lines of code were removed from the end of the file. As you suggested in the last comment, I turned off the "Format on save" and it no longer happened. I then tried just using "Format Document" and again the lines were removed. I tried to strip down the code file until it no longer happens. It maybe has something to do with some characters in a comment in the code. Please have a look at the attached file. FormatCodeBug.zip

I am using Notepad++ v7.5.6 (64-bit) Build time : Mar 19 2018 - 00:23:17 Path : C:\Program Files\Notepad++\notepad++.exe Admin mode : OFF Local Conf mode : OFF OS : Windows 8.1 (64-bit) Plugins : CSScriptNpp.x64.dll DSpellCheck.dll mimeTools.dll NppConverter.dll PluginManager.dll XMLTools.dll CSScript version 1.7.9 (64 bit)

Thanks

oleg-shilo commented 6 years ago

Great, I have managed to reproduce it with your sample. Interestingly enough the same formatting engine+algorithm is used in my extension for VSCode and it works perfectly well there. But not in Notepad++.

I also found that removing German (I assume it is German) code comments completely eliminates the problem.

It might be related to the Scintilla ridiculous encoding/rendering mechanism, which counts the length of the rendered text not in characters but in bytes the text takes in the memory.

Will have a look. May be there is something that can be done when the plugin replaces the text with its formatted version.

oleg-shilo commented 6 years ago

Fixed. The fix will be available in the next release.

Indeed as I suspected the problem was caused by the Scintilla selection support.

The code below for setting text without scrolling works perfectly well for pure English content but fails for some mixed content as in your case:

   document.SetTargetStart(new Position(start));
   document.SetTargetEnd(new Position(end);
   document.ReplaceTarget(text.Length, text);

Fortunately alternative API works fine: document.SetText(text). Though an extra care needs to be taken about the scrolling.

Mausimo commented 6 years ago

Hi Oleg,

I am using Notepad++ v7.5.6 (64-bit) with CSScriptNpp1.7.11.0 and I am experiencing the problem where lines are removed at the end of the file when I click save. The problem went away once I turned off "Format on save".

oleg-shilo commented 6 years ago

Are they empty lines?

Mausimo commented 6 years ago

No, they are lines of code. It has happened in multiple documents. I first noticed it when I started to get around line ~270, when I hit save it was deleting my bottom code lines. Then I decided to open a pre-existing .cs file that was about ~700 lines. I changed a line or two of code and hit save and it reduced the file to ~250 lines. That is when I found this post and tried disabling "Format on save", which has fixed the issue.

oleg-shilo commented 6 years ago

Thank you, I found a concurrency problem with problem with beNotified during a lengthy formatting operations on large documents. Though in my test the problem is so severe that it crashes the Npp.

I have fixed this problem but not sure if yours is caused by some other reasons. Unfortunately I cannot reproduce your problem so can you please do a simple test for me.

Can you try to format that large file on demand (not on save):

image

Does it do the truncation?

Mausimo commented 6 years ago

I was experiencing the same thing, when I saved the 700+ line document, NPP would crash.

I just tested using Format Document on the 700+ line document and it crashed. I then tried to modify a line of code and use Format Document, it did not crash, but truncated the document to 204 lines.

oleg-shilo commented 6 years ago

Excellent. Then it is the sam bug that I just fixed. The release is on the way...

oleg-shilo commented 6 years ago

Done in the latest release v1.7.12.0