1
1
// simple Find In Files tool for searching files containting given string
2
2
using System ;
3
3
using System . Collections . Generic ;
4
+ using System . Diagnostics ;
4
5
using System . IO ;
5
6
using System . Linq ;
6
7
using System . Windows ;
8
+ using System . Windows . Input ;
7
9
8
10
namespace FindInFiles
9
11
{
@@ -36,9 +38,9 @@ void Start()
36
38
37
39
private void btnFind_Click ( object sender , RoutedEventArgs e )
38
40
{
39
- if ( string . IsNullOrEmpty ( txtString . Text ) == false )
41
+ if ( string . IsNullOrEmpty ( txtSearch . Text ) == false )
40
42
{
41
- SearchFiles ( txtString . Text , txtFolder . Text ) ;
43
+ SearchFiles ( txtSearch . Text , txtFolder . Text ) ;
42
44
}
43
45
}
44
46
@@ -47,10 +49,38 @@ private void btnBrowse_Click(object sender, RoutedEventArgs e)
47
49
// TODO browse for folder
48
50
}
49
51
52
+ // special keys for search field
53
+ private void OnKeyDownSearch ( object sender , KeyEventArgs e )
54
+ {
55
+ switch ( e . Key )
56
+ {
57
+ case Key . Escape :
58
+ txtSearch . Text = "" ;
59
+ break ;
60
+ case Key . Return :
61
+ SearchFiles ( txtSearch . Text , txtFolder . Text ) ;
62
+ break ;
63
+ default :
64
+ break ;
65
+ }
66
+ }
67
+
68
+ // open file on double click
69
+ private void gridResults_MouseDoubleClick ( object sender , MouseButtonEventArgs e )
70
+ {
71
+ if ( gridResults . SelectedItem == null ) return ;
72
+ var selectedRow = gridResults . SelectedItem as ResultItem ;
73
+
74
+ Process myProcess = new Process ( ) ;
75
+ myProcess . StartInfo . FileName = selectedRow . path ;
76
+ //myProcess.StartInfo.Arguments = "-n###" ;// TODO jump to line in notepad++, but need to know linenumber..
77
+ myProcess . Start ( ) ;
78
+ }
79
+
50
80
void SearchFiles ( string searchString , string sourceFolder )
51
81
{
52
82
// validate folder
53
- if ( Directory . Exists ( sourceFolder ) == false )
83
+ if ( Directory . Exists ( sourceFolder ) == false )
54
84
{
55
85
return ;
56
86
}
@@ -74,6 +104,13 @@ void SearchFiles(string searchString, string sourceFolder)
74
104
var o = new ResultItem ( ) ;
75
105
o . path = files [ i ] ;
76
106
o . snippet = wholeFile . Substring ( hitIndex , ( previewSnippetLength + hitIndex >= wholeFile . Length ) ? wholeFile . Length : previewSnippetLength ) ;
107
+ o . snippet = o . snippet . Replace ( '\n ' , '¶' ) ; // replace end of lines
108
+
109
+ // TODO get linenumber, or estimate?
110
+ // but that would mean have to parse again??
111
+ // maybe do this only if selected file.. (but that means need to read file again..)
112
+ // o.lineNumber = ...
113
+
77
114
results . Add ( o ) ;
78
115
continue ;
79
116
}
@@ -87,6 +124,7 @@ public class ResultItem
87
124
{
88
125
public string path { get ; set ; }
89
126
public string snippet { get ; set ; }
127
+ // public int lineNumber { get; set; }
90
128
}
91
129
92
130
0 commit comments