Skip to content

Commit e02cd91

Browse files
committed
Convert string.format() to f-strings
1 parent 0e2d0dd commit e02cd91

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+289
-336
lines changed

latextools/auto_label.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,17 @@ def run(self, edit):
110110
# else insert the label as it is
111111
if len(view.sel()) == 1:
112112
snippet = (
113-
"{before_text}" # leading \label{
114-
"${{1:{label_type}}}:${{2:{label_content}}}"
115-
"{after_text}" # trailing }
116-
"$0".format(**locals())
113+
f"{before_text}" # leading \label{
114+
f"${{1:{label_type}}}:${{2:{label_content}}}"
115+
f"{after_text}" # trailing }
116+
"$0"
117117
)
118118
view.run_command("insert_snippet", {"contents": snippet})
119119
else:
120120
content = (
121-
"{before_text}" # leading \label{
122-
"{label_type}:{label_content}"
123-
"{after_text}".format(**locals()) # trailing }
121+
f"{before_text}" # leading \label{
122+
f"{label_type}:{label_content}"
123+
f"{after_text}" # trailing }
124124
)
125125
view.insert(edit, pos, content)
126126

latextools/biblatex_name_completions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ def _get_replacement(matcher, key):
4848

4949
if matcher.group("ENTRIES").startswith("dna"):
5050
if match.startswith(" "):
51-
return "{0}".format(key)
52-
return " {0}".format(key)
51+
return str(key)
52+
return f" {key}"
5353
else:
5454
return "{0}{1}".format(" " if matcher.group("ENTRIES").startswith(" ") != " " else "", key)
5555

latextools/change_environment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def extract_end_region(region):
9797
elif one_sel:
9898
sublime.status_message(
9999
"The environment begin and end does not match:"
100-
"'{0}' and '{1}'".format(view.substr(begin_region), view.substr(end_region))
100+
f" '{view.substr(begin_region)}' and '{view.substr(end_region)}'"
101101
)
102102
if not new_regions:
103103
sublime.status_message("Environment detection failed")

latextools/context_provider.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def on_query_context(self, view, key, operator, operand, match_all):
5353
except IndexError:
5454
return False
5555
except AttributeError:
56-
logger.error("Unsupported LaTeXTools context: %s", key)
56+
logger.error(f"Unsupported LaTeXTools context: {key}")
5757
return False
5858

5959
try:
@@ -160,8 +160,8 @@ def _inside_envs(self, view, pos, envs):
160160
env.endswith("*"): r"\*",
161161
}.get(True, r"\*?")
162162
env = env.rstrip("*!")
163-
benv = r"\\begin(?:\[[^\]]\])?{{{}{}}}".format(env, star)
164-
eenv = r"\\end{{{}{}}}".format(env, star)
163+
benv = fr"\\begin(?:\[[^\]]\])?{{{env}{star}}}"
164+
eenv = fr"\\end{{{env}{star}}}"
165165

166166
if only_nearest:
167167
real_benv = benv

latextools/delete_temp_files.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def latextools_plugin_loaded():
5353
except OSError:
5454
pass
5555
else:
56-
logger.info("Deleted old temp directory %s", directory)
56+
logger.info(f"Deleted old temp directory {directory}")
5757
except KeyError:
5858
pass
5959

@@ -70,7 +70,7 @@ def run(self):
7070
except FileNotFoundError:
7171
pass
7272
except OSError as e:
73-
logger.error("Can't delete global cache: %s", e)
73+
logger.error(f"Can't delete global cache: {e}")
7474

7575
tex_root = get_tex_root(self.window.active_view())
7676
if not tex_root:
@@ -120,7 +120,7 @@ def run(self):
120120
return
121121

122122
if not os.path.isfile(root_file):
123-
message = "Could not find TEX root {0}.".format(root_file)
123+
message = f"Could not find TEX root {root_file}."
124124
sublime.status_message(message)
125125
logger.error(message)
126126
return
@@ -227,7 +227,7 @@ def _rmtree(self, path):
227227
pass
228228
except OSError:
229229
# report the exception if the folder didn't end up deleted
230-
logger.error("Error while trying to delete %s", path)
230+
logger.error(f"Error while trying to delete {path}")
231231
traceback.print_exc()
232232

