PageRenderTime 27ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/Tools/IronStudio/IronStudio/IronStudio/Navigation/TextViewWrapper.cs

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