summaryrefslogtreecommitdiffstats
path: root/kate/scripts/script-indent-c-test.js
blob: 5d506ca6704e2ead56b689f1cd5ea8ae4440d168 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**KATE
 *NAME: C style indenter
 *COPYRIGHT:
 *Based on work Copyright 2005 by Dominik Haumann
 *Copyright 2005 by Joseph Wenninger
 *Here will be the license text, Dominik has to choose
 * The following line is not empty
 * 
 *An empty line ends this block
 *
 *VERSION: 0.1
 *ANUNKNOWNKEYWORD: Version has to be in the format major.minor (both numbers)
 *IGNOREALSO: All keywords, except COPYRIGHT are expected to have their data on one line
 *UNKNOWN: unknown keywords are simply ignored from the information parser
 *CURRENTLY_KNOWN_KEYWORDS: NAME,VERSION, COPYRIGHT
 *INFORMATION: This block has to begin in the first line at the first character position
 *INFORMATION: It is optional, but at least all files within the kde cvs,
 *INFORMATION: which are ment for publishing are supposed to have at least the
 *INFORMATION: COPYRIGHT block
 *INFORMATION: These files have to be stored as UTF8
 **/

/*
function indentChar() // also possible
{*/

function indentChar(c)
{
    var tabWidth = 4;
    var spaceIndent = true;
    var indentWidth = 4;

    
    var line = view.cursorLine();
    var col = view.cursorColumn();

    var textLine = document.textLine( line );
    var prevLine = document.textLine( line - 1 );

    var prevIndent = prevLine.match(/^\s*/);
    var addIndent = "";

    function unindent()
    {
    //     if (
    }

    // unindent } and {, if not in a comment
    if ( textLine.search( /^\s*\/\// ) == -1 )
    {
        if ( /*textLine.charAt( col-1 )*/ c == '}' || /*textLine.c( col-1 )*/ c == '{')
        {
            if ( textLine.search(/^\s\s\s\s/) != -1)
            {
               document.removeText( line, 0, line, tabWidth );
               view.setCursorPositionReal( line, col - tabWidth );
          }
       }
    }

}


function firstNonSpace( _text )
{
    for( _i=0; _i < _text.length; _i++ )
    {
        _ch = _text.charAt( _i );
        if( _ch != ' ' && _ch != '\t' )
            return _i;
    }

    return -1;
}

function lastNonSpace( _text )
{
    for( _i=_text.length - 1; _i >= 0; _i-- )
    {
        _ch = _text.charAt( _i );
        if( _ch != ' ' && _ch != '\t' )
            return _i;
    }

    return -1;
}

function indentNewLine()
{
	var tabWidth = 4;
	var spaceIndent = true;
	var indentWidth = 4;

	var strIndentCharacters = "    ";
	var strIndentFiller = "";

	var intStartLine = view.cursorLine();
	var intStartColumn = view.cursorColumn();

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

	var addIndent = "";
    // if previous line ends with a '{' increase indent level
    // if ( prevLine.search( /{\s*$/ ) != -1 )
    // {
    //     if ( spaceIndent )
    //         addIndent = "    ";
    //     else
    //         addIndent = "\t";
    // }
    // else
    {
        var intCurrentLine = intStartLine;
        var openParenCount = 0;
        var openBraceCount = 0;

        label_while:
        while ( intCurrentLine > 0 )
        {
            intCurrentLine--;

            strCurrentLine = document.textLine( intCurrentLine );
            intLastChar = lastNonSpace( strCurrentLine );
            intFirstChar = firstNonSpace( strCurrentLine ) ;

            if ( strCurrentLine.search( /\/\// ) == -1 )
            {

                // look through line backwards for interesting characters
                for( intCurrentChar = intLastChar; intCurrentChar >= intFirstChar; --intCurrentChar )
                {
                    ch = strCurrentLine.charAt( intCurrentChar );
                    switch( ch )
                    {
                    case '(': case '[':
                        if( ++openParenCount > 0 )
                        break label_while; //return calcIndentInBracket( begin, cur, pos );
                        break;
                    case ')': case ']': openParenCount--; break;
                    case '{':
                        if( ++openBraceCount > 0 )
                        break label_while; //return calcIndentInBrace( begin, cur, pos );
                        break;
                    case '}': openBraceCount--; lookingForScopeKeywords = false; break;
                    case ';':
                        if( openParenCount == 0 )
                        lookingForScopeKeywords = false;
                        break;
                    }
                }
            }
        }

        strIndentFiller += strCurrentLine.match(/^\s+/);
        if ( strIndentFiller == "null" )
            strIndentFiller = "";

        debug( "line: " + intCurrentLine);
        debug( openParenCount + ", " + openBraceCount);

        while( openParenCount > 0 )
        {
            openParenCount--;
            strIndentFiller += strIndentCharacters;
        }

        while( openBraceCount > 0 )
        {
            openBraceCount--;
            strIndentFiller += strIndentCharacters;
        }
    }

    document.insertText( intStartLine, 0, strIndentFiller );
    view.setCursorPositionReal( intStartLine, document.textLine( intStartLine ).length );
}

indenter.onchar=indentChar
indenter.onnewline=indentNewLine