@@ -27,6 +27,8 @@ const fs = require('fs'),
27
27
*
28
28
*/
29
29
const complete = ( match , prompt , { temporaryContainer, partial= temporaryContainer . partial , dirname= temporaryContainer . dirname } ) => {
30
+ debug ( 'completion' , match , partial , dirname )
31
+
30
32
// in case match includes partial as a prefix
31
33
const partialIdx = match . indexOf ( partial ) ,
32
34
completion = partialIdx >= 0 ? match . substring ( partialIdx + partial . length ) : match
@@ -42,9 +44,11 @@ const complete = (match, prompt, { temporaryContainer, partial=temporaryContaine
42
44
if ( ! err ) {
43
45
if ( stats . isDirectory ( ) ) {
44
46
// add a trailing slash if the dirname/match is a directory
47
+ debug ( 'complete as directory' )
45
48
prompt . value = prompt . value + completion + '/'
46
49
} else {
47
50
// otherwise, dirname/match is not a directory
51
+ debug ( 'complete as file' )
48
52
prompt . value = prompt . value + completion
49
53
}
50
54
} else {
@@ -54,8 +58,11 @@ const complete = (match, prompt, { temporaryContainer, partial=temporaryContaine
54
58
55
59
} else {
56
60
// otherwise, just add the completion to the prompt
61
+ debug ( 'complete as file (alt)' )
57
62
prompt . value = prompt . value + completion
58
63
}
64
+ } else {
65
+ debug ( 'no completion string' )
59
66
}
60
67
}
61
68
@@ -265,54 +272,59 @@ const suggestLocalFile = (last, block, prompt, temporaryContainer, lastIdx) => {
265
272
// dirname will "foo" in the above example; it
266
273
// could also be that last is itself the name
267
274
// of a directory
268
- const lastIsDir = last . charAt ( last . length - 1 ) === '/'
269
- dirname = lastIsDir ? last : path . dirname ( last )
275
+ const lastIsDir = last . charAt ( last . length - 1 ) === '/' ,
276
+ dirname = lastIsDir ? last : path . dirname ( last )
270
277
278
+ debug ( 'suggest local file' , dirname , last )
279
+
271
280
if ( dirname ) {
272
- fs . access ( dirname , err => {
273
- if ( ! err ) {
274
- // then dirname exists! now scan the directory so we can find matches
275
- fs . readdir ( dirname , ( err , files ) => {
276
- if ( ! err ) {
277
- const partial = path . basename ( last ) ,
278
- matches = files . filter ( f => ( lastIsDir || f . indexOf ( partial ) === 0 )
279
- && ! f . endsWith ( '~' ) && ! f . startsWith ( '.' ) )
280
-
281
- if ( matches . length === 1 ) {
282
- //
283
- // then there is one unique match, so autofill it now;
284
- // completion will be the bit we have to append to the current prompt.value
285
- //
286
- complete ( matches [ 0 ] , prompt , { temporaryContainer, partial, dirname } )
287
-
288
- } else if ( matches . length > 1 ) {
289
- //
290
- // then there are multiple matches, present the choices
291
- //
292
-
293
- // make a temporary div to house the completion options,
294
- // and attach it to the block that encloses the current prompt
295
- if ( ! temporaryContainer ) {
296
- temporaryContainer = makeCompletionContainer ( block , prompt , partial , dirname , lastIdx )
297
- }
281
+ // then dirname exists! now scan the directory so we can find matches
282
+ fs . readdir ( dirname , ( err , files ) => {
283
+ if ( err ) {
284
+ debug ( 'fs.readdir error' , err )
285
+
286
+ } else {
287
+ debug ( 'fs.readdir success' )
288
+
289
+ const partial = path . basename ( last ) ,
290
+ matches = files . filter ( f => ( lastIsDir || f . indexOf ( partial ) === 0 )
291
+ && ! f . endsWith ( '~' ) && ! f . startsWith ( '.' ) )
292
+
293
+ if ( matches . length === 1 ) {
294
+ //
295
+ // then there is one unique match, so autofill it now;
296
+ // completion will be the bit we have to append to the current prompt.value
297
+ //
298
+ debug ( 'singleton file completion' , matches [ 0 ] )
299
+ complete ( matches [ 0 ] , prompt , { temporaryContainer, partial, dirname } )
300
+
301
+ } else if ( matches . length > 1 ) {
302
+ //
303
+ // then there are multiple matches, present the choices
304
+ //
305
+ debug ( 'multi file completion' )
306
+
307
+ // make a temporary div to house the completion options,
308
+ // and attach it to the block that encloses the current prompt
309
+ if ( ! temporaryContainer ) {
310
+ temporaryContainer = makeCompletionContainer ( block , prompt , partial , dirname , lastIdx )
311
+ }
298
312
299
- // add each match to that temporary div
300
- matches . forEach ( ( match , idx ) => {
301
- const { option, optionInner } = addSuggestion ( temporaryContainer , partial , dirname , prompt ) ( match , idx )
313
+ // add each match to that temporary div
314
+ matches . forEach ( ( match , idx ) => {
315
+ const { option, optionInner } = addSuggestion ( temporaryContainer , partial , dirname , prompt ) ( match , idx )
302
316
303
- // see if the match is a directory, so that we add a trailing slash
304
- fs . lstat ( expandHomeDir ( path . join ( dirname , match ) ) , ( err , stats ) => {
305
- if ( ! err && stats . isDirectory ( ) ) {
306
- optionInner . innerText = match + '/'
307
- } else {
308
- optionInner . innerText = match
309
- }
310
- option . setAttribute ( 'data-value' , option . innerText )
311
- } )
312
- } )
313
- }
314
- }
315
- } )
317
+ // see if the match is a directory, so that we add a trailing slash
318
+ fs . lstat ( expandHomeDir ( path . join ( dirname , match ) ) , ( err , stats ) => {
319
+ if ( ! err && stats . isDirectory ( ) ) {
320
+ optionInner . innerText = match + '/'
321
+ } else {
322
+ optionInner . innerText = match
323
+ }
324
+ option . setAttribute ( 'data-value' , option . innerText )
325
+ } )
326
+ } )
327
+ }
316
328
}
317
329
} )
318
330
}
0 commit comments