public static List<EditSpan> ReformatCode(IVsTextLines pBuffer, TextSpan span, int tabSize)
{
string filePath = FilePathUtilities.GetFilePath(pBuffer);
// Return dynamic scanner based on file extension
List<EditSpan> changeList = new List<EditSpan>();
string codeToFormat;
int endOfFirstLineIndex;
// Get 1st line and parse custom define
pBuffer.GetLengthOfLine(0, out endOfFirstLineIndex);
pBuffer.GetLineText(0, 0, 0, endOfFirstLineIndex, out codeToFormat);
Dictionary<string,string> defines = ParseDefineLine(codeToFormat);
AsmHighlighterScanner scanner = AsmHighlighterScannerFactory.GetScanner(filePath);
Scanner lexer = scanner.Lexer;
// Iterate on each line of the selection to format
for (int line = span.iStartLine; line <= span.iEndLine; line++)
{
int lineLength;
pBuffer.GetLengthOfLine(line, out lineLength);
pBuffer.GetLineText(line, 0, line, lineLength, out codeToFormat);
string codeToAssemble = ConvertToFasm(lexer, codeToFormat, defines);
lexer.SetSource(codeToFormat, 0);
int state = 0;
int start, end;
bool instructionFound = false, commentFound = false;
int commentStart = 0;
AsmHighlighterToken token = (AsmHighlighterToken)lexer.GetNext(ref state, out start, out end);
while (token != AsmHighlighterToken.EOF )
{
switch (token)
{
case AsmHighlighterToken.INSTRUCTION:
case AsmHighlighterToken.FPUPROCESSOR:
case AsmHighlighterToken.SIMDPROCESSOR:
instructionFound = true;
break;
case AsmHighlighterToken.COMMENT_LINE:
if (!commentFound)
{
commentFound = true;
commentStart = start;
}
break;
}
if ( instructionFound && commentFound )
{
byte[] buffer = null;
try
{
buffer = ManagedFasm.Assemble(codeToAssemble);
}
catch (Exception ex)
{
// Unable to parse instruction... skip
}
if (buffer != null)
{
}
TextSpan editTextSpan = new TextSpan();
editTextSpan.iStartLine = line;
editTextSpan.iEndLine = line;
editTextSpan.iStartIndex = commentStart;
editTextSpan.iEndIndex = commentStart+1;
if ((codeToFormat.Length - commentStart) > 2 && codeToFormat.Substring(commentStart, 2) == ";#")
{
editTextSpan.iEndIndex = editTextSpan.iEndIndex + 2;
}
string text = ";#" + ((buffer == null) ? "?" : string.Format("{0:X}",buffer.Length));
changeList.Add(new EditSpan(editTextSpan, text));
break;
}
token = (AsmHighlighterToken)lexer.GetNext(ref state, out start, out end);
}
}
return changeList;
}