diff options
author | 2011-03-17 17:41:02 -0400 | |
---|---|---|
committer | 2011-03-17 17:41:02 -0400 | |
commit | a1179904c022d6ccd22ebd00a4db2618a9d9ca34 (patch) | |
tree | 825bae061b80facf01a5fbf921e9120042c4211c /modules/textadept | |
parent | 8f061a7148266ce8d4a05459f7de680a47b8899e (diff) | |
download | textadept-a1179904c022d6ccd22ebd00a4db2618a9d9ca34.tar.gz textadept-a1179904c022d6ccd22ebd00a4db2618a9d9ca34.zip |
Performance and speed improvements.
Diffstat (limited to 'modules/textadept')
-rw-r--r-- | modules/textadept/adeptsense.lua | 2 | ||||
-rw-r--r-- | modules/textadept/editing.lua | 21 | ||||
-rw-r--r-- | modules/textadept/find.lua | 56 | ||||
-rw-r--r-- | modules/textadept/menu.lua | 10 | ||||
-rw-r--r-- | modules/textadept/mime_types.lua | 4 | ||||
-rw-r--r-- | modules/textadept/snapopen.lua | 2 | ||||
-rw-r--r-- | modules/textadept/snippets.lua | 5 |
7 files changed, 57 insertions, 43 deletions
diff --git a/modules/textadept/adeptsense.lua b/modules/textadept/adeptsense.lua index a1dfc03c..95de505e 100644 --- a/modules/textadept/adeptsense.lua +++ b/modules/textadept/adeptsense.lua @@ -635,7 +635,7 @@ function load_ctags(sense, tag_file, nolocations) -- Update completions. -- If no class structure is found, the global namespace is used. for _, key in ipairs{ 'class', 'interface', 'struct', 'union', '' } do - local class = (#key == 0) and '' or ext_fields:match(key..':(%S+)') + local class = (key == '') and '' or ext_fields:match(key..':(%S+)') if class then if not completions[class] then completions[class] = { fields = {}, functions = {} } diff --git a/modules/textadept/editing.lua b/modules/textadept/editing.lua index a24347df..629ee017 100644 --- a/modules/textadept/editing.lua +++ b/modules/textadept/editing.lua @@ -176,12 +176,13 @@ function autocomplete_word(word_chars) 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 == 0 then return end + if not root or root == '' then return end + local patt = '^['..word_chars..']+' buffer.target_start, buffer.target_end = 0, buffer.length buffer.search_flags = 1048580 -- word start and match case local match_pos = buffer:search_in_target(root) while match_pos ~= -1 do - local s, e = buffer_text:find('^['..word_chars..']+', match_pos + 1) + local s, e = buffer_text:find(patt, match_pos + 1) local match = buffer_text:sub(s, e) if not completions[match] and #match > #root then c_list[#c_list + 1] = match @@ -251,15 +252,17 @@ function prepare_for_save() local buffer = buffer buffer:begin_undo_action() -- Strip trailing whitespace. + local line_end_position = buffer.line_end_position + local char_at = buffer.char_at local lines = buffer.line_count for line = 0, lines - 1 do local s = buffer:position_from_line(line) - local e = buffer.line_end_position[line] + local e = line_end_position[line] local i = e - 1 - local c = buffer.char_at[i] + local c = char_at[i] while i >= s and c == 9 or c == 32 do i = i - 1 - c = buffer.char_at[i] + c = char_at[i] end if i < e - 1 then buffer.target_start, buffer.target_end = i + 1, e @@ -326,7 +329,7 @@ function enclose(left, right) local buffer = buffer buffer:begin_undo_action() local txt = buffer:get_sel_text() - if #txt == 0 then + if txt == '' then buffer:word_left_extend() txt = buffer:get_sel_text() end @@ -418,11 +421,13 @@ end -- Converts indentation between tabs and spaces. function convert_indentation() local buffer = buffer + local line_indentation = buffer.line_indentation + local line_indent_position = buffer.line_indent_position buffer:begin_undo_action() for line = 0, buffer.line_count do local s = buffer:position_from_line(line) - local indent = buffer.line_indentation[line] - local indent_pos = buffer.line_indent_position[line] + local indent = line_indentation[line] + local indent_pos = line_indent_position[line] current_indentation = buffer:text_range(s, indent_pos) if buffer.use_tabs then new_indentation = ('\t'):rep(indent / buffer.tab_width) diff --git a/modules/textadept/find.lua b/modules/textadept/find.lua index e4b1d967..dcb570ba 100644 --- a/modules/textadept/find.lua +++ b/modules/textadept/find.lua @@ -3,6 +3,7 @@ local L = _G.locale.localize local events = _G.events local find = gui.find +local c = _SCINTILLA.constants local MARK_FIND = 0 local MARK_FIND_COLOR = 0x4D9999 @@ -32,7 +33,6 @@ function find.find_in_files(utf8_dir) if not find.match_case then text = text:lower() end if find.whole_word then text = '[^%W_]'..text..'[^%W_]' end local match_case, whole_word = find.match_case, find.whole_word - local string_find, format = string.find, string.format local matches = { 'Find: '..text } function search_file(file) local line_num = 1 @@ -40,9 +40,9 @@ function find.find_in_files(utf8_dir) local optimized_line = line if not match_case then optimized_line = line:lower() end if whole_word then optimized_line = ' '..line..' ' end - if string_find(optimized_line, text) then + if optimized_line:find(text) then file = file:iconv('UTF-8', _CHARSET) - matches[#matches + 1] = format('%s:%s:%s', file, line_num, line) + matches[#matches + 1] = ('%s:%s:%s'):format(file, line_num, line) end line_num = line_num + 1 end @@ -52,7 +52,7 @@ function find.find_in_files(utf8_dir) for file in lfs_dir(directory) do if not file:find('^%.%.?$') then -- ignore . and .. local path = directory..'/'..file - local type = lfs_attributes(path).mode + local type = lfs_attributes(path, 'mode') if type == 'directory' then search_dir(path) elseif type == 'file' then @@ -82,7 +82,7 @@ end -- internally, and should not be set otherwise. -- @return position of the found text or -1 local function find_(text, next, flags, nowrap, wrapped) - if #text == 0 then return end + if text == '' then return end local buffer = buffer local first_visible_line = buffer.first_visible_line -- for 'no results found' @@ -94,7 +94,6 @@ local function find_(text, next, flags, nowrap, wrapped) end if not flags then - local find, c = find, _SCINTILLA.constants flags = 0 if find.match_case then flags = flags + c.SCFIND_MATCHCASE end if find.whole_word then flags = flags + c.SCFIND_WHOLEWORD end @@ -152,7 +151,6 @@ events.connect('find', find_) -- Flags other than SCFIND_MATCHCASE are ignored. -- @param text The text to find. local function find_incremental(text) - local c = _SCINTILLA.constants local flags = find.match_case and c.SCFIND_MATCHCASE or 0 buffer:goto_pos(find.incremental_start or 0) find_(text, true, flags) @@ -191,6 +189,26 @@ events.connect('command_entry_command', function(text) end end, 1) -- place before command_entry.lua's handler (if necessary) +-- Optimize for speed. +local loadstring = _G.loadstring +local pcall = _G.pcall + +-- Runs the given code. +-- This function is passed to string.gsub() in the replace() function. +-- @param code The code to run. +local function run(code) + local ok, val = pcall(loadstring('return '..code)) + if not ok then + gui.dialog('ok-msgbox', + '--title', L('Error'), + '--text', L('An error occured:'), + '--informative-text', val:gsub('"', '\\"'), + '--no-cancel') + error() + end + return val +end + -- Replaces found text. -- 'find_' is called first, to select any found text. The selected text is then -- replaced by the specified replacement text. @@ -200,29 +218,19 @@ end, 1) -- place before command_entry.lua's handler (if necessary) -- sequences for embedding Lua code for any search. -- @see find local function replace(rtext) - if #buffer:get_sel_text() == 0 then return end + if buffer:get_sel_text() == '' then return end if find.in_files then find.in_files = false end local buffer = buffer buffer:target_from_selection() rtext = rtext:gsub('%%%%', '\\037') -- escape '%%' - if find.captures then - for i, v in ipairs(find.captures) do - v = v:gsub('%%', '%%%%') -- escape '%' for gsub + local captures = find.captures + if captures then + for i = 1, #captures do + local v = captures[i]:gsub('%%', '%%%%') -- escape '%' for gsub rtext = rtext:gsub('%%'..i, v) end end - local ok, rtext = pcall(rtext.gsub, rtext, '%%(%b())', function(code) - local ok, val = pcall(loadstring('return '..code)) - if not ok then - gui.dialog('ok-msgbox', - '--title', L('Error'), - '--text', L('An error occured:'), - '--informative-text', val:gsub('"', '\\"'), - '--no-cancel') - error() - end - return val - end) + local ok, rtext = pcall(rtext.gsub, rtext, '%%(%b())', run) if ok then rtext = rtext:gsub('\\037', '%%') -- unescape '%' buffer:replace_target(rtext:gsub('\\[abfnrtv\\]', escapes)) @@ -276,7 +284,7 @@ local function replace_all(ftext, rtext, flags) buffer:set_sel(anchor, current_pos) buffer:marker_delete_handle(end_marker) end - gui.statusbar_text = string.format("%d %s", count, L('replacement(s) made')) + gui.statusbar_text = ("%d %s"):format(count, L('replacement(s) made')) buffer:end_undo_action() end events.connect('replace_all', replace_all) diff --git a/modules/textadept/menu.lua b/modules/textadept/menu.lua index a341b736..5dab1ff7 100644 --- a/modules/textadept/menu.lua +++ b/modules/textadept/menu.lua @@ -332,10 +332,10 @@ local function read_menu_table(menu) gtkmenu.title = menu.title for _, menuitem in ipairs(menu) do if menuitem.title then - table.insert(gtkmenu, read_menu_table(menuitem)) + gtkmenu[#gtkmenu + 1] = read_menu_table(menuitem) else local menu_id = #menu_actions + 1 - table.insert(gtkmenu, { menuitem[1], menu_id }) + gtkmenu[#gtkmenu + 1] = { menuitem[1], menu_id } if menuitem[2] then menu_actions[menu_id] = menuitem[2] end end end @@ -353,8 +353,8 @@ end function set_menubar(menubar) menu_actions = {} local _menubar = {} - for _, menu in ipairs(menubar) do - _menubar[#_menubar + 1] = gui.gtkmenu(read_menu_table(menu)) + for i = 1, #menubar do + _menubar[#_menubar + 1] = gui.gtkmenu(read_menu_table(menubar[i])) end gui.menubar = _menubar end @@ -368,7 +368,7 @@ function set_contextmenu(menu_table) context_actions = {} local context_menu = {} for menu_id, menuitem in ipairs(menu_table) do - table.insert(context_menu, { menuitem[1], menu_id + 1000 }) + context_menu[#context_menu + 1] = { menuitem[1], menu_id + 1000 } if menuitem[2] then context_actions[menu_id] = menuitem[2] end end gui.context_menu = gui.gtkmenu(context_menu) diff --git a/modules/textadept/mime_types.lua b/modules/textadept/mime_types.lua index f642e3ed..0d402fd2 100644 --- a/modules/textadept/mime_types.lua +++ b/modules/textadept/mime_types.lua @@ -147,9 +147,9 @@ local function set_lexer(buffer, lang) if ws_styles[lang] then return end -- Create the ws_styles[lexer] lookup table for get_lexer(). - local ws, find = {}, string.find + local ws = {} for i = 0, 255 do - ws[i] = find(get_style_name(buffer, i), 'whitespace') and true or false + ws[i] = buffer:private_lexer_call(i):find('whitespace') ~= nil end ws_styles[lang] = ws end diff --git a/modules/textadept/snapopen.lua b/modules/textadept/snapopen.lua index 74c2c2fc..fb7edc7c 100644 --- a/modules/textadept/snapopen.lua +++ b/modules/textadept/snapopen.lua @@ -69,7 +69,7 @@ local function add_directory(utf8_dir, list, depth, filter) for file in lfs_dir(dir) do
if not string_match(file, '^%.%.?$') then
file = dir..(not WIN32 and '/' or '\\')..file
- if lfs_attributes(file).mode == 'directory' then
+ if lfs_attributes(file, 'mode') == 'directory' then
if not exclude(file, filter.folders) and depth < DEPTH then
add_directory(file, list, depth + 1, filter)
end
diff --git a/modules/textadept/snippets.lua b/modules/textadept/snippets.lua index 648909e4..6f4c10a2 100644 --- a/modules/textadept/snippets.lua +++ b/modules/textadept/snippets.lua @@ -379,9 +379,10 @@ function _insert(s_text) local ref_line = buffer:line_from_position(start) local isize, ibase = buffer.indent, buffer.line_indentation[ref_line] local inum = ibase / isize -- number of indents needed to match + local line_indentation = buffer.line_indentation for i = 1, count do - local linei = buffer.line_indentation[ref_line + i] - buffer.line_indentation[ref_line + i] = linei + isize * inum + local linei = line_indentation[ref_line + i] + line_indentation[ref_line + i] = linei + isize * inum end end buffer:end_undo_action() |