Skip to content

Commit b664635

Browse files
committed
.
1 parent e605a1e commit b664635

File tree

4 files changed

+109
-39
lines changed

4 files changed

+109
-39
lines changed

autoload/mde_funcs.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ enddef
145145
export def RemoveAll()
146146
# TODO could be refactored to increase speed, but it may not be necessary
147147
const range_info = utils.IsInRange()
148+
echom "A: " .. string(range_info)
148149
const prop_info = highlight.IsOnProp()
149150
const syn_info = synIDattr(synID(line("."), charcol("."), 1), "name")
150151
const is_quote_block = getline('.') =~ '^>\s'

autoload/mde_utils.vim

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,16 @@ enddef
5858

5959
export def RemoveSurrounding(range_info: dict<list<list<number>>> = {})
6060
const style_interval = empty(range_info) ? IsInRange() : range_info
61+
echom string(style_interval)
6162
if !empty(style_interval)
6263
const style = keys(style_interval)[0]
6364
const interval = values(style_interval)[0]
6465

66+
echom "Siamo qui!"
6567
# Remove left delimiter
6668
const lA = interval[0][0]
6769
const cA = interval[0][1]
70+
echom $"(lA, cA): ({lA},{cA})"
6871
const lineA = getline(lA)
6972
var newline = strcharpart(lineA, 0,
7073
\ cA - 1 - strchars(constants.TEXT_STYLES_DICT[style].open_delim))
@@ -216,7 +219,7 @@ export def SurroundSmart(style: string, type: string = '')
216219
# so that all the styles are visible
217220

218221
# Check if A falls in an existing interval
219-
cursor(lA, cA)
222+
setcursorcharpos(lA, cA)
220223
var old_right_delimiter = ''
221224
var found_interval = IsInRange()
222225
if !empty(found_interval)
@@ -255,7 +258,7 @@ export def SurroundSmart(style: string, type: string = '')
255258
endif
256259

257260
# Check if B falls in an existing interval
258-
cursor(lB, cB)
261+
setcursorcharpos(lB, cB)
259262
var old_left_delimiter = ''
260263
found_interval = IsInRange()
261264
if !empty(found_interval)
@@ -427,13 +430,21 @@ export def IsInRange(): dict<list<list<number>>>
427430
return text_style_refined
428431
enddef
429432

433+
def SearchPosChar(pattern: string, options: string): list<number>
434+
# Like 'searchpos()' but the column is converted in char index
435+
var [l, c] = searchpos(pattern, options)
436+
var c_char = strchars(strpart(getline(l), 0, c - 1)) + 1
437+
return [l, c_char]
438+
enddef
439+
430440
# Main function start here
431441
# text_style comes from vim-markdown
432-
const text_style = synIDattr(synID(line("."), charcol("."), 1), "name")
442+
const text_style = synIDattr(synID(line("."), col("."), 1), "name")
443+
echom "text_style: " .. text_style
433444
const text_style_adjusted =
434445
text_style == 'markdownItalic' || text_style == 'markdownBold'
435-
? StarOrUnderscore(synIDattr(synID(line("."), charcol("."), 1), "name"))
436-
: synIDattr(synID(line("."), charcol("."), 1), "name")
446+
? StarOrUnderscore(synIDattr(synID(line("."), col("."), 1), "name"))
447+
: synIDattr(synID(line("."), col("."), 1), "name")
437448
var return_val = {}
438449

439450
if !empty(text_style_adjusted)
@@ -445,38 +456,48 @@ export def IsInRange(): dict<list<list<number>>>
445456
const open_delim =
446457
eval($'constants.TEXT_STYLES_DICT.{text_style_adjusted}.open_delim')
447458

