11import sublime
22import sublime_plugin
33import os .path
4- import searchengines
54
6- basedir = os .getcwdu ()
75
6+ import os , sys , inspect
7+ # realpath() with make your script run, even if you symlink it :)
8+ cmd_folder = os .path .realpath (os .path .abspath (os .path .split (inspect .getfile ( inspect .currentframe () ))[0 ]))
9+ if cmd_folder not in sys .path :
10+ sys .path .insert (0 , cmd_folder )
11+ # use this if you want to include modules from a subforder
12+ cmd_subfolder = os .path .realpath (os .path .abspath (os .path .join (os .path .split (inspect .getfile ( inspect .currentframe () ))[0 ],"subfolder" )))
13+ if cmd_subfolder not in sys .path :
14+ sys .path .insert (0 , cmd_subfolder )
15+ # Info:
16+ # cmd_folder = os.path.dirname(os.path.abspath(__file__)) # DO NOT USE __file__ !!!
17+ # __file__ fails if script is called in different ways on Windows
18+ # __file__ fails if someone does os.chdir() before
19+ # sys.argv[0] also fails because it doesn't not always contains the path
20+
21+ import searchengines
22+
23+ basedir = os .getcwd ()
824
925class SearchInProjectCommand (sublime_plugin .WindowCommand ):
1026 def __init__ (self , window ):
@@ -15,7 +31,7 @@ def __init__(self, window):
1531 def run (self ):
1632 self .settings = sublime .load_settings ('SearchInProject.sublime-settings' )
1733 self .engine_name = self .settings .get ("search_in_project_engine" )
18- pushd = os .getcwdu ()
34+ pushd = os .getcwd ()
1935 os .chdir (basedir )
2036 __import__ ("searchengines.%s" % self .engine_name )
2137 self .engine = searchengines .__dict__ [self .engine_name ].engine_class (self .settings )
@@ -35,23 +51,28 @@ def perform_search(self, text):
3551 self .common_path = self .find_common_path (folders )
3652 self .results = self .engine .run (text , folders )
3753 if self .results :
38- self .results = [[result [0 ].replace (self .common_path , '' ), result [1 ]] for result in self .results ]
54+ self .results = [[result [0 ].replace (self .common_path . replace ( ' \" ' , '' ) , '' ), result [1 ]] for result in self .results ]
3955 self .window .show_quick_panel (self .results , self .goto_result )
4056 else :
4157 self .results = []
4258 self .window .show_quick_panel (["No results" ], None )
4359
4460 def goto_result (self , file_no ):
4561 if file_no != - 1 :
46- file_name = self .common_path + self .results [file_no ][0 ]
62+ file_name = self .common_path . replace ( ' \" ' , '' ) + self .results [file_no ][0 ]
4763 view = self .window .open_file (file_name , sublime .ENCODED_POSITION )
4864 regions = view .find_all (self .last_search_string )
4965 view .add_regions ("search_in_project" , regions , "entity.name.filename.find-in-files" , "circle" , sublime .DRAW_OUTLINED )
5066
5167 def search_folders (self ):
52- return self .window .folders () or [os .path .dirname (self .window .active_view ().file_name ())]
68+ window_folders = self .window .folders ()
69+ for index , item in enumerate (window_folders ):
70+ window_folders [index ] = "\" " + window_folders [index ] + "\" "
71+ file_dirname = ["\" " + os .path .dirname (self .window .active_view ().file_name ()) + "\" " ]
72+ return window_folders or file_dirname
5373
5474 def find_common_path (self , paths ):
75+ paths = [path .replace ("\" " , "" ) for path in paths ]
5576 paths = [path .split ("/" ) for path in paths ]
5677 common_path = []
5778 while 0 not in [len (path ) for path in paths ]:
@@ -60,4 +81,4 @@ def find_common_path(self, paths):
6081 common_path += next_segment
6182 else :
6283 break
63- return "/" .join (common_path ) + "/"
84+ return "\" " + " /" .join (common_path ) + "/\" "
0 commit comments