Skip to content

Commit ac4d9c8

Browse files
committed
refactor: Encapsulate commandFile
1 parent e81ef86 commit ac4d9c8

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

compile_database.py

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
import re
44
import subprocess
5-
from typing import List
5+
from typing import Dict, List
66

77

88
globalStore = {}
@@ -164,37 +164,49 @@ def findSwiftModuleRoot(filename):
164164

165165
return (directory, flagFile, compileFile)
166166

167+
class CompileFileInfo:
168+
def __init__(self, compileFile, store):
169+
self.info = {}
167170

168-
def commandForFile(filename, compileFile, store):
169-
compile_store = store.setdefault("compile", {})
170-
info = compile_store.get(compileFile)
171-
if info is None: # load {filename.lower: command} dict
172-
info = {}
173-
compile_store[compileFile] = info # cache first to avoid re enter when error
174-
171+
# load compileFile into info
175172
import json
176-
177173
with open(compileFile) as f:
178174
m: List[dict] = json.load(f)
179175
for i in m:
180176
command = i.get("command")
181177
if not command:
182178
continue
183179
if files := i.get("files"): # batch files, eg: swift module
184-
info.update((os.path.realpath(f).lower(), command) for f in files)
185-
if fileLists := i.get(
186-
"fileLists"
187-
): # file list store in a dedicated file
188-
info.update(
189-
(os.path.realpath(f).lower(), command)
190-
for l in fileLists
191-
if os.path.isfile(l)
180+
self.info.update((self.key(f), command) for f in files)
181+
if fileLists := i.get("fileLists"): # file list store in a dedicated file
182+
self.info.update(
183+
(self.key(f), command)
184+
for l in fileLists if os.path.isfile(l)
192185
for f in getFileArgs(l, store.setdefault("filelist", {}))
193186
)
194187
if file := i.get("file"): # single file info
195-
info[os.path.realpath(file).lower()] = command
188+
self.info[self.key(file)] = command
189+
190+
def get(self, filename):
191+
if command := self.info.get(filename.lower()):
192+
return command.replace("\\=", "=")
193+
194+
def key(self, filename):
195+
return os.path.realpath(filename).lower()
196+
197+
198+
def commandForFile(filename, compileFile, store: Dict):
199+
"""
200+
command = store["compile"][<compileFile>][filename]
201+
"""
202+
compile_store = store.setdefault("compile", {})
203+
info: CompileFileInfo = compile_store.get(compileFile)
204+
if info is None: # load {filename.lower: command} dict
205+
info = CompileFileInfo(compileFile, store) # cache first to avoid re enter when error
206+
compile_store[compileFile] = info
207+
196208
# xcode 12 escape =, but not recognized...
197-
return info.get(filename.lower(), "").replace("\\=", "=")
209+
return info.get(filename)
198210

199211

200212
def GetFlagsInCompile(filename, compileFile, store):
@@ -209,7 +221,7 @@ def GetFlagsInCompile(filename, compileFile, store):
209221
def GetFlags(filename: str, compileFile=None, **kwargs):
210222
"""sourcekit entry function"""
211223
# NOTE: use store to ensure toplevel storage. child store should be other name
212-
# see store.setdefault to get child attributes
224+
# see store.setdefault to get all child attributes
213225
store = kwargs.get("store", globalStore)
214226
filename = os.path.realpath(filename)
215227

@@ -221,7 +233,7 @@ def GetFlags(filename: str, compileFile=None, **kwargs):
221233
return InferFlagsForSwift(filename, compileFile, store)
222234
return {"flags": [], "do_cache": False}
223235

224-
236+
# TODO: c family infer flags #
225237
def InferFlagsForSwift(filename, compileFile, store):
226238
"""try infer flags by convention and workspace files"""
227239
project_root, flagFile, compileFile = findSwiftModuleRoot(filename)

0 commit comments

Comments
 (0)