Skip to content

Commit 83b0bbe

Browse files
authored
Merge pull request #69 from haya14busa/fix-pos-i
Introduce byte offset
2 parents 7bb0f75 + 7ae2b17 commit 83b0bbe

File tree

10 files changed

+102
-19
lines changed

10 files changed

+102
-19
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
test/*.out
1+
*.out

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ matrix:
1616
- MAKE_TARGET=test
1717
- env:
1818
- VIM_VERSION=installed
19-
- MAKE_TARGET="clean_compiled check js/test py/test"
19+
- MAKE_TARGET="clean_compiled check js/test py/test test/node_position/test_position.out"
2020

2121
install:
2222
- |

Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ js/test: js/vimlparser.js
2525
py/test: py/vimlparser.py
2626
test/run_command.sh python py/vimlparser.py
2727

28+
test/node_position/test_position.out: test/node_position/test_position.vim test/node_position/test_position.ok
29+
vim -u NONE -N --cmd "let &rtp .= ',' . getcwd()" -S test/node_position/test_position.vim
30+
diff -u test/node_position/test_position.ok test/node_position/test_position.out
31+
2832
.PHONY: all clean_compiled check test js/test py/test

autoload/vimlparser.vim

+11-6
Original file line numberDiff line numberDiff line change
@@ -3769,12 +3769,14 @@ function! s:StringReader.__init__(lines)
37693769
let self.buf = []
37703770
let self.pos = []
37713771
let lnum = 0
3772+
let offset = 0
37723773
while lnum < len(a:lines)
37733774
let col = 0
37743775
for c in split(a:lines[lnum], '\zs')
37753776
call add(self.buf, c)
3776-
call add(self.pos, [lnum + 1, col + 1])
3777+
call add(self.pos, [lnum + 1, col + 1, offset])
37773778
let col += len(c)
3779+
let offset += len(c)
37783780
endfor
37793781
while lnum + 1 < len(a:lines) && a:lines[lnum + 1] =~# '^\s*\\'
37803782
let skip = s:TRUE
@@ -3786,18 +3788,21 @@ function! s:StringReader.__init__(lines)
37863788
endif
37873789
else
37883790
call add(self.buf, c)
3789-
call add(self.pos, [lnum + 2, col + 1])
3791+
call add(self.pos, [lnum + 2, col + 1, offset])
37903792
endif
37913793
let col += len(c)
3794+
let offset += len(c)
37923795
endfor
37933796
let lnum += 1
3797+
let offset += 1
37943798
endwhile
37953799
call add(self.buf, '<EOL>')
3796-
call add(self.pos, [lnum + 1, col + 1])
3800+
call add(self.pos, [lnum + 1, col + 1, offset])
37973801
let lnum += 1
3802+
let offset += 1
37983803
endwhile
37993804
" for <EOF>
3800-
call add(self.pos, [lnum + 1, 0])
3805+
call add(self.pos, [lnum + 1, 0, offset])
38013806
let self.i = 0
38023807
endfunction
38033808

@@ -3891,8 +3896,8 @@ function! s:StringReader.getstr(begin, end)
38913896
endfunction
38923897

38933898
function! s:StringReader.getpos()
3894-
let [lnum, col] = self.pos[self.i]
3895-
return {'i': self.i, 'lnum': lnum, 'col': col}
3899+
let [lnum, col, offset] = self.pos[self.i]
3900+
return {'i': self.i, 'lnum': lnum, 'col': col, 'offset': offset}
38963901
endfunction
38973902

38983903
function! s:StringReader.setpos(pos)

js/vimlfunc.js

+3
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ function viml_keys(obj) {
151151
}
152152

153153
function viml_len(obj) {
154+
if (typeof obj === 'string') {
155+
return encodeURIComponent(obj).replace(/%../g, ' ').length;
156+
}
154157
return obj.length;
155158
}
156159

js/vimlparser.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ function viml_keys(obj) {
151151
}
152152

153153
function viml_len(obj) {
154+
if (typeof obj === 'string') {
155+
return encodeURIComponent(obj).replace(/%../g, ' ').length;
156+
}
154157
return obj.length;
155158
}
156159

@@ -3660,14 +3663,16 @@ StringReader.prototype.__init__ = function(lines) {
36603663
this.buf = [];
36613664
this.pos = [];
36623665
var lnum = 0;
3666+
var offset = 0;
36633667
while (lnum < viml_len(lines)) {
36643668
var col = 0;
36653669
var __c7 = viml_split(lines[lnum], "\\zs");
36663670
for (var __i7 = 0; __i7 < __c7.length; ++__i7) {
36673671
var c = __c7[__i7];
36683672
viml_add(this.buf, c);
3669-
viml_add(this.pos, [lnum + 1, col + 1]);
3673+
viml_add(this.pos, [lnum + 1, col + 1, offset]);
36703674
col += viml_len(c);
3675+
offset += viml_len(c);
36713676
}
36723677
while (lnum + 1 < viml_len(lines) && viml_eqregh(lines[lnum + 1], "^\\s*\\\\")) {
36733678
var skip = TRUE;
@@ -3682,18 +3687,21 @@ StringReader.prototype.__init__ = function(lines) {
36823687
}
36833688
else {
36843689
viml_add(this.buf, c);
3685-
viml_add(this.pos, [lnum + 2, col + 1]);
3690+
viml_add(this.pos, [lnum + 2, col + 1, offset]);
36863691
}
36873692
col += viml_len(c);
3693+
offset += viml_len(c);
36883694
}
36893695
lnum += 1;
3696+
offset += 1;
36903697
}
36913698
viml_add(this.buf, "<EOL>");
3692-
viml_add(this.pos, [lnum + 1, col + 1]);
3699+
viml_add(this.pos, [lnum + 1, col + 1, offset]);
36933700
lnum += 1;
3701+
offset += 1;
36943702
}
36953703
// for <EOF>
3696-
viml_add(this.pos, [lnum + 1, 0]);
3704+
viml_add(this.pos, [lnum + 1, 0, offset]);
36973705
this.i = 0;
36983706
}
36993707

@@ -3792,7 +3800,8 @@ StringReader.prototype.getpos = function() {
37923800
var __tmp = this.pos[this.i];
37933801
var lnum = __tmp[0];
37943802
var col = __tmp[1];
3795-
return {"i":this.i, "lnum":lnum, "col":col};
3803+
var offset = __tmp[2];
3804+
return {"i":this.i, "lnum":lnum, "col":col, "offset":offset};
37963805
}
37973806

37983807
StringReader.prototype.setpos = function(pos) {

py/vimlfunc.py

+2
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ def viml_keys(obj):
128128
return obj.keys()
129129

130130
def viml_len(obj):
131+
if type(obj) is str:
132+
return len(obj.encode('utf-8'))
131133
return len(obj)
132134

133135
def viml_printf(*args):

py/vimlparser.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ def viml_keys(obj):
128128
return obj.keys()
129129

130130
def viml_len(obj):
131+
if type(obj) is str:
132+
return len(obj.encode('utf-8'))
131133
return len(obj)
132134

133135
def viml_printf(*args):
@@ -2921,12 +2923,14 @@ def __init__(self, lines):
29212923
self.buf = []
29222924
self.pos = []
29232925
lnum = 0
2926+
offset = 0
29242927
while lnum < viml_len(lines):
29252928
col = 0
29262929
for c in viml_split(lines[lnum], "\\zs"):
29272930
viml_add(self.buf, c)
2928-
viml_add(self.pos, [lnum + 1, col + 1])
2931+
viml_add(self.pos, [lnum + 1, col + 1, offset])
29292932
col += viml_len(c)
2933+
offset += viml_len(c)
29302934
while lnum + 1 < viml_len(lines) and viml_eqregh(lines[lnum + 1], "^\\s*\\\\"):
29312935
skip = TRUE
29322936
col = 0
@@ -2936,14 +2940,17 @@ def __init__(self, lines):
29362940
skip = FALSE
29372941
else:
29382942
viml_add(self.buf, c)
2939-
viml_add(self.pos, [lnum + 2, col + 1])
2943+
viml_add(self.pos, [lnum + 2, col + 1, offset])
29402944
col += viml_len(c)
2945+
offset += viml_len(c)
29412946
lnum += 1
2947+
offset += 1
29422948
viml_add(self.buf, "<EOL>")
2943-
viml_add(self.pos, [lnum + 1, col + 1])
2949+
viml_add(self.pos, [lnum + 1, col + 1, offset])
29442950
lnum += 1
2951+
offset += 1
29452952
# for <EOF>
2946-
viml_add(self.pos, [lnum + 1, 0])
2953+
viml_add(self.pos, [lnum + 1, 0, offset])
29472954
self.i = 0
29482955

29492956
def eof(self):
@@ -3015,8 +3022,8 @@ def getstr(self, begin, end):
30153022
return r
30163023

30173024
def getpos(self):
3018-
lnum, col = self.pos[self.i]
3019-
return AttributeDict({"i":self.i, "lnum":lnum, "col":col})
3025+
lnum, col, offset = self.pos[self.i]
3026+
return AttributeDict({"i":self.i, "lnum":lnum, "col":col, "offset":offset})
30203027

30213028
def setpos(self, pos):
30223029
self.i = pos.i

test/node_position/test_position.ok

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function! F()
2+
let x =
3+
\ 1
4+
5+
let x = "
6+
\1
7+
\2 <- tab
8+
\3 マルチバイト
9+
\4"
10+
endfunction

test/node_position/test_position.vim

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
let s:vimlparser = vimlparser#import()
2+
3+
function! s:run()
4+
let src = [
5+
\ '',
6+
\ 'function! F()',
7+
\ ' let x =',
8+
\ '\ 1',
9+
\ '',
10+
\ ' let x = "',
11+
\ ' \1',
12+
\ ' \2 <- tab',
13+
\ ' \3 マルチバイト',
14+
\ ' \4"',
15+
\ 'endfunction',
16+
\ '',
17+
\ '" END',
18+
\]
19+
let r = s:vimlparser.StringReader.new(src)
20+
let p = s:vimlparser.VimLParser.new(0)
21+
let c = s:vimlparser.Compiler.new()
22+
let toplevel = p.parse(r)
23+
let func = toplevel.body[0]
24+
let body = s:extract_body(func, src)
25+
call writefile(split(body, "\n"), 'test/node_position/test_position.out')
26+
qall!
27+
endfunction
28+
29+
function! s:extract_body(func, src)
30+
let pos = a:func.pos
31+
32+
" FIXME calculating endpos is workaround. Ideally, it should have the end
33+
" position of the node.
34+
35+
let endpos = a:func.endfunction.pos
36+
let endfunc = a:func.endfunction.ea
37+
let cmdlen = endfunc.argpos.offset - endfunc.cmdpos.offset
38+
let endpos.offset += cmdlen
39+
40+
return join(a:src, "\n")[pos.offset : endpos.offset]
41+
endfunction
42+
43+
call s:run()

0 commit comments

Comments
 (0)