Reconfigure CodeEditor
Hi
I try to make several things with VsCodeWindow + some source code inside (loaded directly from network).
1) Is it proper to find language service Guid for appropriate file extension
using iteration over HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\<N>\Languages\File Extensions ?
2) for some (like C# ) language a sample file is decorated (supposedly) with MARKER_REGION_EXPANDED or
MARKER_REGION_COLLAPSED markers and some areas collapsed. I would prefer to disable this feature or wipe these markers out manually. Is it possible? I can't find proper time/place for it. Also i have to disable all possible markers from third part vendors.
3) Supplied "Code Editor" popup in view contains default and third-party items. I want to replace it for some own.
In fact i need only to display text with "default" syntax highlight. Maybe using these code windows isn't appropriate for my case. If i will use language service and colorizer i have
to implement boring color convertion at least.
Regards
modified code from SDK "LanguageServiceInDialog"
INITVIEW[] initView = new INITVIEW[1];
initView[0].fSelectionMargin = 0;
initView[0].IndentStyle = vsIndentStyle.vsIndentStyleSmart;
initView[0].fWidgetMargin = 0;
initView[0].fVirtualSpace = 0;
initView[0].fDragDropMove = 1;
initView[0].fVisibleWhitespace = 0;
initView[0].fHotURLs = 0;
IVsCodeWindowEx codeWindowEx = codeWindow as IVsCodeWindowEx;
int hr = codeWindowEx.Initialize((uint)_codewindowbehaviorflags.CWB_DISABLEDROPDOWNBAR |
(uint)_codewindowbehaviorflags.CWB_DISABLESPLITTER,
0, null, null,
(int)TextViewInitFlags.VIF_SET_WIDGET_MARGIN |
(int)TextViewInitFlags.VIF_SET_SELECTION_MARGIN |
(int)TextViewInitFlags2.VIF_ACTIVEINMODALSTATE |
(int)TextViewInitFlags2.VIF_READONLY |
(int)TextViewInitFlags2.VIF_SUPPRESSBORDER |
(int)TextViewInitFlags2.VIF_SUPPRESS_STATUS_BAR_UPDATE |
(int)TextViewInitFlags2.VIF_SUPPRESSTRACKCHANGES,
initView);
Hi
I resigned from idea of using editor since too much things are not available in vs2003.
So i switched to investigate coloring technology.
This hand-made something even started to work (syntax colouring and default colors)
but still i have several problems.
#1 problem - cache of text buffers, something
I lookup for language service in registry i obtain
two interfaces IVsProvideColorableItems and IVsLanguageInfo
where serviceProvider is global service provider.
I create IVsTextLines from local registry (everything extracted from example from sdk). Difference is i want to use IVsTextLines (as IVstextBuffer) only to to fetch information about "colors" i want to dispose it later.
I use IVsColorizer.CloseColorizer() but is it not enough...
It seems every text buffer still remains in memory - how can i unregister it?
First text buffer has priviledge to be parsed completely in proper way.
If try to "colorize" another text by C# language service it recognizes comments and defition tags (like #region) - rest is mostly CT_USER_TEXT
private void createTextBuffer(String sText, Guid guidCLSIDLangService)
{
ILocalRegistry localRegistry = (ILocalRegistry)CompareViewUtil.QueryService(_serviceProvider, typeof(SLocalRegistry).GUID, typeof(ILocalRegistry).GUID);
// set buffer
Guid guidVsTextBuffer = typeof(VsTextBufferClass).GUID;
IVsTextBuffer textBuffer = CompareViewUtil.CreateObject(_serviceProvider, localRegistry, guidVsTextBuffer, typeof(IVsTextBuffer).GUID) as IVsTextBuffer;
Marshal.ThrowExceptionForHR(textBuffer.InitializeContent(sText, sText.Length));
IVsLanguageInfo vsLanguageInfo = (IVsLanguageInfo)CompareViewUtil.QueryService(_serviceProvider, guidCLSIDLangService, typeof(IVsLanguageInfo).GUID);
IVsColorizer colorizer = null;
int hresult = vsLanguageInfo.GetColorizer(textBuffer as IVsTextLines, out colorizer);
Marshal.ThrowExceptionForHR(hresult);
int uiStartState = 0;
int pfFlag;
colorizer.GetStateMaintenanceFlag(out pfFlag);
if (pfFlag == 1) {
colorizer.GetStartState(out uiStartState);
}
IVsTextLines lines = (IVsTextLines)textBuffer;
hresult = textBuffer.SetLanguageServiceID(ref guidCLSIDLangService);
Marshal.ThrowExceptionForHR(hresult);
int lineCount;
lines.GetLineCount(out lineCount);
_aLineColorizations = new LineColorization[lineCount];
for (int i = 0; i < lineCount; i++)
{
int length;
Marshal.ThrowExceptionForHR(lines.GetLengthOfLine(i, out length));
string text;
Marshal.ThrowExceptionForHR(lines.GetLineText(i, 0, i, length, out text));
uint[] attrs = new uint[length + 1];
IntPtr p = Marshal.StringToBSTR(text);
Marshal.ThrowExceptionForHR(colorizer.ColorizeLine(i, length, p, uiStartState, attrs));
if (pfFlag == 1)
{
uiStartState = colorizer.GetStateAtEndOfLine(i, length, p, uiStartState);
}
Marshal.ZeroFreeBSTR(p);
_aLineColorizations
= new LineColorization(text, attrs, i, length);
}
colorizer.CloseColorizer();
.....
#2 I try to find some proper relationship beetween
output of IVSColorizer and actual font settings for CodeWindow.
I see no way to convert non-localized name from IVsColorableItem
to localized name to get value from storage(?).
private void initializeColor(Guid guidCLSIDLangService)
{
int hresult;
IVsProvideColorableItems vsProvideColorableItems = (IVsProvideColorableItems)CompareViewUtil.QueryService(_serviceProvider, guidCLSIDLangService, typeof(IVsProvideColorableItems).GUID);
IVsFontAndColorStorage vsFontAndColorStorage = (IVsFontAndColorStorage)CompareViewUtil.QueryService(_serviceProvider, typeof(SVsFontAndColorStorage).GUID, typeof(IVsFontAndColorStorage).GUID);
hresult = vsFontAndColorStorage.OpenCategory(ref GUID_TextEditorFontCategory, (uint)(__FCSTORAGEFLAGS.FCSF_READONLY | __FCSTORAGEFLAGS.FCSF_LOADDEFAULTS));
Marshal.ThrowExceptionForHR(hresult);
int colorItemsCount = 0;
vsProvideColorableItems.GetItemCount(out colorItemsCount);
for (int colorIndex = 1; colorIndex <= colorItemsCount; colorIndex++)
{
IVsColorableItem colorableItem;
Marshal.ThrowExceptionForHR(vsProvideColorableItems.GetColorableItem(colorIndex, out colorableItem));
COLORINDEX[] ciFg = new COLORINDEX[1];
COLORINDEX[] ciBg = new COLORINDEX[1];
Marshal.ThrowExceptionForHR(colorableItem.GetDefaultColors(ciFg, ciBg));
string sName;
colorableItem.GetDisplayName(out sName);
uint fontFlags = 0;
Marshal.ThrowExceptionForHR(colorableItem.GetDefaultFontFlags(out fontFlags));
ColorableItemInfo[] colorableItemInfo = new ColorableItemInfo[1];
hresult = vsFontAndColorStorage.GetItem(sName, colorableItemInfo);
Marshal.ThrowExceptionForHR(hresult);
uint crAutoColorFg, crResultColorFg, crAutoColorBg, crResultColorBg;
_colorUtils.GetRGBOfIndex(ciFg[0], out crAutoColorFg);
_colorUtils.GetRGBOfIndex(ciBg[0], out crAutoColorBg);
if (colorableItemInfo[0].bForegroundValid == 1)
{
_colorUtils.GetRGBOfEncodedColor(
colorableItemInfo[0].crForeground, crAutoColorFg, ref GUID_TextEditorFontCategory, out crResultColorFg);
}
else
{
crResultColorFg = crAutoColorFg;
}
if (colorableItemInfo[0].bBackgroundValid == 1)
{
_colorUtils.GetRGBOfEncodedColor(
colorableItemInfo[0].crBackground, crAutoColorBg, ref GUID_TextEditorFontCategory, out crResultColorBg);
}
else
{
crResultColorBg = crAutoColorBg;
}
_textAttrMapping[colorIndex] = new TextAttributes(
System.Drawing.ColorTranslator.FromWin32((int)crResultColorFg),
System.Drawing.ColorTranslator.FromWin32((int)crResultColorBg),
(fontFlags & (uint)FONTFLAGS.FF_BOLD) != 0);
}
hresult = vsFontAndColorStorage.CloseCategory();
Marshal.ThrowExceptionForHR(hresult);
where _colorUtils is type of IVsFontAndColorUtilities
and TextAttributes is my container class.
Regards