Partager via


Colorizer.ColorizeLine(Int32, Int32, IntPtr, Int32, UInt32[]) Method

Definition

Obtains color and font attribute information for each character in the specified line of text.

public:
 virtual int ColorizeLine(int line, int length, IntPtr ptr, int state, cli::array <System::UInt32> ^ attrs);
public virtual int ColorizeLine (int line, int length, IntPtr ptr, int state, uint[] attrs);
abstract member ColorizeLine : int * int * nativeint * int * uint32[] -> int
override this.ColorizeLine : int * int * nativeint * int * uint32[] -> int
Public Overridable Function ColorizeLine (line As Integer, length As Integer, ptr As IntPtr, state As Integer, attrs As UInteger()) As Integer

Parameters

line
Int32

[in] The line number from which the line of text came from.

length
Int32

[in] The number of characters in the given text.

ptr
IntPtr

nativeint

[in] An unmarshaled pointer to a line of text.

state
Int32

[in] The current state as maintained by the parser.

attrs
UInt32[]

[in, out] An array that is filled in with indices into the GetColorableItem(Int32, IVsColorableItem) list as maintained by the LanguageService class.

Returns

Returns the updated state value.

Implements

Examples

The following is how the managed package framework version of the Colorizer class implements this method. Note the process of marshaling the text to a string.

namespace Microsoft.VisualStudio.Package  
{  
    public class Colorizer : IVsColorizer  
{  
        IScanner scanner;  

        public virtual int ColorizeLine(int line,  
                                        int length,  
                                        IntPtr ptr,  
                                        int state,  
                                        uint[] attrs)  
        {  
            int linepos = 0;  
            if (this.scanner != null) {  
                try  
                {  
                    string text = Marshal.PtrToStringUni(ptr, length);  

                    this.scanner.SetSource(text, 0);  

                    TokenInfo tokenInfo = new TokenInfo();  

                    tokenInfo.EndIndex = -1;  

                    while (this.scanner.ScanTokenAndProvideInfoAboutIt(tokenInfo, ref state))  
                    {  
                        if (attrs != null)  
                        {  
                            for (; linepos < tokenInfo.StartIndex; linepos++)  
                                attrs[linepos] = (uint)TokenColor.Text;  

                            for (; linepos <= tokenInfo.EndIndex; linepos++)  
                                attrs[linepos] = (uint)tokenInfo.Color;  
                        }  
                    }  
                }  
                catch (Exception)  
                {  
                    // Ignore exceptions  
                }  
            }  
            if (attrs != null)  
            {  
                // Must initialize the colors in all cases; otherwise, you  
                // get random color effects on the screen.  
                for (; linepos < length; linepos++)  
                    attrs[linepos] = (uint)TokenColor.Text;  
            }  
            return state;  
        }  
    }  
}  

Remarks

This method parses the line and supplies for each character an index into the ColorableItem list as provided by the language service (through GetColorableItem). Typically, this method calls the IScanner object to parse the line into tokens and return a colorable item index for each token.

This method is an implementation of ColorizeLine.

The base method processes the entire line one token at a time by calling the scanner's ScanTokenAndProvideInfoAboutIt method until the line is exhausted and fills in the attrs array for each token using the returned TokenInfo structure for the color information.

Applies to