@@ -5,7 +5,7 @@ import { quote } from 'shell-quote';
55import { window , commands , ExtensionContext , workspace , Selection , QuickPickItem , QuickPickOptions } from 'vscode' ;
66
77interface QuickPickItemWithPath extends QuickPickItem {
8- fullpath ?: string ;
8+ fullPath ?: string ;
99}
1010
1111const projectRoot = workspace . rootPath ? workspace . rootPath : '.' ;
@@ -17,42 +17,40 @@ export function activate(context: ExtensionContext) {
1717 const query = await window . showInputBox ( { prompt : 'Please input search word.' } )
1818 const command = quote ( [ 'git' , 'grep' , '-H' , '-n' , query ] ) ;
1919
20- exec ( command , { cwd : projectRoot } , async ( err , stdout , stderr ) => {
21- if ( stderr ) {
22- window . showErrorMessage ( stderr ) ;
23- return Promise . resolve ( ) ;
24- }
25- const lines = stdout . split ( / \n / ) . filter ( l => l !== '' ) ;
26- const items : QuickPickItemWithPath [ ] = lines . map ( l => {
27- const [ fullPath , line , ...desc ] = l . split ( ':' ) ;
28- const path = fullPath . split ( '/' ) ;
29- return {
30- label : `${ path [ path . length - 1 ] } : ${ line } ` ,
31- description : desc . join ( ':' ) ,
32- fullPath : l ,
33- } ;
34- } ) ;
35- if ( ! lines . length ) {
36- window . showInformationMessage ( 'There are no items' )
37- return Promise . resolve ( ) ;
38- }
39- let item ;
40- try {
41- const options : QuickPickOptions = {
42- matchOnDescription : true ,
20+ const fetchItems = ( ) : Promise < QuickPickItemWithPath [ ] > => new Promise ( ( resolve , reject ) => {
21+ exec ( command , { cwd : projectRoot } , ( err , stdout , stderr ) => {
22+ if ( stderr ) {
23+ window . showErrorMessage ( stderr ) ;
24+ return resolve ( [ ] ) ;
25+ }
26+ const lines = stdout . split ( / \n / ) . filter ( l => l !== '' ) ;
27+ if ( ! lines . length ) {
28+ window . showInformationMessage ( 'There are no items' )
29+ return resolve ( [ ] ) ;
4330 }
44- item = await window . showQuickPick ( items , options ) ;
45- } catch ( e ) {
46- window . showErrorMessage ( e ) ;
47- }
48-
49- const [ file , line ] = item . fullPath . split ( ':' ) ;
50- const doc = await workspace . openTextDocument ( projectRoot + '/' + file ) ;
51- await window . showTextDocument ( doc ) ;
52- window . activeTextEditor . selection = new Selection ( ~ ~ line , 0 , ~ ~ line , 0 ) ;
53- commands . executeCommand ( 'cursorUp' ) ;
54- context . subscriptions . push ( disposable ) ;
31+ return resolve ( lines . map ( l => {
32+ const [ fullPath , line , ... desc ] = l . split ( ':' ) ;
33+ const path = fullPath . split ( '/' ) ;
34+ return {
35+ label : ` ${ path [ path . length - 1 ] } : ${ line } ` ,
36+ description : desc . join ( ':' ) ,
37+ fullPath : l ,
38+ } ;
39+ } ) ) ;
40+
41+ } ) ;
5542 } ) ;
43+
44+ const options : QuickPickOptions = {
45+ matchOnDescription : true ,
46+ } ;
47+ const item = await window . showQuickPick ( fetchItems ( ) , options ) ;
48+ const [ file , line ] = item . fullPath . split ( ':' ) ;
49+ const doc = await workspace . openTextDocument ( projectRoot + '/' + file ) ;
50+ await window . showTextDocument ( doc ) ;
51+ window . activeTextEditor . selection = new Selection ( ~ ~ line , 0 , ~ ~ line , 0 ) ;
52+ commands . executeCommand ( 'cursorUp' ) ;
53+ context . subscriptions . push ( disposable ) ;
5654 } ) ;
5755 } ) ( ) . catch ( ( error ) => {
5856 window . showErrorMessage ( error ) ;
0 commit comments