Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit a8fe878

Browse files
authored
Merge pull request #12521 from thehogfather/Code-folding-issue-12456
Addresses #12456: code folding triangle don't update correctly
2 parents 93ee0a7 + 41f6d66 commit a8fe878

5 files changed

Lines changed: 331 additions & 203 deletions

File tree

src/extensions/default/CodeFolding/foldhelpers/foldcode.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ define(function (require, exports, module) {
3636
lastMark,
3737
foldMarks;
3838
for (i = 0; i < marks.length; ++i) {
39-
if (marks[i].__isFold && force !== "fold") {
39+
if (marks[i].__isFold) {
4040
if (!allowFolded) {
4141
return null;
4242
}
@@ -90,18 +90,25 @@ define(function (require, exports, module) {
9090
});
9191

9292
textRange.on("clear", function (from, to) {
93-
delete cm._lineFolds[pos.line];
94-
CodeMirror.signal(cm, "unfold", cm, from, to, pos.line);
93+
delete cm._lineFolds[from.line];
94+
CodeMirror.signal(cm, "unfold", cm, from, to);
9595
});
9696

9797
if (force === "fold") {
9898
delete range.cleared;
99-
cm._lineFolds[pos.line] = range;
99+
// In some cases such as in xml style files, the start of line folds can span multiple lines.
100+
// For instance the attributes of an element can span multiple lines. In these cases when folding
101+
// we want to render a gutter marker for both the beginning and end of the opening xml tag.
102+
if (pos.line < range.from.line) {
103+
cm._lineFolds[range.from.line] = range;
104+
} else {
105+
cm._lineFolds[pos.line] = range;
106+
}
100107
} else {
101108
delete cm._lineFolds[pos.line];
102109
}
103110

104-
CodeMirror.signal(cm, force, cm, range.from, range.to, pos.line);
111+
CodeMirror.signal(cm, force, cm, range.from, range.to);
105112
return range;
106113
}
107114

src/extensions/default/CodeFolding/foldhelpers/foldgutter.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ define(function (require, exports, module) {
100100
i = sr.to.line + 1;
101101
} else {
102102
range = cm._lineFolds[i] || (func && func(cm, pos));
103+
103104
if (!fade || (fade && $gutter.is(":hover"))) {
104105
if (cm.isFolded(i)) {
105106
// expand fold if invalid
@@ -355,30 +356,23 @@ define(function (require, exports, module) {
355356
* @param {!CodeMirror} cm the CodeMirror instance for the active editor
356357
* @param {!Object} from the ch and line position that designates the start of the region
357358
* @param {!Object} to the ch and line position that designates the end of the region
358-
* @param {?Number} gutterLineNumber the gutter line number that was clicked to signal the fold event
359359
*/
360-
function onFold(cm, from, to, gutterLineNumber) {
361-
var state = cm.state.foldGutter,
362-
line = isNaN(gutterLineNumber) ? from.line : gutterLineNumber;
363-
if (line >= state.from && line < state.to) {
364-
updateFoldInfo(cm, line, line + 1);
365-
}
360+
function onFold(cm, from, to) {
361+
var state = cm.state.foldGutter;
362+
updateFoldInfo(cm, from.line, from.line + 1);
366363
}
367364

368365
/**
369366
* Triggered when a folded code segment is unfolded.
370367
* @param {!CodeMirror} cm the CodeMirror instance for the active editor
371368
* @param {!{line:number, ch:number}} from the ch and line position that designates the start of the region
372369
* @param {!{line:number, ch:number}} to the ch and line position that designates the end of the region
373-
* @param {?Number} gutterLineNumber the gutter line number that was clicked to signal the fold event
374370
*/
375-
function onUnFold(cm, from, to, gutterLineNumber) {
376-
var state = cm.state.foldGutter,
377-
line = isNaN(gutterLineNumber) ? from.line : gutterLineNumber;
371+
function onUnFold(cm, from, to) {
372+
var state = cm.state.foldGutter;
378373
var vp = cm.getViewport();
379-
if (line >= state.from && line < state.to) {
380-
updateFoldInfo(cm, line, Math.min(vp.to, to.line));
381-
}
374+
delete cm._lineFolds[from.line];
375+
updateFoldInfo(cm, from.line, to.line || vp.to);
382376
}
383377

384378
/**
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<html>
2+
<head>
3+
4+
</head>
5+
<body>
6+
7+
<form action="#" method="post">
8+
<div>
9+
<label for="name">Text Input:</label>
10+
<input type="text" name="name" id="name" value="" tabindex="1" />
11+
</div>
12+
<table>
13+
<thead>
14+
<tr>
15+
<th></th>
16+
</tr>
17+
</thead>
18+
<tbody>
19+
<tr>
20+
<td></td>
21+
</tr>
22+
</tbody>
23+
</table>
24+
<div>
25+
<h4>Radio Button Choice</h4>
26+
27+
<label
28+
for="radio-choice-1">
29+
Choice 1
30+
</label>
31+
<input type="radio" name="radio-choice-1" id="radio-choice-1" tabindex="2" value="choice-1" />
32+
33+
<label for="radio-choice-2">Choice 2</label>
34+
<input type="radio" name="radio-choice-2" id="radio-choice-2" tabindex="3" value="choice-2" />
35+
</div>
36+
</form>
37+
</body>
38+
</html>
Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
11
/**
2-
* Synchronises the code folding states in the CM doc to cm._lineFolds cache.
3-
* When an undo operation is done, if folded code fragments are restored, then
4-
* we need to update cm._lineFolds with the fragments
5-
* @param {Object} cm cm the CodeMirror instance for the active editor
6-
* @param {Object} from starting position in the doc to sync the fold states from
7-
* @param {[[Type]]} lineAdded a number to show how many lines where added to the document
8-
*/
9-
function syncDocToFoldsCache(cm, from, lineAdded) {
10-
var minFoldSize = prefs.getSetting("minFoldSize") || 2;
11-
var opts = cm.state.foldGutter.options || {};
12-
var rf = opts.rangeFinder || CodeMirror.fold.auto;
13-
var i, pos, folds, fold, range;
14-
if (lineAdded <= 0) {
15-
return;
16-
}
2+
* Synchronises the code folding states in the CM doc to cm._lineFolds cache.
3+
* When an undo operation is done, if folded code fragments are restored, then
4+
* we need to update cm._lineFolds with the fragments
5+
* @param {Object} cm cm the CodeMirror instance for the active editor
6+
* @param {Object} from starting position in the doc to sync the fold states from
7+
* @param {[[Type]]} lineAdded a number to show how many lines where added to the document
8+
*/
9+
/*global define, brackets, document, window, $, CodeMirror, isFold, prefs*/
10+
11+
function syncDocToFoldsCache(cm, from, lineAdded) {
12+
"use strict";
13+
var minFoldSize = prefs.getSetting("minFoldSize") || 2;
14+
var opts = cm.state.foldGutter.options || {};
15+
var rf = opts.rangeFinder || CodeMirror.fold.auto;
16+
var i, pos, folds, fold, range;
17+
if (lineAdded <= 0) {
18+
return;
19+
}
1720

18-
for (i = from; i <= from + lineAdded; i = i + 1) {
19-
pos = CodeMirror.Pos(i);
20-
folds = cm.doc.findMarksAt(pos).filter(isFold);
21-
fold = folds.length ? fold = folds[0] : undefined;
22-
if (fold) {
23-
range = rf(cm, CodeMirror.Pos(i));
24-
if (range && range.to.line - range.from.line >= minFoldSize) {
25-
cm._lineFolds[i] = range;
26-
i = range.to.line;
27-
} else {
28-
delete cm._lineFolds[i];
29-
}
21+
for (i = from; i <= from + lineAdded; i = i + 1) {
22+
pos = CodeMirror.Pos(i);
23+
folds = cm.doc.findMarksAt(pos).filter(isFold);
24+
fold = folds.length ? fold = folds[0] : undefined;
25+
if (fold) {
26+
range = rf(cm, CodeMirror.Pos(i));
27+
if (range && range.to.line - range.from.line >= minFoldSize) {
28+
cm._lineFolds[i] = range;
29+
i = range.to.line;
30+
} else {
31+
delete cm._lineFolds[i];
3032
}
3133
}
32-
3334
}
35+
36+
}

0 commit comments

Comments
 (0)