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

Import the shortest path with :TsuImport. #104

merged 2 commits into from Dec 18, 2016


None yet
2 participants

bdauria commented Dec 4, 2016

This PR adds the possibility to get the shortest path instead of the complete one using the TsuImport command.
It basically uses vimgrep under the hood and successively check if there is a path exporting the module on the way from the current file to the one containing the imported module.

It starts from the imported module's directory, and walk back toward the current edited file. It stores the current 'shortened path' and reduce it if there is a match against the provided regular expression:

        \"silent! noautocmd vimgrep /export\\s*\\({.*\\(\\s\\|,\\)"
        \. a:module 
        \. substitute(a:current_module_file, '\/', '\\/', '') 
        \."[\\/]*\\(\\'\\|\\\"\\)[;]*/j "
        \. a:module_directory_path 
        \. "*.ts"

if no match is found, the path stored in 'shortened_path' is the one picked up along the way.
The algorithm also loops in the current directory when a match in the module export is found, to try to see if there is another possible path reference. It's the case for instance for the OnInit hook module: It's initially referenced (according to TypeScript navto command) in @angular/core/src/metadata/lifecycle_hooks.d.ts. The module is first exported through the @angular/core/src/metadata.d.ts, which in turn is exported by the core.d.ts file in the same directory.

If an index.d.tsor index.ts is found along the way, the path to find will be modified to include the expression [index]*[/]*, meaning that it could be exported explicitly or through the directory. if such an expression is contained in the final shortest path, it's substituted from it.

The algorithms handle 3 cases:
when the module comes from a package located in node_modules:

from a file located in a directory beyond the current's file path in the tree (relative path starting with ./):

from a file located in a directory before the current's file path (relative path starting with ../):

Also, the noautocmd argument is used for vimgrep which gives a great speed boost to the search. In the end, there is no big difference in the execution time between the command with the shortest path algorithm and without.

To activate the feature, the g:tsuquyomi_shortest_import_path has to be set to 1 explicitly (it's set to 0 by default).

@Quramy Quramy merged commit b7f1995 into Quramy:master Dec 18, 2016

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment