SF.net SVN: geany:[3370] trunk

ntrel at users.sourceforge.net ntrel at xxxxx
Thu Dec 11 18:54:35 UTC 2008


Revision: 3370
          http://geany.svn.sourceforge.net/geany/?rev=3370&view=rev
Author:   ntrel
Date:     2008-12-11 18:54:35 +0000 (Thu, 11 Dec 2008)

Log Message:
-----------
Make snippets only complete for the word stem to the left of the
cursor (#2390597).
Make snippets complete even when text is to the right of the
cursor and the hidden pref is not set, unless the snippet
completion key is space.
Fix possible memory leak when reading current word.
Add editor_read_word_stem().

Modified Paths:
--------------
    trunk/ChangeLog
    trunk/src/editor.c

Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2008-12-11 17:49:27 UTC (rev 3369)
+++ trunk/ChangeLog	2008-12-11 18:54:35 UTC (rev 3370)
@@ -30,6 +30,14 @@
    Make Shift+Mouse wheel scroll the editor view horizontally
    (#2410732).
    Add function sci_scroll_columns().
+ * src/editor.c:
+   Make snippets only complete for the word stem to the left of the
+   cursor (#2390597).
+   Make snippets complete even when text is to the right of the
+   cursor and the hidden pref is not set, unless the snippet
+   completion key is space.
+   Fix possible memory leak when reading current word.
+   Add editor_read_word_stem().
 
 
 2008-12-09  Enrico Tröger  <enrico(dot)troeger(at)uvena(dot)de>

Modified: trunk/src/editor.c
===================================================================
--- trunk/src/editor.c	2008-12-11 17:49:27 UTC (rev 3369)
+++ trunk/src/editor.c	2008-12-11 18:54:35 UTC (rev 3370)
@@ -1158,8 +1158,8 @@
  * NULL terminated in any case, even when the word is truncated because wordlen is too small.
  * position can be -1, then the current position is used.
  * wc are the wordchars to use, if NULL, GEANY_WORDCHARS will be used */
-void editor_find_current_word(GeanyEditor *editor, gint pos, gchar *word, size_t wordlen,
-							  const gchar *wc)
+static void read_current_word(GeanyEditor *editor, gint pos, gchar *word, size_t wordlen,
+							  const gchar *wc, gboolean stem)
 {
 	gint line, line_start, startword, endword;
 	gchar *chunk;
@@ -1188,18 +1188,47 @@
 	 * TODO: improve this code */
 	while (startword > 0 && (strchr(wc, chunk[startword - 1]) || chunk[startword - 1] < 0))
 		startword--;
-	while (chunk[endword] != 0 && (strchr(wc, chunk[endword]) || chunk[endword] < 0))
-		endword++;
-	if(startword == endword)
-		return;
+	if (!stem)
+	{
+		while (chunk[endword] != 0 && (strchr(wc, chunk[endword]) || chunk[endword] < 0))
+			endword++;
+	}
 
-	chunk[endword] = '\0';
+	if (startword != endword)
+	{
+		chunk[endword] = '\0';
 
-	g_strlcpy(word, chunk + startword, wordlen); /* ensure null terminated */
+		g_strlcpy(word, chunk + startword, wordlen); /* ensure null terminated */
+	}
+	else
+		g_strlcpy(word, "", wordlen);
+
 	g_free(chunk);
 }
 
 
+/* Reads the word at given cursor position and writes it into the given buffer. The buffer will be
+ * NULL terminated in any case, even when the word is truncated because wordlen is too small.
+ * position can be -1, then the current position is used.
+ * wc are the wordchars to use, if NULL, GEANY_WORDCHARS will be used */
+void editor_find_current_word(GeanyEditor *editor, gint pos, gchar *word, size_t wordlen,
+							  const gchar *wc)
+{
+	read_current_word(editor, pos, word, wordlen, wc, FALSE);
+}
+
+
+static const gchar *
+editor_read_word_stem(GeanyEditor *editor, gint pos, const gchar *wordchars)
+{
+	static gchar word[GEANY_MAX_WORD_LENGTH];
+
+	read_current_word(editor, pos, word, sizeof word, wordchars, TRUE);
+
+	return (*word) ? word : NULL;
+}
+
+
 static gint find_previous_brace(ScintillaObject *sci, gint pos)
 {
 	gchar c;
@@ -1897,6 +1926,7 @@
 	gboolean result = FALSE;
 	gint lexer, style;
 	gchar *wc;
+	const gchar *word;
 	ScintillaObject *sci;
 
 	if (editor == NULL)
@@ -1904,20 +1934,22 @@
 
 	sci = editor->sci;
 	/* return if we are editing an existing line (chars on right of cursor) */
-	if (! editor_prefs.complete_snippets_whilst_editing && ! at_eol(sci, pos))
+	if (keybindings_lookup_item(GEANY_KEY_GROUP_EDITOR,
+			GEANY_KEYS_EDITOR_COMPLETESNIPPET)->key == GDK_space &&
+		! editor_prefs.complete_snippets_whilst_editing && ! at_eol(sci, pos))
 		return FALSE;
 
 	lexer = SSM(sci, SCI_GETLEXER, 0, 0);
 	style = SSM(sci, SCI_GETSTYLEAT, pos - 2, 0);
 
 	wc = snippets_find_completion_by_name("Special", "wordchars");
-	editor_find_current_word(editor, pos, current_word, sizeof current_word, wc);
+	word = editor_read_word_stem(editor, pos, wc);
 
 	/* prevent completion of "for " */
 	if (! isspace(sci_get_char_at(sci, pos - 1))) /* pos points to the line end char so use pos -1 */
 	{
 		sci_start_undo_action(sci);	/* needed because we insert a space separately from construct */
-		result = snippets_complete_constructs(editor, pos, current_word);
+		result = snippets_complete_constructs(editor, pos, word);
 		sci_end_undo_action(sci);
 		if (result)
 			SSM(sci, SCI_CANCEL, 0, 0);	/* cancel any autocompletion list, etc */


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.



More information about the Commits mailing list