internal static void MarkExpression(Marker marker) { if (!InitCalled) { return; } TextDocument textDocument = (TextDocument)marker.document.Object("TextDocument"); TextSelection textSelection = textDocument.Selection; textSelection.MoveTo(marker.startLine, marker.startCol, false); textSelection.MoveTo(marker.endLine, marker.endCol, true); }
void MoveTo(string fileName,int line,int column) { if (File.Exists(fileName)) { try { OpenFile(fileName); TextSelection ts = m_dte.ActiveDocument.Selection as TextSelection; TextDocument textDoc = ts.Parent; if (line > textDoc.EndPoint.Line) { line = textDoc.EndPoint.Line; column = 1; } if (ts != null && line > 0 && textDoc != null) { ts.MoveTo(line,column); } } catch (Exception) { Logger.Debug("Go to page fail."); } } }
/// <summary> /// 智能分号: /// * 如果在一行的中间任何位置按下分号,则跳到行结尾添加分号,如果行未已经有一个分号则不重复添加 /// * 再按一次分号,则删除在行尾添加的分号,回到刚才的位置插入一个分号 /// </summary> /// <param name="keypress"></param> /// <param name="selection"></param> /// <param name="inStatementCompletion"></param> /// <param name="cancelKeypress"></param> public bool BeforeKeyPress(string keypress, TextSelection selection, bool inStatementCompletion, ref bool cancelKeypress) { if (!selection.IsEmpty || keypress != ";") { _smartSemicolonFallback = false; return(false); } if (_smartSemicolonFallback && selection.CurrentLine == _smartSemicolonLine && selection.ActivePoint.AtEndOfLine && selection.ActivePoint.CreateEditPoint().GetText(-1) == ";") { // 重复输入分号, 删除原来行尾的分号,并退回到原位置插入分号 if (_smartSemicolonDeleteLineEnd) { selection.DeleteLeft(1); } selection.MoveTo(_smartSemicolonLine, _smartSemicolonColumn, false); _smartSemicolonFallback = false; cancelKeypress = false; } else { // 智能分号,记录位置并移动到行尾插入分号 _smartSemicolonFallback = true; _smartSemicolonLine = selection.ActivePoint.Line; _smartSemicolonColumn = selection.ActivePoint.DisplayColumn; selection.EndOfLine(); var caret = selection.ActivePoint.CreateEditPoint(); if (caret.GetText(-1) == ";") { cancelKeypress = true; _smartSemicolonDeleteLineEnd = false; } else { cancelKeypress = false; _smartSemicolonDeleteLineEnd = true; } } return(true); }
static public void OpenFile(string filename, uint line, uint column) { ThreadHelper.ThrowIfNotOnUIThread(); DTE2 applicationObject = ServiceProvider.GetService(typeof(SDTE)) as DTE2; Assumes.Present(applicationObject); Window window = null; Document doc = null; try { window = applicationObject.ItemOperations.OpenFile(filename.Replace('/', '\\')); } catch (Exception) { var content = new ParseMessageContent(); content.Message = "Unable to open file " + filename; ParseMessageWindow.Display(content); return; } if (window == null) { //sometimes it opens but it does not give a window element ( check if opened already ) Document activeDoc = GetActiveDocument(); if (activeDoc != null && Path.GetFileName(activeDoc.FullName) == Path.GetFileName(filename)) { doc = activeDoc; } } else { window.Activate(); doc = window.Document; } if (doc != null) { TextSelection sel = (TextSelection)doc.Selection; sel.MoveTo((int)line, (int)column); } }
public void Navigate(object item) { if (m_dte == null) { return; } var codeItem = item as CodeUIItem; var edgeItem = item as CodeUIEdgeItem; var fileName = ""; int line = 0; int column = 0; bool res = false; string searchToken = ""; try { if (codeItem != null) { codeItem.GetDefinitionPosition(out fileName,out line,out column); if (codeItem.GetKind() == DoxygenDB.EntKind.PAGE) { MoveTo(fileName,line,column); return; } res = ShowItemDefinition(codeItem,fileName); searchToken = codeItem.GetName(); } else if (edgeItem != null) { line = edgeItem.m_line; column = edgeItem.m_column; fileName = edgeItem.m_file; var scene = UIManager.Instance().GetScene(); var itemDict = scene.GetItemDict(); var srcItem = itemDict[edgeItem.m_srcUniqueName]; var tarItem = itemDict[edgeItem.m_tarUniqueName]; searchToken = tarItem.GetName(); if (srcItem.IsFunction()) { if (File.Exists(fileName)) { OpenFile(fileName); var document = m_dte.ActiveDocument; var codeElement = GetCodeElement(srcItem,document); if (codeElement != null) { var funcStart = codeElement.GetStartPoint(vsCMPart.vsCMPartBody); var funcText = UpdateBodyCode(srcItem,codeElement); int offset = -1; if (tarItem.IsFunction()) { offset = FindOffset(funcText,string.Format(@"\b{0}\(",tarItem.GetName())); } if (offset == -1) { offset = FindOffset(funcText,string.Format(@"\b{0}\b",tarItem.GetName())); } if (offset != -1) { var absOffset = funcStart.AbsoluteCharOffset + offset; TextSelection ts = document.Selection as TextSelection; ts.MoveToAbsoluteOffset(absOffset); res = true; } } } } else if (srcItem.GetKind() == DoxygenDB.EntKind.PAGE) { srcItem.GetDefinitionPosition(out fileName,out line,out column); MoveTo(fileName,line,column); return; } else { res = ShowItemDefinition(tarItem,fileName); } } } catch (Exception) { res = false; } if (res == false) { if (File.Exists(fileName)) { try { OpenFile(fileName); TextSelection ts = m_dte.ActiveDocument.Selection as TextSelection; TextDocument textDoc = ts.Parent; if (ts != null && line > 0 && textDoc != null) { var formatStr = string.Format(@"\b{0}\b",searchToken); int beginLine = Math.Max(1,line - 30); int endLine = line + 31; int endOffset = 1; if (endLine > textDoc.EndPoint.Line) { endLine = textDoc.EndPoint.Line; endOffset = textDoc.EndPoint.LineLength + 1; } ts.MoveToLineAndOffset(beginLine,1,false); ts.MoveToLineAndOffset(endLine,endOffset,true); string searchText = ts.Text.Replace("\r\n","\n"); int lineBeginIdx = 0; int bestLine = int.MaxValue,bestColumn = -1; for (int currentLine = beginLine; ; currentLine++) { int lineEndIdx = searchText.IndexOf("\n",lineBeginIdx); if (lineEndIdx == -1) { lineEndIdx = searchText.Length; } string lineStr = searchText.Substring(lineBeginIdx,lineEndIdx - lineBeginIdx); var matches = Regex.Matches(lineStr,formatStr,RegexOptions.ExplicitCapture); foreach (Match nextMatch in matches) { if (Math.Abs(currentLine - line) < Math.Abs(bestLine - line)) { bestLine = currentLine; bestColumn = nextMatch.Index + 1; } } if (lineEndIdx == -1 || lineEndIdx >= searchText.Length - 1) { break; } lineBeginIdx = lineEndIdx + 1; } if (bestColumn != -1) { ts.MoveTo(bestLine,bestColumn); res = true; } if (res == false) { ts.MoveTo(line,column); //ts.GotoLine(line); } } } catch (Exception) { Logger.Debug("Go to page fail."); } } } }