233233
def _rmfile(self, path):
@@ -238,7 +238,7 @@ def _rmfile(self, path):
238238
except OSError:
239239
# basically here for locked files in Windows,
240240
# but who knows what we might find?
241-
logger.error("Error while trying to delete %s", path)
241+
logger.error(f"Error while trying to delete {path}")
242242
traceback.print_exc()
243243

244244
def _clear_dir(self, path):

latextools/detect_spellcheck.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,13 @@ def update_dict_language(view, extract_from_root):
152152
user_sc = get_setting("tex_spellcheck_paths", {}, view)
153153
dict_path = user_sc.get(loc) or get_dict_path(loc)
154154
except DictMissing:
155-
logger.error("dict definition missing for locale '%s'", loc)
155+
logger.error(f"dict definition missing for locale '{loc}'")
156156
return # no dict defined for locale
157157
current_dict = view.settings().get("dictionary")
158158
if current_dict == dict_path:
159159
return # the locale is already set
160160
view.settings().set("dictionary", dict_path)
161-
logger.info("Changed dictionary to '%s'", dict_path)
161+
logger.info(f"Changed dictionary to '{dict_path}'")
162162

163163

164164
class LatextoolsAutoDetectSpellcheckListener(sublime_plugin.EventListener):

latextools/jumpto_anywhere.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def is_correct_ref(c):
7777
refs = list(ana.filter_commands(is_correct_ref))
7878

7979
if len(refs) == 0:
80-
sublime.error_message("No references for '{0}' found.".format(args))
80+
sublime.error_message(f"No references for '{args}' found.")
8181
return
8282
elif len(refs) == 1:
8383
ref = refs[0]
@@ -93,7 +93,7 @@ def _jumpto_ref(view, com_reg, pos):
9393
label_id = _get_selected_arg(view, com_reg, pos)
9494
if not label_id:
9595
return
96-
sublime.status_message("Scanning document for label '{0}'...".format(label_id))
96+
sublime.status_message(f"Scanning document for label '{label_id}'...")
9797
tex_root = get_tex_root(view)
9898
ana = analysis.analyze_document(tex_root)
9999
if ana is None:
@@ -106,12 +106,12 @@ def is_correct_label(c):
106106
try:
107107
label = next(labels)
108108
except Exception:
109-
message = "No matching label found for '{0}'.".format(label_id)
109+
message = f"No matching label found for '{label_id}'."
110110
logger.error(message)
111111
sublime.status_message(message)
112112
return
113113
label_region = label.region
114-
message = "Jumping to label '{0}'.".format(label_id)
114+
message = f"Jumping to label '{label_id}'"
115115
logger.info(message)
116116
sublime.status_message(message)
117117
utils.open_and_select_region(view, label.file_name, label_region)
@@ -122,7 +122,7 @@ def _jumpto_cite(view, com_reg, pos):
122122
bib_key = _get_selected_arg(view, com_reg, pos)
123123
if tex_root is None or not bib_key:
124124
return
125-
message = "Scanning bibliography files for key '{0}'...".format(bib_key)
125+
message = f"Scanning bibliography files for key '{bib_key}'..."
126126
logger.info(message)
127127
sublime.status_message(message)
128128
base_dir = os.path.dirname(tex_root)
@@ -146,15 +146,15 @@ def _jumpto_cite(view, com_reg, pos):
146146
):
147147
continue
148148
region = sublime.Region(start, end)
149-
message = "Jumping to bibliography key '{0}'.".format(bib_key)
149+
message = f"Jumping to bibliography key '{bib_key}'."
150150
logger.info(message)
151151
sublime.status_message(message)
152152
utils.open_and_select_region(view, bib_file, region)
153153
return
154154
except Exception as e:
155-
logger.error("Failed to open file %s:\n %s", bib_file, e)
155+
logger.error(f"Failed to open file {bib_file}:\n {e}")
156156
continue
157-
message = "Entry '{0}' not found in bibliography.".format(bib_key)
157+
message = f"Entry '{bib_key}' not found in bibliography."
158158
logger.error(message)
159159
sublime.status_message(message)
160160

