forked from mikaeljorhult/brackets-todo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
executable file
·519 lines (440 loc) · 13.9 KB
/
main.js
File metadata and controls
executable file
·519 lines (440 loc) · 13.9 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*!
* Brackets Todo 0.7.0
* Display all todo comments in current document or project.
*
* @author Mikael Jorhult
* @license http://mikaeljorhult.mit-license.org MIT
*/
define( function( require, exports, module ) {
'use strict';
// Get dependencies.
var Async = brackets.getModule( 'utils/Async' ),
Menus = brackets.getModule( 'command/Menus' ),
NativeApp = brackets.getModule( 'utils/NativeApp' ),
CommandManager = brackets.getModule( 'command/CommandManager' ),
Commands = brackets.getModule( 'command/Commands' ),
EditorManager = brackets.getModule( 'editor/EditorManager' ),
DocumentManager = brackets.getModule( 'document/DocumentManager' ),
PanelManager = brackets.getModule( 'view/PanelManager' ),
Resizer = brackets.getModule( 'utils/Resizer' ),
AppInit = brackets.getModule( 'utils/AppInit' ),
FileSystem = brackets.getModule( 'filesystem/FileSystem' ),
ExtensionUtils = brackets.getModule( 'utils/ExtensionUtils' ),
// Extension basics.
COMMAND_ID = 'mikaeljorhult.bracketsTodo.enable',
// Todo modules.
Events = require( 'modules/Events' ),
Files = require( 'modules/Files' ),
FileManager = require( 'modules/FileManager' ),
ParseUtils = require( 'modules/ParseUtils' ),
Paths = require( 'modules/Paths' ),
Settings = require( 'modules/Settings' ),
SettingsManager = require( 'modules/SettingsManager' ),
Strings = require( 'modules/Strings' ),
Tags = require( 'modules/Tags' ),
// Mustache templates.
todoPanelTemplate = require( 'text!html/panel.html' ),
todoResultsTemplate = require( 'text!html/results.html' ),
todoRowTemplate = require( 'text!html/row.html' ),
todoToolbarTemplate = require( 'text!html/tools.html' ),
// Setup extension.
todos = [],
$todoPanel,
$todoIcon = $( '<a href="#" title="' + Strings.EXTENSION_NAME + '" id="brackets-todo-icon"></a>' ),
// Get view menu.
menu = Menus.getMenu( Menus.AppMenuBar.VIEW_MENU );
// Register extension.
CommandManager.register( Strings.EXTENSION_NAME, COMMAND_ID, toggleTodo );
// Add command to menu.
if ( menu !== undefined ) {
menu.addMenuDivider();
menu.addMenuItem( COMMAND_ID, 'Ctrl-Alt-T' );
}
// Load stylesheet.
ExtensionUtils.loadStyleSheet( module, 'todo.css' );
/**
* Set state of extension.
*/
function toggleTodo() {
var enabled = SettingsManager.isExtensionEnabled();
enableTodo( !enabled );
}
/**
* Initialize extension.
*/
function enableTodo( enabled, startup ) {
// Should extension be enabled or not?
if ( enabled ) {
// No need to load settings on startup as it's done on project load.
if ( startup === true ) {
// Only display panel.
Resizer.show( $todoPanel );
} else {
// Load settings and then show panel.
SettingsManager.loadSettings( function() {
// Show panel.
Resizer.show( $todoPanel );
} );
}
} else {
// Hide panel.
Resizer.hide( $todoPanel );
// Remove active class from icon.
$todoIcon.removeClass( 'active' );
}
// Save enabled state.
SettingsManager.setExtensionEnabled( enabled );
// Mark menu item as enabled/disabled.
CommandManager.get( COMMAND_ID ).setChecked( enabled );
}
/**
* Main functionality: Find and show comments.
*/
function run() {
// Get all todo comments.
findTodo();
}
/**
* Go through current document and find each comment.
*/
function findTodo( callback ) {
var filesPromise = FileManager.getFiles(),
todoArray = [];
filesPromise.done( function( files ) {
// Bail if no files.
if ( files.length === 0 ) {
setTodos( todoArray );
// Run callback when completed.
if ( callback ) {
callback();
}
return;
}
// Go through each file asynchronously.
Async.doInParallel( files, function( fileInfo ) {
var result = new $.Deferred();
// Parse each file.
DocumentManager.getDocumentForPath( fileInfo.fullPath ).done( function( currentDocument ) {
// Pass file to parsing.
todoArray = ParseUtils.parseFile( currentDocument, todos );
} ).always( function() {
// Move on to next file.
result.resolve();
} );
return result.promise();
} ).always( function() {
// Add file visibility state.
$.each( todoArray, function( index, file ) {
file.isExpanded( Files.isExpanded( file.path() ) );
} );
// Store array of todos.
setTodos( todoArray );
// Run callback when completed.
if ( callback ) {
callback();
}
} );
} );
}
/**
* Store array of todos.
*/
function setTodos( todoArray ) {
todos = todoArray;
// Publish event.
Events.publish( 'todos:updated' );
}
/**
* Take found todos and add them to panel.
*/
function printTodo() {
var project = ( Settings.get().search.scope === 'project' ? true : false ),
resultsHTML = Mustache.render( todoResultsTemplate, {
todos: renderTodo()
} );
resultsHTML = $( resultsHTML );
// Show file rows if project search scope.
if ( project ) {
$todoPanel.removeClass( 'current' );
} else {
$todoPanel.addClass( 'current' );
}
// Empty container element and apply results template.
$todoPanel.find( '.table-container' )
.empty()
.append( resultsHTML );
}
/**
* Filter todos by tag.
*/
function filterTodosByTag( beforeFilter ) {
// Bail if no files are available.
if ( beforeFilter.length === 0 ) {
return beforeFilter;
}
// Go through each file and only return those with visible comments.
return beforeFilter.filter( function( file ) {
return file.hasVisibleTodos();
} );
}
function sortTodos( beforeFilter ) {
var sortDone = Settings.get().sort.done;
// Go through each file for tasks.
$.each( todos, function( index, file ) {
var todoArray = file.todos();
// Sort tasks by done status if enabled in settings.
if ( sortDone ) {
// Replace old todos with sorted array.
file.todos( todoArray.sort( function( a, b ) {
// Use line to differentiate between to tasks with same status.
if ( a.isDone() === b.isDone() ) {
return ( a.line() < b.line() ? -1 : 1 );
} else {
// One of the tasks are done. Check which one.
if ( a.isDone() ) {
return 1;
} else if ( b.isDone() ) {
return -1;
}
}
// Will not ever happen.
return 0;
} ) );
}
} );
return beforeFilter;
}
/**
* Render HTML for each file row.
*/
function renderTodo() {
var resultsHTML = Mustache.render( todoRowTemplate, {
files: setTodosVisible( sortTodos( filterTodosByTag( todos ) ) )
} );
return resultsHTML;
}
/**
* Keep file visibility as before after file changed.
*/
function setTodosVisible( todos ) {
// Go through each file and expand where file was last expanded.
$.each( todos, function( index, file ) {
file.isExpanded( Files.isExpanded( file.path() ) );
} );
return todos;
}
/**
* Count number of occurences of each tag.
*/
function countByTag( tag ) {
var count = 0;
// Go through each file.
$.each( todos, function( index, file ) {
// Go through each comment.
$.each( file.todos(), function( index, comment ) {
// If comment is of requested type, add one to count.
if ( comment.tag() === tag ) {
count++;
}
} );
} );
return count;
}
/**
* Update toolbar.
*/
function updateTools() {
// Render toolbar and replace old element.
$todoPanel.find( '.tools' )
.html( renderTools() );
}
/**
* Render toolbar.
*/
function renderTools() {
var tags = SettingsManager.getTags();
// Count number of occurences of each tags.
$.each( tags, function( index, tag ) {
tag.count( countByTag( tag.tag() ) );
} );
// Render and return toolbar.
return Mustache.render( todoToolbarTemplate, {
tags: tags,
strings: Strings
} );
}
/**
* Listen for save or refresh and look for todos when needed.
*/
function registerListeners() {
var $documentManager = $( DocumentManager );
// Listeners bound to Todo modules.
Events.subscribe( 'settings:loaded', function() {
// Empty array of files.
setTodos( [] );
// Call parsing function.
run();
} );
Events.subscribe( 'todos:updated', function() {
updateTools();
printTodo();
} );
// Listeners for file changes.
FileSystem.on( 'change', function( event, file ) {
// Bail if not a file or file is outside current project root.
if ( file === null || file.isFile !== true || file.fullPath.indexOf( Paths.projectRoot() ) === -1 ) {
return false;
}
// Reload settings if .todo of current project was updated.
if ( file.fullPath === Paths.todoFile() ) {
SettingsManager.loadSettings();
} else {
// Get document from path and parse.
DocumentManager.getDocumentForPath( file.fullPath ).done( function( document ) {
setTodos( ParseUtils.parseFile( document, todos ) );
} );
}
} );
FileSystem.on( 'rename', function( event, oldName, newName ) {
var todoPath = Paths.todoFile();
// Reload settings if .todo of current project was updated.
if ( newName === todoPath || oldName === todoPath ) {
SettingsManager.loadSettings();
} else {
// If not .todo, parse all files.
run();
}
} );
// Listeners bound to Brackets modules.
$documentManager
.on( 'currentDocumentChange.todo', function() {
var currentDocument = DocumentManager.getCurrentDocument(),
$scrollTarget;
// Bail if no files are open.
if ( !currentDocument ) {
return;
}
// No need to do anything if scope is project.
if ( Settings.get().search.scope === 'project' ) {
// Look for current file in list.
$scrollTarget = $todoPanel.find( '.file' ).filter( '[data-file="' + currentDocument.file.fullPath + '"]' );
// If there's a target, scroll to it.
if ( $scrollTarget.length > 0 ) {
// Close all auto-opened files before opening another.
$scrollTarget.siblings( '.auto-opened' )
.trigger( 'click' )
.removeClass( 'auto-opened' );
// No need to open it if already open.
if ( !$scrollTarget.hasClass( 'expanded' ) ) {
$scrollTarget.trigger( 'click' );
$scrollTarget.addClass( 'auto-opened' );
}
// Scroll to target.
$todoPanel.children( '.table-container' ).scrollTop( $scrollTarget.position().top );
}
} else {
// Empty stored todos and parse current document.
setTodos( ParseUtils.parseFile( currentDocument, [] ) );
}
} )
.on( 'pathDeleted.todo', function( event, deletedPath ) {
var todoPath = Paths.todoFile();
// Reload settings if .todo of current project was deleted.
if ( deletedPath === todoPath ) {
SettingsManager.loadSettings();
}
// Parse path that was deleted to remove from list.
setTodos( ParseUtils.removeFile( deletedPath, todos ) );
} );
}
// Register panel and setup event listeners.
AppInit.appReady( function() {
var todoHTML = Mustache.render( todoPanelTemplate, {
tools: renderTools(),
strings: Strings
} );
// Create and cache todo panel.
PanelManager.createBottomPanel( 'mikaeljorhult.bracketsTodo.panel', $( todoHTML ), 100 );
$todoPanel = $( '#brackets-todo' );
// Close panel when close button is clicked.
$todoPanel
.on( 'click', '.close', function() {
enableTodo( false );
} )
.on( 'click', '.indicator', function() {
var todoFilePath = Paths.todoFile();
// Check if there is a file with the name .todo.
FileSystem.resolve( todoFilePath, function( error, entry ) {
// Check if the todo file is present.
if ( entry !== undefined ) {
// Open .todo filein editor.
CommandManager.execute( Commands.FILE_OPEN, { fullPath: todoFilePath } ).done( function() {
// Set focus on editor.
EditorManager.focusEditor();
} );
} else {
// Show settings dialog.
SettingsManager.showSettingsDialog();
}
} );
} )
.on( 'click', 'a[ rel="external" ]', function() {
// Open link in default browser.
NativeApp.openURLInDefaultBrowser( $( this ).data( 'href' ) );
return false;
} )
.on( 'click', '.file', function() {
// Change classes and toggle visibility of todos.
$( this )
.toggleClass( 'expanded' )
.toggleClass( 'collapsed' );
// Store array of expanded files.
Files.saveExpanded( $.makeArray( $todoPanel.find( '.file.expanded' ).map( function() {
return $( this ).data( 'file' );
} ) ) );
} )
.on( 'click', '.comment', function() {
var $this = $( this );
// Open file that todo originate from.
CommandManager.execute( Commands.FILE_OPEN, { fullPath: $this.parents( '.file' ).data( 'file' ) } ).done( function() {
// Set cursor position at start of todo.
EditorManager.getCurrentFullEditor()
.setCursorPos( $this.data( 'line' ) - 1, $this.data( 'char' ), true );
// Set focus on editor.
EditorManager.focusEditor();
} );
return false;
} )
.on( 'click', '.collapse-all', function() {
// Click all expanded files to collapse them.
$todoPanel.find( '.file.expanded' )
.trigger( 'click' );
} )
.on( 'click', '.expand-all', function() {
// Click all collapsed files to expand them.
$todoPanel.find( '.file.collapsed' )
.trigger( 'click' );
} )
.on( 'click', '.tags a', function() {
// Show or hide clicked tag.
var $this = $( this )
.toggleClass( 'visible' );
// Save names of hidden tags.
Tags.saveHidden( $.makeArray( $this.parent().children().not( '.visible' ).map( function() {
return $( this ).data( 'name' );
} ) ) );
// Update list of comments.
Events.publish( 'todos:updated' );
} );
// Setup listeners.
registerListeners();
// Add listener for toolbar icon..
$todoIcon.click( function() {
CommandManager.execute( COMMAND_ID );
} ).appendTo( '#main-toolbar .buttons' );
// Enable extension if loaded last time.
if ( SettingsManager.isExtensionEnabled() ) {
enableTodo( true, true );
}
} );
} );