Colorizer::ColorizeLine Method (Int32, Int32, IntPtr, Int32, array<UInt32>^)
Obtains color and font attribute information for each character in the specified line of text.
Assembly: Microsoft.VisualStudio.Package.LanguageService.14.0 (in Microsoft.VisualStudio.Package.LanguageService.14.0.dll)
public: virtual int ColorizeLine( int line, int length, IntPtr ptr, int state, array<unsigned int>^ attrs )
Parameters
- line
-
Type:
System::Int32
[in] The line number from which the line of text came from.
- length
-
Type:
System::Int32
[in] The number of characters in the given text.
- ptr
-
Type:
System::IntPtr
[in] An unmarshaled pointer to a line of text.
- state
-
Type:
System::Int32
[in] The current state as maintained by the parser.
- attrs
-
Type:
array<System::UInt32>^
[in, out] An array that is filled in with indices into the GetColorableItem list as maintained by the LanguageService class.
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.
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; } } }