1
1
import sys
2
2
import os .path
3
- import re
4
- from enum import Enum , auto
5
3
from collections import namedtuple
6
4
import pynvim
7
5
sys .path .append (os .path .dirname (__file__ ))
@@ -12,40 +10,18 @@ def q(string):
12
10
return '"' + string + '"'
13
11
14
12
15
- Range = namedtuple ('Range' , ['start' , 'end' ])
16
-
17
13
Pos = namedtuple ('Pos' , ['line' , 'col' ])
18
14
19
15
PosPair = namedtuple ('PosPair' , ['startpos' , 'endpos' ])
20
16
21
17
22
- class Tag (Enum ):
23
- Paragraph = auto ()
24
- Heading = auto ()
25
- BlockQuote = auto ()
26
- CodeBlock = auto ()
27
- List = auto ()
28
- Item = auto ()
29
- FootnoteDefinition = auto ()
30
- Table = auto ()
31
- TableHead = auto ()
32
- TableRow = auto ()
33
- TableCell = auto ()
34
- Emphasis = auto ()
35
- Strong = auto ()
36
- Strikethrough = auto ()
37
- Link = auto ()
38
- Image = auto ()
39
-
40
-
41
18
@pynvim .plugin
42
19
class CommonMark (object ):
43
20
def __init__ (self , vim ):
44
21
self .vim = vim
45
22
self .namespace = self .vim .new_highlight_source ()
46
23
self .vim .command ('hi cmarkEmphasis gui=italic' )
47
24
self .vim .command ('hi cmarkStrong gui=bold' )
48
- self .vim .command ('hi link cmarkHeading Directory' )
49
25
50
26
def echo (self , string ):
51
27
self .vim .command ('echom ' + q (string ))
@@ -90,39 +66,28 @@ def to_line_highlights(self, startpos, endpos):
90
66
tail = PosPair (Pos (endpos .line , 0 ), Pos (endpos .line , endpos .col ))
91
67
return [head , * body , tail ]
92
68
93
- def build_hl (self , group , lnum , start_col = 0 , end_col = - 1 ):
94
- return [group , lnum , start_col , end_col ]
95
-
96
69
def highlight (self ):
97
- if self .vim .current .buffer .options ['filetype' ] != 'commonmark ' :
70
+ if self .vim .current .buffer .options ['filetype' ] != 'pandoc ' :
98
71
return
99
72
buf_str = "\n " .join (self .vim .current .buffer )
100
- offsets = libpulldowncmark .get_offsets (buf_str )
73
+ offsets = libvim_commonmark .get_offsets (buf_str )
101
74
hls = []
102
75
for i in offsets :
103
- ranges , typ = offsets [i ]
104
-
105
- # internal vim byte counts start on 1,
106
- # not 0 as in pulldowm-cmark
107
- rng = Range (* map (lambda x : int (x ) + 1 , ranges .split ('..' )))
76
+ data = offsets [i ]
77
+ typ = data ['group' ]
108
78
109
79
# canonical positions of the offsets
110
- startpos = self .offset2pos (rng . start )
111
- endpos = self .offset2pos (rng . end )
80
+ startpos = self .offset2pos (data [ ' start' ] )
81
+ endpos = self .offset2pos (data [ ' end' ] )
112
82
113
83
# self.echo(self.vim.funcs.fnameescape(type))
114
- if typ in [Tag .Emphasis .name , Tag .Strong .name ]:
115
- line_highlights = self .to_line_highlights (startpos , endpos )
116
- for lh in line_highlights :
117
- hls .append (self .build_hl ('cmark' + typ ,
118
- lh .startpos .line - 1 ,
119
- lh .startpos .col ,
120
- lh .endpos .col ))
121
- elif re .match (Tag .Heading .name , typ ):
84
+ if typ in ('cmarkEmphasis' , 'cmarkStrong' ):
122
85
line_highlights = self .to_line_highlights (startpos , endpos )
123
86
for lh in line_highlights :
124
- hls .append (('cmarkHeading' ,
125
- lh .startpos .line - 1 ))
87
+ hls .append ([typ ,
88
+ lh .startpos .line - 1 ,
89
+ lh .startpos .col ,
90
+ lh .endpos .col ])
126
91
127
92
if len (hls ) > 0 :
128
93
self .vim .current .buffer .update_highlights (self .namespace ,
0 commit comments