Skip to content

Commit f6887d1

Browse files
committed
fix: this commit addresses issue #2
- mark code blocks as `rust` before sending content to popup window; - in neovim, set the local `conceallevel` to `2` in the popup window, did not find the equivalent in vim (please help pointing out if there is any); - mark the content title as level one header - remove the trailing white spaces if in neovim, those spaces were meant to be used as padding as popup window lacks padding option in neovim, remove them for now; - add tests for the function that mark code blocks as `rust` Ref: [issue 2](#2)
1 parent fe94bf0 commit f6887d1

File tree

2 files changed

+96
-3
lines changed

2 files changed

+96
-3
lines changed

autoload/rustcexplain/popup.vim

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ set cpoptions&vim
33

44

55
function! rustcexplain#popup#OpenPopupWindow(rustc_cmd, err_code) abort
6+
let l:message = systemlist(a:rustc_cmd)
7+
let l:message = s:mark_code_blocks_as_rust(l:message)
8+
69
let l:winid = popup_create(
7-
\ systemlist(a:rustc_cmd),
10+
\ l:message,
811
\ { 'title': ' Explain [' . a:err_code . '] ',
912
\ 'close': 'button',
1013
\ 'pos': 'center',
@@ -26,8 +29,8 @@ endfunction
2629

2730
function! rustcexplain#popup#OpenNeovimFloatWindow(rustc_cmd, err_code) abort
2831
let l:message = systemlist(a:rustc_cmd)
29-
let l:message = ['Explain [' . a:err_code .']', ''] + l:message
30-
let l:message = map(l:message, {k, v -> ' ' . v . ' '})
32+
let l:message = ['# Explain [' . a:err_code .']', ''] + l:message
33+
let l:message = s:mark_code_blocks_as_rust(l:message)
3134

3235
let l:uis = nvim_list_uis()
3336
if len(l:uis) > 0
@@ -57,6 +60,7 @@ function! rustcexplain#popup#OpenNeovimFloatWindow(rustc_cmd, err_code) abort
5760
\ }
5861
let l:winid = nvim_open_win(l:buf, 1, l:opts)
5962
call nvim_win_set_option(l:winid, 'winhl', 'Normal:Cursorline')
63+
call nvim_set_option_value('conceallevel', 2, {'win': l:winid, 'scope': 'local'})
6064
call nvim_buf_set_var(l:buf, 'ale_enabled', 0)
6165

6266
call setbufvar(winbufnr(l:winid), '&filetype', 'markdown')
@@ -69,6 +73,21 @@ function! rustcexplain#popup#OpenNeovimFloatWindow(rustc_cmd, err_code) abort
6973
endfor
7074
endfunction
7175

76+
" to mark code blocks in `rustc --explain` as `rust` as requested in
77+
" https://github.com/yining/vim-rustcexplain/issues/2
78+
function! s:mark_code_blocks_as_rust(lines) abort
79+
let l:lines = []
80+
" only change the opening '```' to '```rust'
81+
let l:opening_flag = v:true
82+
for l:line in a:lines
83+
if match(l:line, '^\s*```\s*$') >= 0
84+
let l:line = l:opening_flag ? '```rust' : l:line
85+
let l:opening_flag = !l:opening_flag
86+
endif
87+
let l:lines = l:lines + [l:line]
88+
endfor
89+
return l:lines
90+
endfunction
7291

7392
" ref: https://github.com/prabirshrestha/vim-lsp/issues/975#issuecomment-751658462
7493
function! s:popup_filter(winid, key) abort

test/mark_code_block_rust_tests.vim

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
scriptencoding utf8
2+
3+
let s:suite = themis#suite('tests code blocks are properly marked as rust')
4+
let s:assert = themis#helper('assert')
5+
let s:scope = themis#helper('scope')
6+
7+
function! s:suite.before_each()
8+
let s:funcs = s:scope.funcs('autoload/rustcexplain/popup.vim')
9+
endfunction
10+
11+
function! s:suite.after_each()
12+
endfunction
13+
14+
function! s:suite.test_empty()
15+
let l:input = []
16+
let l:expected = []
17+
let l:result = s:funcs.mark_code_blocks_as_rust(l:input)
18+
call s:assert.equals(l:expected, l:result)
19+
endfunction
20+
21+
function! s:suite.test_one_block()
22+
let l:input = [
23+
\ '```',
24+
\ 'code',
25+
\ '```',
26+
\]
27+
let l:expected = [
28+
\ '```rust',
29+
\ 'code',
30+
\ '```',
31+
\]
32+
let l:result = s:funcs.mark_code_blocks_as_rust(l:input)
33+
call s:assert.equals(l:expected, l:result)
34+
endfunction
35+
36+
function! s:suite.test_one_and_half_block()
37+
let l:input = [
38+
\ '```',
39+
\ 'code',
40+
\ '```',
41+
\ '```',
42+
\ 'code',
43+
\]
44+
let l:expected = [
45+
\ '```rust',
46+
\ 'code',
47+
\ '```',
48+
\ '```rust',
49+
\ 'code',
50+
\]
51+
let l:result = s:funcs.mark_code_blocks_as_rust(l:input)
52+
call s:assert.equals(l:expected, l:result)
53+
endfunction
54+
55+
function! s:suite.test_simple()
56+
let l:input = [
57+
\ '```',
58+
\ '```',
59+
\ '```',
60+
\ '```',
61+
\ '```',
62+
\ '```',
63+
\]
64+
let l:expected = [
65+
\ '```rust',
66+
\ '```',
67+
\ '```rust',
68+
\ '```',
69+
\ '```rust',
70+
\ '```',
71+
\]
72+
let l:result = s:funcs.mark_code_blocks_as_rust(l:input)
73+
call s:assert.equals(l:expected, l:result)
74+
endfunction

0 commit comments

Comments
 (0)