AsmHighlighter.AsmHighlighterFormatHelper.ReformatCode C# (CSharp) Method

ReformatCode() public static method

public static ReformatCode ( IVsTextLines pBuffer, TextSpan span, int tabSize ) : List
pBuffer IVsTextLines
span TextSpan
tabSize int
return List
        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;
        }

Usage Example

Exemplo n.º 1
0
        private void DoFormatting(EditArray mgr, TextSpan span)
        {
            // Make sure there is one space after every comma unless followed
            // by a tab or comma is at end of line.
            IVsTextLines pBuffer = GetTextLines();

            if (pBuffer != null)
            {
                List <EditSpan> changeList = AsmHighlighterFormatHelper.ReformatCode(pBuffer, span, LanguageService.GetLanguagePreferences().TabSize);
                if (changeList != null)
                {
                    foreach (EditSpan editSpan in changeList)
                    {
                        // Add edit operation
                        mgr.Add(editSpan);
                    }
                }
            }
        }