Skip to content

Commit

Permalink
Auto-indent should happen for each selection.
Browse files Browse the repository at this point in the history
  • Loading branch information
orbitalquark committed Feb 22, 2025
1 parent 0ff6075 commit 3d5fb26
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
23 changes: 13 additions & 10 deletions modules/textadept/editing.lua
Original file line number Diff line number Diff line change
Expand Up @@ -530,16 +530,19 @@ end)
-- Auto-indent on return.
events.connect(events.CHAR_ADDED, function(code)
if not M.auto_indent or code ~= string.byte('\n') then return end
local line = buffer:line_from_position(buffer.current_pos)
if line > 1 and buffer:get_line(line - 1):find('^[\r\n]+$') and
buffer:get_line(line):find('^[^\r\n]') then
return -- do not auto-indent when pressing enter from start of previous line
end
local i = line - 1
while i >= 1 and buffer:get_line(i):find('^[\r\n]+$') do i = i - 1 end
if i >= 1 then
buffer.line_indentation[line] = buffer.line_indentation[i]
buffer:vc_home()
for i = 1, buffer.selections do
local line = buffer:line_from_position(buffer.selection_n_caret[i])
if line > 1 and buffer:get_line(line - 1):find('^[\r\n]+$') and
buffer:get_line(line):find('^[^\r\n]') then
return -- do not auto-indent when pressing enter from start of previous line
end
local j = line - 1
while j >= 1 and buffer:get_line(j):find('^[\r\n]+$') do j = j - 1 end
if j >= 1 then
buffer.line_indentation[line] = buffer.line_indentation[j]
local indent_pos = buffer.line_indent_position[line]
buffer.selection_n_start[i], buffer.selection_n_end[i] = indent_pos, indent_pos
end
end
end)

Expand Down
19 changes: 19 additions & 0 deletions modules/textadept/editing_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,25 @@ test('editing.auto_indent should not auto-indent an already indented line', func
test.assert_equal(indent, buffer.tab_width)
end)

test('editing.auto_indent should auto-indent for each selection', function()
local indented_word = '\tword'
buffer:append_text(test.lines{indented_word, indented_word})
buffer:set_selection(buffer.line_end_position[1], buffer.line_end_position[1])
buffer:add_selection(buffer.line_end_position[2], buffer.line_end_position[2])

test.type('\n')

test.assert_equal(buffer:get_text(), test.lines{indented_word, '\t', indented_word, '\t'})
test.assert_equal(2, buffer.selections)
test.assert_equal(4, buffer.line_count)
test.assert_equal(buffer.line_indentation[2], buffer.line_indentation[1])
test.assert_equal(buffer.line_indentation[4], buffer.line_indentation[3])
test.assert_equal(buffer.selection_n_start[1], buffer.line_indent_position[2])
test.assert_equal(buffer.selection_n_end[1], buffer.line_indent_position[2])
test.assert_equal(buffer.selection_n_start[2], buffer.line_indent_position[4])
test.assert_equal(buffer.selection_n_end[2], buffer.line_indent_position[4])
end)

test('editing.auto_enclose should wrap selections in typed punctuation', function()
local _<close> = test.mock(textadept.editing, 'auto_enclose', true)
local word = 'word'
Expand Down

0 comments on commit 3d5fb26

Please sign in to comment.