Skip to content

Commit 7af8f9e

Browse files
committed
Allow overriding filetype detection
1 parent c87c49a commit 7af8f9e

File tree

6 files changed

+86
-6
lines changed

6 files changed

+86
-6
lines changed

autoload/inline_edit.vim

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,16 @@ function! inline_edit#MarkdownFencedCode()
6060
return []
6161
endif
6262
let start = line('.') + 1
63-
let filetype = matchlist(getline('.'), start_pattern, 0)[1]
64-
let filetype = tolower(filetype)
63+
let filetype_match = matchlist(getline('.'), start_pattern, 0)
64+
65+
if len(filetype_match) > 0
66+
let filetype = filetype_match[1]
67+
let filetype = tolower(filetype)
68+
else
69+
" The start pattern doesn't match, the end one does. It's a weird
70+
" scenario, but it works out in practice.
71+
let filetype = ''
72+
endif
6573

6674
" find end of area
6775
if searchpair(start_pattern, '', end_pattern, 'W') <= 0

autoload/inline_edit/controller.vim

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,16 @@ function! inline_edit#controller#VisualEdit(filetype) dict
7171
call self.NewProxy(start, end, filetype, indent)
7272
endfunction
7373

74-
function! inline_edit#controller#PatternEdit(pattern) dict
74+
function! inline_edit#controller#PatternEdit(pattern, filetype_override) dict
7575
let pattern = extend({
7676
\ 'sub_filetype': &filetype,
7777
\ 'indent_adjustment': 0,
7878
\ }, a:pattern)
7979

80+
if a:filetype_override != ''
81+
let pattern.sub_filetype = a:filetype_override
82+
endif
83+
8084
call inline_edit#PushCursor()
8185

8286
" find start of area
@@ -131,12 +135,16 @@ function! inline_edit#controller#PatternEdit(pattern) dict
131135
return 1
132136
endfunction
133137

134-
function! inline_edit#controller#IndentEdit(pattern) dict
138+
function! inline_edit#controller#IndentEdit(pattern, filetype_override) dict
135139
let pattern = extend({
136140
\ 'sub_filetype': &filetype,
137141
\ 'indent_adjustment': 0,
138142
\ }, a:pattern)
139143

144+
if a:filetype_override != ''
145+
let pattern.sub_filetype = a:filetype_override
146+
endif
147+
140148
call inline_edit#PushCursor()
141149

142150
" find start of area

plugin/inline_edit.vim

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ call add(g:inline_edit_patterns, {
4343
\ 'main_filetype': 'markdown',
4444
\ 'callback': 'inline_edit#MarkdownFencedCode',
4545
\ })
46+
4647
call add(g:inline_edit_patterns, {
4748
\ 'main_filetype': 'rmd',
4849
\ 'callback': 'inline_edit#MarkdownFencedCode',
@@ -194,11 +195,16 @@ function! s:InlineEdit(count, filetype)
194195

195196
if !empty(result)
196197
call call(controller.NewProxy, result, controller)
198+
199+
if a:filetype != ''
200+
let &filetype = a:filetype
201+
endif
202+
197203
return
198204
endif
199-
elseif get(entry, 'indent_based', 0) && controller.IndentEdit(entry)
205+
elseif get(entry, 'indent_based', 0) && controller.IndentEdit(entry, a:filetype)
200206
return
201-
elseif !get(entry, 'indent_based', 0) && controller.PatternEdit(entry)
207+
elseif !get(entry, 'indent_based', 0) && controller.PatternEdit(entry, a:filetype)
202208
return
203209
endif
204210
endfor

spec/plugin/editing_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
buffer_contents = get_buffer_contents
1717
expect(buffer_contents.strip).to eq 'alert("Foo");'
1818

19+
filetype = vim.echo('&filetype')
20+
expect(filetype).to eq 'javascript'
21+
1922
vim.search 'Foo'
2023
vim.normal 'cwBar'
2124
vim.write
@@ -28,4 +31,18 @@
2831
</script>
2932
EOF
3033
end
34+
35+
specify "explicit filetype override" do
36+
set_file_contents <<~HTML
37+
<script>
38+
alert("Foo");
39+
</script>
40+
HTML
41+
42+
vim.search 'script'
43+
vim.command 'InlineEdit typescript'
44+
45+
filetype = vim.echo('&filetype')
46+
expect(filetype).to eq 'typescript'
47+
end
3148
end

spec/plugin/haml_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,19 @@
4444
.content { border: 1px solid black; }
4545
HTML
4646
end
47+
48+
specify "explicit filetype" do
49+
set_file_contents <<~HTML
50+
%body
51+
:javascript
52+
alert("OK");
53+
HTML
54+
55+
vim.search 'alert'
56+
vim.command 'InlineEdit typescript'
57+
58+
buffer_contents = get_buffer_contents
59+
60+
expect(vim.echo('&filetype')).to eq 'typescript'
61+
end
4762
end

spec/plugin/markdown_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,30 @@ def foo
5454
}
5555
HTML
5656
end
57+
58+
specify "unknown code block with explicit filetype" do
59+
set_file_contents <<~HTML
60+
Some text.
61+
62+
```
63+
fn foo() {
64+
println!("OK");
65+
}
66+
```
67+
68+
Some other text.
69+
HTML
70+
71+
vim.search 'fn foo'
72+
vim.command 'InlineEdit rust'
73+
74+
buffer_contents = get_buffer_contents
75+
76+
expect(vim.echo('&filetype')).to eq 'rust'
77+
expect(buffer_contents).to eq <<~HTML
78+
fn foo() {
79+
println!("OK");
80+
}
81+
HTML
82+
end
5783
end

0 commit comments

Comments
 (0)