diff options
author | 2021-04-11 09:34:17 -0400 | |
---|---|---|
committer | 2021-04-11 09:34:17 -0400 | |
commit | de3a745e1af2e441de868c2aa4849102d376acb5 (patch) | |
tree | c2d7767600dc519b2613ddecaf7e53fb5e8867a2 /modules/textadept/history.lua | |
parent | 03fab17277fee7387fd93a9c2774b1ebf3f80fe4 (diff) | |
download | textadept-de3a745e1af2e441de868c2aa4849102d376acb5.tar.gz textadept-de3a745e1af2e441de868c2aa4849102d376acb5.zip |
Initial pass reformatting all code.
Use clang-format, LuaFormatter, and 100 character limit on lines.
Diffstat (limited to 'modules/textadept/history.lua')
-rw-r--r-- | modules/textadept/history.lua | 87 |
1 files changed, 39 insertions, 48 deletions
diff --git a/modules/textadept/history.lua b/modules/textadept/history.lua index 0c6113e4..da9f5885 100644 --- a/modules/textadept/history.lua +++ b/modules/textadept/history.lua @@ -4,14 +4,13 @@ local M = {} --[[ This comment is for LuaDoc. --- --- Records buffer positions within Textadept views over time and allows for --- navigating through that history. +-- Records buffer positions within Textadept views over time and allows for navigating through +-- that history. -- --- This module listens for text edit events and buffer switch events. Each time --- an insertion or deletion occurs, its location is recorded in the current --- view's location history. If the edit is close enough to the previous record, --- the previous record is amended. Each time a buffer switch occurs, the before --- and after locations are also recorded. +-- This module listens for text edit events and buffer switch events. Each time an insertion +-- or deletion occurs, its location is recorded in the current view's location history. If the +-- edit is close enough to the previous record, the previous record is amended. Each time a +-- buffer switch occurs, the before and after locations are also recorded. -- @field minimum_line_distance (number) -- The minimum number of lines between distinct history records. -- The default value is `3`. @@ -24,14 +23,15 @@ M.minimum_line_distance = 3 M.maximum_history_size = 100 -- Map of views to their history records. --- Each record has a `pos` field that points to the current history position in --- the associated view. +-- Each record has a `pos` field that points to the current history position in the associated view. -- @class table -- @name view_history -local view_history = setmetatable({}, {__index = function(t, view) - t[view] = {pos = 0} - return t[view] -end}) +local view_history = setmetatable({}, { + __index = function(t, view) + t[view] = {pos = 0} + return t[view] + end +}) -- Listens for text insertion and deletion events and records their locations. events.connect(events.MODIFIED, function(position, mod, text, length) @@ -50,8 +50,7 @@ events.connect(events.MODIFIED, function(position, mod, text, length) M.record(nil, buffer:line_from_position(position), buffer.column[position]) end) --- Do not record positions during buffer switches when jumping backwards or --- forwards. +-- Do not record positions during buffer switches when jumping backwards or forwards. local jumping = false -- Jumps to the given record in the current view's history. @@ -64,8 +63,7 @@ local function jump(record) else for _, buffer in ipairs(_BUFFERS) do if buffer.filename == filename or buffer._type == filename or - not buffer.filename and not buffer._type and - filename == _L['Untitled'] then + (not buffer.filename and not buffer._type and filename == _L['Untitled']) then view:goto_buffer(buffer) break end @@ -84,12 +82,15 @@ function M.back() local record = history[history.pos] local line = buffer:line_from_position(buffer.current_pos) if buffer.filename ~= record.filename and buffer._type ~= record.filename or - math.abs(record.line - line) > M.minimum_line_distance then - -- When navigated away from the most recent record, and if that record is - -- not a soft record, jump back to it first, then navigate backwards. - if not record.soft then jump(record) return end - -- Otherwise, update the soft record with the current position and - -- immediately navigate backwards. + math.abs(record.line - line) > M.minimum_line_distance then + -- When navigated away from the most recent record, and if that record is not a soft record, + -- jump back to it first, then navigate backwards. + if not record.soft then + jump(record) + return + end + -- Otherwise, update the soft record with the current position and immediately navigate + -- backwards. M.record(record.filename, nil, nil, record.soft) end if history.pos > 1 then history.pos = history.pos - 1 end @@ -110,15 +111,14 @@ end --- -- Records the given location in the current view's history. --- @param filename Optional string filename, buffer type, or identifier of the --- buffer to store. If `nil`, uses the current buffer. --- @param line Optional Integer line number to store. If `nil`, uses the current --- line. --- @param column Optional integer column number on line *line* to store. If --- `nil`, uses the current column. --- @param soft Optional flag that indicates whether or not this record should be --- skipped when navigating backward towards it, and updated when navigating --- away from it. The default value is `false`. +-- @param filename Optional string filename, buffer type, or identifier of the buffer to store. If +-- `nil`, uses the current buffer. +-- @param line Optional Integer line number to store. If `nil`, uses the current line. +-- @param column Optional integer column number on line *line* to store. If `nil`, uses the +-- current column. +-- @param soft Optional flag that indicates whether or not this record should be skipped when +-- navigating backward towards it, and updated when navigating away from it. The default +-- value is `false`. -- @name record function M.record(filename, line, column, soft) if not assert_type(filename, 'string/nil', 1) then @@ -127,17 +127,14 @@ function M.record(filename, line, column, soft) if not assert_type(line, 'number/nil', 2) then line = buffer:line_from_position(buffer.current_pos) end - if not assert_type(column, 'number/nil', 3) then - column = buffer.column[buffer.current_pos] - end + if not assert_type(column, 'number/nil', 3) then column = buffer.column[buffer.current_pos] end local history = view_history[view] if #history > 0 then local record = history[history.pos] if filename == record.filename and - (math.abs(record.line - line) <= M.minimum_line_distance or - record.soft) then - -- If the most recent record is close enough (distance-wise), or if that - -- record is a soft record, update it instead of recording a new one. + (math.abs(record.line - line) <= M.minimum_line_distance or record.soft) then + -- If the most recent record is close enough (distance-wise), or if that record is a soft + -- record, update it instead of recording a new one. record.line, record.column = line, column record.soft = soft and record.soft return @@ -146,17 +143,13 @@ function M.record(filename, line, column, soft) if history.pos < #history then for i = history.pos + 1, #history do history[i] = nil end -- clear forward end - history[#history + 1] = { - filename = filename, line = line, column = column, soft = soft - } + history[#history + 1] = {filename = filename, line = line, column = column, soft = soft} if #history > M.maximum_history_size then table.remove(history, 1) end history.pos = #history end -- Softly record positions when switching between buffers. -local function record_switch() - if not jumping then M.record(nil, nil, nil, true) end -end +local function record_switch() if not jumping then M.record(nil, nil, nil, true) end end events.connect(events.BUFFER_BEFORE_SWITCH, record_switch) events.connect(events.BUFFER_AFTER_SWITCH, record_switch) events.connect(events.FILE_OPENED, record_switch) @@ -164,8 +157,6 @@ events.connect(events.FILE_OPENED, record_switch) --- -- Clears all view history. -- @name clear -function M.clear() - for view in pairs(view_history) do view_history[view] = {pos = 0} end -end +function M.clear() for view in pairs(view_history) do view_history[view] = {pos = 0} end end return M |