From 8bcdb16e18641e1cea08ebb614d70d98a3c27426 Mon Sep 17 00:00:00 2001 From: mitchell <70453897+667e-11@users.noreply.github.com> Date: Mon, 26 Sep 2011 18:11:57 -0400 Subject: Lua code cleanup. --- modules/textadept/adeptsense.lua | 5 +--- modules/textadept/bookmarks.lua | 5 ++-- modules/textadept/command_entry.lua | 15 ++++------- modules/textadept/editing.lua | 19 ++++++-------- modules/textadept/find.lua | 15 +++++------ modules/textadept/keys.lua | 2 +- modules/textadept/mime_types.lua | 52 +++++++++++-------------------------- modules/textadept/run.lua | 5 ++-- modules/textadept/session.lua | 16 ++++-------- modules/textadept/snippets.lua | 2 +- 10 files changed, 48 insertions(+), 88 deletions(-) (limited to 'modules/textadept') diff --git a/modules/textadept/adeptsense.lua b/modules/textadept/adeptsense.lua index f54c3064..20dae39d 100644 --- a/modules/textadept/adeptsense.lua +++ b/modules/textadept/adeptsense.lua @@ -575,10 +575,7 @@ function get_apidoc(sense, symbol) if entity == '' then class = sense:get_class(entity) end if type(class) ~= 'string' then class = entity end -- fall back to entity for i, apidoc in ipairs(apidocs) do - if apidoc:sub(1, #class) == class then - apidocs.pos = i - break - end + if apidoc:sub(1, #class) == class then apidocs.pos = i break end end return apidocs end diff --git a/modules/textadept/bookmarks.lua b/modules/textadept/bookmarks.lua index 57e3da64..a032d19c 100644 --- a/modules/textadept/bookmarks.lua +++ b/modules/textadept/bookmarks.lua @@ -86,5 +86,6 @@ function goto() end if buffer then buffer:marker_set_back(MARK_BOOKMARK, MARK_BOOKMARK_COLOR) end -events.connect(events.VIEW_NEW, - function() buffer:marker_set_back(MARK_BOOKMARK, MARK_BOOKMARK_COLOR) end) +events.connect(events.VIEW_NEW, function() + buffer:marker_set_back(MARK_BOOKMARK, MARK_BOOKMARK_COLOR) +end) diff --git a/modules/textadept/command_entry.lua b/modules/textadept/command_entry.lua index b2eab2c1..b574f9dd 100644 --- a/modules/textadept/command_entry.lua +++ b/modules/textadept/command_entry.lua @@ -19,10 +19,7 @@ local env = setmetatable({}, { end, __newindex = function(t, k, v) for _, t2 in ipairs{ buffer, view, gui } do - if t2[k] ~= nil then - t2[k] = v - return - end + if t2[k] ~= nil then t2[k] = v return end end rawset(t, k, v) end, @@ -33,18 +30,16 @@ events.connect(events.COMMAND_ENTRY_COMMAND, function(command) local f, err = loadstring(command) if err then error(err) end gui.command_entry.focus() -- toggle focus to hide - setfenv(f, env) - f() + setfenv(f, env)() events.emit(events.UPDATE_UI) end) events.connect(events.COMMAND_ENTRY_KEYPRESS, function(code) - local ce = gui.command_entry if keys.KEYSYMS[code] == 'esc' then - ce.focus() -- toggle focus to hide + gui.command_entry.focus() -- toggle focus to hide return true elseif keys.KEYSYMS[code] == '\t' then - local substring = ce.entry_text:match('[%w_.:]+$') or '' + local substring = gui.command_entry.entry_text:match('[%w_.:]+$') or '' local path, o, prefix = substring:match('^([%w_.:]-)([.:]?)([%w_]*)$') local f, err = loadstring('return ('..path..')') if type(f) == "function" then setfenv(f, env) end @@ -83,7 +78,7 @@ events.connect(events.COMMAND_ENTRY_KEYPRESS, function(code) end end table.sort(cmpls) - ce.show_completions(cmpls) + gui.command_entry.show_completions(cmpls) return true end end) diff --git a/modules/textadept/editing.lua b/modules/textadept/editing.lua index bb859b7e..cb340cfc 100644 --- a/modules/textadept/editing.lua +++ b/modules/textadept/editing.lua @@ -199,8 +199,7 @@ function autocomplete_word(word_chars) 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 - completions[match] = true + c_list[#c_list + 1], completions[match] = match, true end buffer.target_start, buffer.target_end = match_pos + 1, buffer.length match_pos = buffer:search_in_target(root) @@ -277,12 +276,10 @@ 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 line_end_position, char_at = buffer.line_end_position, buffer.char_at local lines = buffer.line_count for line = 0, lines - 1 do - local s = buffer:position_from_line(line) - local e = line_end_position[line] + local s, e = buffer:position_from_line(line), line_end_position[line] local i = e - 1 local c = char_at[i] while i >= s and c == 9 or c == 32 do @@ -312,9 +309,8 @@ events.connect(events.FILE_BEFORE_SAVE, prepare_for_save) -- is deleted. function current_word(action) local buffer = buffer - local s = buffer:word_start_position(buffer.current_pos) - local e = buffer:word_end_position(buffer.current_pos) - buffer:set_sel(s, e) + buffer:set_sel(buffer:word_start_position(buffer.current_pos), + buffer:word_end_position(buffer.current_pos)) if action == 'delete' then buffer:delete_back() end end @@ -478,8 +474,9 @@ local function clear_highlighted_words() buffer.indicator_current = INDIC_HIGHLIGHT buffer:indicator_clear_range(0, buffer.length) end -events.connect(events.KEYPRESS, - function(code) if K[code] == 'esc' then clear_highlighted_words() end end) +events.connect(events.KEYPRESS, function(code) + if K[code] == 'esc' then clear_highlighted_words() end +end) --- -- Highlights all occurances of the word under the caret and adds markers to the diff --git a/modules/textadept/find.lua b/modules/textadept/find.lua index 409ffe4c..8debc7ec 100644 --- a/modules/textadept/find.lua +++ b/modules/textadept/find.lua @@ -238,8 +238,7 @@ local function replace(rtext) 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) + rtext = rtext:gsub('%%'..i, captures[i]:gsub('%%', '%%%%')) end end local ok, rtext = pcall(rtext.gsub, rtext, '%%(%b())', run) @@ -301,6 +300,7 @@ local function replace_all(ftext, rtext, flags) end events.connect(events.REPLACE_ALL, replace_all) +-- TODO: refactor? -- When the user double-clicks a found file, go to the line in the file the text -- was found at. -- @param pos The position of the caret. @@ -354,10 +354,8 @@ function find.goto_file_in_list(next) line = line + (next and 1 or -1) if line > buffer.line_count - 1 then line = 0 end if line < 0 then line = buffer.line_count - 1 end - if line == orig_line then -- prevent infinite loops - gui.goto_view(orig_view) - return - end + -- Prevent infinite loops. + if line == orig_line then gui.goto_view(orig_view) return end if buffer:get_line(line):match('^(.+):(%d+):.+$') then buffer:goto_line(line) goto_file(buffer.current_pos, line) @@ -371,5 +369,6 @@ function find.goto_file_in_list(next) end if buffer then buffer:marker_set_back(MARK_FIND, MARK_FIND_COLOR) end -events.connect(events.VIEW_NEW, - function() buffer:marker_set_back(MARK_FIND, MARK_FIND_COLOR) end) +events.connect(events.VIEW_NEW, function() + buffer:marker_set_back(MARK_FIND, MARK_FIND_COLOR) +end) diff --git a/modules/textadept/keys.lua b/modules/textadept/keys.lua index 5672933a..d3bdb1ec 100644 --- a/modules/textadept/keys.lua +++ b/modules/textadept/keys.lua @@ -94,7 +94,7 @@ local function constantize_menu_buffer_functions() for _, f in ipairs(menu_buffer_functions) do buffer[f] = buffer[f] end end events.connect(events.BUFFER_NEW, constantize_menu_buffer_functions) --- Scintilla's first buffer doesn't have this. +-- Scintilla's first buffer does not have this. if not RESETTING then constantize_menu_buffer_functions() end --[[ diff --git a/modules/textadept/mime_types.lua b/modules/textadept/mime_types.lua index 3d461db4..119d5b55 100644 --- a/modules/textadept/mime_types.lua +++ b/modules/textadept/mime_types.lua @@ -100,11 +100,7 @@ end for lexer in pairs(lexers_found) do lexers[#lexers + 1] = lexer end table.sort(lexers) --- --- Returns the name of the style associated with a style number. --- @param buffer The buffer to get the style name of. --- @param style_num A style number in the range 0 <= style_num < 256. --- @see buffer.style_at +-- LuaDoc is in core/.buffer.luadoc. local function get_style_name(buffer, style_num) buffer:check_global() if style_num < 0 or style_num > 255 then error('0 <= style_num < 256') end @@ -117,31 +113,22 @@ end -- @class table -- @name ws_styles local ws_styles = {} - local SETDIRECTPOINTER = _SCINTILLA.properties.doc_pointer[2] local SETLEXERLANGUAGE = _SCINTILLA.functions.set_lexer_language[1] --- --- Replacement for buffer:set_lexer_language(). --- Sets a buffer._lexer field so it can be restored without querying the --- mime-types tables. Also if the user manually sets the lexer, it should be --- restored. --- Loads the language-specific module if it exists. --- @param buffer The buffer to set the lexer language of. --- @param lang The string language to set. --- @usage buffer:set_lexer('language_name') +-- LuaDoc is in core/.buffer.luadoc. local function set_lexer(buffer, lang) buffer:check_global() buffer._lexer = lang buffer:private_lexer_call(SETDIRECTPOINTER, buffer.direct_pointer) buffer:private_lexer_call(SETLEXERLANGUAGE, lang) - local ret, err = pcall(require, lang) - if ret then - ret, err = pcall(require, lang..'.post_init') + local ok, err = pcall(require, lang) + if ok then + ok, err = pcall(require, lang..'.post_init') _m[lang].set_buffer_properties() events.emit(events.LANGUAGE_MODULE_LOADED, lang) end local module_not_found = "^module '"..lang.."[^\']*' not found:" - if not ret and not err:find(module_not_found) then error(err) end + if not ok and not err:find(module_not_found) then error(err) end buffer:colourise(0, -1) if ws_styles[lang] then return end @@ -154,12 +141,7 @@ local function set_lexer(buffer, lang) end local GETLEXERLANGUAGE = _SCINTILLA.functions.get_lexer_language[1] --- --- Replacement for buffer:get_lexer_language(). --- @param buffer The buffer to get the lexer language of. --- @param current If true, returns the lexer at the current caret position. This --- lexer can be different from the lexer passed to buffer:set_lexer(). --- Defaults to false. +-- LuaDoc is in core/.buffer.luadoc. local function get_lexer(buffer, current) buffer:check_global() local lexer = buffer:private_lexer_call(GETLEXERLANGUAGE) @@ -173,7 +155,7 @@ events.connect(events.BUFFER_NEW, function() buffer.set_lexer, buffer.get_lexer = set_lexer, get_lexer buffer.get_style_name = get_style_name end, 1) --- Scintilla's first buffer doesn't have these. +-- Scintilla's first buffer does not have these. if not RESETTING then buffer.set_lexer, buffer.get_lexer = set_lexer, get_lexer buffer.get_style_name = get_style_name @@ -192,10 +174,7 @@ local function handle_new() end if not lexer then for patt, lex in pairs(patterns) do - if line:find(patt) then - lexer = lex - break - end + if line:find(patt) then lexer = lex break end end end if not lexer and buffer.filename then @@ -203,6 +182,8 @@ local function handle_new() end buffer:set_lexer(lexer or 'container') end +events.connect(events.FILE_OPENED, handle_new) +events.connect(events.FILE_SAVED_AS, handle_new) -- Sets the buffer's lexer based on filename, shebang words, or -- first line pattern. @@ -210,14 +191,11 @@ local function restore_lexer() buffer:private_lexer_call(SETDIRECTPOINTER, buffer.direct_pointer) buffer:private_lexer_call(SETLEXERLANGUAGE, buffer._lexer or 'container') end +events.connect(events.BUFFER_AFTER_SWITCH, restore_lexer) +events.connect(events.VIEW_NEW, restore_lexer, 1) -local connect = events.connect -connect(events.FILE_OPENED, handle_new) -connect(events.FILE_SAVED_AS, handle_new) -connect(events.BUFFER_AFTER_SWITCH, restore_lexer) -connect(events.VIEW_NEW, restore_lexer, 1) -connect(events.RESET_AFTER, - function() buffer:set_lexer(buffer._lexer or 'container') end) +events.connect(events.RESET_AFTER, + function() buffer:set_lexer(buffer._lexer or 'container') end) --- -- Prompts the user to select a lexer from a filtered list for the current diff --git a/modules/textadept/run.lua b/modules/textadept/run.lua index 34171ce2..567178fb 100644 --- a/modules/textadept/run.lua +++ b/modules/textadept/run.lua @@ -68,9 +68,8 @@ local function command(cmd_table) if not buffer.filename then return end buffer:save() local action = cmd_table[buffer.filename:match('[^.]+$')] - if action then - return execute(type(action) == 'function' and action() or action) - end + if not action then return end + return execute(type(action) == 'function' and action() or action) end --- diff --git a/modules/textadept/session.lua b/modules/textadept/session.lua index 1178380f..9890b7f2 100644 --- a/modules/textadept/session.lua +++ b/modules/textadept/session.lua @@ -33,10 +33,7 @@ MAX_RECENT_FILES = 10 function load(filename) local not_found = {} local f = io.open(filename or DEFAULT_SESSION, 'rb') - if not f then - io.close_all() - return false - end + if not f then io.close_all() return false end local current_view, splits = 1, { [0] = {} } local lfs_attributes = lfs.attributes for line in f:lines() do @@ -50,8 +47,7 @@ function load(filename) not_found[#not_found + 1] = filename end else - new_buffer() - buffer._type = filename + new_buffer()._type = filename events.emit(events.FILE_OPENED, filename) end -- Restore saved buffer selection and view. @@ -62,7 +58,7 @@ function load(filename) buffer._anchor, buffer._current_pos = anchor, current_pos buffer._first_visible_line = first_visible_line buffer:line_scroll(0, - buffer:visible_from_doc_line(first_visible_line)) + buffer:visible_from_doc_line(first_visible_line)) buffer:set_sel(anchor, current_pos) elseif line:find('^%s*split%d:') then local level, num, type, size = @@ -77,8 +73,7 @@ function load(filename) if buf_idx > #_BUFFERS then buf_idx = #_BUFFERS end view:goto_buffer(buf_idx) elseif line:find('^current_view:') then - local view_idx = line:match('^current_view: (%d+)') - current_view = tonumber(view_idx) or 1 + current_view = tonumber(line:match('^current_view: (%d+)')) or 1 elseif line:find('^size:') then local width, height = line:match('^size: (%d+) (%d+)$') if width and height then gui.size = { width, height } end @@ -97,8 +92,7 @@ function load(filename) gui.dialog('msgbox', '--title', L('Session Files Not Found'), '--text', L('The following session files were not found'), - '--informative-text', - string.format('%s', table.concat(not_found, '\n'))) + '--informative-text', table.concat(not_found, '\n')) end return true end diff --git a/modules/textadept/snippets.lua b/modules/textadept/snippets.lua index a89931d1..337e056e 100644 --- a/modules/textadept/snippets.lua +++ b/modules/textadept/snippets.lua @@ -420,7 +420,7 @@ _snippet_mt = { local INDIC_HIDDEN = _SCINTILLA.constants.INDIC_HIDDEN if buffer then buffer.indic_style[INDIC_SNIPPET] = INDIC_HIDDEN end events.connect(events.VIEW_NEW, - function() buffer.indic_style[INDIC_SNIPPET] = INDIC_HIDDEN end) + function() buffer.indic_style[INDIC_SNIPPET] = INDIC_HIDDEN end) --- -- Provides access to snippets from _G. -- cgit v1.2.3