Skip to content

Commit

Permalink
Add kind icons to BibTex completions
Browse files Browse the repository at this point in the history
  • Loading branch information
deathaxe committed Jan 16, 2025
1 parent 91996c0 commit 82d488b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
24 changes: 15 additions & 9 deletions latextools/biblatex_crossref_completions.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ def validator(s):

contents = view.substr(sublime.Region(0, view.size()))
for entry_type, key in re.findall(
r"(@(?!preamble|comment|string)[a-zA-Z]+)\s*\{\s*([^,]+)\b",
contents,
re.IGNORECASE,
r"(@(?!preamble|comment|string)[a-zA-Z]+)\s*\{\s*([^,]+)\b", contents, re.IGNORECASE
):
if validator(entry_type):
keys.append(key)
Expand Down Expand Up @@ -113,14 +111,22 @@ def _get_replacement(matcher, key):

def get_completions_if_matches(regex, line, get_key_list_func, view):
matcher = regex.match(line)
if matcher:
return (
[(key, _get_replacement(matcher, key)) for key in sorted(set(get_key_list_func(view)))],
sublime.INHIBIT_WORD_COMPLETIONS,
)
else:
if not matcher:
return []

KIND_INFO = [sublime.KindId.NAVIGATION, "r", "Reference"]

completions = [
sublime.CompletionItem(
trigger=name,
completion=_get_replacement(matcher, name),
kind=KIND_INFO
)
for name in get_key_list_func(view)
]

return sublime.CompletionList(completions, sublime.INHIBIT_WORD_COMPLETIONS)


class BiblatexCrossrefCompletions(sublime_plugin.EventListener):
def on_query_completions(self, view, prefix, locations):
Expand Down
26 changes: 17 additions & 9 deletions latextools/biblatex_name_completions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
VALUE_REGEX = r"[\s~]*(?P<ENTRIES>(?:dna[\s~]+.+)+)?[\s~]*(?P<OPEN>\{)?(?P<EQUALS>\s*=\s*)?"

ON_NAME_FIELD_REGEX = re.compile(
VALUE_REGEX + r"(?:" + r"|".join((s[::-1] for s in NAME_FIELDS)) + r")\b",
re.IGNORECASE,
VALUE_REGEX + r"(?:" + r"|".join((s[::-1] for s in NAME_FIELDS)) + r")\b", re.IGNORECASE
)


Expand All @@ -42,7 +41,7 @@ def _get_replacement(matcher, key):

return "{0}{1}{2}".format(
"" if equals else "= " if match.startswith(" ") else " = ",
("" if matcher.group("OPEN") else "{" if not equals or match.startswith(" ") else " {"),
"" if matcher.group("OPEN") else "{" if not equals or match.startswith(" ") else " {",
key,
)

Expand Down Expand Up @@ -107,7 +106,7 @@ def get_names(contents):
break
in_entry = False

return sorted(set(names))
return set(names)


class BiblatexNameCompletions(sublime_plugin.EventListener):
Expand All @@ -122,10 +121,19 @@ def on_query_completions(self, view, prefix, locations):
current_line = current_line[len(prefix) :]

matcher = ON_NAME_FIELD_REGEX.match(current_line)
if matcher:
return (
[(name, _get_replacement(matcher, name)) for name in get_names_from_view(view)],
sublime.INHIBIT_WORD_COMPLETIONS,
if not matcher:
return []

KIND_INFO = [sublime.KindId.VARIABLE, "n", "Name"]

completions = [
sublime.CompletionItem(
trigger=name,
completion=_get_replacement(matcher, name),
kind=KIND_INFO,
details=" ",
)
for name in get_names_from_view(view)
]

return []
return sublime.CompletionList(completions, sublime.INHIBIT_WORD_COMPLETIONS)

0 comments on commit 82d488b

Please sign in to comment.