forked from dail8859/LuaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuaAutoIndent.lua
More file actions
Latest commit
76 lines (65 loc) · 2.89 KB
/
Copy pathLuaAutoIndent.lua
File metadata and controls
76 lines (65 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
-- Regexs to determine when to indent or unindent
-- From: https://github.com/sublimehq/Packages/blob/master/Lua/Indent.tmPreferences
localdecreaseIndentPattern=[[^\s*(elseif|else|end|\})\s*$]]
localincreaseIndentPattern=[[^\s*(else|elseif|for|(local\s+)?function|if|repeat|until|while)\b((?!end).)*$|\{\s*$]]
localdo_increase=false
-- Get the start and end position of a specific line number
localfunctiongetLinePositions(line_num)
localstart_pos=editor:PositionFromLine(line_num)
localend_pos=start_pos+editor:LineLength(line_num)
returnstart_pos, end_pos
end
-- Check any time a character is added
localfunctionautoIndent_OnChar(ch)
ifch=="\n" then
-- Get the previous line
localline_num=editor:LineFromPosition(editor.CurrentPos) -1
localstart_pos, end_pos=getLinePositions(line_num)
ifeditor:findtext(increaseIndentPattern, SCFIND_REGEXP, start_pos, end_pos) then
-- This has to be delayed because N++'s auto-indentation hasn't triggered yet
do_increase=true
end
else
localline_num=editor:LineFromPosition(editor.CurrentPos)
localstart_pos, end_pos=getLinePositions(line_num)
ifeditor:findtext(decreaseIndentPattern, SCFIND_REGEXP, start_pos, end_pos) then
-- The pattern matched, now check the previous line's indenation
ifline_num>1andeditor.LineIndentation[line_num-1] <=editor.LineIndentation[line_num] then
editor.LineIndentation[line_num] =editor.LineIndentation[line_num] -4
end
end
end
returnfalse
end
-- Work around N++'s auto indentation by delaying the indentation change
localfunctionautoIndent_OnUpdateUI(flags)
ifdo_increasethen
do_increase=false
-- Now the the indentation can be increased since N++'s auto-indentation is done by now
editor:Tab()
end
returnfalse
end
-- See if the auto indentation should be turned on or off
localfunctioncheckAutoIndent(bufferid)
-- Make sure the event handlers are completely removed.
-- There are some cases where they can get registered more than once.
whilenpp.RemoveEventHandler("OnChar", autoIndent_OnChar) doend
whilenpp.RemoveEventHandler("OnUpdateUI", autoIndent_OnUpdateUI) doend
ifnpp.BufferLangType[bufferid] ==L_LUAthen
do_increase=false
-- Register the event handlers
npp.AddEventHandler("OnChar", autoIndent_OnChar)
npp.AddEventHandler("OnUpdateUI", autoIndent_OnUpdateUI)
end
end
-- Only turn on the auto indentation when it is actually a lua file
-- Check it when the file is switched to and if the language changes
npp.AddEventHandler("OnSwitchFile", function(filename, bufferid)
checkAutoIndent(bufferid)
returnfalse
end)
npp.AddEventHandler("OnLangChange", function()
checkAutoIndent(npp.CurrentBufferID)
returnfalse
end)