-
-
Notifications
You must be signed in to change notification settings - Fork 606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add fuzzy matching algorithm for tab completions #1855
Open
Snehil-Shah
wants to merge
27
commits into
stdlib-js:develop
Choose a base branch
from
Snehil-Shah:repl-fuzzy
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
dc73372
feat: add fuzzy matching algorithm for tab completions
Snehil-Shah 3cae6a7
refactor: fix function jsdoc
Snehil-Shah abe7525
Merge branch 'stdlib-js:develop' into repl-fuzzy
Snehil-Shah dcb6014
feat: rewrite the completer engine
Snehil-Shah bef5dd3
feat: improve fuzzy algorithm and sort by relevancy
Snehil-Shah 0aee34d
feat: add support for highlighted completions in terminal mode
Snehil-Shah 7efe1a7
fix: wrong completion previews displayed for fuzzy completion
Snehil-Shah b0ca5c1
fix: suggestions
Snehil-Shah 7cc9270
feat: new tab completions UI with navigation
Snehil-Shah 24da634
refactor: move `fuzzyMatch` to a module & don't fuzzy match for previews
Snehil-Shah 6818055
fix: changes requested
Snehil-Shah 95a72a4
Merge branch 'develop' into repl-fuzzy
Snehil-Shah 0c8b11a
style: lint merged changes
Snehil-Shah 6b7dc99
feat: add a setting to control fuzzy completions
Snehil-Shah 237fb7f
fix: limit height of completions & fix completions with prefixes
Snehil-Shah 576f402
feat: fuzzy completions only when no exact completions
Snehil-Shah 89b5001
fix: bug in completer
Snehil-Shah c464944
test: add tests for tab completions
Snehil-Shah 6d6d7c9
docs: fix test name and comments
Snehil-Shah 2c15d15
fix: tune fuzzy algorithm
Snehil-Shah 0e54cf5
docs: document setting
Snehil-Shah 7128fac
Merge branch 'develop' into repl-fuzzy
Snehil-Shah fa22afb
fix: abnormal completer behavior
Snehil-Shah 7b8074a
refactor: move flag to the completer engine namespace
Snehil-Shah cc2f3c5
refactor: abstract logics into private methods
Snehil-Shah 85efce4
fix: make completer `SIGWINCH` aware
Snehil-Shah 023c31a
test: update tests for TTY
Snehil-Shah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,21 +16,25 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
/* eslint-disable max-statements */ | ||
|
||
'use strict'; | ||
|
||
// MODULES // | ||
|
||
var resolve = require( 'path' ).resolve; | ||
var statSync = require( 'fs' ).statSync; // TODO: replace with stdlib equivalent | ||
Check warning on line 26 in lib/node_modules/@stdlib/repl/lib/complete_require.js
|
||
var logger = require( 'debug' ); | ||
var readDir = require( '@stdlib/fs/read-dir' ).sync; | ||
var startsWith = require( '@stdlib/string/starts-with' ); | ||
var extname = require( '@stdlib/utils/extname' ); | ||
var cwd = require( '@stdlib/process/cwd' ); | ||
var indexRegExp = require( './regexp_index.js' ); // eslint-disable-line stdlib/no-require-index | ||
var indexRegExp = require( './regexp_index.js' ); | ||
var relativePathRegExp = require( './regexp_relative_require_path.js' ); | ||
var pathRegExp = require( './regexp_path.js' ); | ||
var contains = require( './contains.js' ); | ||
var fuzzyMatch = require( './fuzzy_match.js' ); | ||
var sortFuzzyCompletions = require( './sort_fuzzy_completions.js' ); | ||
|
||
|
||
// VARIABLES // | ||
|
@@ -49,14 +53,17 @@ | |
* @param {string} path - path to complete | ||
* @param {Array} paths - module search paths | ||
* @param {Array} exts - supported `require` extensions | ||
* @param {boolean} isFuzzy - boolean indicating if the completions should be strictly fuzzy | ||
* @returns {string} path filter | ||
*/ | ||
function complete( out, path, paths, exts ) { | ||
function complete( out, path, paths, exts, isFuzzy ) { | ||
var fuzzyResults = []; | ||
var filter; | ||
var sfiles; | ||
var subdir; | ||
var files; | ||
var stats; | ||
var match; | ||
var dir; | ||
var ext; | ||
var re; | ||
|
@@ -157,6 +164,89 @@ | |
} | ||
} | ||
} | ||
out.sort(); | ||
|
||
// Only fuzzy search, when no exact candidates found... | ||
if ( !isFuzzy || out.length !== 0 ) { | ||
return filter; | ||
} | ||
// Start searching for fuzzy completions... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't as potentially perf prohibitive as |
||
debug( 'Searching paths for fuzzy completions: %s', paths.join( ', ' ) ); | ||
for ( i = 0; i < paths.length; i++ ) { | ||
// Resolve the subdirectory path to a file system path: | ||
dir = resolve( paths[ i ], subdir ); | ||
debug( 'Resolved directory: %s', dir ); | ||
|
||
debug( 'Reading directory contents...' ); | ||
files = readDir( dir ); | ||
if ( files instanceof Error ) { | ||
debug( 'Unable to read directory: %s. Error: %s', dir, files.message ); | ||
continue; | ||
} | ||
for ( j = 0; j < files.length; j++ ) { | ||
f = files[ j ]; | ||
match = fuzzyMatch( f, filter ); | ||
if ( !match ) { | ||
debug( '%s does not fuzzy match filter %s. Skipping...', f, filter ); | ||
continue; | ||
} | ||
f = resolve( dir, f ); | ||
debug( 'Examining path: %s', f ); | ||
try { | ||
stats = statSync( f ); | ||
if ( stats.isDirectory() ) { | ||
debug( 'Path resolves to a subdirectory.' ); | ||
fuzzyResults.push({ | ||
'score': match.score, | ||
'completion': match.completion + '/' | ||
}); | ||
debug( 'Found a fuzzy completion: %s', fuzzyResults[ fuzzyResults.length-1 ].completion ); | ||
|
||
debug( 'Reading subdirectory contents...' ); | ||
sfiles = readDir( f ); | ||
if ( sfiles instanceof Error ) { | ||
debug( 'Unable to read subdirectory: %s. Error: %s', f, sfiles.message ); | ||
continue; | ||
} | ||
for ( k = 0; k < sfiles.length; k++ ) { | ||
if ( re.test( sfiles[ k ] ) ) { | ||
// Since the subdirectory contains an `index` file, one can simply "require" the subdirectory, thus eliding the full file path: | ||
debug( 'Subdirectory contains an `index` file.' ); | ||
|
||
fuzzyResults.push( match ); | ||
debug( 'Found a fuzzy completion: %s', fuzzyResults[ fuzzyResults.length-1 ].completion ); | ||
} else if ( sfiles[ k ] === 'package.json' ) { | ||
// Since the subdirectory contains a `package.json` file, we **ASSUME** one can simply "require" the subdirectory, thus eliding the full file path (WARNING: we do NOT explicitly check that the main entry point actually exists!): | ||
debug( 'Subdirectory contains a `package.json` file.' ); | ||
|
||
fuzzyResults.push( match ); | ||
debug( 'Found a fuzzy completion: %s', fuzzyResults[ fuzzyResults.length-1 ].completion ); | ||
} | ||
} | ||
} else if ( stats.isFile() ) { | ||
debug( 'Path resolves to a file.' ); | ||
ext = extname( files[ j ] ); | ||
if ( contains( exts.length, exts, 1, 0, ext ) ) { | ||
debug( 'File has supported extension: %s', ext ); | ||
|
||
fuzzyResults.push( match ); | ||
debug( 'Found a fuzzy completion: %s', fuzzyResults[ fuzzyResults.length-1 ].completion ); | ||
} | ||
} else { | ||
debug( 'Path resolves to neither a directory nor a file. Skipping path...' ); | ||
continue; | ||
} | ||
} catch ( err ) { | ||
debug( 'Error: %s', err.message ); | ||
debug( 'Skipping path...' ); | ||
continue; | ||
} | ||
} | ||
} | ||
fuzzyResults = sortFuzzyCompletions( fuzzyResults ); | ||
for ( i = 0; i < fuzzyResults.length; i++ ) { | ||
out.push( fuzzyResults[ i ] ); | ||
} | ||
return filter; | ||
} | ||
|
||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could potentially lead to an enormous number of potential completions. File system access isn't cheap, so I am not sure the best approach. It could be that we'd require matches to meet a higher threshold.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this carries performance risks, we can stick to exact matches in this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be worth seeing how other command-line tools supporting fuzzy walking of the file system handle this. I am sure there have been more than one tool written in Rust for this purpose. Maybe they restrict the number of matches more stringently?