Skip to content
This repository was archived by the owner on Jan 27, 2023. It is now read-only.

Commit ca199c8

Browse files
Merge pull request #3 from martinfinke/master
Sublime Text 3 and spaces in paths
2 parents 572cbd2 + c99a553 commit ca199c8

3 files changed

Lines changed: 45 additions & 10 deletions

File tree

search_in_project.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
import sublime
22
import sublime_plugin
33
import 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

925
class 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) + "/\""

searchengines/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ def _command_line(self, query, folders):
4848
self.mandatory_options,
4949
self.common_options,
5050
query
51-
] + folders)
51+
] + folders)
5252

5353
def _sanitize_output(self, output):
54-
return unicode(output, errors='replace')
54+
return output.decode('utf-8')
5555

5656
def _parse_output(self, output):
5757
lines = output.split("\n")

searchengines/the_silver_searcher.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
1-
import base
1+
import os, sys, inspect
2+
# realpath() with make your script run, even if you symlink it :)
3+
cmd_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0]))
4+
if cmd_folder not in sys.path:
5+
sys.path.insert(0, cmd_folder)
6+
# use this if you want to include modules from a subforder
7+
cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"subfolder")))
8+
if cmd_subfolder not in sys.path:
9+
sys.path.insert(0, cmd_subfolder)
10+
# Info:
11+
# cmd_folder = os.path.dirname(os.path.abspath(__file__)) # DO NOT USE __file__ !!!
12+
# __file__ fails if script is called in different ways on Windows
13+
# __file__ fails if someone does os.chdir() before
14+
# sys.argv[0] also fails because it doesn't not always contains the path
215

16+
import base
317

418
class TheSilverSearcher (base.Base):
519
pass

0 commit comments

Comments
 (0)