thehogfather / brackets-code-folding

Code folding extension for brackets IDE
134 stars 42 forks source link

Unexpected code folding occur and expected code folding arrows are not appeared #116

Closed cithukyaw closed 9 years ago

cithukyaw commented 9 years ago

Some unexpected code folding occur at the top of the file. Although I have previously open them, they keep folding on every reload. The arrows are not appearing in some cases. Please check the following illustrations.

code-folding

Here is my code folding setting:

code-folding-settings

My version is 0.2.26

thehogfather commented 9 years ago

Are there any steps to consistently reproduce this issue? For instance, is the file being changed from a different editor (or another instance of brackets), or after a git pull?

cithukyaw commented 9 years ago

the file being is changed from a different editor

Yes

the file being is changed from another instance of brackets

Yes

after a git pull

Yes, when some changes made from git such as discard hunk, discard file, pull, etc.

thehogfather commented 9 years ago

Ok that makes sense. Since fold ranges are persisted as line numbers. The problem is that the changes made to the text sometimes invalidate the saved fold states - e.g. when new lines are added or removed. This is quite straightforward to detect and the inconsistent fold states can be cleared.

cithukyaw commented 9 years ago

Okay. In addition to new lines or remove, It is affected by even single character change. I tested with a single space change in a line.

thehogfather commented 9 years ago

Thanks for the info. Can you test the latest version on master by installing manually and let me know if the issue has been resolved?

cithukyaw commented 9 years ago

Thanks for the fix. It sounds good, but it stills have some issues. Let's say for example, I have the following saved code folding state.

1. this.create = function(req, resp, params) { <> };
2. this.read = function(req, resp, params) { <> };
3. this.update = function(req, resp, params) { <> };

Here are some test cases and their results:

  1. When I added a new line (blank line or whatever) between the line 1 and 2 from a different editor, the code folding at line 1 was opened. (Expected: should not be opened)
  2. When I added a new line between 2 and 3, the code folding at line 1 and 2 are opened. (Expected: should not be opened)
  3. When I updated the code in the function update at line 3 by adding a new line or removing a line, the code folding at line 1 and 2 are opened. (Expected: should not be opened)
  4. When I removed a space between the close parenthese and the open curly brace at the line 3, the code folding of the line 3 was opened. (Expected: should not be opened, but it is acceptable.)
  5. When I updated the code in the function update at line 3 without adding new line, e.g. adding/removing a character, all folding states remain as their previous states. (Expected: Ok)
thehogfather commented 9 years ago

Thanks for the example. Let's make the expectations a bit more explicit (all with respect to the example you've given above).

  1. When adding a new line (blank line or whatever) between the line 1 and 2 from a different editor, the code folding at line 1 should not be opened. Any folded regions below the new line however would be cleared (e.g., codes folded at lines 2 and 3 should be opened).
  2. When I adding a new line between 2 and 3 from a different editor, the code folding at line 1 and 2 should not be opened. As with case 1 (above), any folded regions below the new line would be cleared (e.g., code folded at line 3 should be opened).
  3. When updating the code in the function update at line 3 by adding a new line or removing a line, the code folding at line 1 and 2 should not be opened. Code folded at line 3 should be opened since the additional/ removed line changes the folded range that is stored in Brackets.
  4. When removing a space between the close parentheses and the open curly brace at the line 3, the code folding of the line 3 should be opened (since currently range changes are determined based on line number and character position of the start and end of the range). It is straightforward to make the comparison based on start and end lines only - this way modifications that don't change the number of lines in the range preserve the folded states. This option is more forgiving.
  5. When updating the code in the function update at line 3 without adding new line, e.g. adding/removing a character, all folding state remains as their previous states.

The main reason for these differences when editing from outside brackets is that the changeEvent in codeMirror is different when the document's entire content is being set (this is essentially what happens when the document is modified externally). The event is modelled as if the entire document content has been deleted and then replaced with new content even if one character or one line was added. This is understandable since there could have been multiple edits on various lines for the change event.

Thanks for testing! If you disagree of have ideas about how to better resolve the issue do shout. I'll push more fixes shortly.

cithukyaw commented 9 years ago

I agree with most of your expectations except 1 and 2 of

Any folded regions below the new line however would be cleared

Let's say for example, I have a long lines of code file and it has many folded regions. If I added a new line at the top or almost top of the file, all intentional folded regions below the new line would be cleared and opened. I believe it is not a comfortable situation.

I expected to not clear them, but does it seem impossible because of the folded regions saved with line numbers and because of the line numbers changed? However, I have an idea with (may-be) less possibility to resolve it. I commented in your foldgutter.js. Please check it

thehogfather commented 9 years ago

Unfortunately if those changes are made from outside brackets then this is a consistent way to reflect the changes on the folded regions. See my response to your comment to see the reason why. Feel free to suggest any clean, predictable and stable workarounds.

cithukyaw commented 9 years ago

Another problem noticed. git stash and git stash pop which contained the working file did not make any changes to the file unless F5 is pressed. I'm not sure if it is related to this fix.

