Skip to content

Commit

Permalink
Added filter to remove the unique sections from auto completion
Browse files Browse the repository at this point in the history
Fixed detection of section with newlines

Added setting option to force allow duplicate sections
  • Loading branch information
thatsIch committed Dec 27, 2016
1 parent b7b9ec8 commit 0f53433
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
5 changes: 5 additions & 0 deletions Rainmeter.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@
//(e.g. after typing a section head and hitting enter)
"smart_indent": false,

// If you set this to 'true' you can auto complete unique sections
// like [Rainmeter] within the same file.
// Default: false
"allow_completion_section_duplicates": false,

//File extensions to use with Rainmeter.
"extensions":
[
Expand Down
2 changes: 1 addition & 1 deletion completion/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ContextSensAutoCompletion(object):
comment_exp = re.compile(r'^\s*;.*')

# enable searching for [] in multiline environment
bracket_expression = re.compile(r'^\s*\[.+\]\s*$', re.MULTILINE)
bracket_expression = re.compile(r'^\s*(\[.+\])\s*$', re.MULTILINE)
section_expression = re.compile(r'^\s*\[(.+)\]\s*$', re.I)
key_expression = re.compile(r'^\s*(.+)\s*=?\s*(.*?)\s*$', re.MULTILINE)
key_value_expression = re.compile(r'^\s*(.+?)\s*=\s*(.*?)\s*$', re.MULTILINE)
Expand Down
42 changes: 27 additions & 15 deletions completion/section.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,19 @@ def __get_compiled_key_completions(options):
"""
keys = []
for option in options:
title = option['title'] + "\t" + option['hint']
section = option['title']
display = option['title'] + "\t" + option['hint']

if 'value' in option:
result = option['value']
else:
result = option['title']
if 'unique' in option:
unique = option['unique']
else:
unique = False

pair = (title, result)
pair = (section, display, result, unique)
keys.append(pair)

return keys
Expand All @@ -77,19 +82,28 @@ def __filter_completions_by_sec(self, sections):
# filter by already existing keys
completions = []

settings = sublime.load_settings("Rainmeter.sublime-settings")
allow_duplicates = settings.get("allow_completion_section_duplicates", False)

for completion in self.all_key_completions:
# trigger is not used here
_, content = completion

contained = 0
# value not used here
for section in sections:
if section.casefold() == content.casefold():
contained = 1
break

if contained == 0:
completions.append(completion)
section_id, display, content, unique = completion

# we only need to search for duplicates
# if we are having a unique section like [Rainmeter]
# and not allow duplicates
if unique and not allow_duplicates:
contained = 0
# value not used here
for section in sections:
if section.casefold() == section_id.casefold():
contained = 1
break

if contained == 0:
completions.append((display, content))
else:
completions.append((display, content))

return completions

Expand All @@ -105,9 +119,7 @@ def get_key_context_completion(self, prefix, line_content, sections):
# return None

self.__lazy_initialize_completions()
print("--- sections:", sections)
completions = self.__filter_completions_by_sec(sections)
print("--- completions:", completions)
# no results, means all keys are used up
if not completions:
return None
Expand Down
3 changes: 3 additions & 0 deletions completion/section.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
title: "[Rainmeter]"
hint: Defines skin options
value: "[Rainmeter]\n$1"
unique: true
-
title: "[Metadata]"
hint: Presented in Manage window
value: "[Metadata]\n$1"
unique: true
-
title: "[Variables]"
hint: Reusable text strings
value: "[Variables]\n$1"
unique: true
-
title: "[Measure]"
hint: Retrieve information
Expand Down

0 comments on commit 0f53433

Please sign in to comment.