diff options
-rwxr-xr-x | pgcli/main.py | 15 | ||||
-rw-r--r-- | pgcli/pgclirc | 3 | ||||
-rw-r--r-- | pgcli/pgcompleter.py | 19 | ||||
-rw-r--r-- | tests/test_smart_completion_public_schema_only.py | 15 |
4 files changed, 46 insertions, 6 deletions
diff --git a/pgcli/main.py b/pgcli/main.py index 4d29e0aa..8db9f7d1 100755 --- a/pgcli/main.py +++ b/pgcli/main.py @@ -143,11 +143,16 @@ class PGCli(object): # Initialize completer smart_completion = c['main'].as_bool('smart_completion') - self.settings = {'casing_file': get_casing_file(c), - 'generate_casing_file': c['main'].as_bool('generate_casing_file'), - 'generate_aliases': c['main'].as_bool('generate_aliases'), - 'asterisk_column_order': c['main']['asterisk_column_order'], - 'single_connection': single_connection} + keyword_casing = c['main']['keyword_casing'] + self.settings = { + 'casing_file': get_casing_file(c), + 'generate_casing_file': c['main'].as_bool('generate_casing_file'), + 'generate_aliases': c['main'].as_bool('generate_aliases'), + 'asterisk_column_order': c['main']['asterisk_column_order'], + 'single_connection': single_connection, + 'keyword_casing': keyword_casing, + } + completer = PGCompleter(smart_completion, pgspecial=self.pgspecial, settings=self.settings) self.completer = completer diff --git a/pgcli/pgclirc b/pgcli/pgclirc index f235425d..92a2c414 100644 --- a/pgcli/pgclirc +++ b/pgcli/pgclirc @@ -24,6 +24,9 @@ generate_aliases = False # %USERPROFILE% is typically C:\Users\{username} log_file = default +# keyword casing preference. Possible values "lower", "upper", "auto" +keyword_casing = upper + # casing_file location. # In Unix/Linux: ~/.config/pgcli/casing # In Windows: %USERPROFILE%\AppData\Local\dbcli\pgcli\casing diff --git a/pgcli/pgcompleter.py b/pgcli/pgcompleter.py index 580bbc9d..81a415a1 100644 --- a/pgcli/pgcompleter.py +++ b/pgcli/pgcompleter.py @@ -60,6 +60,11 @@ class PGCompleter(Completer): self.asterisk_column_order = settings.get( 'asterisk_column_order', 'table_order') + keyword_casing = settings.get('keyword_casing', 'upper').lower() + if keyword_casing not in ('upper', 'lower', 'auto'): + keyword_casing = 'upper' + self.keyword_casing = keyword_casing + self.reserved_words = set() for x in self.keywords: self.reserved_words.update(x.split()) @@ -612,7 +617,19 @@ class PGCompleter(Completer): meta='database') def get_keyword_matches(self, _, word_before_cursor): - return self.find_matches(word_before_cursor, self.keywords, + casing = self.keyword_casing + if casing == 'auto': + if word_before_cursor and word_before_cursor[-1].islower(): + casing = 'lower' + else: + casing = 'upper' + + if casing == 'upper': + keywords = [k.upper() for k in self.keywords] + else: + keywords = [k.lower() for k in self.keywords] + + return self.find_matches(word_before_cursor, keywords, mode='strict', meta='keyword') def get_path_matches(self, _, word_before_cursor): diff --git a/tests/test_smart_completion_public_schema_only.py b/tests/test_smart_completion_public_schema_only.py index c1d65ab5..dab64757 100644 --- a/tests/test_smart_completion_public_schema_only.py +++ b/tests/test_smart_completion_public_schema_only.py @@ -914,3 +914,18 @@ def test_cte_qualified_columns(completer, complete_event, text): complete_event) expected = [Completion('foo', 0, display_meta='column')] assert set(expected) == set(result) + + +@pytest.mark.parametrize('keyword_casing,expected,texts', [ + ('upper', 'SELECT', ('', 's', 'S', 'Sel')), + ('lower', 'select', ('', 's', 'S', 'Sel')), + ('auto', 'SELECT', ('', 'S', 'SEL', 'seL')), + ('auto', 'select', ('s', 'sel', 'SEl')), +]) +def test_keyword_casing_upper(keyword_casing, expected, texts): + for text in texts: + completer_ = testdata.get_completer({'keyword_casing': keyword_casing}) + completions = completer_.get_completions( + Document(text=text, cursor_position=len(text)), complete_event) + assert expected in [cpl.text for cpl in completions] + |