Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: not determined folding widgets for html tags #5548

Merged
merged 1 commit into from
May 8, 2024
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
32 changes: 14 additions & 18 deletions src/mode/folding/php.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
"use strict";

var oop = require("../../lib/oop");
var MixedFoldMode = require("./mixed").FoldMode;
var CstyleFoldMode = require("./cstyle").FoldMode;
var Range = require("../../range").Range;
var TokenIterator = require("../../token_iterator").TokenIterator;


var FoldMode = exports.FoldMode = function () {
this.cstyleFoldMode = new CstyleFoldMode();
MixedFoldMode.call(this, this, {
"js-": new CstyleFoldMode(),
"css-": new CstyleFoldMode(),
"php-": this
});
};

oop.inherits(FoldMode, MixedFoldMode);
oop.inherits(FoldMode, CstyleFoldMode);

(function () {
this.getFoldWidgetRangeBase = this.getFoldWidgetRange;
this.getFoldWidgetBase = this.getFoldWidget;

this.indentKeywords = {
"if": 1,
"while": 1,
Expand All @@ -34,31 +30,31 @@ oop.inherits(FoldMode, MixedFoldMode);
"endswitch": -1
};

this.foldingStartMarker = /(?:\s|^)(if|else|elseif|while|for|foreach|switch).*\:/i;
this.foldingStopMarker = /(?:\s|^)(endif|endwhile|endfor|endforeach|endswitch)\;/i;
this.foldingStartMarkerPhp = /(?:\s|^)(if|else|elseif|while|for|foreach|switch).*\:/i;
this.foldingStopMarkerPhp = /(?:\s|^)(endif|endwhile|endfor|endforeach|endswitch)\;/i;

this.getFoldWidgetRange = function (session, foldStyle, row) {
var line = session.doc.getLine(row);
var match = this.foldingStartMarker.exec(line);
var match = this.foldingStartMarkerPhp.exec(line);
if (match) {
return this.phpBlock(session, row, match.index + 2);
}

var match = this.foldingStopMarker.exec(line);
var match = this.foldingStopMarkerPhp.exec(line);
if (match) {
return this.phpBlock(session, row, match.index + 2);
}
return this.cstyleFoldMode.getFoldWidgetRange(session, foldStyle, row);
return this.getFoldWidgetRangeBase(session, foldStyle, row);
};


// must return "" if there's no fold, to enable caching
this.getFoldWidget = function (session, foldStyle, row) {
var line = session.getLine(row);
var isStart = this.foldingStartMarker.test(line);
var isEnd = this.foldingStopMarker.test(line);
var isStart = this.foldingStartMarkerPhp.test(line);
var isEnd = this.foldingStopMarkerPhp.test(line);
if (isStart && !isEnd) {
var match = this.foldingStartMarker.exec(line);
var match = this.foldingStartMarkerPhp.exec(line);
var keyword = match && match[1].toLowerCase();
if (keyword) {
var type = session.getTokenAt(row, match.index + 2).type;
Expand All @@ -68,7 +64,7 @@ oop.inherits(FoldMode, MixedFoldMode);
}
}
if (isEnd && foldStyle === "markbeginend") {
var match = this.foldingStopMarker.exec(line);
var match = this.foldingStopMarkerPhp.exec(line);
var keyword = match && match[1].toLowerCase();
if (keyword) {
var type = session.getTokenAt(row, match.index + 2).type;
Expand All @@ -77,7 +73,7 @@ oop.inherits(FoldMode, MixedFoldMode);
}
}
}
return this.cstyleFoldMode.getFoldWidget(session, foldStyle, row);
return this.getFoldWidgetBase(session, foldStyle, row);
};

this.phpBlock = function (session, row, column, tokenRange) {
Expand Down
59 changes: 52 additions & 7 deletions src/mode/folding/php_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,38 @@ module.exports = {

"test: php folding with alternative syntax": function () {
var session = new EditSession([
'<?php', 'function checkNumber($number)', '{', ' switch ($number) {', ' case 0:',
' echo "Number is zero again";', ' if ($number == 0):',
' echo "Number is zero";', ' elseif ($number > 0):',
' echo "Number is positive";', ' else:',
' echo "Number is negative";', 'endif;', ' break;', ' default:',
' echo "Number is not zero";', ' }', 'foreach (array(1, 2, 3) as $num):',
' echo "Num: $num";', ' endforeach;', '}', '?>'
'<?php',
'function checkNumber($number)',
'{',
' switch ($number) {',
' case 0:',
' echo "Number is zero again";',
' if ($number == 0):',
' echo "Number is zero";',
' elseif ($number > 0):',
' echo "Number is positive";',
' else:',
' echo "Number is negative";',
'endif;',
' break;',
' default:',
' echo "Number is not zero";',
' }', 'foreach (array(1, 2, 3) as $num):',
' echo "Num: $num";',
' endforeach;',
'}',
'?>',
'',
'<script>',
' function test() {',
' ',
' }',
'</script>',
'<style>',
' div {',
' color: red;',
' }',
'</style>'
]);

session.setFoldStyle("markbeginend");
Expand All @@ -41,6 +66,18 @@ module.exports = {
assert.equal(session.getFoldWidget(17), "start");
assert.equal(session.getFoldWidget(19), "end");
assert.equal(session.getFoldWidget(20), "end");
assert.equal(session.getFoldWidget(21), "");
assert.equal(session.getFoldWidget(22), "");
assert.equal(session.getFoldWidget(23), "start");
assert.equal(session.getFoldWidget(24), "start");
assert.equal(session.getFoldWidget(25), "");
assert.equal(session.getFoldWidget(26), "end");
assert.equal(session.getFoldWidget(27), "end");
assert.equal(session.getFoldWidget(28), "start");
assert.equal(session.getFoldWidget(29), "start");
assert.equal(session.getFoldWidget(30), "");
assert.equal(session.getFoldWidget(31), "end");
assert.equal(session.getFoldWidget(32), "end");

assert.range(session.getFoldWidgetRange(2), 2, 1, 20, 0); // Range for the function's foldable section
assert.range(session.getFoldWidgetRange(3), 3, 21, 16, 7); // Range for the 'switch' statement
Expand All @@ -50,6 +87,14 @@ module.exports = {
assert.range(session.getFoldWidgetRange(12), 10, 16, 12, 0); // Range for the 'endif' line
assert.range(session.getFoldWidgetRange(17), 17, 33, 19, 3);
assert.range(session.getFoldWidgetRange(19), 17, 33, 19, 3);
assert.range(session.getFoldWidgetRange(23), 23, 8, 27, 0); // Range for script tag
assert.range(session.getFoldWidgetRange(24), 24, 21, 26, 4); // Range for cstyle { } block
assert.range(session.getFoldWidgetRange(26), 24, 21, 26, 4); // Range for closing cstyle { } block
assert.range(session.getFoldWidgetRange(27), 23, 8, 27, 0); // Range for closing script tag
assert.range(session.getFoldWidgetRange(28), 28, 7, 32, 0); // Range for openning style tag
assert.range(session.getFoldWidgetRange(29), 29, 9, 31, 4); // Range for cstyle { } block
assert.range(session.getFoldWidgetRange(31), 29, 9, 31, 4); // Range for closing cstyle { } block
assert.range(session.getFoldWidgetRange(32), 28, 7, 32, 0); // Range for closing style tag
}
};

Expand Down
15 changes: 13 additions & 2 deletions src/mode/php.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ var WorkerClient = require("../worker/worker_client").WorkerClient;
var PhpCompletions = require("./php_completions").PhpCompletions;
var PhpFoldMode = require("./folding/php").FoldMode;
var unicode = require("../unicode");
var MixedFoldMode = require("./folding/mixed").FoldMode;
var HtmlFoldMode = require("./folding/html").FoldMode;
var CstyleFoldMode = require("./folding/cstyle").FoldMode;
var HtmlMode = require("./html").Mode;
var JavaScriptMode = require("./javascript").Mode;
var CssMode = require("./css").Mode;
Expand All @@ -18,7 +21,11 @@ var PhpMode = function(opts) {
this.$outdent = new MatchingBraceOutdent();
this.$behaviour = this.$defaultBehaviour;
this.$completer = new PhpCompletions();
this.foldingRules = new PhpFoldMode();
this.foldingRules = new MixedFoldMode(new HtmlFoldMode(), {
"js-": new CstyleFoldMode(),
"css-": new CstyleFoldMode(),
"php-": new PhpFoldMode()
});
};
oop.inherits(PhpMode, TextMode);

Expand Down Expand Up @@ -91,7 +98,11 @@ var Mode = function(opts) {
"css-": CssMode,
"php-": PhpMode
});
this.foldingRules = new PhpFoldMode();
this.foldingRules = new MixedFoldMode(new HtmlFoldMode(), {
"js-": new CstyleFoldMode(),
"css-": new CstyleFoldMode(),
"php-": new PhpFoldMode()
});
};
oop.inherits(Mode, HtmlMode);

Expand Down
Loading