PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/2.0/Source/Orcas/LanguageService/_ContainedLanguage/TextViewWrapper.cs

#
C# | 327 lines | 260 code | 52 blank | 15 comment | 18 complexity | 248b9d909371ad7022868733b5625ce6 MD5 | raw file
Possible License(s): CPL-1.0, GPL-2.0, CC-BY-SA-3.0, MPL-2.0-no-copyleft-exception, Apache-2.0
  1. /***************************************************************************
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. This code is licensed under the Visual Studio SDK license terms.
  4. THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
  5. ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
  6. IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
  7. PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
  8. ***************************************************************************/
  9. using System;
  10. using System.Runtime.InteropServices;
  11. using Microsoft.VisualStudio.OLE.Interop;
  12. using Microsoft.VisualStudio.TextManager.Interop;
  13. using VSConstants = Microsoft.VisualStudio.VSConstants;
  14. using ErrorHandler = Microsoft.VisualStudio.ErrorHandler;
  15. namespace PHP.VisualStudio.PhalangerLanguageService {
  16. internal class TextViewWrapper : IVsTextView, IConnectionPointContainer {
  17. // This class is an helper class that provides an empty implementation of
  18. // IConnectionPoint. Is is needed only because some client of the text view
  19. // need to subscribe for events, but this implementation does not raise
  20. // any event.
  21. private class TextViewConnectionPoint : IConnectionPoint {
  22. private uint nextCoockie;
  23. #region IConnectionPoint Members
  24. public void Advise(object pUnkSink, out uint pdwCookie) {
  25. pdwCookie = ++nextCoockie;
  26. }
  27. public void EnumConnections(out IEnumConnections ppEnum) {
  28. throw new NotImplementedException();
  29. }
  30. public void GetConnectionInterface(out Guid pIID) {
  31. throw new NotImplementedException();
  32. }
  33. public void GetConnectionPointContainer(out IConnectionPointContainer ppCPC) {
  34. throw new NotImplementedException();
  35. }
  36. public void Unadvise(uint dwCookie) {
  37. // Do Nothing
  38. }
  39. #endregion
  40. }
  41. private IVsIntellisenseHost intellisenseHost;
  42. private IVsContainedLanguageHost languageHost;
  43. private IVsTextBufferCoordinator bufferCoordinator;
  44. private IOleCommandTarget nextTarget;
  45. private IOleCommandTarget installedFilter;
  46. internal TextViewWrapper(IVsContainedLanguageHost languageHost, IVsIntellisenseHost intellisenseHost, IVsTextBufferCoordinator coordinator, IOleCommandTarget nextTarget) {
  47. if (null == intellisenseHost) {
  48. throw new ArgumentNullException("buffer");
  49. }
  50. this.intellisenseHost = intellisenseHost;
  51. this.bufferCoordinator = coordinator;
  52. this.languageHost = languageHost;
  53. this.nextTarget = nextTarget;
  54. }
  55. internal IVsTextViewFilter InstalledFilter {
  56. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  57. get {
  58. return installedFilter as IVsTextViewFilter;
  59. }
  60. }
  61. internal TextSpan GetPrimarySpan(TextSpan secondary) {
  62. TextSpan[] primary = new TextSpan[1];
  63. ErrorHandler.ThrowOnFailure(bufferCoordinator.MapSecondaryToPrimarySpan(secondary, primary));
  64. return primary[0];
  65. }
  66. #region IVsTextView Members
  67. public int AddCommandFilter(IOleCommandTarget pNewCmdTarg, out IOleCommandTarget ppNextCmdTarg) {
  68. ppNextCmdTarg = nextTarget;
  69. installedFilter = pNewCmdTarg;
  70. return VSConstants.S_OK;
  71. }
  72. public int CenterColumns(int iLine, int iLeftCol, int iColCount) {
  73. return VSConstants.S_OK;
  74. }
  75. public int CenterLines(int iTopLine, int iCount) {
  76. return VSConstants.S_OK;
  77. }
  78. public int ClearSelection(int fMoveToAnchor) {
  79. throw new NotImplementedException();
  80. }
  81. public int CloseView() {
  82. this.intellisenseHost = null;
  83. this.bufferCoordinator = null;
  84. this.languageHost = null;
  85. return VSConstants.S_OK;
  86. }
  87. public int EnsureSpanVisible(TextSpan span) {
  88. if (null == this.languageHost) {
  89. return VSConstants.S_OK;
  90. }
  91. return this.languageHost.EnsureSpanVisible(span);
  92. }
  93. public int GetBuffer(out IVsTextLines ppBuffer) {
  94. return this.bufferCoordinator.GetSecondaryBuffer(out ppBuffer);
  95. }
  96. public int GetCaretPos(out int piLine, out int piColumn) {
  97. TextSpan originalSpan = new TextSpan();
  98. ErrorHandler.ThrowOnFailure(
  99. this.intellisenseHost.GetContextCaretPos(out originalSpan.iStartLine, out originalSpan.iStartIndex));
  100. originalSpan.iEndLine = originalSpan.iStartLine;
  101. originalSpan.iEndIndex = originalSpan.iStartIndex;
  102. TextSpan[] convertedSpan = new TextSpan[1];
  103. ErrorHandler.ThrowOnFailure(bufferCoordinator.MapPrimaryToSecondarySpan(originalSpan, convertedSpan));
  104. piLine = convertedSpan[0].iStartLine;
  105. piColumn = convertedSpan[0].iStartIndex;
  106. return VSConstants.S_OK;
  107. }
  108. public int GetLineAndColumn(int iPos, out int piLine, out int piIndex) {
  109. IVsTextLines buffer;
  110. ErrorHandler.ThrowOnFailure(bufferCoordinator.GetSecondaryBuffer(out buffer));
  111. return buffer.GetLineIndexOfPosition(iPos, out piLine, out piIndex);
  112. }
  113. public int GetLineHeight(out int piLineHeight) {
  114. throw new NotImplementedException();
  115. }
  116. public int GetNearestPosition(int iLine, int iCol, out int piPos, out int piVirtualSpaces) {
  117. throw new NotImplementedException();
  118. }
  119. public int GetPointOfLineColumn(int iLine, int iCol, Microsoft.VisualStudio.OLE.Interop.POINT[] ppt) {
  120. throw new NotImplementedException();
  121. }
  122. public int GetScrollInfo(int iBar, out int piMinUnit, out int piMaxUnit, out int piVisibleUnits, out int piFirstVisibleUnit) {
  123. throw new NotImplementedException();
  124. }
  125. public int GetSelectedText(out string pbstrText) {
  126. TextSpan[] span = new TextSpan[1];
  127. ErrorHandler.ThrowOnFailure(intellisenseHost.GetContextSelection(span));
  128. TextSpan[] convertedSpan = new TextSpan[1];
  129. ErrorHandler.ThrowOnFailure(bufferCoordinator.MapPrimaryToSecondarySpan(span[0], convertedSpan));
  130. IVsTextLines buffer;
  131. ErrorHandler.ThrowOnFailure(bufferCoordinator.GetSecondaryBuffer(out buffer));
  132. return buffer.GetLineText(convertedSpan[0].iStartLine, convertedSpan[0].iStartIndex, convertedSpan[0].iEndLine, convertedSpan[0].iEndIndex, out pbstrText);
  133. }
  134. public int GetSelection(out int piAnchorLine, out int piAnchorCol, out int piEndLine, out int piEndCol) {
  135. TextSpan[] span = new TextSpan[1];
  136. ErrorHandler.ThrowOnFailure(intellisenseHost.GetContextSelection(span));
  137. TextSpan[] convertedSpan = new TextSpan[1];
  138. ErrorHandler.ThrowOnFailure(bufferCoordinator.MapPrimaryToSecondarySpan(span[0], convertedSpan));
  139. piAnchorLine = convertedSpan[0].iStartLine;
  140. piAnchorCol = convertedSpan[0].iStartIndex;
  141. piEndLine = convertedSpan[0].iEndLine;
  142. piEndCol = convertedSpan[0].iEndIndex;
  143. return VSConstants.S_OK;
  144. }
  145. public int GetSelectionDataObject(out Microsoft.VisualStudio.OLE.Interop.IDataObject ppIDataObject) {
  146. throw new NotImplementedException();
  147. }
  148. public TextSelMode GetSelectionMode() {
  149. throw new NotImplementedException();
  150. }
  151. public int GetSelectionSpan(TextSpan[] pSpan) {
  152. TextSpan[] primarySpan = new TextSpan[1];
  153. ErrorHandler.ThrowOnFailure(intellisenseHost.GetContextSelection(primarySpan));
  154. return bufferCoordinator.MapPrimaryToSecondarySpan(primarySpan[0], pSpan);
  155. }
  156. public int GetTextStream(int iTopLine, int iTopCol, int iBottomLine, int iBottomCol, out string pbstrText) {
  157. throw new NotImplementedException();
  158. }
  159. public IntPtr GetWindowHandle() {
  160. IntPtr hWnd;
  161. ErrorHandler.ThrowOnFailure(intellisenseHost.GetHostWindow(out hWnd));
  162. return hWnd;
  163. }
  164. public int GetWordExtent(int iLine, int iCol, uint dwFlags, TextSpan[] pSpan) {
  165. throw new NotImplementedException();
  166. }
  167. public int HighlightMatchingBrace(uint dwFlags, uint cSpans, TextSpan[] rgBaseSpans) {
  168. if ((null == rgBaseSpans) || (rgBaseSpans.Length == 0)) {
  169. throw new ArgumentNullException("rgBaseSpans");
  170. }
  171. if ((uint)rgBaseSpans.Length != cSpans) {
  172. throw new System.ArgumentOutOfRangeException("cSpans");
  173. }
  174. TextSpan[] convertedSpans = new TextSpan[cSpans];
  175. TextSpan[] workingCopy = new TextSpan[1];
  176. for (int i = 0; i < cSpans; ++i) {
  177. ErrorHandler.ThrowOnFailure(bufferCoordinator.MapSecondaryToPrimarySpan(rgBaseSpans[i], workingCopy));
  178. convertedSpans[i] = workingCopy[0];
  179. }
  180. return intellisenseHost.HighlightMatchingBrace(dwFlags, cSpans, convertedSpans);
  181. }
  182. public int Initialize(IVsTextLines pBuffer, IntPtr hwndParent, uint InitFlags, INITVIEW[] pInitView) {
  183. return VSConstants.S_OK;
  184. }
  185. public int PositionCaretForEditing(int iLine, int cIndentLevels) {
  186. throw new NotImplementedException();
  187. }
  188. public int RemoveCommandFilter(Microsoft.VisualStudio.OLE.Interop.IOleCommandTarget pCmdTarg) {
  189. this.installedFilter = null;
  190. return VSConstants.S_OK;
  191. }
  192. public int ReplaceTextOnLine(int iLine, int iStartCol, int iCharsToReplace, string pszNewText, int iNewLen) {
  193. IVsTextLines buffer;
  194. ErrorHandler.ThrowOnFailure(bufferCoordinator.GetSecondaryBuffer(out buffer));
  195. GCHandle handle = GCHandle.Alloc(pszNewText, GCHandleType.Pinned);
  196. try {
  197. TextSpan[] span = new TextSpan[1];
  198. IntPtr textPtr = handle.AddrOfPinnedObject();
  199. int textLen = string.IsNullOrEmpty(pszNewText) ? 0 : pszNewText.Length;
  200. ErrorHandler.ThrowOnFailure(
  201. buffer.ReplaceLines(iLine, iStartCol, iLine, iStartCol + iCharsToReplace, textPtr, textLen, span));
  202. } finally {
  203. if ((null != handle) && (handle.IsAllocated)) {
  204. handle.Free();
  205. }
  206. }
  207. return VSConstants.S_OK;
  208. }
  209. public int RestrictViewRange(int iMinLine, int iMaxLine, IVsViewRangeClient pClient) {
  210. throw new NotImplementedException();
  211. }
  212. public int SendExplicitFocus() {
  213. throw new NotImplementedException();
  214. }
  215. public int SetBuffer(IVsTextLines pBuffer) {
  216. return VSConstants.S_OK;
  217. }
  218. public int SetCaretPos(int iLine, int iColumn) {
  219. TextSpan original = new TextSpan();
  220. original.iStartLine = original.iEndLine = iLine;
  221. original.iStartIndex = original.iEndIndex = iColumn;
  222. TextSpan[] converted = new TextSpan[1];
  223. ErrorHandler.ThrowOnFailure(bufferCoordinator.MapSecondaryToPrimarySpan(original, converted));
  224. return intellisenseHost.SetContextCaretPos(converted[0].iStartLine, converted[0].iStartIndex);
  225. }
  226. public int SetScrollPosition(int iBar, int iFirstVisibleUnit) {
  227. throw new NotImplementedException();
  228. }
  229. public int SetSelection(int iAnchorLine, int iAnchorCol, int iEndLine, int iEndCol) {
  230. TextSpan original = new TextSpan();
  231. original.iStartLine = iAnchorLine;
  232. original.iStartIndex = iAnchorCol;
  233. original.iEndLine = iEndLine;
  234. original.iEndIndex = iEndCol;
  235. TextSpan[] converted = new TextSpan[1];
  236. ErrorHandler.ThrowOnFailure(bufferCoordinator.MapSecondaryToPrimarySpan(original, converted));
  237. return intellisenseHost.SetContextSelection(converted[0].iStartLine, converted[0].iStartIndex, converted[0].iEndLine, converted[0].iEndIndex);
  238. }
  239. public int SetSelectionMode(TextSelMode iSelMode) {
  240. throw new NotImplementedException();
  241. }
  242. public int SetTopLine(int iBaseLine) {
  243. throw new NotImplementedException();
  244. }
  245. public int UpdateCompletionStatus(IVsCompletionSet pCompSet, uint dwFlags) {
  246. return intellisenseHost.UpdateCompletionStatus(pCompSet, dwFlags);
  247. }
  248. public int UpdateTipWindow(IVsTipWindow pTipWindow, uint dwFlags) {
  249. return intellisenseHost.UpdateTipWindow(pTipWindow, dwFlags);
  250. }
  251. public int UpdateViewFrameCaption() {
  252. return VSConstants.S_OK;
  253. }
  254. #endregion
  255. #region IConnectionPointContainer Members
  256. void IConnectionPointContainer.EnumConnectionPoints(out IEnumConnectionPoints ppEnum) {
  257. throw new NotImplementedException();
  258. }
  259. private TextViewConnectionPoint connectionPoint;
  260. void IConnectionPointContainer.FindConnectionPoint(ref Guid riid, out IConnectionPoint ppCP) {
  261. ppCP = null;
  262. if (typeof(IVsTextViewEvents).GUID == riid) {
  263. if (null == connectionPoint) {
  264. connectionPoint = new TextViewConnectionPoint();
  265. }
  266. ppCP = connectionPoint;
  267. return;
  268. }
  269. throw new ArgumentOutOfRangeException("riid");
  270. }
  271. #endregion
  272. }
  273. }