|
a/metric-providers/org.ossmeter.metricprovider.rascal/modules/LOC.rsc |
|
b/metric-providers/org.ossmeter.metricprovider.rascal/modules/LOC.rsc |
|
... |
|
... |
4 |
import analysis::graphs::Graph;
|
4 |
import analysis::graphs::Graph;
|
5 |
import IO;
|
5 |
import IO;
|
6 |
import List;
|
6 |
import List;
|
7 |
import String;
|
7 |
import String;
|
8 |
import Set;
|
8 |
import Set;
|
|
|
9 |
import Manager;
|
9 |
|
10 |
|
10 |
alias locResult = tuple[int total, int comment, int empty, int source];
|
11 |
alias locResult = tuple[int total, int comment, int empty, int source];
|
11 |
|
12 |
|
12 |
int countCommentedLoc(M3 projectModel, loc cu)
|
13 |
locResult countLoc(unknownFileType(int lines)) {
|
13 |
= (0 | it + (doc.end.line - doc.begin.line + 1 - checkForSourceLines(doc)) | doc <- projectModel@documentation[cu]);
|
14 |
return <lines, -1, -1, lines>;
|
14 |
|
|
|
15 |
private int checkForSourceLines(loc commentLoc, str fileContents) {
|
|
|
16 |
str comment = substring(fileContents, commentLoc.offset, commentLoc.offset + commentLoc.length);
|
|
|
17 |
// We will check to see if there are any source code in the commented lines
|
|
|
18 |
loc commentedLines = commentLoc;
|
|
|
19 |
// start from start of the line
|
|
|
20 |
commentedLines.begin.column = 0;
|
|
|
21 |
// increase to the next line to cover the full line
|
|
|
22 |
commentedLines.end.line += 1;
|
|
|
23 |
// we won't take any character from the next line
|
|
|
24 |
commentedLines.end.column = 0;
|
|
|
25 |
list[str] contents = split("\n", fileContents)[commentedLines.begin.line-1..commentedLines.end.line-1];
|
|
|
26 |
str commentedLinesSrc = intercalate("\n", contents);
|
|
|
27 |
// since we look till the start of the next line, we need to make sure we remove the extra \n from the end
|
|
|
28 |
if (isEmpty(last(contents)))
|
|
|
29 |
commentedLinesSrc = replaceLast(commentedLinesSrc, "\n" , "");
|
|
|
30 |
return size(split(trim(comment), trim(commentedLinesSrc)));
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
str removeComments(str contents, M3 fileM3) {
|
|
|
34 |
set[loc] compilationUnit = {src | <name, src> <- fileM3@declarations, isCompilationUnit(name)};
|
|
|
35 |
assert size(compilationUnit) == 1 : "More than one compilation unit in a file???";
|
|
|
36 |
loc cu = getOneFrom(compilationUnit);
|
|
|
37 |
list[str] listContents = split("\n", contents);
|
|
|
38 |
list[str] result = listContents;
|
|
|
39 |
for (loc commentLoc <- fileM3@documentation[cu]) {
|
|
|
40 |
// remove comments
|
|
|
41 |
result = result - slice(listContents, commentLoc.begin.line - 1, commentLoc.end.line - commentLoc.begin.line + 1);
|
|
|
42 |
}
|
|
|
43 |
return intercalate("\n", result);
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
locResult countLoc(loc file) {
|
|
|
47 |
locResult result = <-1,-1,-1,-1>;
|
|
|
48 |
|
|
|
49 |
str fileContents = readFile(file);
|
|
|
50 |
|
|
|
51 |
result.total = size(split("\n", fileContents));
|
|
|
52 |
result.comment = -1;
|
|
|
53 |
result.empty = -1;
|
|
|
54 |
result.source = result.total;
|
|
|
55 |
|
|
|
56 |
return result;
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
loc getCompilationUnit(M3 fileM3) {
|
|
|
60 |
set[loc] compilationUnit = {src | <name, src> <- fileM3@declarations, isCompilationUnit(name)};
|
|
|
61 |
assert size(compilationUnit) == 1 : "More than one compilation unit in a file???";
|
|
|
62 |
loc compilationUnitSrc = getOneFrom(compilationUnit);
|
|
|
63 |
|
|
|
64 |
return compilationUnitSrc;
|
|
|
65 |
}
|
15 |
}
|
66 |
|
16 |
|
67 |
locResult countLoc(M3 fileM3) {
|
17 |
locResult countLoc(M3 fileM3) {
|
68 |
locResult result = <-1,-1,-1,-1>;
|
18 |
return <fileM3.total, fileM3.comments, fileM3.empty, fileM3.source>;
|
69 |
|
19 |
} |
70 |
loc compilationUnitSrc = getCompilationUnit(fileM3);
|
|
|
71 |
|
|
|
72 |
str fileContents = readFile(compilationUnitSrc);
|
|
|
73 |
|
|
|
74 |
result.total = compilationUnitSrc.end.line;
|
|
|
75 |
result.comment = countCommentedLoc(fileM3, fileContents);
|
|
|
76 |
result.empty = countEmptyLoc(fileM3, fileContents);
|
|
|
77 |
result.source = result.total - result.comment - result.empty;
|
|
|
78 |
return result;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
int countTotalLoc(M3 fileM3) {
|
|
|
82 |
loc compilationUnit = getCompilationUnit(fileM3);
|
|
|
83 |
return compilationUnit.end.line;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
int countCommentedLoc(M3 fileM3, str fileContents) {
|
|
|
87 |
int result = 0;
|
|
|
88 |
set[loc] comments = { src | <name, src> <- fileM3@documentation, isCompilationUnit(name) };
|
|
|
89 |
for (source <- comments) {
|
|
|
90 |
result += source.end.line - source.begin.line + 1 - checkForSourceLines(source, fileContents);
|
|
|
91 |
}
|
|
|
92 |
return result;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
int countEmptyLoc(M3 fileM3, str fileContents)
|
|
|
96 |
= (0 | it + 1 | /^\s*$/ <- split("\n", removeComments(fileContents, fileM3)));
|
|
|
97 |
|
|
|
98 |
tuple[str language, int count] locPerLanguage(M3 fileM3) {
|
|
|
99 |
return <"java", countTotalLoc(fileM3)>;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
tuple[str language, int count] locPerLanguage(loc file) {
|
|
|
103 |
return <file.extension, size(readFileLines(file))>;
|
|
|
104 |
}
|
|
|