Skip to content

Commit bfa509b

Browse files
ratijasFichteFoll
authored andcommitted
Settings: Fix completion values (#348)
The root cause was that text manipulation was reordered in a wrong way: JSON-encoded string was converted to snippet-field-content-encoded string first, but then it was treated as a simply JSON-encoded string again. Namely, stripping curly brackets away _after_ encoding them as \\} broke cursor things in a weird ways. Used pretty printing just because it looks better. But it required extra whitespace stripping: both outside and inside stripped brackets/braces. Also changed variable name from `encoded` to `content` in fmt, so they can never be accidentally reordered again. Fixes #347
1 parent ae1fb8a commit bfa509b

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

plugins/settings/known_settings.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,14 @@ def key_completions(self, view, prefix, point):
433433

434434
return completions
435435

436+
@staticmethod
437+
def _encode_snippet_field_default_content(content: str) -> str:
438+
"""Escape string for snippet's default content context."""
439+
return content \
440+
.replace("\\", "\\\\") \
441+
.replace("$", "\\$") \
442+
.replace("}", "\\}")
443+
436444
@staticmethod
437445
def _key_snippet(key, value, bol="", eol=",\n"):
438446
"""Create snippet with default value depending on type.
@@ -450,19 +458,16 @@ def _key_snippet(key, value, bol="", eol=",\n"):
450458
Returns:
451459
string: the contents field to insert into completions entry
452460
"""
453-
encoded = sublime.encode_value(value)
454-
encoded = encoded.replace("\\", "\\\\") # escape snippet markers
455-
encoded = encoded.replace("$", "\\$")
456-
encoded = encoded.replace("}", "\\}")
461+
encoded = sublime.encode_value(value, pretty=True).strip()
457462

458463
if isinstance(value, str):
459464
# create the snippet for json strings and exclude quotation marks
460465
# from the input field {1:}
461466
#
462467
# "key": "value"
463468
#
464-
fmt = '{bol}"{key}": "${{1:{encoded}}}"{eol}'
465-
encoded = encoded[1:-1] # strip quotation
469+
fmt = '{bol}"{key}": "${{1:{content}}}"{eol}'
470+
encoded = encoded[1:-1]
466471
elif isinstance(value, list):
467472
# create the snippet for json lists and exclude brackets
468473
# from the input field {1:}
@@ -472,8 +477,8 @@ def _key_snippet(key, value, bol="", eol=",\n"):
472477
# value
473478
# ]
474479
#
475-
fmt = '{bol}"{key}":\n[\n\t${{1:{encoded}}}\n]{eol}'
476-
encoded = encoded[1:-1] # strip brackets
480+
fmt = '{bol}"{key}":\n[\n\t${{1:{content}}}\n]{eol}'
481+
encoded = encoded[1:-1].strip()
477482
elif isinstance(value, dict):
478483
# create the snippet for json dictionaries braces
479484
# from the input field {1:}
@@ -483,11 +488,13 @@ def _key_snippet(key, value, bol="", eol=",\n"):
483488
# value
484489
# }
485490
#
486-
fmt = '{bol}"{key}":\n{{\n\t${{1:{encoded}}}\n}}{eol}'
487-
encoded = encoded[1:-1] # strip braces
491+
fmt = '{bol}"{key}":\n{{\n\t${{1:{content}}}\n}}{eol}'
492+
encoded = encoded[1:-1].strip()
488493
else:
489-
fmt = '{bol}"{key}": ${{1:{encoded}}}{eol}'
490-
return fmt.format(**locals())
494+
fmt = '{bol}"{key}": ${{1:{content}}}{eol}'
495+
# `encoded` is like a JSON string, but with quotes/braces/brackets stripped.
496+
content = KnownSettings._encode_snippet_field_default_content(encoded)
497+
return fmt.format(key=key, content=content, bol=bol, eol=eol)
491498

492499
def value_completions(self, view, prefix, point):
493500
"""Create a list with completions for all known settings values.

0 commit comments

Comments
 (0)