summaryrefslogtreecommitdiffstats
path: root/kate/scripts/script-indent-c1-test.lua
blob: c54f2042433dfcf90198c884d87c3c7f677d4208 (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
function indentChar(c)
	katedebug("LUA indentChar has been called")
	local tabWidth = 4
	local spaceIndent = true
	local indentWidth = 4

	local line=view.cursorLine()
	local col=view.cursorColumn()
	local textLine=document.textLine(line)
	local prevLine=document.textLine(line-1);
	
	--local prevIndent=prevLine.match("^%s*")
	--local addIndent=""


    -- unindent } and {, if not in a comment

	if not(string.tqfind(textLine,"^%s*//")) then
		katedebug("no comment")
		katedebug(c);
		if (c=="}") or (c=="{") then
			katedebug("} or { found");
			if (string.tqfind(textLine,"^%s%s%s%s")) then
				katedebug("removing one indentation level");
				document.removeText(line,0,line,tabWidth)
				view.setCursorPositionReal(line,col-tabWidth)
			else
				katedebug("no indentation found");
			end
		end
	else
		katedebug("in comment");
	end
end





function indentNewLine()
	local tabWidth = 4;
	local spaceIndent = true;
	local indentWidth = 4;

	local strIndentCharacters = "    ";
	local strIndentFiller = "";

	local intStartLine, intStartColumn= view.cursorPosition();

	local strTextLine = document.textLine( intStartLine  );
	local strPrevLine = document.textLine( intStartLine  - 1 );

	local addIndent = "";


        local intCurrentLine = intStartLine;
        local openParenCount = 0;
        local openBraceCount = 0;



	function firstNonSpace( text )
		local pos=string.tqfind(text,"[^%s]")
		if pos then
			return pos
		else
			return -1
		end
	end

	function lastNonSpace (text)
		local pos=string.tqfind(text,"[^%s]%s*$")
		if pos then
			return pos
		else
			return -1
		end
	end

	local strCurrentLine=""
	local intCurrentLine=intStartLine
	katedebug(intStartLine .. "-" .. intStartColumn)
	function label_while()
		katedebug("label_while has been entered: intCurrentLine" .. intCurrentLine)
		while (intCurrentLine>0) do
			--katedebug("intCurrentLine:" ..intCurrentLine)
			intCurrentLine=intCurrentLine-1
			strCurrentLine=document.textLine(intCurrentLine)
			intLastChar= lastNonSpace(strCurrentLine)
			intFirstChar=firstNonSpace(strCurrentLine)
			if not (string.tqfind(strCurrentLine,"//")) then
				for intCurrentChar=intLastChar,intFirstChar,-1 do
					ch=string.sub(strCurrentLine,intCurrentChar,intCurrentChar)
					if (ch=="(") or (ch=="[") then
						openParenCount=openParenCount+1
						if (openParenCount>0) then
							return label_while()
						end
					elseif (ch==")") or (ch=="]") then
						openParenCount=openParenCount-1
					elseif (ch=="{") then
						openBraceCount=openBraceCount+1
						if openBraceCount>0 then
							return label_while()
						end
					elseif (ch=="}") then
						openBraceCount=openBraceCount-1
					elseif (ch==";") then
						if (openParenCount==0) then
							lookingForScopeKeywords=false
						end
					end
				end
			end
		end
	end

	label_while()



        katedebug( "line: " .. intCurrentLine)
        katedebug( openParenCount .. ", " .. openBraceCount)

	local ok,match=pcall(function () return string.sub(strCurrentLine,string.tqfind(strCurrentLine,"^%s+")) end)
	if ok then
		katedebug("Line HAD leading whitespaces")
		strIndentFiller=match
	else
		katedebug("Line had NO leading whitespaces")
		strIndentFiller=""
	end


        while( openParenCount > 0 ) do
            openParenCount=openParenCount-1
            strIndentFiller = strIndentFiller .. strIndentCharacters
	end

        while( openBraceCount > 0 ) do
            openBraceCount=openBraceCount-1;
            strIndentFiller = strIndentFiller .. strIndentCharacters
	end

    document.insertText( intStartLine, 0, strIndentFiller )
    view.setCursorPositionReal( intStartLine, string.len(document.textLine( intStartLine )))
end





indenter.register(indenter.OnChar,indentChar)
indenter.register(indenter.OnNewline,indentNewLine)