448-
var open_delim_pos = searchpos($'\V{open_delim}', 'bW')
449-
var current_style = synIDattr(synID(line("."), charcol("."), 1), "name")
459+
# TODO: the searchpos() return a byte index!
460+
var open_delim_pos = SearchPosChar($'\V{open_delim}', 'bW')
461+
462+
# echom $"open_del_pos_back: {open_delim_pos}"
463+
var current_style = synIDattr(synID(line("."), col("."), 1), "name")
450464
# We search for a markdown delimiter or an htmlTag.
451465
while current_style != $'{text_style}Delimiter'
452466
&& current_style != 'htmlTag'
453467
&& open_delim_pos != [0, 0]
454-
open_delim_pos = searchpos($'\V{open_delim}', 'bW')
455-
current_style = synIDattr(synID(line("."), charcol("."), 1), "name")
468+
open_delim_pos = SearchPosChar($'\V{open_delim}', 'bW')
469+
current_style = synIDattr(synID(line("."), col("."), 1), "name")
456470
endwhile
457471

458472
# To avoid infinite loops if some weird delimited text is highlighted
459473
if open_delim_pos == [0, 0]
460474
return {}
461475
endif
462476
open_delim_pos[1] += strchars(open_delim)
477+
# echom "open del pos_back_plus_delim: " .. string(open_delim_pos)
463478

464479
# Search end delimiter.
465480
# The end delimiter may be a blank line, hence
466481
# things become a bit cumbersome.
467482
setcursorcharpos(saved_curpos[1 : 2])
468483
const close_delim =
469484
eval($'constants.TEXT_STYLES_DICT.{text_style_adjusted}.close_delim')
470-
var close_delim_pos = searchpos($'\V{close_delim}', 'nW')
471-
var blank_line_pos = searchpos($'^$', 'nW')
485+
var close_delim_pos = SearchPosChar($'\V{close_delim}', 'W')
486+
var blank_line_pos = SearchPosChar('^$', 'W')
472487
var first_met = [0, 0]
473-
current_style = synIDattr(synID(line("."), charcol("."), 1), "name")
488+
current_style = synIDattr(synID(line("."), col("."), 1), "name")
489+
echom $"close_delim_pos: '{close_delim_pos}'"
490+
echom $"blank_line_pos: '{blank_line_pos}'"
474491

492+
# The while loop is to robustify because you ultimately want to get a
493+
# '*Delimiter' text-style, like for example 'markdownBoldDelimiter'
475494
while current_style != $'{text_style}Delimiter'
476495
&& current_style != 'htmlEndTag'
477496
&& getline(line('.')) !~ '^$'
478-
close_delim_pos = searchpos($'\V{close_delim}', 'nW')
479-
blank_line_pos = searchpos($'^$', 'nW')
497+
close_delim_pos = SearchPosChar($'\V{close_delim}', 'W')
498+
blank_line_pos = SearchPosChar('^$', 'W')
499+
echom $"close_delim_pos_inside: '{close_delim_pos}'"
500+
echom $"blank_line_pos_inside: '{blank_line_pos}'"
480501
if close_delim_pos == [0, 0]
481502
first_met = blank_line_pos
482503
elseif blank_line_pos == [0, 0]
@@ -487,8 +508,9 @@ export def IsInRange(): dict<list<list<number>>>
487508
: blank_line_pos
488509
endif
489510
setcursorcharpos(first_met)
490-
current_style = synIDattr(synID(line("."), charcol("."), 1), "name")
511+
current_style = synIDattr(synID(line("."), col("."), 1), "name")
491512
endwhile
513+
echom $"first met_before: {first_met}"
492514

493515
# If we hit a blank line, then we take the previous line and last column,
494516
# to keep consistency in returning open-intervals
@@ -498,6 +520,7 @@ export def IsInRange(): dict<list<list<number>>>
498520
else
499521
first_met[1] -= 1
500522
endif
523+
echom $"first met: {first_met}"
501524

502525
setcursorcharpos(saved_curpos[1 : 2])
503526
return_val = {[text_style_adjusted]: [open_delim_pos, first_met]}

