Skip to content

Commit

Permalink
Import completion
Browse files Browse the repository at this point in the history
* Add navto command client

* (wip) navto func

* Add search command

* wip

* move import functions

* Replace identifier

* add import command

* Add docs

* wip

* refactor

* Move test files

* Modify runner

* Add test

* Add cache to external-check

* Add getImportDeclarations test

* Modify runner

* Add doc
  • Loading branch information
Quramy committed May 10, 2016
1 parent 6e9bb5e commit 56ab358
Show file tree
Hide file tree
Showing 53 changed files with 758 additions and 76 deletions.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ Type `<C-^>` in normal mode or visual mode, Tsuquyomi shows a list of location w

Alternatively, call the Ex command `:TsuquyomiReferences`.

### Keyword search
Call the Ex command `:TsuquyomiSearch {keyword}` to get the list of locations which contain the keyword. This command searches the keyword from opened or referenced files in your project.

### Show quickfix
When a buffer is saved, Tsuquyomi checks syntax and semantics.
And if it contains errors, Tsuquyomi shows them to Vim quickfix window.
Expand Down Expand Up @@ -245,6 +248,43 @@ And execute the following command, you can confirm the path of tsserver:
:echo tsuquyomi#config#tsscmd()
```

### Create es6 import declaration
**It's highly experimental**

For example, if your buffer is the following state:

```ts
readFile('hoge', 'utf-8', (err, content) => {
if(!err) console.log(content);
});
```

Move cursor onto `readFile` and call `:TsuImport`, so Tsuquyomi appends the import declaration.

```ts
import { readFile } from 'fs';
readFile('hoge', 'utf-8', (err, content) => {
if(!err) console.log(content);
});
```

This command has the following limitation:

* This command searches aliases from only files already opened or referenced
* This command searches aliases which are exported explicitly.

In other words, if your project has the following 2 files, `import { foo } from './lib';` is a valid declaration, but Tsuquyomi creates only `import { foo } from './lib/foo';`.

```ts
/* lib/index.ts */
export * from './foo';
```

```ts
/* lib/foo.ts */
export const var foo = 'FOO'
```

### More details
If you want more details, please see [doc](doc/tsuquyomi.txt).

Expand Down
91 changes: 90 additions & 1 deletion autoload/tsuquyomi.vim
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function! s:flash()
endfunction

function! s:is_valid_identifier(symbol_str)
return a:symbol_str =~ '[A-Za-z_\$][A-Za-z_\$0-9]*'
return a:symbol_str =~ '^[A-Za-z_\$][A-Za-z_\$0-9]*$'
endfunction
" ### Utilites }}}

Expand Down Expand Up @@ -718,6 +718,95 @@ function! tsuquyomi#navBar()
endfunction
" #### NavBar }}}

" #### Navto {{{
function! tsuquyomi#navto(term, kindModifiers, matchKindType)

if len(a:term) < 3
echom "[Tsuquyomi] search term's length should be greater than 3."
return [[], 0]
endif

if len(s:checkOpenAndMessage([expand('%:p')])[1])
return [[], 0]
endif

call s:flash()

let l:filename = expand('%:p')

let result_list = tsuquyomi#tsClient#tsNavto(tsuquyomi#bufManager#normalizePath(l:filename), a:term, 100)

if len(result_list)
let list = []
for result in result_list
let flg = 1
if a:matchKindType == 1
let flg = flg && (result.matchKind=='prefix' || result.matchKind=='exact')
elseif a:matchKindType == 2
let flg = flg && (result.matchKind=='exact')
endif
if a:kindModifiers != ''
let flg = flg && has_key(result, 'kindModifiers') && result.kindModifiers=~a:kindModifiers
endif
if flg
call add(list, result)
endif
endfor
return [list, 1]
else
echohl Error
echom "[Tsuquyomi] Nothing was hit."
echohl none
return [[], 0]
endif

endfunction

function! tsuquyomi#navtoByLoclist(term, kindModifiers, matchKindType)
let [result_list, res_code] = tsuquyomi#navto(a:term, a:kindModifiers, a:matchKindType)
if res_code
let l:location_list = []
for navtoItem in result_list
let text = navtoItem.kind.' '.navtoItem.name
if has_key(navtoItem, 'kindModifiers')
let text = navtoItem.kindModifiers.' '.text
endif
if has_key(navtoItem, 'containerName')
if has_key(navtoItem, 'containerKind')
let text = text.' in '.navtoItem.containerKind.' '.navtoItem.containerName
else
let text = text.' in '.navtoItem.containerName
endif
endif
let l:location_info = {
\'filename': navtoItem.file,
\'lnum': navtoItem.start.line,
\'col': navtoItem.start.offset,
\'text': text
\}
call add(l:location_list, l:location_info)
endfor
if(len(l:location_list) > 0)
call setloclist(0, l:location_list, 'r')
lwindow
endif
endif
endfunction

function! tsuquyomi#navtoByLoclistContain(term)
call tsuquyomi#navtoByLoclist(a:term, '', 0)
endfunction

function! tsuquyomi#navtoByLoclistPrefix(term)
call tsuquyomi#navtoByLoclist(a:term, '', 1)
endfunction

function! tsuquyomi#navtoByLoclistExact(term)
call tsuquyomi#navtoByLoclist(a:term, '', 2)
endfunction

" #### Navto }}}

" ### Public functions }}}

let &cpo = s:save_cpo
Expand Down
Loading

0 comments on commit 56ab358

Please sign in to comment.