jeudi 10 avril 2014

chaîne - Application qui indente un code code en c# - Stack Overflow


My application should read a C# code sample that is unindented, then indent the code programatically. The way I am doing it may not be correct but still could achieve partial results.


I could set white spaces when a { is found then continue with the same amount of space for rest of the lines being read. When another { is found again add spaces and continue with this new space for rest of lines. For that this is what I did:


    private void btn_format_Click(object sender, EventArgs e)
{
string lineInfo = "";
string fl = "";
string ctab= char.ConvertFromUtf32(32)+char.ConvertFromUtf32(32)+char.ConvertFromUtf32(32);
foreach (string line in txt_codepage.Lines) // text_codepage is a textbox with code
{
if (line.Contains("{"))
{
string l = line.Replace("{", ctab+"{");
lineInfo = lineInfo + (l + "\n");
fl = fl + ctab;
ctab = ctab + ctab;
}
else
{
lineInfo = lineInfo + (char.ConvertFromUtf32(32)+fl+ line + "\n");
}

I could achieve the proper indentation that I want till here. Now when I find a } I should do the reverse process but unfortunately that is not possible with strings. The reverse process that I meant is this:


            if (line.Contains("}"))
{
string l = line.Replace(ctab + "}", "}");
lineInfo = lineInfo + (l + "\n");
fl = fl - ctab;
ctab = ctab - ctab;
}
else
{
lineInfo = lineInfo - (char.ConvertFromUtf32(32) + fl + line + "\n");

}
}
MessageBox.Show(lineInfo.ToString());

I know the above part of the code is a complete blunder but let me know how to achieve it in correct way




You can go and check out codemaid, an open source VS add in for cleaning code




If you want parse string, you should use StringBuilder instead string concatenations (concatenations is to slow). I wrote some code, to demonstrate how you can parse CS or other code. It is not a full example, just a basic concepts.


If you want learn more about parsers you can read Compilers: Principles, Techniques, and Tools.


public static string IndentCSharpCode(string code)
{
const string INDENT_STEP = " ";

if (string.IsNullOrWhiteSpace(code))
{
return code;
}

var result = new StringBuilder();
var indent = string.Empty;
var lineContent = false;
var stringDefinition = false;

for (var i = 0; i < code.Length; i++)
{
var ch = code[i];

if (ch == '"' && !stringDefinition)
{
result.Append(ch);
stringDefinition = true;
continue;
}

if (ch == '"' && stringDefinition)
{
result.Append(ch);
stringDefinition = false;
continue;
}

if (stringDefinition)
{
result.Append(ch);
continue;
}

if (ch == '{' && !stringDefinition)
{
if (lineContent)
{
result.AppendLine();
}

result.Append(indent).Append("{");

if (lineContent)
{
result.AppendLine();
}

indent += INDENT_STEP;
lineContent = false;

continue;
}

if (ch == '}' && !stringDefinition)
{
if (indent.Length != 0)
{
indent = indent.Substring(0, indent.Length - INDENT_STEP.Length);
}

if (lineContent)
{
result.AppendLine();
}

result.Append(indent).Append("}");

if (lineContent)
{
result.AppendLine();
}


lineContent = false;

continue;
}

if (ch == '\r')
{
continue;
}

if ((ch == ' ' || ch == '\t') && !lineContent)
{
continue;
}

if (ch == '\n')
{
lineContent = false;
result.AppendLine();

continue;
}

if (!lineContent)
{
result.Append(indent);
lineContent = true;
}

result.Append(ch);
}

return result.ToString();
}



Remove all of the whitespace from the line using String.Trim() and then add just the tabs you want. Also, your code would be much more readable if you could avoid char.ConvertFromUtf32(32) - why write that instead of " " or ' '?



My application should read a C# code sample that is unindented, then indent the code programatically. The way I am doing it may not be correct but still could achieve partial results.


I could set white spaces when a { is found then continue with the same amount of space for rest of the lines being read. When another { is found again add spaces and continue with this new space for rest of lines. For that this is what I did:


    private void btn_format_Click(object sender, EventArgs e)
{
string lineInfo = "";
string fl = "";
string ctab= char.ConvertFromUtf32(32)+char.ConvertFromUtf32(32)+char.ConvertFromUtf32(32);
foreach (string line in txt_codepage.Lines) // text_codepage is a textbox with code
{
if (line.Contains("{"))
{
string l = line.Replace("{", ctab+"{");
lineInfo = lineInfo + (l + "\n");
fl = fl + ctab;
ctab = ctab + ctab;
}
else
{
lineInfo = lineInfo + (char.ConvertFromUtf32(32)+fl+ line + "\n");
}

I could achieve the proper indentation that I want till here. Now when I find a } I should do the reverse process but unfortunately that is not possible with strings. The reverse process that I meant is this:


            if (line.Contains("}"))
{
string l = line.Replace(ctab + "}", "}");
lineInfo = lineInfo + (l + "\n");
fl = fl - ctab;
ctab = ctab - ctab;
}
else
{
lineInfo = lineInfo - (char.ConvertFromUtf32(32) + fl + line + "\n");

}
}
MessageBox.Show(lineInfo.ToString());

I know the above part of the code is a complete blunder but let me know how to achieve it in correct way



You can go and check out codemaid, an open source VS add in for cleaning code



If you want parse string, you should use StringBuilder instead string concatenations (concatenations is to slow). I wrote some code, to demonstrate how you can parse CS or other code. It is not a full example, just a basic concepts.


If you want learn more about parsers you can read Compilers: Principles, Techniques, and Tools.


public static string IndentCSharpCode(string code)
{
const string INDENT_STEP = " ";

if (string.IsNullOrWhiteSpace(code))
{
return code;
}

var result = new StringBuilder();
var indent = string.Empty;
var lineContent = false;
var stringDefinition = false;

for (var i = 0; i < code.Length; i++)
{
var ch = code[i];

if (ch == '"' && !stringDefinition)
{
result.Append(ch);
stringDefinition = true;
continue;
}

if (ch == '"' && stringDefinition)
{
result.Append(ch);
stringDefinition = false;
continue;
}

if (stringDefinition)
{
result.Append(ch);
continue;
}

if (ch == '{' && !stringDefinition)
{
if (lineContent)
{
result.AppendLine();
}

result.Append(indent).Append("{");

if (lineContent)
{
result.AppendLine();
}

indent += INDENT_STEP;
lineContent = false;

continue;
}

if (ch == '}' && !stringDefinition)
{
if (indent.Length != 0)
{
indent = indent.Substring(0, indent.Length - INDENT_STEP.Length);
}

if (lineContent)
{
result.AppendLine();
}

result.Append(indent).Append("}");

if (lineContent)
{
result.AppendLine();
}


lineContent = false;

continue;
}

if (ch == '\r')
{
continue;
}

if ((ch == ' ' || ch == '\t') && !lineContent)
{
continue;
}

if (ch == '\n')
{
lineContent = false;
result.AppendLine();

continue;
}

if (!lineContent)
{
result.Append(indent);
lineContent = true;
}

result.Append(ch);
}

return result.ToString();
}


Remove all of the whitespace from the line using String.Trim() and then add just the tabs you want. Also, your code would be much more readable if you could avoid char.ConvertFromUtf32(32) - why write that instead of " " or ' '?


0 commentaires:

Enregistrer un commentaire