Skip to content

Commit 4567538

Browse files
committed
Format on save should ignore selected text.
1 parent fb88a82 commit 4567538

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

README.md

+1-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Ctrl+Shift+J | ⌘⇧J | M-S-J | Reformat paragraph
2222
## `format.code`()
2323

2424
Reformats using a code formatter for the current buffer's lexer language either the selected
25-
text or the current paragraph, according to the rules of `textadept.editing.filter_through()`.
25+
text or the current buffer, according to the rules of `textadept.editing.filter_through()`.
2626

2727
See also: [`format.commands`](#format.commands)
2828

@@ -33,7 +33,6 @@ Map of lexer languages to string code formatter commands or functions that retur
3333
commands.
3434

3535
Fields:
36-
3736
- `lua`:
3837
- `cpp`:
3938
- `go`:
@@ -60,7 +59,6 @@ List of footer lines to ignore when reformatting paragraphs.
6059
These can be Doxygen footers for example.
6160

6261
Fields:
63-
6462
- `*/`:
6563

6664
Usage:
@@ -77,7 +75,6 @@ List of header lines to ignore when reformatting paragraphs.
7775
These can be LuaDoc/LDoc or Doxygen headers for example.
7876

7977
Fields:
80-
8178
- `---`:
8279
- `/**`:
8380

@@ -125,7 +122,6 @@ line prefixes. For example, LuaDoc/LDoc comments start with '---' but continue w
125122
and Doxygen comments start with '/**' but continue with ' *'.
126123

127124
Fields:
128-
129125
- `[/**]`:
130126
- `[---]`:
131127

init.lua

+4-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ M.on_save = true
8181
M.line_length = 100
8282

8383
--- Reformats using a code formatter for the current buffer's lexer language either the selected
84-
-- text or the current paragraph, according to the rules of `textadept.editing.filter_through()`.
84+
-- text or the current buffer, according to the rules of `textadept.editing.filter_through()`.
8585
-- @see commands
8686
function M.code()
8787
local command = M.commands[buffer.lexer_language]
@@ -98,7 +98,10 @@ events.connect(events.FILE_BEFORE_SAVE, function(filename)
9898
if filename then
9999
for _, patt in ipairs(M.ignore_file_patterns) do if filename:find(patt) then return end end
100100
end
101+
local selection = buffer.selection_serialized
102+
buffer:set_empty_selection(buffer.current_pos)
101103
M.code()
104+
buffer.selection_serialized = selection
102105
end)
103106

104107
--- Reformats using the Unix `fmt` tool either the selected text or the current paragraph,

init_test.lua

+14
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ test('format.code should not format on save if format.on_save is disabled', func
3333
end)
3434
if not have_clang_format then skip('clang-format is not available') end
3535

36+
test('format.on_save should ignore selected text', function()
37+
local _<close> = test.mock(io, 'ensure_final_newline', false)
38+
local file = 'file.c'
39+
local dir<close> = test.tmpdir{['.clang-format'] = 'BasedOnStyle: LLVM', file}
40+
io.open_file(dir / file)
41+
buffer:append_text('int main(){return 0;}')
42+
buffer:word_right_end_extend()
43+
44+
buffer:save()
45+
46+
test.assert_equal(buffer:get_text(), 'int main() { return 0; }')
47+
test.assert_equal(buffer:get_sel_text(), 'int')
48+
end)
49+
3650
test('format.code should ignore saving files matching format.ignore_file_patterns', function()
3751
local subdir = 'subdir'
3852
local subfile = 'subfile.c'

0 commit comments

Comments
 (0)