1+ define ( [
2+ 'hr/promise' ,
3+ 'hr/utils' ,
4+ 'hr/hr' ,
5+ 'models/command' ,
6+ 'core/commands/menu' ,
7+ 'core/backends/rpc' ,
8+ 'core/search' ,
9+ 'core/files' ,
10+ 'utils/dialogs'
11+ ] , function ( Q , _ , hr , Command , menu , rpc , search , files , dialogs ) {
12+ var OPTIONS = [
13+ 'query' , 'casesensitive' , 'replacement' , 'pattern' , 'maxresults' ,
14+ 'wholeword' , 'regexp' , 'replaceAll'
15+ ] ;
16+
17+
18+ // Normalize results as a buffer
19+ var normResults = function ( results ) {
20+ // Header
21+ var buffer = 'Searching 1 file for "' + results . options . query + '"' ;
22+ if ( results . options . casesensitive ) buffer += " (case sensitive)"
23+ buffer += '\n\n' ;
24+
25+ _ . each ( results . files , function ( lines , path ) {
26+ buffer += path + "\n" ;
27+ _ . each ( lines , function ( line ) {
28+ buffer += line . line + " " + line . content + "\n" ;
29+ } ) ;
30+ buffer += '\n\n' ;
31+ } ) ;
32+
33+ // Footer
34+ buffer += results . matches + " matches across " + _ . size ( results . files ) + " files" ;
35+
36+ return buffer ;
37+ } ;
38+
39+ // Do a basic search
40+ var searchCode = function ( options ) {
41+ options = _ . extend ( { } , options || { } ) ;
42+ return rpc . execute ( "search/code" , _ . pick ( options , OPTIONS ) ) ;
43+ } ;
44+
45+ var doSearch = function ( _args ) {
46+ return searchCode ( _args )
47+ . then ( function ( results ) {
48+ return normResults ( results ) ;
49+ } )
50+ . then ( function ( buffer ) {
51+ return files . openNew ( "Find Results" , buffer ) ;
52+ } )
53+ . fail ( function ( err ) {
54+ console . error ( "error" , err ) ;
55+ } ) ;
56+ } ;
57+
58+ // Command search code
59+ var commandSearch = Command . register ( "code.search" , {
60+ title : "Find in Files" ,
61+ category : "Find" ,
62+ shortcuts : [
63+ "mod+shift+f"
64+ ] ,
65+ action : function ( args ) {
66+ if ( _ . isString ( args ) ) args = { 'query' : args } ;
67+ args = _ . defaults ( args || { } , { } ) ;
68+
69+ if ( ! args . query ) {
70+ return dialogs . fields ( "Find in Files" , {
71+ 'query' : {
72+ 'label' : "Find" ,
73+ 'type' : "text"
74+ } ,
75+ 'casesensitive' : {
76+ 'label' : "Case Sensitive" ,
77+ 'type' : "checkbox"
78+ }
79+ } , args )
80+ . then ( doSearch ) ;
81+ }
82+
83+ return doSearch ( args ) ;
84+ }
85+ } ) ;
86+
87+ // Command replace code
88+ var commandReplace = Command . register ( "code.replace" , {
89+ title : "Replace in Files" ,
90+ category : "Find" ,
91+ shortcuts : [ ] ,
92+ action : function ( args ) {
93+ if ( _ . isString ( args ) ) args = { 'query' : args } ;
94+ args = _ . defaults ( args || { } , { } ) ;
95+
96+ if ( ! args . query ) {
97+ return dialogs . fields ( "Find and Replace in Files" , {
98+ 'query' : {
99+ 'label' : "Find" ,
100+ 'type' : "text"
101+ } ,
102+ 'replacement' : {
103+ 'label' : "Replace" ,
104+ 'type' : "text"
105+ } ,
106+ 'casesensitive' : {
107+ 'label' : "Case Sensitive" ,
108+ 'type' : "checkbox"
109+ }
110+ } , args )
111+ . then ( doSearch ) ;
112+ }
113+
114+ return doSearch ( args ) ;
115+ }
116+ } )
117+
118+
119+ // Create find menu
120+ menu . register ( "find" , {
121+ title : "Find" ,
122+ position : 5
123+ } ) . menuSection ( [
124+ commandSearch ,
125+ commandReplace
126+ ] ) ;
127+
128+ return {
129+ search : searchCode
130+ } ;
131+ } ) ;
0 commit comments