Skip to content

Commit 5986b64

Browse files
authored
Merge pull request #95 from ProAlexUSC/master
feat: improve new file detection logic and code formatting
2 parents 937bc9c + e193539 commit 5986b64

File tree

1 file changed

+44
-9
lines changed

1 file changed

+44
-9
lines changed

compile_database.py

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def readFileArgs(path):
8888
return cmd_split(f.read())
8989

9090

91-
# 以文件里的内容作为命令行参数,会进行shell分词展开
91+
# Use file content as command line arguments, will perform shell word splitting
9292
def getFileArgs(path, cache) -> List[str]:
9393
files = cache.get(path)
9494
if files is None:
@@ -165,9 +165,11 @@ def findSwiftModuleRoot(filename):
165165

166166
return (directory, flagFile, compileFile)
167167

168+
168169
def filekey(filename):
169170
return os.path.realpath(filename).lower()
170171

172+
171173
class CompileFileInfo:
172174
def __init__(self, compileFile, store):
173175
self.file_info = {} # {file: command}
@@ -225,22 +227,51 @@ def new_file(self, filename):
225227
return {filename_key} # already handled
226228

227229
dir = os.path.dirname(filename_key)
228-
samefile = next(
229-
(v for v in self.groupby_dir().get(dir, ()) if v.endswith(".swift")), None
230+
231+
# 1. First look for existing swift files in the current directory
232+
similar_compiled_file = next(
233+
(v for v in self.groupby_dir().get(
234+
dir, ()) if v.endswith(".swift")), None
230235
)
231-
if not samefile:
236+
237+
# 2. If not found in current directory, search upward through parent directories until project root
238+
if not similar_compiled_file:
239+
current_dir = dir
240+
while current_dir and current_dir != "/":
241+
parent_dir = os.path.dirname(current_dir)
242+
243+
# Look for swift files in parent directory
244+
similar_compiled_file = next(
245+
(v for v in self.groupby_dir().get(
246+
parent_dir, ()) if v.endswith(".swift")), None
247+
)
248+
if similar_compiled_file:
249+
break
250+
251+
# If reached project root, stop searching
252+
if isProjectRoot(parent_dir):
253+
break
254+
255+
current_dir = parent_dir
256+
257+
if not similar_compiled_file:
232258
return
233259

234-
command = self.file_info[samefile]
260+
logging.info(
261+
f"Found new file: {filename} with similar_compiled_file: {similar_compiled_file}")
262+
263+
command = self.file_info[similar_compiled_file]
264+
235265
cmd_match = next(cmd_split_pattern.finditer(command), None)
236266
if not cmd_match:
237267
return
238-
assert self.cmd_info # init in groupby_dir
268+
assert self.cmd_info # init in groupby_dir
239269
module_files = self.cmd_info.pop(command)
240270
index = cmd_match.end()
241271
from shlex import quote
242272

243-
command = "".join((command[:index], " ", quote(filename), command[index:]))
273+
command = "".join(
274+
(command[:index], " ", quote(filename), command[index:]))
244275

245276
# update command info
246277
self.groupby_dir()[dir].add(filename_key)
@@ -250,11 +281,14 @@ def new_file(self, filename):
250281
self.file_info[v] = command
251282
return module_files
252283

284+
253285
def newfileForCompileFile(filename, compileFile, store) -> Optional[set[str]]:
254-
if not compileFile: return None
286+
if not compileFile:
287+
return None
255288
info = compileFileInfoFromStore(compileFile, store)
256289
return info.new_file(filename)
257290

291+
258292
def commandForFile(filename, compileFile, store: Dict):
259293
"""
260294
command = store["compile"][<compileFile>][filename]
@@ -291,7 +325,8 @@ def GetFlags(filename: str, compileFile=None, store=None):
291325
"""sourcekit entry function"""
292326
# NOTE: use store to ensure toplevel storage. child store should be other name
293327
# see store.setdefault to get all child attributes
294-
if store is None: store = globalStore
328+
if store is None:
329+
store = globalStore
295330

296331
if compileFile:
297332
if final_flags := GetFlagsInCompile(filename, compileFile, store):

0 commit comments

Comments
 (0)