Skip to content

Commit

Permalink
add support of #include: in cwl files
Browse files Browse the repository at this point in the history
  • Loading branch information
hadisfr committed Jun 30, 2018
1 parent 27519ca commit 3a65bf0
Showing 1 changed file with 43 additions and 21 deletions.
64 changes: 43 additions & 21 deletions latex_cwl_completions.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ def is_cwl_available(view=None):
return CWL_COMPLETION_ENABLED


def get_cwl_file_name(package):
if package.endswith('.cwl'):
cwl_file = package
else:
cwl_file = '{0}.cwl'.format(package)
return cwl_file

# returns the cwl completions instances
def get_cwl_completions():
plugin_loaded()
Expand Down Expand Up @@ -123,10 +130,7 @@ def get_completions(self, env=False):
return []

for package in packages:
if package.endswith('.cwl'):
cwl_file = package
else:
cwl_file = '{0}.cwl'.format(package)
cwl_file = get_cwl_file_name(package)

# some hacks for particular packages that do not match
# the standard pattern
Expand Down Expand Up @@ -191,6 +195,17 @@ def get_packages(self):
analysis.ONLY_COMMANDS_WITH_ARGS
)
])

packages = [get_cwl_file_name(package) for package in packages]

flag = True
while flag:
packages_set = set(packages)
for package in packages:
packages_set = packages_set.union(set(self._dependencies.get(package, [])))
flag = len(packages_set) > len(packages)
packages = list(packages_set)

# TODO - Attempt to read current buffer

return packages
Expand Down Expand Up @@ -221,10 +236,11 @@ def hack2():
sublime.set_timeout(hack2, 1)

# callback when completions are loaded
def _on_completions(self, completions, environment_completions):
def _on_completions(self, completions, environment_completions, dependencies):
with self._WLOCK:
self._completions = completions
self._environment_completions = environment_completions
self._dependencies = dependencies
self._started = False
self._completed = True

Expand Down Expand Up @@ -409,6 +425,7 @@ def _check_if_cwl_enabled(view=None):
def cwl_parsing_handler(callback):
completion_results = {}
environment_results = {}
dependencies_result = {}
cwl_files, use_package = get_cwl_package_files()

for cwl_file in cwl_files:
Expand Down Expand Up @@ -454,13 +471,12 @@ def cwl_parsing_handler(callback):
)
continue

completions = parse_cwl_file(base_name, s)
completions, environments, dependencies = parse_cwl_file(base_name, s)
completion_results[base_name] = completions

environments = parse_cwl_file(base_name, s, parse_line_as_environment)
environment_results[base_name] = environments
dependencies_result[base_name] = dependencies

callback(completion_results, environment_results)
callback(completion_results, environment_results, dependencies_result)


# gets a list of all cwl package files available, whether in the
Expand Down Expand Up @@ -501,8 +517,11 @@ def parse_line_as_command(line):


# actually does the parsing of the cwl files
def parse_cwl_file(cwl, s, parse_line=parse_line_as_command):
completions = []
def parse_cwl_file(cwl, s):
parse_lines = {"completions": parse_line_as_command, "environments": parse_line_as_environment}
results = {"completions": list(), "environments": list()}
dependencies = []

method = os.path.splitext(cwl)[0]

# we need some state tracking to ignore keyval data
Expand All @@ -518,6 +537,8 @@ def parse_cwl_file(cwl, s, parse_line=parse_line_as_command):
KEYVAL = True
if line.startswith('#endkeyvals') or line.startswith('#endif'):
KEYVAL = False
if line.startswith('#include:') and not KEYVAL:
dependencies.append(get_cwl_file_name(line[len('#include:'):]))

continue

Expand All @@ -534,19 +555,20 @@ def parse_cwl_file(cwl, s, parse_line=parse_line_as_command):
# a # char
line = line.rstrip()

result = parse_line(line)
if result is None:
continue
keyword, insertion = result
for (key, parse_line) in parse_lines.items():
result = parse_line(line)
if result is None:
continue
keyword, insertion = result

# pad the keyword with spaces; this is to keep the size of the
# autocompletions consistent regardless of the returned results
keyword = keyword.ljust(50)
# pad the keyword with spaces; this is to keep the size of the
# autocompletions consistent regardless of the returned results
keyword = keyword.ljust(50)

item = (u'%s\t%s' % (keyword, method), insertion)
completions.append(item)
item = (u'%s\t%s' % (keyword, method), insertion)
results[key].append(item)

return completions
return results["completions"], results["environments"], dependencies


# ensure that CWL_COMPLETIONS has a value
Expand Down

0 comments on commit 3a65bf0

Please sign in to comment.