Esempio n. 1
0
        public async Task Indent(params string[] arg)
        {
            p.doAction(Context.User, Context.Guild.Id, Program.Module.Code);
            if (arg.Length == 0)
            {
                await ReplyAsync(Sentences.indenteHelp(Context.Guild.Id));

                return;
            }
            List <string> code   = new List <string>();
            string        curr   = "";
            bool          inCond = false;

            foreach (string s in arg)
            {
                if (s.StartsWith("```"))
                {
                    curr += s;
                    code.Add(curr);
                    curr = "";
                }
                else if (s[s.Length - 1] == ';')
                {
                    curr += s;
                    code.Add(curr);
                    curr = "";
                }
                else if (s == "try" || s == "{" || s == "}")
                {
                    code.Add(curr);
                    code.Add(s);
                    curr = "";
                }
                else if (s.StartsWith("for") || s.StartsWith("while") || s.StartsWith("if") || s.StartsWith("else") || s.StartsWith("catch"))
                {
                    curr  += s + ' ';
                    inCond = true;
                }
                else if (inCond && s[s.Length - 1] == ')')
                {
                    curr += s;
                    code.Add(curr);
                    curr   = "";
                    inCond = false;
                }
                else
                {
                    curr += s + ' ';
                }
            }
            string finalStr    = "";
            int    currIndente = 0;
            bool   tmpIdent    = false;

            foreach (string s in code)
            {
                if (s == "")
                {
                    continue;
                }
                string line = s;
                if (line.StartsWith("for") || line.StartsWith("while") || line.StartsWith("if") || line.StartsWith("else") || s.StartsWith("catch"))
                {
                    tmpIdent = true;
                    for (int i = 0; i < currIndente; i++)
                    {
                        finalStr += '\t';
                    }
                    finalStr += line + Environment.NewLine;
                    continue;
                }
                else if (line[0] == '{')
                {
                    for (int i = 0; i < currIndente; i++)
                    {
                        finalStr += '\t';
                    }
                    finalStr += line + Environment.NewLine;
                    currIndente++;
                    tmpIdent = false;
                    continue;
                }
                else if (line[0] == '}')
                {
                    currIndente--;
                    tmpIdent = false;
                }
                for (int i = 0; i < currIndente; i++)
                {
                    finalStr += '\t';
                }
                if (tmpIdent)
                {
                    finalStr += '\t';
                }
                finalStr += line + Environment.NewLine;
                tmpIdent  = false;
            }
            await ReplyAsync(finalStr);
        }