[code] Autocomplete feature request

From: Brian Schott <briancschott.att.gmail.com>
Date: Tue, 13 Mar 2012 19:39:20 -0700

I've modified my copy of editing.lua to allow autocomplete_word to
take an optional list of words that are treated as if they were
present in the buffer. This is useful for language modules that wish
to provide autocomplete for keywords and built-in types. The patch is
included below:

diff -r c885da8e6beb modules/textadept/editing.lua
--- a/modules/textadept/editing.lua Thu Mar 08 17:52:03 2012 -0500
+++ b/modules/textadept/editing.lua Tue Mar 13 19:30:41 2012 -0700
@@ -179,15 +179,26 @@
 -- Pops up an autocompletion list for the current word based on other words in
 -- the document.
 -- @param word_chars String of chars considered to be part of words.
+-- @param default_words a list of words that are considered to be in the
+-- document even if they are not. This parameter may be nil.
 -- @return `true` if there were completions to show; `false` otherwise.
 -- @name autocomplete_word
-function M.autocomplete_word(word_chars)
+function M.autocomplete_word(word_chars, default_words)
   local buffer = buffer
   local caret, length = buffer.current_pos, buffer.length
   local completions, c_list = {}, {}
   local buffer_text = buffer:get_text(buffer.length)
   local root = buffer_text:sub(1, caret):match('['..word_chars..']+$')
   if not root or root == '' then return end
+ if default_words then
+ for _, word in ipairs(default_words) do
+ if word:match('^'..root) then
+ c_list[#c_list + 1] = word
+ -- the gsub call here is for compatability with registered images
+ completions[word:gsub('%?%d+$', '')] = true
+ end
+ end
+ end
   local patt = '^['..word_chars..']+'
   buffer.target_start, buffer.target_end = 0, buffer.length
   buffer.search_flags = _SCINTILLA.constants.SCFIND_WORDSTART
@@ -216,7 +227,7 @@
     else
       -- Scintilla does not emit AUTO_C_SELECTION in this case. This is
       -- necessary for autocompletion with multiple selections.
- events.emit(events.AUTO_C_SELECTION, c_list[1], caret - #root)
+ events.emit(events.AUTO_C_SELECTION, c_list[1]:gsub('%?%d+$',
''), caret - #root)
     end
     return true
   end

-- 
You are subscribed to code.att.foicica.com.
To change subscription settings, send an e-mail to code+help.att.foicica.com.
To unsubscribe, send an e-mail to code+unsubscribe.att.foicica.com.
Received on Tue 13 Mar 2012 - 22:39:20 EDT

This archive was generated by hypermail 2.2.0 : Wed 14 Mar 2012 - 06:37:16 EDT