PageRenderTime 58ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/LuaLangPack/LuaLangPack/VsPkg.cs

https://github.com/luaforge/lualangpack
C# | 296 lines | 212 code | 38 blank | 46 comment | 13 complexity | e64a69a8a8ffbf7f17170999cac16a38 MD5 | raw file
  1. // VsPkg.cs : Implementation of LuaLangPack
  2. //
  3. using System;
  4. using System.Diagnostics;
  5. using System.Globalization;
  6. using System.Runtime.InteropServices;
  7. using System.ComponentModel.Design;
  8. using Microsoft.Win32;
  9. using Microsoft.VisualStudio;
  10. using Microsoft.VisualStudio.Shell.Interop;
  11. using Microsoft.VisualStudio.OLE.Interop;
  12. using Microsoft.VisualStudio.Shell;
  13. using Microsoft.VisualStudio.Package;
  14. using Microsoft.VisualStudio.TextManager.Interop;
  15. using EnvDTE;
  16. using IOleServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
  17. [assembly:ComVisible(true)]
  18. namespace Vsip.LuaLangPack
  19. {
  20. /// <summary>
  21. /// This is the class that implements the package exposed by this assembly.
  22. ///
  23. /// The minimum requirement for a class to be considered a valid package for Visual Studio
  24. /// is to implement the IVsPackage interface and register itself with the shell.
  25. /// This package uses the helper classes defined inside the Managed Package Framework (MPF)
  26. /// to do it: it derives from the Package class that provides the implementation of the
  27. /// IVsPackage interface and uses the registration attributes defined in the framework to
  28. /// register itself and its components with the shell.
  29. /// // In order be loaded inside Visual Studio in a machine that has not the VS SDK installed,
  30. // package needs to have a valid load key (it can be requested at
  31. // http://msdn.microsoft.com/vstudio/extend/). This attributes tells the shell that this
  32. // package has a load key embedded in its resources.
  33. // A Visual Studio component can be registered under different regitry roots; for instance
  34. // when you debug your package you want to register it in the experimental hive. This
  35. // attribute specifies the registry root to use if no one is provided to regpkg.exe with
  36. // the /root switch.
  37. /// </summary>
  38. [PackageRegistration(UseManagedResourcesOnly = true)]
  39. [DefaultRegistryRoot("Software\\Microsoft\\VisualStudio\\8.0Exp")]
  40. [InstalledProductRegistration(true, "Lua Language Integration Pack", "Provides Lua 5.0 language integration with Visual Studio", "0.3", IconResourceID = 400)]
  41. [ProvideLoadKey("Standard", "0.3", "Lua Language Integration Pack", "trystan.org", 110)]
  42. [ProvideProjectFactory(typeof(LuaProjectFactory), "Lua Project", "LuaProject Project Files (*.luaproj);*.luaproj", "luaproj", "luaproj", "..\\Templates\\Projects")]
  43. [ProvideProjectItem(typeof(LuaProjectFactory), "Lua Items", "..\\Templates\\ProjectItems", 500)]
  44. [ProvideServiceAttribute(typeof(LuaLangService), ServiceName = "VS 2005 Lua Language Service")]
  45. [ProvideLanguageServiceAttribute(typeof(LuaLangService), "VS 2005 Lua Language Service", 103, CodeSense = true, AutoOutlining = true, EnableAsyncCompletion = true, EnableCommenting = true)]
  46. [ProvideLanguageExtensionAttribute(typeof(LuaLangService),".lua")]
  47. [ProvideObject(typeof(LuaPropertyPage))]
  48. [Guid("b4d22fe4-9555-4288-a8f9-70ed3119eaad")]
  49. public sealed class LuaLangPack : Package, IOleComponent
  50. {
  51. private uint m_componentID;
  52. /// <summary>
  53. /// Default constructor of the package.
  54. /// Inside this method you can place any initialization code that does not require
  55. /// any Visual Studio service because at this point the package object is created but
  56. /// not sited yet inside Visual Studio environment. The place to do all the other
  57. /// initialization is the Initialize method.
  58. /// </summary>
  59. public LuaLangPack()
  60. {
  61. Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering constructor for: {0}", this.ToString()));
  62. }
  63. protected override void Initialize()
  64. {
  65. base.Initialize(); // required
  66. this.RegisterProjectFactory(new LuaProjectFactory(this));
  67. // Proffer the service.
  68. IServiceContainer serviceContainer = this as IServiceContainer;
  69. LuaLangService langService = new LuaLangService();
  70. langService.SetSite(this);
  71. serviceContainer.AddService(typeof(LuaLangService),
  72. langService,
  73. true);
  74. // Register a timer to call our language service during
  75. // idle periods.
  76. IOleComponentManager mgr = GetService(typeof(SOleComponentManager))
  77. as IOleComponentManager;
  78. if (m_componentID == 0 && mgr != null)
  79. {
  80. OLECRINFO[] crinfo = new OLECRINFO[1];
  81. crinfo[0].cbSize = (uint)Marshal.SizeOf(typeof(OLECRINFO));
  82. crinfo[0].grfcrf = (uint)_OLECRF.olecrfNeedIdleTime |
  83. (uint)_OLECRF.olecrfNeedPeriodicIdleTime;
  84. crinfo[0].grfcadvf = (uint)_OLECADVF.olecadvfModal |
  85. (uint)_OLECADVF.olecadvfRedrawOff |
  86. (uint)_OLECADVF.olecadvfWarningsOff;
  87. crinfo[0].uIdleTimeInterval = 1000;
  88. int hr = mgr.FRegisterComponent(this, crinfo, out m_componentID);
  89. }
  90. Trace.WriteLine( "LuaLangPack: Finished Initialization" );
  91. }
  92. protected override void Dispose(bool disposing)
  93. {
  94. if (m_componentID != 0)
  95. {
  96. IOleComponentManager mgr = GetService(typeof(SOleComponentManager))
  97. as IOleComponentManager;
  98. if (mgr != null)
  99. {
  100. int hr = mgr.FRevokeComponent(m_componentID);
  101. }
  102. m_componentID = 0;
  103. }
  104. base.Dispose(disposing);
  105. }
  106. #region IOleComponent Members
  107. public int FDoIdle(uint grfidlef)
  108. {
  109. bool bPeriodic = (grfidlef & (uint)_OLEIDLEF.oleidlefPeriodic) != 0;
  110. // Use typeof(MyLanguageService) because we need to
  111. // reference the GUID for our language service.
  112. LanguageService service = GetService(typeof(LuaLangService))
  113. as LanguageService;
  114. if (service != null)
  115. {
  116. service.OnIdle(bPeriodic);
  117. }
  118. return 0;
  119. }
  120. public int FContinueMessageLoop(uint uReason,
  121. IntPtr pvLoopData,
  122. MSG[] pMsgPeeked)
  123. {
  124. return 1;
  125. }
  126. public int FPreTranslateMessage(MSG[] pMsg)
  127. {
  128. return 0;
  129. }
  130. public int FQueryTerminate(int fPromptUser)
  131. {
  132. return 1;
  133. }
  134. public int FReserved1(uint dwReserved,
  135. uint message,
  136. IntPtr wParam,
  137. IntPtr lParam)
  138. {
  139. return 1;
  140. }
  141. public IntPtr HwndGetWindow(uint dwWhich, uint dwReserved)
  142. {
  143. return IntPtr.Zero;
  144. }
  145. public void OnActivationChange(IOleComponent pic,
  146. int fSameComponent,
  147. OLECRINFO[] pcrinfo,
  148. int fHostIsActivating,
  149. OLECHOSTINFO[] pchostinfo,
  150. uint dwReserved)
  151. {
  152. }
  153. public void OnAppActivate(int fActive, uint dwOtherThreadID)
  154. {
  155. }
  156. public void OnEnterState(uint uStateID, int fEnter)
  157. {
  158. }
  159. public void OnLoseActivation()
  160. {
  161. }
  162. public void Terminate()
  163. {
  164. }
  165. #endregion
  166. /// <summary>
  167. /// Helper function that will load a resource string using the standard Visual Studio Resource Manager
  168. /// Service (SVsResourceManager). Because of the fact that it is using a service, this method can be
  169. /// called only after the package is sited.
  170. /// </summary>
  171. internal static string GetResourceString(string resourceName)
  172. {
  173. string resourceValue;
  174. IVsResourceManager resourceManager = (IVsResourceManager)GetGlobalService(typeof(SVsResourceManager));
  175. if (resourceManager == null)
  176. {
  177. throw new InvalidOperationException("Could not get SVsResourceManager service. Make sure the package is Sited before calling this method.");
  178. }
  179. Guid packageGuid = typeof(LuaLangPack).GUID;
  180. int hr = resourceManager.LoadResourceString(ref packageGuid, -1, resourceName, out resourceValue);
  181. Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(hr);
  182. return resourceValue;
  183. }
  184. internal static string GetResourceString(int resourceID)
  185. {
  186. return GetResourceString(string.Format("@{0}", resourceID));
  187. }
  188. }
  189. [GuidAttribute("74F2664D-668E-4058-AF23-3C959863AEE8")]
  190. public class LuaProjectFactory : Microsoft.VisualStudio.Package.ProjectFactory
  191. {
  192. public LuaProjectFactory(LuaLangPack package) : base(package)
  193. {
  194. Trace.WriteLine("LuaLangPack: Project Factory Construction");
  195. }
  196. protected override Microsoft.VisualStudio.Package.ProjectNode CreateProject()
  197. {
  198. LuaPrj project = new LuaPrj(this.Package as LuaLangPack);
  199. project.SetSite((IOleServiceProvider)((System.IServiceProvider)this.Package).GetService(typeof(IOleServiceProvider)));
  200. Trace.WriteLine("LuaLangPack: Create Project");
  201. return project;
  202. }
  203. }
  204. [Guid("B416BD01-3431-4262-87D4-977196E5E832")]
  205. public class LuaPrj : Microsoft.VisualStudio.Package.ProjectNode
  206. {
  207. private LuaLangPack package;
  208. public LuaPrj(LuaLangPack pkg)
  209. {
  210. Trace.WriteLine("LuaLangPack: Project Node Construction");
  211. this.package = pkg;
  212. }
  213. public override Guid ProjectGuid
  214. {
  215. get
  216. {
  217. return typeof(LuaProjectFactory).GUID;
  218. }
  219. }
  220. public override string ProjectType
  221. {
  222. get
  223. {
  224. return "LuaPrj";
  225. }
  226. }
  227. public override int ImageIndex
  228. {
  229. get { return NoImage; }
  230. }
  231. public override object GetIconHandle(bool open)
  232. {
  233. return typeof(ProjectNode).Assembly.GetManifestResourceStream("Resources.Lua Project.ico");
  234. }
  235. protected override Guid[] GetConfigurationIndependentPropertyPages()
  236. {
  237. Trace.WriteLine("LuaLangPack: Prop Page Guids");
  238. Guid[] result = new Guid[1];
  239. result[0] = typeof(LuaPropertyPage).GUID;
  240. return result;
  241. }
  242. /// <summary>
  243. /// Returns the configuration dependent property pages.
  244. /// Specify here a property page. By returning no property page the configuartion dependent properties will be neglected.
  245. /// Overriding, but current implementation does nothing
  246. /// To provide configuration specific page project property page, this should return an array bigger then 0
  247. /// (you can make it do the same as GetPropertyPageGuids() to see its impact)
  248. /// </summary>
  249. /// <param name="config"></param>
  250. /// <returns></returns>
  251. protected override Guid[] GetConfigurationDependentPropertyPages()
  252. {
  253. Trace.WriteLine("LuaLangPack: Conf Dependent Prop Page Guids");
  254. Guid[] result = new Guid[0];
  255. return result;
  256. }
  257. }
  258. }