From 0f534335067653c01bd5ea1c7ef784bbaf9c8432 Mon Sep 17 00:00:00 2001 From: thatsIch Date: Tue, 27 Dec 2016 01:15:00 +0100 Subject: [PATCH] Added filter to remove the unique sections from auto completion Fixed detection of section with newlines Added setting option to force allow duplicate sections --- Rainmeter.sublime-settings | 5 +++++ completion/completion.py | 2 +- completion/section.py | 42 ++++++++++++++++++++++++-------------- completion/section.yaml | 3 +++ 4 files changed, 36 insertions(+), 16 deletions(-) diff --git a/Rainmeter.sublime-settings b/Rainmeter.sublime-settings index 989fae8..9bde9ac 100644 --- a/Rainmeter.sublime-settings +++ b/Rainmeter.sublime-settings @@ -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": [ diff --git a/completion/completion.py b/completion/completion.py index d9a9c07..5c6e2cd 100644 --- a/completion/completion.py +++ b/completion/completion.py @@ -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) diff --git a/completion/section.py b/completion/section.py index cffb521..bfefd6b 100644 --- a/completion/section.py +++ b/completion/section.py @@ -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 @@ -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 @@ -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 diff --git a/completion/section.yaml b/completion/section.yaml index bcc647b..7c0d649 100644 --- a/completion/section.yaml +++ b/completion/section.yaml @@ -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