@@ -6,42 +6,39 @@ import { window, commands, ExtensionContext, QuickPickItem, QuickPickOptions, wo
66
77const projectRoot = workspace . rootPath ? workspace . rootPath : '.' ;
88
9- // this method is called when your extension is activated
10- // your extension is activated the very first time the command is executed
119export function activate ( context : ExtensionContext ) {
1210
13- // Use the console to output diagnostic information (console.log) and errors (console.error)
14- // This line of code will only be executed once when your extension is activated
15- console . log ( 'Congratulations, your extension "vscode-git-grep" is now active!' ) ;
16-
17- // The command has been defined in the package.json file
18- // Now provide the implementation of the command with registerCommand
19- // The commandId parameter must match the command field in package.json
20- let disposable = commands . registerCommand ( 'extension.gitGrep' , ( ) => {
21- // The code you place here will be executed every time your command is executed
22- window . showInputBox ( { prompt : 'search...' } ) . then ( ( query ) => {
11+ ( async ( ) => {
12+ const disposable = commands . registerCommand ( 'extension.gitGrep' , async ( ) => {
13+ const query = await window . showInputBox ( { prompt : 'search...' } )
2314 const command = quote ( [ 'git' , 'grep' , '-H' , '-n' , query ] ) ;
24- exec ( command , { cwd : projectRoot } , ( err , stdout , stderr ) => {
15+
16+ exec ( command , { cwd : projectRoot } , async ( err , stdout , stderr ) => {
2517 const lines = stdout . split ( / \n / ) ;
26- console . log ( lines [ 0 ] )
27- window . showQuickPick ( lines ) . then ( ( l ) => {
28- const [ file , line ] = l . split ( ':' ) ;
29- console . log ( file )
30- workspace . openTextDocument ( projectRoot + '/' + file ) . then ( doc => {
31- console . log ( "openTextDocument success" , doc . fileName ) ;
32- window . showTextDocument ( doc ) . then ( ( ) => {
33- const newSection = new Selection ( ~ ~ line , 0 , ~ ~ line , 0 ) ;
34- window . activeTextEditor . selection = newSection ;
35- commands . executeCommand ( 'cursorUp' ) ;
36- } ) ;
37- } ) ;
38- } ) ;
18+ console . log ( 'err' , err )
19+ if ( err ) {
20+ console . log ( stderr ) ;
21+ window . showErrorMessage ( stderr ) ;
22+ return ;
23+ }
24+ if ( lines . length === 1 && lines [ 0 ] === '' ) {
25+ window . showInformationMessage ( 'There are no items' )
26+ return Promise . resolve ( ) ;
27+ }
28+ const l = await window . showQuickPick ( lines ) ;
29+ const [ file , line ] = l . split ( ':' ) ;
30+ const doc = await workspace . openTextDocument ( projectRoot + '/' + file ) ;
31+ await window . showTextDocument ( doc ) ;
32+ const selection = new Selection ( ~ ~ line , 0 , ~ ~ line , 0 ) ;
33+ window . activeTextEditor . selection = selection ;
34+ commands . executeCommand ( 'cursorUp' ) ;
3935 context . subscriptions . push ( disposable ) ;
40- } ) ;
36+ } )
4137 } ) ;
38+ } ) ( ) . catch ( ( error ) => {
39+ window . showErrorMessage ( error ) ;
4240 } ) ;
4341}
4442
45- // this method is called when your extension is deactivated
4643export function deactivate ( ) {
4744}
0 commit comments