分享

VB代码格式化(网摘)

 hdzgx 2019-10-17

一个的项目代码,实在无法看。都没有对齐。

找了一些工具,一般就是插件。还有两个不好用。也找到了一个网站的C#源码,但是也不好用。静下心,自己写了经过测试OK!

当然,没有经过大众的测试!采用C# Visual Studio 2017 Windows 10。

简单思路:

关键字分为前缀和后缀,前缀主要是下行需要缩进的关键字(加Tab键)。后缀主要是下行需要收缩的关键字(减Tab键);

PrefixString 有两个的特殊关键字,Else Elseif他们是本行缩进,下行不变。

根据本代码的情况,对于跳转代码的标记,只是遇到next时判断前面是否有纯数字的内容,有就认为他是代码位置。假如跳转代码的标记作为单行出现,没有这个问题。

看看最简单的代码格式化代码!

源码如下:

//前缀
private static string[] PrefixString = new string[]
        {
            "Begin ",
            "BeginProperty ",
            "Sub ",
            "If ",
            "Else",
            "ElseIf ",
            "For ",
            "With ",
            "While ",
            "Function ",
            "Type ",
            "Enum ",
            "Select ",
            "Do ",
            "Loop "
        };
 //后缀
        private static string[] SuffixString = new string[]
        {
            "End",
            "EndProperty",
            "Next",
            "Wend"
        };
        private bool DisposedPrefix(string line, ref bool bMoveBefore)
        {
            bool result = false;
            int tempint0;
            int tempint1;
            bMoveBefore = false;
            for (int i = 0; i < PrefixString.Length; i++)
            {
                tempint0 = line.IndexOf(PrefixString[i], StringComparison.OrdinalIgnoreCase);
                if (tempint0 < 0) continue;
                if (tempint0 > 0)
                {
                    tempint1 = line.IndexOf('\'');
                    if(tempint1 != -1 && tempint1 < tempint0)
                    {
                        continue;
                    }
                    if (line[tempint0 - 1] != ' ')
                    {
                        continue;
                    }
                }
                switch (i)
                {
                    case 3:
                        tempint1 = line.IndexOf("Then", tempint0, line.Length - tempint0, StringComparison.OrdinalIgnoreCase);
                        if (tempint1 == -1) continue;
                        tempint0 = line.IndexOf('\'', tempint1);
                        if(tempint0 == -1)
                        {
                            if(string.IsNullOrEmpty(line.Substring(tempint1 + 4).Trim()))
                            {
                                result = true;
                            }
                        }
                        else
                        {
                            if(string.IsNullOrEmpty(line.Substring(tempint1 + 4, tempint0 - tempint1 - 4).Trim()))
                            {
                                result = true;
                            }
                        }
                        break;
                    case 4:
                    case 5:
                        bMoveBefore = true;
                        break;
                    default:
                        result = true;
                        break;
                }
                break;
            }
            return result;
        }
        private bool DisposedSuffix(string line)
        {
            bool result = false;
            int tempint0;
            int tempint1;
            for(int i = 0; i < SuffixString.Length; i++)
            {
                tempint0 = line.IndexOf(SuffixString[i], StringComparison.OrdinalIgnoreCase);
                if (tempint0 < 0) continue;
                if (tempint0 > 0)
                {
                    tempint1 = line.IndexOf('\'');
                    if (tempint1 != -1 && tempint1 < tempint0)
                    {
                        continue;
                    }
                    if (tempint0 + SuffixString[i].Length < line.Length)
                    {
                        if(line[tempint0 + SuffixString[i].Length] != ' ')
                        {
                            continue;
                        }
                    }
                    if (i == 2)
                    {
                        string ts = line.Substring(0, tempint0 - 1).Trim();
                        int ta;
                        if (!string.IsNullOrEmpty(ts))
                        {
                            if(!int.TryParse(ts, out ta))
                            {
                                continue;
                            }
                        }
                    }
                }
                result = true;
            }
            return result;
        }
        public string IndentVBCode(string source)
        {
            string[] lines = source.Split('\n');
            StringBuilder sb = new StringBuilder();
            string sTabs = "";
            bool bMoveBefore = false;
            foreach (string line in lines)
            {
                string tmpLine = line.Trim();
                if(string.IsNullOrEmpty(tmpLine))
                {
                    continue;
                }
                if (DisposedPrefix(tmpLine, ref bMoveBefore))
                {
                    sb.Append(sTabs);
                    sb.AppendLine(tmpLine);
                    sTabs += "\t";
                    continue;
                }
                else
                {
                    if(bMoveBefore)
                    {
                        if(sTabs.Length > 0) sb.Append(sTabs.Substring(0, sTabs.Length - 1));
                        sb.AppendLine(tmpLine);
                        continue;
                    }
                    if (DisposedSuffix(tmpLine))
                    {
                        if (sTabs.Length > 0)
                        {
                            sTabs = sTabs.Remove(0, 1);
                        }
                    }
                }
                sb.Append(sTabs);
                sb.AppendLine(tmpLine);
            }
            return sb.ToString();
        }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多