Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Fix semicolon handling around arguments blocks
A semicolon must behave the same as a comment between a function
declaration line and and an arguments block beginning.
A semicolon must end a function declaration even if there's no
following line break.
  • Loading branch information
oswald3141 committed Jun 25, 2023
commit fd6ed923c060e0295cf58a8aa8bf2c7c7e5d7480
9 changes: 8 additions & 1 deletion lexers/LexMatlab.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,19 @@ static void ColouriseMatlabOctaveDoc(
funcDeclarationLine = false;
}

// Semicolon ends function declaration
// This condition is for one line functions support
if (sc.chPrev == ';') {
funcDeclarationLine = false;
}

// Only comments allowed between the function declaration and the
// arguments code block
if (expectingArgumentsBlock && !(funcDeclarationLine || inArgumentsScope)) {
if ((sc.state != SCE_MATLAB_KEYWORD) &&
(sc.state != SCE_MATLAB_COMMENT) &&
(sc.state != SCE_MATLAB_DEFAULT)) {
(sc.state != SCE_MATLAB_DEFAULT) &&
!(sc.state == SCE_MATLAB_OPERATOR && sc.chPrev == ';')) {
expectingArgumentsBlock = 0;
styler.SetLineState(curLine, ComposeLineState(
commentDepth, foldingLevel, expectingArgumentsBlock, inClassScope, inArgumentsScope));
Expand Down
14 changes: 14 additions & 0 deletions test/examples/matlab/ArgumentsBlock.m.matlab
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ arguments = 10;
y = x + arguments;
end

% Semicolon is equivalent to a comment
function y = foo(x)
;;;;;;;
arguments
x
end
y = x + 2;
end

% Arguments block is illegal in nested functions,
% but lexer should process it anyway
function y = foo (x)
Expand Down Expand Up @@ -121,3 +130,8 @@ arguments = {"now", "it's", "variable"};
[a, b] = bar(x, y, arguments);

end

% One line function with arguments block.
% This code style is rarely used (if at all), but the
% lexer shouldn't break
function y = foo(x); arguments; x; end; y = bar(x); end
14 changes: 14 additions & 0 deletions test/examples/matlab/ArgumentsBlock.m.matlab.folded
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@
0 401 401 | y = x + arguments;
0 401 400 | end
1 400 400
0 400 400 % Semicolon is equivalent to a comment
2 400 401 + function y = foo(x)
0 401 401 | ;;;;;;;
2 401 402 + arguments
0 402 402 | x
0 402 401 | end
0 401 401 | y = x + 2;
0 401 400 | end
1 400 400
0 400 400 % Arguments block is illegal in nested functions,
0 400 400 % but lexer should process it anyway
2 400 401 + function y = foo (x)
Expand Down Expand Up @@ -121,4 +130,9 @@
0 400 400 [a, b] = bar(x, y, arguments);
1 400 400
0 400 3ff end
1 3ff 3ff
0 3ff 3ff % One line function with arguments block.
0 3ff 3ff % This code style is rarely used (if at all), but the
0 3ff 3ff % lexer shouldn't break
0 3ff 3ff function y = foo(x); arguments; x; end; y = bar(x); end
1 3ff 3ff
14 changes: 14 additions & 0 deletions test/examples/matlab/ArgumentsBlock.m.matlab.styled
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@
{7}y{0} {6}={0} {7}x{0} {6}+{0} {7}arguments{6};{0}
{4}end{0}

{1}% Semicolon is equivalent to a comment{0}
{4}function{0} {7}y{0} {6}={0} {7}foo{6}({7}x{6}){0}
{6};;;;;;;{0}
{4}arguments{0}
{7}x{0}
{4}end{0}
{7}y{0} {6}={0} {7}x{0} {6}+{0} {3}2{6};{0}
{4}end{0}

{1}% Arguments block is illegal in nested functions,{0}
{1}% but lexer should process it anyway{0}
{4}function{0} {7}y{0} {6}={0} {7}foo{0} {6}({7}x{6}){0}
Expand Down Expand Up @@ -121,3 +130,8 @@
{6}[{7}a{6},{0} {7}b{6}]{0} {6}={0} {7}bar{6}({7}x{6},{0} {7}y{6},{0} {7}arguments{6});{0}

{4}end{0}

{1}% One line function with arguments block.{0}
{1}% This code style is rarely used (if at all), but the{0}
{1}% lexer shouldn't break{0}
{4}function{0} {7}y{0} {6}={0} {7}foo{6}({7}x{6});{0} {4}arguments{6};{0} {7}x{6};{0} {4}end{6};{0} {7}y{0} {6}={0} {7}bar{6}({7}x{6});{0} {4}end{0}