-
Notifications
You must be signed in to change notification settings - Fork 19
/
helper.js
123 lines (108 loc) · 4.22 KB
/
helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
var helper = {};
var vscode = require('vscode');
var Window = vscode.window;
var Workspace = vscode.workspace;
var language = require('./language');
var Todo = require('./models').Todo;
helper.getUsersWorkspaceConfigurations = function() {
return Workspace.getConfiguration();
};
helper.getFileExtensionForLanguage = function(choosenLanguage) {
return language[choosenLanguage].extension;
};
helper.getFileExludeForLanguage = function(choosenLanguage, workspaceConfig) {
var exclude = '{' + '.vscode' + language[choosenLanguage].exclude;
if (workspaceConfig.todoIgnore) {
var usersExclude = '';
for (var i = 0; i < workspaceConfig.todoIgnore.length; i++) {
var conf = workspaceConfig.todoIgnore[i].trim();
if (conf !== '') {
usersExclude += ',' + conf;
}
}
exclude += usersExclude + '}';
}
return exclude;
};
helper.getScanRegexForLanguage = function(choosenLanguage, workspaceConfig) {
//TODO: Add per-language specific scan expressions
var regex = "(?:TODO|FIXME)\\s*\\W{0,1}(\\s+.*|(?:\\w|\\d).*)$";
if(workspaceConfig.todoScanRegex){
regex = workspaceConfig.todoScanRegex;
}
return regex;
};
var getTodoMessage = function(lineText, match) {
var todoMessage = lineText.substring(lineText.indexOf(match[1]), lineText.length);
if (todoMessage.length > 60) {
todoMessage = todoMessage.substring(0, 57).trim();
todoMessage += '...';
}
return todoMessage;
};
var getTodoLocation = function(pathWithoutFile, todoMessage, line, match) {
var rootPath = Workspace.rootPath + '/';
var outputFile = pathWithoutFile.replace(rootPath, '');
var todoLocation = outputFile + ' ' + (line + 1) + ':' + (todoMessage.indexOf(match[1]) + 1);
if (todoLocation.length > 50) {
todoLocation = '...' + todoLocation.substring(todoLocation.length - 47, todoLocation.length);
}
return todoLocation;
};
var findTodosinSpecifiedFile = function(file, todos, todosList, scanRegex) {
var fileInUri = file.uri.toString();
var pathWithoutFile = fileInUri.substring(7, fileInUri.length);
var regex = new RegExp(scanRegex, "i");
for (var line = 0; line < file.lineCount; line++) {
var lineText = file.lineAt(line).text;
var match = lineText.match(regex);
if (match !== null) {
if (!todos.hasOwnProperty(pathWithoutFile)) {
todos[pathWithoutFile] = [];
}
var todoMessage = getTodoMessage(lineText, match);
var todoLocation = getTodoLocation(pathWithoutFile, todoMessage, line, match);
var todo = new Todo(todoLocation, pathWithoutFile, line, todoMessage, 10);
todosList.push(todo);
todos[pathWithoutFile].push(todo);
}
}
};
var findTodosinFiles = function(files, choosenLanguage, scanRegex, done) {
var todos = {};
todosList = [];
var times = 0;
if (files.length === 0) {
Window.showInformationMessage('There is no **' + choosenLanguage + '** files in the open project.');
// errors
done({ message: 'no files' }, null, null);
} else {
for (var i = 0; i < files.length; i++) {
Workspace.openTextDocument(files[i]).then(function(file) {
findTodosinSpecifiedFile(file, todos, todosList, scanRegex);
}).then(function() {
times++;
if (times === files.length) {
return done(null, todos, todosList);
}
});
}
}
};
helper.findFiles = function(extension, exclude, choosenLanguage, scanRegex, done) {
Workspace.findFiles(extension, exclude, 1000).then(function(files) {
findTodosinFiles(files, choosenLanguage, scanRegex, function(err, todos, todosList) {
done(err, todos, todosList);
});
});
};
helper.createStatusBarItem = function() {
var statusBarItem = Window.createStatusBarItem(vscode.StatusBarAlignment.Left);
statusBarItem.text = 'TODO:s';
statusBarItem.tooltip = 'Show TODO:s';
statusBarItem.command = 'extension.showTodos';
statusBarItem.color = '#FFFFFF';
statusBarItem.show();
return statusBarItem;
};
module.exports = helper;