@@ -173,19 +173,19 @@ def _jumpto_glo(view, com_reg, pos, acr=False):
173173
try:
174174
entry = next(c for c in commands if c.args == iden)
175175
except Exception:
176-
message = "Glossary definition not found for '{0}'".format(iden)
176+
message = f"Glossary definition not found for '{iden}'"
177177
logger.error(message)
178178
sublime.status_message(message)
179179
return
180-
message = "Jumping to Glossary '{0}'.".format(iden)
180+
message = f"Jumping to Glossary '{iden}'."
181181
logger.info(message)
182182
sublime.status_message(message)
183183
utils.open_and_select_region(view, entry.file_name, entry.args_region)
184184

185185

186186
def _jumpto_pkg_doc(view, com_reg, pos):
187187
def view_doc(package):
188-
message = "Try opening documentation for package '{0}'".format(package)
188+
message = f"Try opening documentation for package '{package}'"
189189
logger.info(message)
190190
sublime.status_message(message)
191191
view.window().run_command("latextools_view_doc", {"file": package})
@@ -215,10 +215,10 @@ def _opt_jumpto_self_def_command(view, com_reg):
215215
cana = analysis.get_analysis(tex_root)
216216
new_commands = cana.filter_commands(newcommand_keywords)
217217
if command not in [c.args for c in new_commands]:
218-
logger.error("Command not defined (cached) '%s'", command)
218+
logger.error(f"Command not defined (cached) '{command}'")
219219
return False
220220

221-
message = "Scanning document for command definition of '{0}'".format(command)
221+
message = f"Scanning document for command definition of '{command}'"
222222
logger.info(message)
223223
sublime.status_message(message)
224224
# analyze the document to retrieve the correct position of the
@@ -228,12 +228,12 @@ def _opt_jumpto_self_def_command(view, com_reg):
228228
try:
229229
new_com_def = next(filter(lambda c: c.args == command, new_commands))
230230
except Exception:
231-
logger.error("Command not self defined '%s'", command)
231+
logger.error(f"Command not self defined '{command}'")
232232
return False
233233
file_name = new_com_def.file_name
234234
region = new_com_def.args_region
235235

