Scite 自動展開

lua-users home
Wiki

輸入時,擴充套件文字例如「if」和「for」為正確縮排的子句。我覺得這很實用,因為這樣就不必輸入括號和大括號,然後花時間正確格式化。

輸入

if |

變成

if (|) {

}

以及

for |

變成

for (|; ; ) {

}

「|」表示展開前和展開後的游標位置。

只會在開啟檔案的副檔名為 c 或標頭檔時進行展開。此外,腳本還會辨識您在已註解節區輸入時,不會自動展開。


local in_word,current_word, substituting
local find = string.find   


-- Expand "if " to
-- if () {
--     
-- }
-- And set the cursor between the ()
-- Also works for "while "
function expandIf()
	-- The text is "if "
	editor:AddText("(")
	-- "if ("
	-- Remember where to bring the cursor to
	local tmp = editor.CurrentPos
	editor:AddText(") {")
	local line = editor:LineFromPosition(editor.CurrentPos)
	local tmpi = editor.LineIndentation[line]

	editor:AddText("\n")
	while tmpi >= 0 do
		editor:AddText("\t")
		tmpi = tmpi - 4
	end
	editor:AddText("\n")
	tmpi = editor.LineIndentation[line]
	while tmpi > 0 do
		editor:AddText("\t")
		tmpi = tmpi - 4
	end
	editor:AddText("}")
	
	-- Bring the cursor into the "()"
	editor:GotoPos(tmp)
end


function expandElse()
	local line = editor:LineFromPosition(editor.CurrentPos)
	local tmpi = editor.LineIndentation[line]

	editor:AddText("{\n")
	while tmpi >= 0 do
		editor:AddText("\t")
		tmpi = tmpi - 4
	end
	local tmp = editor.CurrentPos
	editor:AddText("\n")
	tmpi = editor.LineIndentation[line]
	while tmpi > 0 do
		editor:AddText("\t")
		tmpi = tmpi - 4
	end
	editor:AddText("}")
	editor:GotoPos(tmp)
end


function expandFor()
	local line = editor:LineFromPosition(editor.CurrentPos)
	local tmpi = editor.LineIndentation[line]

	editor:AddText("(")
	local tmp = editor.CurrentPos
	editor:AddText("; ; ) {\n")
	while tmpi >= 0 do
		editor:AddText("\t")
		tmpi = tmpi - 4
	end
	editor:AddText("\n")
	tmpi = editor.LineIndentation[line]
	while tmpi > 0 do
		editor:AddText("\t")
		tmpi = tmpi - 4
	end
	editor:AddText("}")
	editor:GotoPos(tmp)
end


function expandDo()
	local line = editor:LineFromPosition(editor.CurrentPos)
	local tmpi = editor.LineIndentation[line]

	editor:AddText("{\n")
	while tmpi >= 0 do
		editor:AddText("\t")
		tmpi = tmpi - 4
	end
	editor:AddText("\n")
	tmpi = editor.LineIndentation[line]
	while tmpi > 0 do
		editor:AddText("\t")
		tmpi = tmpi - 4
	end
	editor:AddText("} while (")
	local tmp = editor.CurrentPos
	editor:AddText(");")
	editor:GotoPos(tmp)
end

function expandCout()
	editor:AddText(" << \"")
	local tmp = editor.CurrentPos
	editor:AddText("\" << endl;")
	editor:GotoPos(tmp)
end

function OnChar(c)
	if not substituting then
		return false
	end
	
	-- Only activate on the space character
	if c == ' ' then
		-- The currentPos is the character after the cursor
		-- Get the style of the word that was just typed
		-- That is, the style of the character before the space
		cstyle = editor.StyleAt[editor.CurrentPos-2]
		
		-- If the typed word is a part of code
		-- Rather than a part of comments
		if cstyle == 0 or cstyle == 32 or cstyle == 4 or cstyle == 5 or cstyle == 10 or cstyle == 11 or cstyle == 16 then
			
			-- Get the word that was just typed
				local p, original_pos
				local lineStart
				
				-- get the current position and the start of the current line
				lineStart = editor:PositionFromLine(editor:LineFromPosition(editor.CurrentPos))
				original_pos = editor.CurrentPos
				
				-- Find the beginning of the current word
				p = editor.CurrentPos-2
				-- Stop on the space/tab/newline character
				while (p >= lineStart) and (find(string.char(editor.CharAt[p]), '%w')) do
					p = p - 1
				end
				-- Increment 1 to get the first character of the current word
				p = p + 1
				
				-- Select the word and get it
				editor:SetSel(p, editor.CurrentPos-1)
				current_word = editor:GetSelText()
				-- Clear the selection
				editor:SetSel(original_pos, original_pos)
			
			-- Got the word, now expand
				if current_word == "elseif" then
					editor:GotoPos(editor.CurrentPos - 3)
					editor:AddText(" ")
					editor:GotoPos(editor.CurrentPos + 3)
					expandIf()
				-- if and while have the same expansion
				elseif current_word == "if" or current_word == "while"  then
					expandIf()
				elseif current_word == "else" then
					expandElse()
				elseif current_word == "for" then
					expandFor()
				elseif current_word == "do" then
					expandDo()
				elseif current_word == "cout" then
					expandCout()
				end
			-- Expansion complete
		end
	end


	-- don't interfere with usual processing!
	return false
end  



function OnOpen(f)
	local ext = props['FileExt']
	if ext == 'h' or ext == 'c' or ext == 'hpp' or ext == 'cpp' or ext == 'cxx' or ext == 'cs' then 
		substituting = true
	else
		substituting = false
	end
end

function OnSwitchFile(f)
	local ext = props['FileExt']
	if ext == 'h' or ext == 'c' or ext == 'hpp' or ext == 'cpp' or ext == 'cxx' or ext == 'cs' then 
		substituting = true
	else
		substituting = false
	end
end

最初是基於 SteveDonovan 替換字詞的程式碼。


最新異動 · 偏好設定
編輯 · 歷史
最後編輯於 2008 年 12 月 9 日上午 10:15 GMT (細微差異)