thehogfather commented 9 years ago

@cithukyaw Could you further explain your latest comment? e.g., what is the expected vs actual outcome? Is the issue even related to code folding?

cithukyaw commented 9 years ago

Sorry, I guess it is not related to code folding. The code editor is not refreshing when any change was made from outside (git in this case). I'm not sure what is related to the issue. I will remove my unrelated comments.

thehogfather commented 9 years ago

no problem. Depending on whether you are using the git extension or git commandline to issue the stash commands you might want a report and issue appropriately.

nathanziarek commented 9 years ago

I'm having what appears to be a similar problem. I am not using an external editor and only "git" through Bracket's git plugin.

(Hopefully I'm not showing anything I shouldn't be here!)

You'll notice in this shot that the "var gulp ..." does not provide any folding arrow (although clicking where the arrow would be works). The "var server..." line shows itself as collapsed when it isn't. Clicking the collapse arrow collapses not just the "server." stuff but also the next 155 lines.

Let me know what else I can provide!

screen shot 2015-01-29 at 2 18 48 pm

thehogfather commented 9 years ago

@nathanziarek can you install the latest version of code-folding extension from git (if you don't already have it) and let me know if the issue persists.

nathanziarek commented 9 years ago

Having done that, I'm seeing the same thing. It saved the collapsed sections as well. Is there another preference file I should blast to see if it's just something corrupted?

I'm new to the guts of Brackets and didn't know there was a developer console ... not sure if this helps?

Exception in 'activeEditorChange' listener on Object TypeError: Cannot read property 'lastIndexOf' of undefined TypeError: Cannot read property 'lastIndexOf' of undefined
    at findOpening (file:///Applications/Brackets.app/Contents/www/thirdparty/CodeMirror2/addon/fold/brace-fold.js:20:43)
    at Array.<anonymous> (file:///Applications/Brackets.app/Contents/www/thirdparty/CodeMirror2/addon/fold/brace-fold.js:34:51)
    at file:///Users/nathanziarek/Library/Application%20Support/Brackets/extensions/user/brackets-code-folding/foldhelpers/foldcode.js:210:33
    at file:///Users/nathanziarek/Library/Application%20Support/Brackets/extensions/user/brackets-code-folding/foldhelpers/foldcode.js:132:29
    at Array.forEach (native)
    at null.getValidFolds (file:///Users/nathanziarek/Library/Application%20Support/Brackets/extensions/user/brackets-code-folding/foldhelpers/foldcode.js:130:22)
    at restoreLineFolds (file:///Users/nathanziarek/Library/Application%20Support/Brackets/extensions/user/brackets-code-folding/main.js:123:32)
    at onActiveEditorChanged (file:///Users/nathanziarek/Library/Application%20Support/Brackets/extensions/user/brackets-code-folding/main.js:263:4)
    at Object.trigger (/utils/EventDispatcher.js:222:40)
    at _notifyActiveEditorChanged (/editor/EditorManager.js:165:17)main.js:116 (anonymous function)
thehogfather commented 9 years ago

Looks like the saved fold states were being loaded. Shift-Alt-1 to clear all regions. Then try to reproduce the original issue. Please post any additional errors that appear in the console. They are useful.

nathanziarek commented 9 years ago

Shift-Alt-1 cleared the current folding, but didn't fix the issue. Uninstalled, reinstalled from master and same weird artifacts (notice the folded triangle at the last line):

screen shot 2015-01-29 at 4 11 26 pm

The error is different. Maybe this is an interaction between the git plugin and code folding? If so, I'm sorry to have wasted your time!

screen shot 2015-01-29 at 4 18 23 pm

thehogfather commented 9 years ago

@nathanziarek could you find the state.json file in your app-data directory. It should be in the same directory as the brackets.json file opened by Debug > Open Preferences File. To completely clear the old folded states, you need to find the code-folding.folds key in the state.json file and delete that. Then try to reproduce the issue. I'm not 100% convinced it is an interaction between the two extensions. I'd love to get to the bottom of it.

Cheers

nathanziarek commented 9 years ago

That seems to have done it!

For posterity: Maybe it's obvious, but I couldn't edit the state.json file in Brackets--any time I edited it and saved it would magically plop that key right back in there.

There is still one remaining issue: When I fold my first line, it collapses the entire document.

screen shot 2015-02-02 at 11 04 05 am ...becomes... screen shot 2015-02-02 at 11 04 20 am

I saved the code-folding.folds JSON if it was interesting. Nothing confidential in there I can see ... let me know if it'd be helpful.

thehogfather commented 9 years ago

I think your first line is being folded based on the level of indentation of the lines in the file. You can disable indentation folding in the Settings View > Code Folding Settings and uncheck always use indent fold. If that does not fix it we can troubleshoot further.

nathanziarek commented 9 years ago

You should always remember to think before posting supposed bugs. I thought of that right after I posted and was just editing my message 😀

it's working great again. Thanks for the help!

thehogfather commented 9 years ago

no problem.