@@ -4,187 +4,6 @@ local guide = require 'parser.guide'
44local config = require ' config'
55local util = require ' utility'
66
7-
8- local function insertIndentation (uri , position , edits )
9- local text = files .getText (uri )
10- local state = files .getState (uri )
11- local row = guide .rowColOf (position )
12- if not state or not text then
13- return
14- end
15- local offset = state .lines [row ]
16- local indent = text :match (' ^%s*' , offset )
17- for _ , edit in ipairs (edits ) do
18- edit .text = edit .text :gsub (' \n ' , ' \n ' .. indent )
19- end
20- end
21-
22- local function findForward (uri , position , ...)
23- local text = files .getText (uri )
24- local state = files .getState (uri )
25- if not state or not text then
26- return nil
27- end
28- local offset = guide .positionToOffset (state , position )
29- local firstOffset = text :match (' ^[ \t ]*()' , offset + 1 )
30- if not firstOffset then
31- return nil
32- end
33- for _ , symbol in ipairs { ... } do
34- if text :sub (firstOffset , firstOffset + # symbol - 1 ) == symbol then
35- return guide .offsetToPosition (state , firstOffset - 1 ), symbol
36- end
37- end
38- return nil
39- end
40-
41- local function findBackward (uri , position , ...)
42- local text = files .getText (uri )
43- local state = files .getState (uri )
44- if not state or not text then
45- return nil
46- end
47- local offset = guide .positionToOffset (state , position )
48- local lastOffset = lookBackward .findAnyOffset (text , offset )
49- for _ , symbol in ipairs { ... } do
50- if text :sub (lastOffset - # symbol + 1 , lastOffset ) == symbol then
51- return guide .offsetToPosition (state , lastOffset )
52- end
53- end
54- return nil
55- end
56-
57- local function checkSplitOneLine (results , uri , position , ch )
58- if ch ~= ' \n ' then
59- return
60- end
61-
62- local fPosition , fSymbol = findForward (uri , position , ' end' , ' }' )
63- if not fPosition or not fSymbol then
64- return
65- end
66- local bPosition = findBackward (uri , position , ' then' , ' do' , ' )' , ' {' )
67- if not bPosition then
68- return
69- end
70- local edits = {}
71- edits [# edits + 1 ] = {
72- start = bPosition ,
73- finish = position ,
74- text = ' \n\t ' ,
75- }
76- edits [# edits + 1 ] = {
77- start = position ,
78- finish = fPosition + 1 ,
79- text = ' ' ,
80- }
81- edits [# edits + 1 ] = {
82- start = fPosition + 1 ,
83- finish = fPosition + 1 ,
84- text = ' \n ' .. fSymbol :sub (1 , 1 )
85- }
86- insertIndentation (uri , bPosition , edits )
87- for _ , edit in ipairs (edits ) do
88- results [# results + 1 ] = edit
89- end
90- end
91-
92- local function getIndent (state , row )
93- local offset = state .lines [row ]
94- local indent = state .lua :match (' ^[\t ]*' , offset )
95- return indent
96- end
97-
98- local function isInBlock (state , position )
99- local block = guide .eachSourceContain (state .ast , position , function (source )
100- if source .type == ' ifblock'
101- or source .type == ' elseifblock' then
102- if source .keyword [4 ] and source .keyword [4 ] <= position then
103- return true
104- end
105- end
106- if source .type == ' else' then
107- if source .keyword [2 ] and source .keyword [2 ] <= position then
108- return true
109- end
110- end
111- if source .type == ' while' then
112- if source .keyword [4 ] and source .keyword [4 ] <= position then
113- return true
114- end
115- end
116- if source .type == ' repeat' then
117- if source .keyword [2 ] and source .keyword [2 ] <= position then
118- return true
119- end
120- end
121- if source .type == ' loop' then
122- if source .keyword [4 ] and source .keyword [4 ] <= position then
123- return true
124- end
125- end
126- if source .type == ' in' then
127- if source .keyword [6 ] and source .keyword [6 ] <= position then
128- return true
129- end
130- end
131- if source .type == ' do' then
132- if source .keyword [2 ] and source .keyword [2 ] <= position then
133- return true
134- end
135- end
136- if source .type == ' function' then
137- if source .args and source .args .finish <= position then
138- return true
139- end
140- if not source .keyword [3 ] or source .keyword [3 ] >= position then
141- return true
142- end
143- end
144- if source .type == ' table' then
145- if source .start + 1 == position then
146- return true
147- end
148- end
149- end )
150- return block ~= nil
151- end
152-
153- local function checkWrongIndentation (results , uri , position , ch )
154- if ch ~= ' \n ' then
155- return
156- end
157- local state = files .getState (uri )
158- if not state then
159- return
160- end
161- local row = guide .rowColOf (position )
162- if row <= 0 then
163- return
164- end
165- local myIndent = getIndent (state , row )
166- local lastIndent = getIndent (state , row - 1 )
167- if # myIndent <= # lastIndent then
168- return
169- end
170- if not util .stringStartWith (myIndent , lastIndent ) then
171- return
172- end
173- local lastOffset = lookBackward .findAnyOffset (state .lua , guide .positionToOffset (state , position ) - 1 )
174- if not lastOffset then
175- return
176- end
177- local lastPosition = guide .offsetToPosition (state , lastOffset )
178- if isInBlock (state , lastPosition ) then
179- return
180- end
181- results [# results + 1 ] = {
182- start = position - # myIndent + # lastIndent ,
183- finish = position ,
184- text = ' ' ,
185- }
186- end
187-
1887local function typeFormat (results , uri , position , ch , options )
1898 if ch ~= ' \n ' then
1909 return
@@ -218,22 +37,12 @@ return function (uri, position, ch, options)
21837 return nil
21938 end
22039
221- local results = {}
222- -- split `function () $ end`
223- checkSplitOneLine (results , uri , position , ch )
224- if # results > 0 then
225- return results
226- end
227-
228- checkWrongIndentation (results , uri , position , ch )
229- if # results > 0 then
230- return results
231- end
232-
23340 if TEST then
23441 return nil
23542 end
23643
44+ local results = {}
45+
23746 typeFormat (results , uri , position , ch , options )
23847 if # results > 0 then
23948 return results
0 commit comments