236-
message = "Jumping to definition of '{0}'".format(command)
236+
message = f"Jumping to definition of '{command}'"
237237
logger.info(message)
238238
sublime.status_message(message)
239239
utils.open_and_select_region(view, file_name, region)
@@ -291,23 +291,23 @@ def is_inside(g):
291291
pos = sel.b - line_r.begin() - com_reg.start()
292292
# check if its a ref
293293
if NEW_STYLE_REF_REGEX.match(reversed_command):
294-
sublime.status_message("Jump to reference '{0}'".format(args))
294+
sublime.status_message(f"Jump to reference '{args}'")
295295
_jumpto_ref(view, com_reg, pos)
296296
# check if it is a cite
297297
elif NEW_STYLE_CITE_REGEX.match(reversed_command):
298-
sublime.status_message("Jump to citation '{0}'".format(args))
298+
sublime.status_message(f"Jump to citation '{args}'")
299299
_jumpto_cite(view, com_reg, pos)
300300
elif command == "label":
301301
_show_usage_label(view, args)
302302
elif GLO_LINE_RE.match(reversed_command):
303-
sublime.status_message("Jump to glossary '{0}'".format(args))
303+
sublime.status_message(f"Jump to glossary '{args}'")
304304
_jumpto_glo(view, com_reg, pos)
305305
elif ACR_LINE_RE.match(reversed_command):
306-
sublime.status_message("Jump to acronym '{0}'".format(args))
306+
sublime.status_message(f"Jump to acronym '{args}'")
307307
_jumpto_glo(view, com_reg, pos, acr=True)
308308
# check if it is any kind of input command
309309
elif any(reg.match(com_reg.group(0)) for reg in INPUT_REG_EXPS):
310-
sublime.status_message("Jump to file '{0}'".format(args))
310+
sublime.status_message(f"Jump to file '{args}'")
311311
self.view.run_command(
312312
"latextools_jumpto_file",
313313
{
@@ -345,7 +345,7 @@ def match_selector(selector):
345345
elif fallback_command:
346346
if set_caret:
347347
self._set_caret(event)
348-
logger.debug("Run command '%s'", fallback_command)
348+
logger.debug(f"Run command '{fallback_command}'")
349349
self.view.run_command(fallback_command)
350350

351351
def _set_caret(self, event):

latextools/jumpto_pdf.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,14 @@ def get_viewer():
4242
raise NoViewerException()
4343

4444
try:
45-
viewer = get_plugin(viewer_name + "_viewer")
45+
viewer = get_plugin(f"{viewer_name}_viewer")
4646
except NoSuchPluginException:
4747
try:
4848
viewer = get_plugin(viewer_name)
4949
except NoSuchPluginException:
5050
sublime.error_message(
51-
"Cannot find viewer "
52-
+ viewer_name
53-
+ ".\n"
54-
+ "Please check your LaTeXTools Preferences."
51+
f"Cannot find viewer {viewer_name}.\n"
52+
"Please check your LaTeXTools Preferences."
5553
)
5654
raise NoViewerException()
5755

@@ -62,7 +60,7 @@ def get_viewer():
6260

6361
if not viewer.supports_platform(sublime.platform()):
6462
sublime.error_message(
65-
viewer_name + " does not support the current platform. "
63+
f"{viewer_name} does not support the current platform. "
6664
"Please change the viewer in your LaTeXTools Preferences."
6765
)
6866
raise NoViewerException()
@@ -102,19 +100,13 @@ def run(self, **args):
102100
if from_keybinding:
103101
forward_sync = True
104102

105-
logger.debug(
106-
"from_keybinding=%s, keep_focus=%s, forward_sync=%s",
107-
from_keybinding,
108-
keep_focus,
109-
forward_sync,
110-
)
103+
logger.debug(f"from_keybinding={from_keybinding}, keep_focus={keep_focus}, forward_sync={forward_sync}")
111104

112105
file_name = view.file_name()
113106
if not is_tex_file(file_name):
114107
if from_keybinding:
115-
sublime.error_message(
116-
"%s is not a TeX source file: cannot jump." % (os.path.basename(file_name),)
117-
)
108+
file_name = os.path.basename(file_name) if file_name else 'untitled'
109+
sublime.error_message(f"{file_name} is not a TeX source file: cannot jump.")
118110
return
119111
# If not invoked from keybinding, it is invoked by the
120112
# compilation process. Then, we should go forward with
@@ -136,13 +128,13 @@ def run(self, **args):
136128
pdffile = os.path.join(os.path.dirname(root), file_name + ".pdf")
137129

138130
if not os.path.exists(pdffile):
139-
logger.error("Expected PDF file %s not found", pdffile)
131+
logger.error(f"Expected PDF file {pdffile} not found")
140132
return
141133

142134
pdffile = os.path.realpath(pdffile)
143135

144136
(row, col) = view.rowcol(view.sel()[0].end())
145-
logger.debug("Jump to row: %d col: %d", row, col)
137+
logger.debug(f"Jump to row: {row} col: {col}")
146138
# column is actually ignored up to 0.94
147139
# HACK? It seems we get better results incrementing row
148140
row += 1
@@ -224,7 +216,7 @@ def run(self, **args):
224216

225217
pdffile = os.path.normpath(pdffile)
226218
if not os.path.exists(pdffile):
227-
logger.error("Expected PDF file %s not found", pdffile)
219+
logger.error(f"Expected PDF file {pdffile} not found")
228220
return
229221

230222
pdffile = os.path.realpath(pdffile)
@@ -236,7 +228,7 @@ def run(self, **args):
236228
return
237229
elif not os.path.exists(pdffile):
238230
# note: error_message() already logs to console
239-
sublime.error_message('PDF file "{0}" does not exist.'.format(pdffile))
231+
sublime.error_message(f'PDF file "{pdffile}" does not exist.')
240232
return
241233

242234
try:

0 commit comments

Comments
 (0)