[geany/geany] 4980a0: Move language-specific parts of whether to trigger scope autocompletion to tm_parser

Jiří Techet git-noreply at xxxxx
Sun Jan 9 22:32:40 UTC 2022


Branch:      refs/heads/master
Author:      Jiří Techet <techet at gmail.com>
Committer:   Enrico Tröger <enrico.troeger at uvena.de>
Date:        Sun, 09 Jan 2022 22:32:40 UTC
Commit:      4980a03d50e975f9c64538e8fc8beefd5ea2f8ef
             https://github.com/geany/geany/commit/4980a03d50e975f9c64538e8fc8beefd5ea2f8ef

Log Message:
-----------
Move language-specific parts of whether to trigger scope autocompletion to tm_parser


Modified Paths:
--------------
    src/editor.c
    src/tagmanager/tm_parser.c
    src/tagmanager/tm_parser.h

Modified: src/editor.c
47 lines changed, 20 insertions(+), 27 deletions(-)
===================================================================
@@ -642,20 +642,23 @@ static void show_tags_list(GeanyEditor *editor, const GPtrArray *tags, gsize roo
 }
 
 
-/* do not use with long strings */
-static gboolean match_last_chars(ScintillaObject *sci, gint pos, const gchar *str)
+static gint scope_autocomplete_suffix(ScintillaObject *sci, TMParserType lang,
+	gint pos, gboolean *scope_sep)
 {
-	gsize len = strlen(str);
+	const gchar *sep = tm_parser_context_separator(lang);
+	const gsize max_len = 3;
+	gboolean is_scope_sep;
 	gchar *buf;
 
-	g_return_val_if_fail(len < 100, FALSE);
+	buf = g_alloca(max_len + 1);
+	sci_get_text_range(sci, pos - max_len, pos, buf);
 
-	if ((gint)len > pos)
-		return FALSE;
-
-	buf = g_alloca(len + 1);
-	sci_get_text_range(sci, pos - len, pos, buf);
-	return strcmp(str, buf) == 0;
+	is_scope_sep = g_str_has_suffix(buf, sep);
+	if (scope_sep)
+		*scope_sep = is_scope_sep;
+	if (is_scope_sep)
+		return strlen(sep);
+	return tm_parser_scope_autocomplete_suffix(lang, buf);
 }
 
 
@@ -706,6 +709,7 @@ static gboolean autocomplete_scope(GeanyEditor *editor, const gchar *root, gsize
 	gboolean ret = FALSE;
 	const gchar *current_scope;
 	const gchar *context_sep = tm_parser_context_separator(ft->lang);
+	gint autocomplete_suffix_len;
 
 	if (autocomplete_scope_shown)
 	{
@@ -720,22 +724,13 @@ static gboolean autocomplete_scope(GeanyEditor *editor, const gchar *root, gsize
 			typed = sci_get_char_at(sci, pos - 1);
 	}
 
-	/* make sure to keep in sync with similar checks below */
-	if (match_last_chars(sci, pos, context_sep))
-	{
-		pos -= strlen(context_sep);
-		scope_sep_typed = TRUE;
-	}
-	else if (typed == '.')
-		pos -= 1;
-	else if ((ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP) &&
-			match_last_chars(sci, pos, "->"))
-		pos -= 2;
-	else if (ft->id == GEANY_FILETYPES_CPP && match_last_chars(sci, pos, "->*"))
-		pos -= 3;
-	else
+	autocomplete_suffix_len = scope_autocomplete_suffix(sci, ft->lang, pos,
+		&scope_sep_typed);
+	if (autocomplete_suffix_len == 0)
 		return FALSE;
 
+	pos -= autocomplete_suffix_len;
+
 	/* allow for a space between word and operator */
 	while (pos > 0 && isspace(sci_get_char_at(sci, pos - 1)))
 		pos--;
@@ -765,9 +760,7 @@ static gboolean autocomplete_scope(GeanyEditor *editor, const gchar *root, gsize
 	pos -= strlen(name);
 	while (pos > 0 && isspace(sci_get_char_at(sci, pos - 1)))
 		pos--;
-	/* make sure to keep in sync with similar checks above */
-	member = match_last_chars(sci, pos, ".") || match_last_chars(sci, pos, context_sep) ||
-			 match_last_chars(sci, pos, "->") || match_last_chars(sci, pos, "->*");
+	member = scope_autocomplete_suffix(sci, ft->lang, pos, NULL) > 0;
 
 	if (symbols_get_current_scope(editor->document, &current_scope) == -1)
 		current_scope = "";


Modified: src/tagmanager/tm_parser.c
27 lines changed, 27 insertions(+), 0 deletions(-)
===================================================================
@@ -818,6 +818,33 @@ void tm_parser_verify_type_mappings(void)
 }
 
 
+/* When the suffix of 'str' is an operator that should trigger scope
+ * autocompletion, this function should return the length of the operator,
+ * zero otherwise. */
+gint tm_parser_scope_autocomplete_suffix(TMParserType lang, const gchar *str)
+{
+	const gchar *sep = tm_parser_context_separator(lang);
+
+	if (g_str_has_suffix(str, sep))
+		return strlen(sep);
+
+	switch (lang)
+	{
+		case TM_PARSER_C:
+		case TM_PARSER_CPP:
+			if (g_str_has_suffix(str, "."))
+				return 1;
+			else if (g_str_has_suffix(str, "->"))
+				return 2;
+			else if (lang == TM_PARSER_CPP && g_str_has_suffix(str, "->*"))
+				return 3;
+		default:
+			break;
+	}
+	return 0;
+}
+
+
 /* Get the name of constructor method. Arguments of this method will be used
  * for calltips when creating an object using the class name
  * (e.g. after the opening brace in 'c = MyClass()' in Python) */


Modified: src/tagmanager/tm_parser.h
2 lines changed, 2 insertions(+), 0 deletions(-)
===================================================================
@@ -124,6 +124,8 @@ gchar tm_parser_get_tag_kind(TMTagType type, TMParserType lang);
 
 TMTagType tm_parser_get_subparser_type(TMParserType lang, TMParserType sublang, TMTagType type);
 
+gint tm_parser_scope_autocomplete_suffix(TMParserType lang, const gchar *str);
+
 const gchar *tm_parser_get_constructor_method(TMParserType lang);
 
 gchar *tm_parser_update_scope(TMParserType lang, gchar *scope);



--------------
This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).


More information about the Commits mailing list