Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/extensions/default/CodeFolding/foldhelpers/foldcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ define(function (require, exports, module) {
lastMark,
foldMarks;
for (i = 0; i < marks.length; ++i) {
if (marks[i].__isFold && force !== "fold") {
if (marks[i].__isFold) {
if (!allowFolded) {
return null;
}
Expand Down Expand Up @@ -90,18 +90,25 @@ define(function (require, exports, module) {
});

textRange.on("clear", function (from, to) {
delete cm._lineFolds[pos.line];
CodeMirror.signal(cm, "unfold", cm, from, to, pos.line);
delete cm._lineFolds[from.line];
CodeMirror.signal(cm, "unfold", cm, from, to);
});

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

CodeMirror.signal(cm, force, cm, range.from, range.to, pos.line);
CodeMirror.signal(cm, force, cm, range.from, range.to);
return range;
}

Expand Down
22 changes: 8 additions & 14 deletions src/extensions/default/CodeFolding/foldhelpers/foldgutter.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ define(function (require, exports, module) {
i = sr.to.line + 1;
} else {
range = cm._lineFolds[i] || (func && func(cm, pos));

if (!fade || (fade && $gutter.is(":hover"))) {
if (cm.isFolded(i)) {
// expand fold if invalid
Expand Down Expand Up @@ -355,30 +356,23 @@ define(function (require, exports, module) {
* @param {!CodeMirror} cm the CodeMirror instance for the active editor
* @param {!Object} from the ch and line position that designates the start of the region
* @param {!Object} to the ch and line position that designates the end of the region
* @param {?Number} gutterLineNumber the gutter line number that was clicked to signal the fold event
*/
function onFold(cm, from, to, gutterLineNumber) {
var state = cm.state.foldGutter,
line = isNaN(gutterLineNumber) ? from.line : gutterLineNumber;
if (line >= state.from && line < state.to) {
updateFoldInfo(cm, line, line + 1);
}
function onFold(cm, from, to) {
var state = cm.state.foldGutter;
updateFoldInfo(cm, from.line, from.line + 1);
}

/**
* Triggered when a folded code segment is unfolded.
* @param {!CodeMirror} cm the CodeMirror instance for the active editor
* @param {!{line:number, ch:number}} from the ch and line position that designates the start of the region
* @param {!{line:number, ch:number}} to the ch and line position that designates the end of the region
* @param {?Number} gutterLineNumber the gutter line number that was clicked to signal the fold event
*/
function onUnFold(cm, from, to, gutterLineNumber) {
var state = cm.state.foldGutter,
line = isNaN(gutterLineNumber) ? from.line : gutterLineNumber;
function onUnFold(cm, from, to) {
var state = cm.state.foldGutter;
var vp = cm.getViewport();
if (line >= state.from && line < state.to) {
updateFoldInfo(cm, line, Math.min(vp.to, to.line));
}
delete cm._lineFolds[from.line];
updateFoldInfo(cm, from.line, to.line || vp.to);
}

/**
Expand Down
38 changes: 38 additions & 0 deletions src/extensions/default/CodeFolding/unittest-files/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<html>
<head>

</head>
<body>

<form action="#" method="post">
<div>
<label for="name">Text Input:</label>
<input type="text" name="name" id="name" value="" tabindex="1" />
</div>
<table>
<thead>
<tr>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
<div>
<h4>Radio Button Choice</h4>

<label
for="radio-choice-1">
Choice 1
</label>
<input type="radio" name="radio-choice-1" id="radio-choice-1" tabindex="2" value="choice-1" />

<label for="radio-choice-2">Choice 2</label>
<input type="radio" name="radio-choice-2" id="radio-choice-2" tabindex="3" value="choice-2" />
</div>
</form>
</body>
</html>
59 changes: 31 additions & 28 deletions src/extensions/default/CodeFolding/unittest-files/test.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
/**
* Synchronises the code folding states in the CM doc to cm._lineFolds cache.
* When an undo operation is done, if folded code fragments are restored, then
* we need to update cm._lineFolds with the fragments
* @param {Object} cm cm the CodeMirror instance for the active editor
* @param {Object} from starting position in the doc to sync the fold states from
* @param {[[Type]]} lineAdded a number to show how many lines where added to the document
*/
function syncDocToFoldsCache(cm, from, lineAdded) {
var minFoldSize = prefs.getSetting("minFoldSize") || 2;
var opts = cm.state.foldGutter.options || {};
var rf = opts.rangeFinder || CodeMirror.fold.auto;
var i, pos, folds, fold, range;
if (lineAdded <= 0) {
return;
}
* Synchronises the code folding states in the CM doc to cm._lineFolds cache.
* When an undo operation is done, if folded code fragments are restored, then
* we need to update cm._lineFolds with the fragments
* @param {Object} cm cm the CodeMirror instance for the active editor
* @param {Object} from starting position in the doc to sync the fold states from
* @param {[[Type]]} lineAdded a number to show how many lines where added to the document
*/
/*global define, brackets, document, window, $, CodeMirror, isFold, prefs*/
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put this as the first line followed by an empty line.
I think you can remove some of this too.
isFold and prefs cannot be required?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the html, this is a test file loaded up during the unit tests. the isFold and prefs are just there to silence jshint errors.


function syncDocToFoldsCache(cm, from, lineAdded) {
"use strict";
var minFoldSize = prefs.getSetting("minFoldSize") || 2;
var opts = cm.state.foldGutter.options || {};
var rf = opts.rangeFinder || CodeMirror.fold.auto;
var i, pos, folds, fold, range;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brackets style is to use a combined var.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, I dont think this file should conform to the brackets style - they are test files and should be able to accommodate plausible scenarios that might appear in projects of anyone using brackets regardless of style.

if (lineAdded <= 0) {
return;
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you put this directly aftrer "use strict". I don't remember if JSLint complains.


for (i = from; i <= from + lineAdded; i = i + 1) {
pos = CodeMirror.Pos(i);
folds = cm.doc.findMarksAt(pos).filter(isFold);
fold = folds.length ? fold = folds[0] : undefined;
if (fold) {
range = rf(cm, CodeMirror.Pos(i));
if (range && range.to.line - range.from.line >= minFoldSize) {
cm._lineFolds[i] = range;
i = range.to.line;
} else {
delete cm._lineFolds[i];
}
for (i = from; i <= from + lineAdded; i = i + 1) {
pos = CodeMirror.Pos(i);
folds = cm.doc.findMarksAt(pos).filter(isFold);
fold = folds.length ? fold = folds[0] : undefined;
if (fold) {
range = rf(cm, CodeMirror.Pos(i));
if (range && range.to.line - range.from.line >= minFoldSize) {
cm._lineFolds[i] = range;
i = range.to.line;
} else {
delete cm._lineFolds[i];
}
}

}

}
Loading