@@ -992,28 +992,43 @@ local function bindGeneric(binded)
992
992
end
993
993
end
994
994
995
- local function bindDocsBetween (state , binded , bindSources , start , finish )
996
- guide .eachSourceBetween (state .ast , start , finish , function (src )
997
- if src .start and src .start < start then
998
- return
995
+ local function bindDocsBetween (sources , binded , bindSources , start , finish )
996
+ -- 用二分法找到第一个
997
+ local max = # sources
998
+ local index
999
+ local left = 1
1000
+ local right = max
1001
+ for _ = 1 , 1000 do
1002
+ index = left + (right - left ) // 2
1003
+ if index <= left then
1004
+ index = left
1005
+ break
1006
+ elseif index >= right then
1007
+ index = right
1008
+ break
999
1009
end
1000
- if src .type == ' local'
1001
- or src .type == ' setlocal'
1002
- or src .type == ' setglobal'
1003
- or src .type == ' setfield'
1004
- or src .type == ' setmethod'
1005
- or src .type == ' setindex'
1006
- or src .type == ' tablefield'
1007
- or src .type == ' tableindex'
1008
- or src .type == ' function'
1009
- or src .type == ' ...' then
1010
- src .bindDocs = binded
1011
- bindSources [# bindSources + 1 ] = src
1010
+ local src = sources [index ]
1011
+ if src .start < start then
1012
+ left = index
1013
+ else
1014
+ right = index
1012
1015
end
1013
- end )
1016
+ end
1017
+ for i = index - 1 , max do
1018
+ local src = sources [i ]
1019
+ if src then
1020
+ if src .start > finish then
1021
+ break
1022
+ end
1023
+ if src .start >= start then
1024
+ src .bindDocs = binded
1025
+ bindSources [# bindSources + 1 ] = src
1026
+ end
1027
+ end
1028
+ end
1014
1029
end
1015
1030
1016
- local function bindDoc (state , lns , binded )
1031
+ local function bindDoc (sources , lns , binded )
1017
1032
if not binded then
1018
1033
return
1019
1034
end
@@ -1030,24 +1045,42 @@ local function bindDoc(state, lns, binded)
1030
1045
local row = guide .positionOf (lns , lastDoc .finish )
1031
1046
local cstart , cfinish = guide .lineRange (lns , row )
1032
1047
local nstart , nfinish = guide .lineRange (lns , row + 1 )
1033
- bindDocsBetween (state , binded , bindSources , cstart , cfinish )
1048
+ bindDocsBetween (sources , binded , bindSources , cstart , cfinish )
1034
1049
if # bindSources == 0 then
1035
- bindDocsBetween (state , binded , bindSources , nstart , nfinish )
1050
+ bindDocsBetween (sources , binded , bindSources , nstart , nfinish )
1036
1051
end
1037
1052
end
1038
1053
1039
1054
local function bindDocs (state )
1040
1055
local lns = lines (nil , state .lua )
1056
+ local sources = {}
1057
+ guide .eachSource (state .ast , function (src )
1058
+ if src .type == ' local'
1059
+ or src .type == ' setlocal'
1060
+ or src .type == ' setglobal'
1061
+ or src .type == ' setfield'
1062
+ or src .type == ' setmethod'
1063
+ or src .type == ' setindex'
1064
+ or src .type == ' tablefield'
1065
+ or src .type == ' tableindex'
1066
+ or src .type == ' function'
1067
+ or src .type == ' ...' then
1068
+ sources [# sources + 1 ] = src
1069
+ end
1070
+ end )
1071
+ table.sort (sources , function (a , b )
1072
+ return a .start < b .start
1073
+ end )
1041
1074
local binded
1042
1075
for _ , doc in ipairs (state .ast .docs ) do
1043
1076
if not isNextLine (lns , binded , doc ) then
1044
- bindDoc (state , lns , binded )
1077
+ bindDoc (sources , lns , binded )
1045
1078
binded = {}
1046
1079
state .ast .docs .groups [# state .ast .docs .groups + 1 ] = binded
1047
1080
end
1048
1081
binded [# binded + 1 ] = doc
1049
1082
end
1050
- bindDoc (state , lns , binded )
1083
+ bindDoc (sources , lns , binded )
1051
1084
end
1052
1085
1053
1086
return function (_ , state )
0 commit comments