Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
NShaderLanguageService.cs
Go to the documentation of this file.
1 #region Header Licence
2 // ---------------------------------------------------------------------
3 //
4 // Copyright (c) 2009 Alexandre Mutel and Microsoft Corporation.
5 // All rights reserved.
6 //
7 // This code module is part of NShader, a plugin for visual studio
8 // to provide syntax highlighting for shader languages (hlsl, glsl, cg)
9 //
10 // ------------------------------------------------------------------
11 //
12 // This code is licensed under the Microsoft Public License.
13 // See the file License.txt for the license details.
14 // More info on: http://nshader.codeplex.com
15 //
16 // ------------------------------------------------------------------
17 #endregion
18 using System;
19 using System.Collections.Generic;
20 using System.ComponentModel.Composition;
21 using System.Linq;
22 using System.Runtime.InteropServices;
23 using System.Drawing;
24 using EnvDTE80;
25 using Microsoft.VisualStudio;
26 using Microsoft.VisualStudio.Editor;
27 using Microsoft.VisualStudio.Package;
28 using Microsoft.VisualStudio.Shell;
29 using Microsoft.VisualStudio.Shell.Interop;
30 using Microsoft.VisualStudio.Text.Classification;
31 using Microsoft.VisualStudio.Text.Formatting;
32 using Microsoft.VisualStudio.TextManager.Interop;
33 using SiliconStudio.Paradox.VisualStudio.Classifiers;
34 using IOleServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
35 
36 namespace NShader
37 {
39  {
40  private VisualStudioThemeEngine themeEngine;
41  private NShaderColorableItem[] m_colorableItems;
42 
43  private LanguagePreferences m_preferences;
44 
45  public override void Initialize()
46  {
47  base.Initialize();
48 
49  EnsureInitialized();
50  }
51 
52  private void EnsureInitialized()
53  {
54  // Check if already initialized
55  if (m_colorableItems != null)
56  return;
57 
58  // Initialize theme engine
59  themeEngine = new VisualStudioThemeEngine(Site);
60  themeEngine.OnThemeChanged += themeEngine_OnThemeChanged;
61 
62  var currentTheme = themeEngine.GetCurrentTheme();
63 
64  m_colorableItems = new NShaderColorableItem[]
65  {
66  /*1*/ new NShaderColorableItem(currentTheme, "Paradox Shader Language - Keyword", "Paradox Shader Language - Keyword", COLORINDEX.CI_BLUE, COLORINDEX.CI_AQUAMARINE, COLORINDEX.CI_USERTEXT_BK, Color.Empty, Color.FromArgb(86, 156, 214), Color.Empty, FONTFLAGS.FF_DEFAULT),
67  /*2*/ new NShaderColorableItem(currentTheme, "Paradox Shader Language - Comment", "Paradox Shader Language - Comment", COLORINDEX.CI_DARKGREEN, COLORINDEX.CI_GREEN, COLORINDEX.CI_USERTEXT_BK, Color.Empty, Color.FromArgb(87, 166, 74), Color.Empty, FONTFLAGS.FF_DEFAULT),
68  /*3*/ new NShaderColorableItem(currentTheme, "Paradox Shader Language - Identifier", "Paradox Shader Language - Identifier", COLORINDEX.CI_SYSPLAINTEXT_FG, COLORINDEX.CI_SYSPLAINTEXT_FG, COLORINDEX.CI_USERTEXT_BK, FONTFLAGS.FF_DEFAULT),
69  /*4*/ new NShaderColorableItem(currentTheme, "Paradox Shader Language - String", "Paradox Shader Language - String", COLORINDEX.CI_RED, COLORINDEX.CI_RED, COLORINDEX.CI_USERTEXT_BK, Color.Empty, Color.FromArgb(214, 157, 133), Color.Empty, FONTFLAGS.FF_DEFAULT),
70  /*5*/ new NShaderColorableItem(currentTheme, "Paradox Shader Language - Number", "Paradox Shader Language - Number", COLORINDEX.CI_DARKBLUE, COLORINDEX.CI_BLUE, COLORINDEX.CI_USERTEXT_BK, Color.Empty, Color.FromArgb(181, 206, 168), Color.Empty, FONTFLAGS.FF_DEFAULT),
71  /*6*/ new NShaderColorableItem(currentTheme, "Paradox Shader Language - Intrinsic", "Paradox Shader Language - Intrinsic", COLORINDEX.CI_MAROON, COLORINDEX.CI_CYAN, COLORINDEX.CI_USERTEXT_BK, Color.Empty, Color.FromArgb(239, 242, 132), Color.Empty, FONTFLAGS.FF_BOLD),
72  /*7*/ new NShaderColorableItem(currentTheme, "Paradox Shader Language - Special", "Paradox Shader Language - Special", COLORINDEX.CI_AQUAMARINE, COLORINDEX.CI_MAGENTA, COLORINDEX.CI_USERTEXT_BK, Color.Empty, Color.FromArgb(78, 201, 176), Color.Empty, FONTFLAGS.FF_DEFAULT),
73  /*8*/ new NShaderColorableItem(currentTheme, "Paradox Shader Language - Preprocessor", "Paradox Shader Language - Preprocessor", COLORINDEX.CI_DARKGRAY, COLORINDEX.CI_LIGHTGRAY, COLORINDEX.CI_USERTEXT_BK, Color.Empty, Color.FromArgb(155, 155, 155), Color.Empty, FONTFLAGS.FF_DEFAULT),
74  };
75  }
76 
77  public override void Dispose()
78  {
79  themeEngine.OnThemeChanged -= themeEngine_OnThemeChanged;
80  themeEngine.Dispose();
81 
82  base.Dispose();
83  }
84 
85  void themeEngine_OnThemeChanged(object sender, EventArgs e)
86  {
87  var colorUtilities = Site.GetService(typeof(SVsFontAndColorStorage)) as IVsFontAndColorUtilities;
88  var currentTheme = themeEngine.GetCurrentTheme();
89 
90  var store = Package.GetGlobalService(typeof(SVsFontAndColorStorage)) as IVsFontAndColorStorage;
91  store.OpenCategory(DefGuidList.guidTextEditorFontCategory, (uint)(__FCSTORAGEFLAGS.FCSF_LOADDEFAULTS | __FCSTORAGEFLAGS.FCSF_NOAUTOCOLORS | __FCSTORAGEFLAGS.FCSF_PROPAGATECHANGES));
92 
93  // Update each colorable item
94  foreach (var colorableItem in m_colorableItems)
95  {
96  // Get display name of setting
97  string displayName;
98  colorableItem.GetDisplayName(out displayName);
99 
100  // Get new color
101  var hiColor = currentTheme == VisualStudioTheme.Dark ? colorableItem.HiForeColorDark : colorableItem.HiForeColorLight;
102  var colorIndex = currentTheme == VisualStudioTheme.Dark ? colorableItem.ForeColorDark : colorableItem.ForeColorLight;
103 
104  uint color;
105  if (hiColor != Color.Empty)
106  color = hiColor.R | ((uint)hiColor.G << 8) | ((uint)hiColor.B << 16);
107  else
108  colorUtilities.EncodeIndexedColor(colorIndex, out color);
109 
110  // Update color in settings
111  store.SetItem(displayName, new[] { new ColorableItemInfo { bForegroundValid = 1, crForeground = color } });
112  }
113  }
114 
115  public override int GetItemCount(out int count)
116  {
117  count = m_colorableItems.Length;
118  return VSConstants.S_OK;
119  }
120 
121  public override int GetColorableItem(int index, out IVsColorableItem item)
122  {
123  if (index < 1)
124  {
125  throw new ArgumentOutOfRangeException("index");
126  }
127 
128  item = m_colorableItems[index-1];
129  return VSConstants.S_OK;
130  }
131 
132  public override LanguagePreferences GetLanguagePreferences()
133  {
134  if (m_preferences == null)
135  {
136  m_preferences = new LanguagePreferences(this.Site,
137  typeof(NShaderLanguageService).GUID,
138  this.Name);
139  m_preferences.Init();
140  }
141  return m_preferences;
142  }
143 
144  public override IScanner GetScanner(IVsTextLines buffer)
145  {
146  string filePath = FilePathUtilities.GetFilePath(buffer);
147  // Return dynamic scanner based on file extension
148  return NShaderScannerFactory.GetShaderScanner(filePath);
149  }
150 
151  public override Source CreateSource(IVsTextLines buffer)
152  {
153  return new NShaderSource(this, buffer, GetColorizer(buffer));
154  }
155 
156  public override Colorizer GetColorizer(IVsTextLines buffer)
157  {
158  EnsureInitialized();
159 
160  // Clear font cache
161  // http://social.msdn.microsoft.com/Forums/office/en-US/54064c52-727d-4015-af70-c72e44d116a7/vs2012-fontandcolors-text-editor-category-for-language-service-colors?forum=vsx
162  IVsFontAndColorStorage storage;
163  Guid textMgrIID = new Guid(
164 //#if VISUALSTUDIO_11_0
165  "{E0187991-B458-4F7E-8CA9-42C9A573B56C}" /* 'Text Editor Language Services Items' category discovered in the registry. Resetting TextEditor has no effect. */
166 //#else
167 // FontsAndColorsCategory.TextEditor
168 //#endif
169  );
170  if (null != (storage = GetService(typeof(IVsFontAndColorStorage)) as IVsFontAndColorStorage) &&
171  VSConstants.S_OK == storage.OpenCategory(ref textMgrIID, (uint)(__FCSTORAGEFLAGS.FCSF_READONLY | __FCSTORAGEFLAGS.FCSF_LOADDEFAULTS)))
172  {
173  bool missingColor = false;
174  try
175  {
176  ColorableItemInfo[] info = new ColorableItemInfo[1];
177  for (int i = 0; i < m_colorableItems.Length; ++i)
178  {
179  string colorName;
180  m_colorableItems[i].GetDisplayName(out colorName);
181  if (ErrorHandler.Failed(storage.GetItem(colorName, info)))
182  {
183  missingColor = true;
184  break;
185  }
186  }
187  }
188  finally
189  {
190  storage.CloseCategory();
191  }
192  if (missingColor)
193  {
194  IOleServiceProvider oleProvider;
195  // The service and interface guids are different, so we need to go to the OLE layer to get the service
196  Guid iid = typeof(IVsFontAndColorCacheManager).GUID;
197  Guid sid = typeof(SVsFontAndColorCacheManager).GUID;
198  IntPtr pCacheManager;
199  if (null != (oleProvider = GetService(typeof(IOleServiceProvider)) as IOleServiceProvider) &&
200  VSConstants.S_OK == oleProvider.QueryService(ref sid, ref iid, out pCacheManager) &&
201  pCacheManager != IntPtr.Zero)
202  {
203  try
204  {
205  IVsFontAndColorCacheManager cacheManager = (IVsFontAndColorCacheManager)Marshal.GetObjectForIUnknown(pCacheManager);
206  cacheManager.ClearCache(ref textMgrIID);
207  }
208  finally
209  {
210  Marshal.Release(pCacheManager);
211  }
212  }
213  }
214  }
215 
216  return base.GetColorizer(buffer);
217  }
218 
219  public override AuthoringScope ParseSource(ParseRequest req)
220  {
221  // req.FileName
222  return new TestAuthoringScope();
223  }
224 
225  public override string GetFormatFilterList()
226  {
227  return "";
228  }
229 
230  public override string Name
231  {
232  get { return "Paradox Shader Language"; }
233  }
234 
235  internal class TestAuthoringScope : AuthoringScope
236  {
237  public override string GetDataTipText(int line, int col, out TextSpan span)
238  {
239  span = new TextSpan();
240  return null;
241  }
242 
243  public override Declarations GetDeclarations(IVsTextView view,
244  int line,
245  int col,
246  TokenInfo info,
247  ParseReason reason)
248  {
249  return null;
250  }
251 
252  public override Methods GetMethods(int line, int col, string name)
253  {
254  return null;
255  }
256 
257  public override string Goto(VSConstants.VSStd97CmdID cmd, IVsTextView textView, int line, int col, out TextSpan span)
258  {
259  span = new TextSpan();
260  return null;
261  }
262  }
263 
264  }
265 }
override IScanner GetScanner(IVsTextLines buffer)
Microsoft.VisualStudio.OLE.Interop.IServiceProvider IOleServiceProvider
override int GetItemCount(out int count)
override AuthoringScope ParseSource(ParseRequest req)
override Colorizer GetColorizer(IVsTextLines buffer)
_In_ size_t count
Definition: DirectXTexP.h:174
SiliconStudio.Core.Mathematics.Color Color
Definition: ColorPicker.cs:14
override int GetColorableItem(int index, out IVsColorableItem item)
override Source CreateSource(IVsTextLines buffer)
override LanguagePreferences GetLanguagePreferences()