[c#]代码库
using System;
using System.Text;
using System.Windows.Forms;
namespace VBcodeFormating
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
public string IndentVBCode(string source)
{
string[] lines = source.Split('\n');
StringBuilder sb = new StringBuilder();
string sTabs = "";
int count = 0;
bool isCaseBlock = false;
foreach (string line in lines)
{
string tmpLine = line.Trim();
switch (tmpLine)
{
case "End Sub":
case "End Function":
case "End Type":
case "End Enum":
case "End Property":
sTabs = "";
break;
case "End If":
case "#End If":
case "End Select":
case "Else":
case "#Else":
case "End With":
case "Loop":
if (sTabs.StartsWith("\t"))
sTabs = sTabs.Substring(1);
break;
}
if (sTabs.StartsWith("\t") && ((tmpLine.StartsWith("Next") || tmpLine.StartsWith("Loop Until") || tmpLine.StartsWith("Loop While") || tmpLine.StartsWith("Wend") || tmpLine.StartsWith("ElseIf"))))
sTabs = sTabs.Substring(1);
if (tmpLine.StartsWith("End Select"))
isCaseBlock = false;
if (isCaseBlock && !tmpLine.StartsWith("Case "))
sb.Append("\t");
sb.Append(sTabs);
sb.AppendLine(tmpLine);
if ((tmpLine.StartsWith("Sub ") || tmpLine.StartsWith("Private Sub ") || tmpLine.StartsWith("Public Sub ") || tmpLine.StartsWith("Function ") || tmpLine.StartsWith("Private Function ") || tmpLine.StartsWith("Public Function ") || tmpLine.StartsWith("Type ") || tmpLine.StartsWith("Private Type ") || tmpLine.StartsWith("Public Type ") || tmpLine.StartsWith("Enum ") || tmpLine.StartsWith("Private Enum ") || tmpLine.StartsWith("Public Enum ") || tmpLine.StartsWith("Property ") || tmpLine.StartsWith("Private Property ") || tmpLine.StartsWith("Public Property ")))
sTabs += "\t";
if (tmpLine.Contains(" "))
{
string sCodeString = tmpLine.Split(' ')[0];
switch (sCodeString)
{
case "For":
case "With":
case "Select":
case "Do":
sTabs += "\t";
break;
case "ElseIf":
case "If":
case "#If":
if (tmpLine.EndsWith("Then") || (tmpLine.EndsWith("_") && lines[++count].Trim().EndsWith("Then")))
sTabs += "\t";
break;
case "Case":
isCaseBlock = true;
break;
}
}
else if (tmpLine == "Else" || tmpLine == "#Else")
sTabs += "\t";
}
return sb.ToString();
}
private void btnPaste_Click(object sender, EventArgs e)
{
syntaxEditor1.Text = IndentVBCode(Clipboard.GetText());
}
private void btnCopy_Click(object sender, EventArgs e)
{
Clipboard.SetText(syntaxEditor1.Text);
}
private void syntaxEditor1_PasteDragDrop(object sender, ActiproSoftware.SyntaxEditor.PasteDragDropEventArgs e)
{
syntaxEditor1.Text = IndentVBCode(e.Text);
}
private void btnCodeFormat_Click(object sender, EventArgs e)
{
syntaxEditor1.Text = IndentVBCode(syntaxEditor1.Text);
}
}
}
[源代码打包下载]
by: 发表于:2017-12-18 09:36:30 顶(0) | 踩(0) 回复
??
回复评论