doc/markdown_extras.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ REQUIREMENTS *markdown-extras-requirements*
3434
Vim 9.0 is required.
3535
You must set a |localleader| key and your |vimrc| shall include the following
3636
lines:
37-
>
37+
>vim
3838
filetype indent plugin on
3939
syntax on
4040
<
@@ -153,7 +153,7 @@ for possible rendering targets. The rendered file will automatically open
153153
You can also pass arguments to `pandoc` via the key
154154
`pandoc_args` of the |g:markdown_extras_config| dictionary.
155155
You could for example set the following:
156-
>
156+
>vim
157157
g:markdown_extras_config = {}
158158
g:markdown_extras_config['pandoc_args'] =
159159
[$'--css="{$HOME}/dotfiles/my_css_style.css"',
@@ -178,7 +178,7 @@ filling it with he various keys.
178178
Default |true|.
179179
"pandoc_args" List of arguments to be passed to |compiler-pandoc|.
180180
Example:
181-
>
181+
>vim
182182
g:markdown_extras_config['pandoc_args'] =
183183
['--css="path/to/my_css_style.css"', '--metadata',
184184
'key=value']`
@@ -241,7 +241,7 @@ If such a file does not exists, create it.
241241

242242
For example, to trigger links autocompletion when you hit `[`, add the following
243243
lines to your `~/.vim/after/ftplugin/markdown.vim` file:
244-
>
244+
>vim
245245
setlocal completeopt=menu,menuone,noselect
246246
import autoload "mde_funcs.vim"
247247
setlocal omnifunc=mde_funcs.OmniFunc
@@ -264,7 +264,7 @@ COMMANDS *markdown-extras-commands*
264264
plugin.
265265

266266
Example:
267-
>
267+
>vim
268268
:MDEIndex # Uses g:markdown_extras_index
269269
:MDEIndex [
270270
'local/path',
@@ -283,7 +283,7 @@ COMMANDS *markdown-extras-commands*
283283
]
284284
<
285285
Or it can be used in a script, like for example:
286-
>
286+
>vim
287287
def Foo()
288288
const my_index = {
289289
foo: '/Users/ubaldot/home/Documents/foo.vim',
@@ -316,7 +316,7 @@ MAPPINGS *markdown-extras-mappings*
316316

317317
Note: all the key-bindings that require a motion can also be used in visual
318318
mode. For example, you can set the following:
319-
>
319+
>vim
320320
nnoremap <buffer> B <Plug>MarkdownBold
321321
xnoremap <buffer> B <Plug>MarkdownBold
322322
<

test/test_utils.vim

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,19 @@ const lines_2 =<< trim END
133133
> alias consequatur aut perferendis doloribus asperiores repellat.
134134
END
135135

136+
const src_name_multibyte = 'multibyte_test.md'
137+
const lines_multibyte =<< trim END
138+
当然,这里有一段简短的中文文本:
139+
140+
学习 Vim 是一项非常有趣的挑战。虽然一开始可能感觉有些困难,但只要坚持练习,
141+
就能够逐渐掌握它强大的功能,并大幅提高编辑效率。
142+
143+
需要**某种特定风格或主题**(比如技术、文学、对话等)吗?
144+
145+
学习 Vim 是一项_非常有趣的挑战。虽然一开始可能感觉有些困难,但只要坚持练习,
146+
就能够逐渐掌握它强大_的功能,并大幅提高编辑效率。
147+
END
148+
136149
def Generate_testfile(lines: list<string>, src_name: string)
137150
writefile(lines, src_name)
138151
enddef
@@ -173,53 +186,54 @@ def g:Test_IsInRange()
173186
cursor(1, 27)
174187
var expected_value = {'markdownBold': [[1, 23], [1, 37]]}
175188
var range = utils.IsInRange()
176-
assert_equal(expected_value, range)
189+
echom assert_equal(expected_value, range)
177190

178191
# On the border
179192
cursor(1, 37)
180193
range = utils.IsInRange()
181194
assert_equal(expected_value, range)
182195

183-
# On the delimiter
196+
# # On the delimiter
184197
cursor(1, 38)
185198
expected_value = {}
186199
range = utils.IsInRange()
187-
assert_equal(expected_value, range)
200+
echom assert_equal(expected_value, range)
188201

189202
cursor(5, 18)
190203
expected_value = {'markdownItalic': [[4, 18], [5, 29]]}
191204
range = utils.IsInRange()
192205
assert_equal(expected_value, range)
193206

194-
# Test singularity: cursor on a delimiter
207+
# # Test singularity: cursor on a delimiter
195208
cursor(14, 21)
196209
range = utils.IsInRange()
197210
assert_true(empty(range))
198211

199-
# Normal Test
212+
# # Normal Test
200213
cursor(14, 25)
201214
expected_value = {'markdownBoldU': [[14, 22], [16, 14]]}
202215
range = utils.IsInRange()
203216
assert_equal(expected_value, range)
204217

205-
# End of paragraph with no delimiter
218+
# # End of paragraph with no delimiter
206219
cursor(21, 43)
207220
expected_value = {'markdownStrike': [[21, 39], [22, 26]]}
221+
message clear
208222
range = utils.IsInRange()
209-
assert_equal(expected_value, range)
223+
echom assert_equal(expected_value, range)
210224

211-
cursor(24, 10)
212-
expected_value = {}
213-
range = utils.IsInRange()
214-
assert_equal(expected_value, range)
225+
# cursor(24, 10)
226+
# expected_value = {}
227+
# range = utils.IsInRange()
228+
# assert_equal(expected_value, range)
215229

216-
cursor(31, 18)
217-
expected_value = {'markdownLinkText': [[31, 16], [31, 26]]}
218-
range = utils.IsInRange()
219-
assert_equal(expected_value, range)
230+
# cursor(31, 18)
231+
# expected_value = {'markdownLinkText': [[31, 16], [31, 26]]}
232+
# range = utils.IsInRange()
233+
# assert_equal(expected_value, range)
220234

221-
:%bw!
222-
Cleanup_testfile(src_name_1)
235+
# :%bw!
236+
# Cleanup_testfile(src_name_1)
223237
enddef
224238

225239
def g:Test_SurroundSimple_one_line()
@@ -641,3 +655,35 @@ def g:Test_unset_quote_block()
641655
:%bw!
642656
Cleanup_testfile(src_name_1)
643657
enddef
658+
659+
def g:Test_multibyte_isInRange()
660+
vnew
661+
Generate_testfile(lines_multibyte, src_name_multibyte)
662+
exe $"edit {src_name_multibyte}"
663+
setlocal conceallevel=0
664+
665+
setcursorcharpos(6, 6)
666+
var expected_value = {'markdownBold': [[6, 3], [6, 13]]}
667+
var range = utils.IsInRange()
668+
echom $"range: {range}"
669+
# assert_equal(expected_value, range)
670+
671+
# setcursorcharpos(8, 28)
672+
# expected_value = {'markdownBold': [[8, 12], [9, 10]]}
673+
# range = utils.IsInRange()
674+
# echom $"{range}"
675+
enddef
676+
def g:Test_multibyte_surrounding()
677+
vnew
678+
Generate_testfile(lines_multibyte, src_name_multibyte)
679+
exe $"edit {src_name_multibyte}"
680+
setlocal conceallevel=0
681+
682+
setcursorcharpos(1, 5)
683+
setcharpos("'[", [0, 1, 4, 0])
684+
setcharpos("']", [0, 1, 15, 0])
685+
utils.SurroundSmart('markdownItalicU')
686+
var expected_value = '当然,_这里有一段简短的中文文本_:'
687+
echom assert_equal(expected_value, getline(1))
688+
689+
enddef

0 commit comments

Comments
 (0)