开发者

Indenting Code Algorithm

I need to write a program that indents a block of code just like visual studio. I need the logic be开发者_开发技巧hind this. Thanks

I dont need the program ..I need the logic


Put the scintilla into your program, and you're done.


The basic logic is to find the blocks. For example if you have:

for(int i = 0; i < 10; i++){
    print i
    for(int j = 0; j < 10; j++){
        print j
    }
}

by seeing {, you will find out that a block will start. You can use stacks to keep track of the blocks. for example if you see {, push it to the stack. the number of element in the stack indicates the size of your indentation because if you push 3 { into the stack, it means you are in the third nested block so you have to use 3 tabs to indent. Now if you see any }, just pop the last { from the stack. It means that your block is done.

This will work for blocks using { and }. You can use the same idea for other situations as well. For example, if you find a for syntax and no { following it, it means it is a single line for loop.


Do you mean the "Increase indent" function triggered by pressing "TAB" when having selected a code block?

This can be built using simple String options: Split the code at the newline char (e.g. Envirnoment.NewLine in C#) and then iterate over the lines and add some tabs or white spaces in front of them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