我正在编写我的第一个简单的sublime text 3插件。 它的目的是在预先编译的代码中添加if if语句,以避免在另一个ide中溢出解析器堆栈。
现在它正在寻找最接近的结果,如果在发生时并添加它自己的结束,如果,但是如果它在任何一端如果行。
if (1=1)
%Layout__control__IDs[$Layout__control__count] := $Layout__Unit7Control3LabelID
%Layout__control__types[$Layout__control__count] := 3
if (1=1)
%Layout__Page__IDs[$Layout__control__count] := $Layout__Unit7Control3LabelID
%Layout__control__Page[$Layout__control__count] := $Layout__Page__count
end if
here is plugin inserted string
HEYEND
!Layout__control__names[$Layout__control__count] := "Unit7Control3Label"
!Layout__LabelsNames[$Layout__LabelsIDsCount] := "Unit7Control3Label"
add_menu_item($Layout__control,"Unit7Control3Label",$Layout__control__count)
if (%Layout__LabelsAssigned[search(%Layout__LabelsIDs,$Layout__Unit7Control3LabelID)]=0)
%Layout__LabelsX[$Layout__LabelsIDsCount] := -1
%Layout__LabelsY[$Layout__LabelsIDsCount] := -1
end if
%Layout__LabelsW[$Layout__LabelsIDsCount] := -1
%Layout__LabelsH[$Layout__LabelsIDsCount] := -1
!Layout__LabelsTexts[$Layout__LabelsIDsCount] := ""
%Layout__LabelsTextAl[$Layout__LabelsIDsCount] := 0
%Layout__LabelsFont[$Layout__LabelsIDsCount] := 0
%Layout__LabelsTextPosY[$Layout__LabelsIDsCount] := 0
!Layout__LabelsImgs[$Layout__LabelsIDsCount] := ""
inc($Layout__LabelsIDsCount)
inc($Layout__control__count)
end if
如何检查end的标识量是否为行以避免这种情况?
插件代码:
import sublime, sublime_plugin
class PrefixrCommand(sublime_plugin.TextCommand):
def run(self, edit):
# find string with 'on init'
startreg = self.view.find("on init", 0)
# make int to tuple obj with row and col nr
start = startreg.end()
# find the end of 'init'
InitEndReg = self.view.find("end on", start)
InitEnd = InitEndReg.end()
# get tuple obj with row & col
StartRowCol = self.view.rowcol(start)
StartRowCol += (1000,0)
ofset = self.view.text_point(StartRowCol[0]+StartRowCol[2],0)
# if ofset < end on
if InitEnd > ofset:
# add string to the start point
self.view.insert(edit, start, "\nHEYSTART\n")
# find 'end if' region after ofset
EndIfReg = self.view.find("end if", ofset)
EndIf = EndIfReg.end()
EndIfReg = self.view.full_line (EndIf)
EndIf = EndIfReg.end()
self.view.insert(edit, EndIf, "\nHEYEND\n")
StartWhileRowCol = self.view.rowcol(EndIf)
StartWhileRowCol += (2,0)
StartWhile = self.view.text_point(StartWhileRowCol[0]+StartWhileRowCol[2],0)
# получаем tuple объект с номером строки и буквы
StartRowCol = self.view.rowcol(StartWhile)
# типа увеличиваем номер строки на 1000, на самом деле добавляя еще два элемента к объекту
StartRowCol += (1000,0)
# добавляем первый номер строки ко второму (1000)
ofset = self.view.text_point(StartRowCol[0]+StartRowCol[2],0)
while InitEnd > ofset:
if InitEnd > ofset:
# добавляем строку в начало "итерации"
self.view.insert(edit, StartWhile, "\nHEYSTART\n")
# ищем регион с end if после строки офсета
EndIfReg = self.view.find("end if", ofset)
# переводим ее в tuple объект для вызова функции API
EndIf = EndIfReg.end()
EndIfReg = self.view.full_line (EndIf)
EndIf = EndIfReg.end()
# вставляем строку после end if
self.view.insert(edit, EndIf, "\nHEYEND\n")
StartWhileRowCol = self.view.rowcol(EndIf)
StartWhileRowCol += (2,0)
StartWhile = self.view.text_point(StartWhileRowCol[0]+StartWhileRowCol[2],0)
# получаем tuple объект с номером строки и буквы
StartRowCol = self.view.rowcol(StartWhile)
# типа увеличиваем номер строки на 1000, на самом деле добавляя еще два элемента к объекту
StartRowCol += (1000,0)
# добавляем первый номер строки ко второму (1000)
ofset = self.view.text_point(StartRowCol[0]+StartRowCol[2],0)