Skip to content

Commit 24ff197

Browse files
authored
Merge pull request #2 from bertramr/matlab_indentation_check
Matlab indentation check
2 parents 726f6b4 + a5cdefa commit 24ff197

7 files changed

Lines changed: 128 additions & 13 deletions

File tree

analyze_file.m

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919
% (c) 2016, Bastian Bechtold
2020
% This code is licensed under the terms of the BSD 3-clause license
2121

22-
beginnings = {'for' 'parfor' 'while' 'if' 'switch' 'classdef' ...
23-
'events' 'properties' 'enumeration' 'methods' ...
24-
'function'};
22+
beginnings = check_settings('beginnings');
2523

2624
blocks = struct('name', {}, 'body', {}, 'nesting', {}, ...
2725
'children', {}, 'variables', {}, ...

check.m

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -958,11 +958,9 @@ function print_report(report, indentation, filename)
958958
% The correct indentation for the current line is (by default):
959959
% (nesting + correction)*4 spaces
960960

961-
beginnings = {'for' 'parfor' 'while' 'if' 'switch' 'classdef' ...
962-
'events' 'properties' 'enumeration' 'methods' ...
963-
'function'};
964-
middles = {'else' 'elseif' 'case' 'otherwise'};
965-
961+
beginnings = check_settings('beginnings');
962+
middles = check_settings('middles');
963+
966964
% deactivate function file rules in class files:
967965
if first_token.isEqual('keyword', 'classdef')
968966
function_nesting = nan;

check_settings.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,18 @@
7474
% languages would think this behavior funny:
7575
settings.indentation_check_like_matlab = true;
7676

77+
% keywords for tokenize_code
78+
settings.keywords = {'for' 'try' 'while' 'if' 'else' 'elseif' 'switch' ...
79+
'case' 'otherwise' 'function' 'classdef' 'methods' ...
80+
'properties' 'events' 'enumeration' 'parfor' ...
81+
'return' 'break' 'continue' 'catch'};
82+
83+
% keyword beginnings which are considered for indentation calculation
84+
settings.beginnings = {'for' 'parfor' 'while' 'if' 'switch' 'classdef' ...
85+
'events' 'properties' 'enumeration' 'methods' ...
86+
'function' 'try'};
87+
% keyword middles which are considered for indentation calculation
88+
settings.middles = {'else' 'elseif' 'case' 'otherwise' 'catch'};
89+
7790
value = settings.(name);
7891
end

testFiles/MatlabIndentedClass.m

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
classdef MatlabIndentedClass
2+
%MATLABINDENTEDCLASS This is an example class for testing the
3+
% indentation check
4+
%
5+
% Some more comments to make the checker happy
6+
% Some more comments to make the checker happy
7+
% returns a new OBJ.
8+
9+
%
10+
%
11+
properties(Access = private)
12+
foobar
13+
end
14+
15+
methods(Access = protected)
16+
17+
function output1 = foo_function(input1, input2)
18+
%FOO_FUNCTION This is an example function for testing the
19+
% indentation check
20+
% output1 = foo_function: input1, input2
21+
% Some more comments to make the checker happy
22+
23+
try
24+
input1 = 42;
25+
catch
26+
input2 = 42;
27+
end
28+
% Some more comments to make the checker happy
29+
if input1
30+
output1 = 1;
31+
elseif input2
32+
output1 = 2;
33+
else
34+
output1 = 0;
35+
end
36+
37+
end
38+
39+
function foobar = second_function(barfoo)
40+
%SECOND_FUNCTION This is an example function for testing the
41+
% indentation check
42+
% foobar, barfoo
43+
foobar = barfoo;
44+
end
45+
46+
function varargout = variable_length_of_in_and_output(varargin)
47+
%VARIABLE_LENGTH_OF_IN_AND_OUTPUT is provided with input param
48+
% VARARGIN and output parameter VARARGOUT
49+
varargout = varargin;
50+
end
51+
52+
function output = test_linebreak_with_continuation_operator(inputarg)
53+
%TEST_LINEBREAK_WITH_CONTINUATION_OPERATOR is a test to verify
54+
% line continuation operator
55+
% INPUTARG, OUTPUT
56+
57+
assignment_at_first_line = ...
58+
inputarg;
59+
60+
assignment_at_second_line = ... some comment
61+
assignment_at_first_line;
62+
63+
output = .... 4 dots give also comment
64+
assignment_at_second_line;
65+
end
66+
67+
function test_switch_case(inputarg)
68+
%TEST_SWITCH_CASE test indentation of switch case
69+
% INPUTARG
70+
% Some more comments to make the checker happy
71+
72+
switch inputarg
73+
case 1
74+
return
75+
case 2
76+
return
77+
otherwise
78+
return
79+
end
80+
end
81+
end
82+
end

test_MatlabIndentedClass.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function test_MatlabIndentedClass()
2+
3+
assert(check_settings('indentation_check_like_matlab') == true)
4+
5+
addpath('testFiles')
6+
check('testFiles/MatlabIndentedClass.m');
7+
8+
end

test_check.m

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,21 @@
112112
tokens = tokenize_code('[a,b],');
113113
assert(tokens(3).hasType('punctuation'))
114114
assert(tokens(6).hasType('linebreak'))
115+
116+
%% comments follow continuation operator
117+
tokens = tokenize_code('... % this is a comment');
118+
assert(tokens(1).hasType('punctuation'));
119+
assert(tokens(3).hasType('comment'));
120+
121+
tokens = tokenize_code('... this is a comment');
122+
assert(tokens(1).hasType('punctuation'));
123+
assert(tokens(2).hasType('space'));
124+
assert(tokens(3).hasType('comment'));
125+
126+
tokens = tokenize_code('....');
127+
assert(tokens(1).hasType('punctuation'));
128+
assert(tokens(2).hasType('comment'));
129+
130+
tokens = tokenize_code('.*...');
131+
assert(tokens(1).hasType('punctuation'));
132+
assert(tokens(2).hasType('punctuation'));

tokenize_code.m

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,13 @@
2727
close_pairs = '}])';
2828
escapes = '!%';
2929

30-
keywords = {'for' 'try' 'while' 'if' 'else' 'elseif' 'switch' ...
31-
'case' 'otherwise' 'function' 'classdef' 'methods' ...
32-
'properties' 'events' 'enumeration' 'parfor' ...
33-
'return' 'break' 'continue'};
30+
keywords = check_settings('keywords');
31+
3432
operators = { '+' '-' '*' '/' '^' '\' ...
3533
'.+' '.-' '.*' './' '.^' '.\' ...
3634
'>' '<' '~' '==' '>=' '<=' '~=' ...
3735
'@' '=' ',' ';' '||' '&&' '|' '&' '...' ':'};
38-
unary_operators = '+-@~';
36+
unary_operators = '+-@~.';
3937

4038
spaces = sprintf(' \t');
4139
breaks = sprintf('\n\r');

0 commit comments

Comments
 (0)