Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions st_cc.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,35 @@ def get_opt(view):
project_settings = view.settings()
include_opts = settings.get("include_options", []) + project_settings.get("cc_include_options", [])

# seach for .clang_complete file and add lines to options
target_file = ".clang_complete"
directory = os.path.dirname(view.file_name())
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EDIT: I jumped the gun on this comment. I notice later on you search up the tree, negating this issue. Ignore this comment.

Won't this not work with nested structures? The .clang_complete file is typically in the root of the project but what if some of my code is in lib/ or src/. Maybe it would be better to use os.path.dirname(sublime.active_window().folders()[0]) which will be the main folder of the current window.

Or even better, use the current window's folder if there is one, and otherwise use the file's folder. Or try both.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for checking it over @trishume

I agree about taking into account the window's folder - it might be best stop the search for .clang_complete when it reaches the root of the folder that's associated with both the window and the file, using sublime.active_window().folders() would work but I think we'd need to search all of them so we choose the folder that the file is buried within

Searching up the entire directory tree works but there's some edge cases where it could pick up a .clang-complete you didn't intent it to use

path = os.path.join(directory, target_file)
target_exists = False

# search up the tree from the active file
i = 0
while(directory != os.path.dirname(directory) and i < 15):
target_exists = os.path.isfile(path)
if target_exists == True:
break;
directory = os.path.dirname(directory)
path = os.path.join(directory, target_file)
i = i + 1

if os.path.isfile(path) == True:
print("adding options from ", path)

lines = [line.rstrip('\n') for line in open(path)]

# resolve relative paths in options
for line in lines:
m = re.match("(-I)([^$]+)", line)
if m != None and os.path.isabs(m.group(2)) == False:
absOptPath = os.path.join(os.path.dirname(path), m.group(2))
line = m.group(1) + absOptPath
include_opts = include_opts + [line]

window = sublime.active_window()
variables = window.extract_variables()
include_opts = sublime.expand_variables(include_opts, variables)
Expand Down