Skip to content

Commit

Permalink
[CSS] Add space after colon only via auto_complete_trailing_spaces (s…
Browse files Browse the repository at this point in the history
…ublimehq#3262)

This commit is the 3rd alternative for sublimehq#2886 and sublimehq#2912.

It uses the default `auto_complete_trailing_spaces` setting to decide
whether to add a space after colons or not when completing properties.

Default behavior (`auto_complete_trailing_spaces`: true):

    font-fam|    =>   font-family: |;

Custom behavior (`auto_complete_trailing_spaces`: false):

    font-fam|    =>   font-family:|;

Default bindings no longer add space when typing `:` (colon):

    font-family|    =>   font-family:|;
  • Loading branch information
deathaxe authored Mar 12, 2022
1 parent 0084ec3 commit f8a5ded
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 30 deletions.
15 changes: 2 additions & 13 deletions CSS/Default.sublime-keymap
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
[
// Add a colon followed by space and semicolon after a property name,
// if the next following token doesn't look like a value.
{ "keys": [":"], "command": "insert_snippet", "args": {"contents": ": $0;"}, "context":
{ "keys": [":"], "command": "insert_snippet", "args": {"contents": ":$0;"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "source.css - meta.selector.css", "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "source.css meta.property-list - meta.selector - meta.group - comment - string", "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\w$", "match_all": false },
{ "key": "following_text", "operator": "regex_contains", "operand": "^\\s*($|[^:;}]+:)", "match_all": false }
]
},
// Add a colon followed by space after a property name,
// if the next character is a semicolon or the following token looks like a value.
{ "keys": [":"], "command": "insert_snippet", "args": {"contents": ": $0"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "source.css - meta.selector.css", "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\w$", "match_all": false },
{ "key": "following_text", "operator": "regex_contains", "operand": "^;|^[^\\s:;][^:]*($|[;}]|//|/\\*)", "match_all": true }
]
},
// Move the cursor to prevent adding a duplicated semicolon.
{ "keys": [";"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
Expand Down
35 changes: 18 additions & 17 deletions CSS/css_completions.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,28 @@ def on_query_completions(self, view, prefix, locations):
return None

def complete_property_name(self, view, prefix, pt):
if match_selector(view, pt, "meta.group"):
# don't append semicolon in groups e.g.: `@media screen (prop: |)`
suffix = ": $0"
else:
suffix = ": $0;"
text = view.substr(sublime.Region(pt, view.line(pt).end()))
matches = self.re_value.search(text)
if matches:
colon, space, value, term = matches.groups()
if colon:
# don't append anything if the next character is a colon
suffix = ""
elif value:
# only append colon if value already exists
suffix = ":" if space else ": "
elif term == ";":
# ommit semicolon if rule is already terminated
else:
colon = ""
space = ""
value = ""
term = ""

# don't append anything if next character is a colon
suffix = ""
if not colon:
# add space after colon if smart typing is enabled
if not space and view.settings().get("auto_complete_trailing_spaces"):
suffix = ": $0"
else:
suffix = ":$0"

# terminate empty value if not within parentheses
if not value and not term and not match_selector(view, pt, "meta.group"):
suffix += ";"

return (
sublime.CompletionItem(
Expand Down Expand Up @@ -136,10 +140,7 @@ def complete_property_value(self, view, prefix, pt):
if values:
details = f"<code>{prop}</code> property-value"

if match_selector(view, pt, "meta.group"):
# don't append semicolon in groups e.g.: `@media screen (prop: val)`
suffix = ""
elif next_none_whitespace(view, pt) == ";":
if match_selector(view, pt, "meta.group") or next_none_whitespace(view, pt) == ";":
suffix = ""
else:
suffix = "$0;"
Expand Down

0 comments on commit f8a5ded

Please sign in to comment.