private void postCompileCpp(string generatedFile, int mode, string functionOfInterest, string curCodeLine) { if (!File.Exists(generatedFile)) { package.showMsgBox("Could not find expected output file\n" + generatedFile); return; } // clean the preprocessed output // TODO: do this in a better way if (mode == 2) { var input = new StreamReader(generatedFile); generatedFile = Path.GetTempFileName() + ".cpp"; var output = new StreamWriter(generatedFile); while (input.Peek() >= 0) { string curReadLine = input.ReadLine(); if (curReadLine != "") { output.WriteLine(curReadLine); } } input.Close(); output.Close(); } // TODO: there are a thousand ways to open a file // dte.Documents.Open(asmFile, EnvDTE.Constants.vsViewKindCode); // dte.ExecuteCommand("File.OpenFile", asmFile); Window tmp = dte.ItemOperations.OpenFile(generatedFile, Constants.vsViewKindCode); TextDocument genFileWindow = (TextDocument)tmp.Document.Object("TextDocument"); // crashes VS // bool ddd = genFileWindow.ReplacePattern("^$\n", "", (int)vsFindOptions.vsFindOptionsRegularExpression); // http://stackoverflow.com/questions/12453160/remove-empty-lines-in-text-using-visual-studio // ^:b*$\n -> ^(?([^\r\n])\s)*\r?$\r?\n // now try to find the function the user was looking at // if it's a template the fullName will be like ns::bar<T> // try to find an instantiation instead then int bracketPos = functionOfInterest.IndexOf("<", StringComparison.Ordinal); if (bracketPos > 0) { functionOfInterest = functionOfInterest.Substring(0, bracketPos + 1); } TextSelection textSelObj = genFileWindow.Selection; // first try to find the function // TODO: for some reason vsFindOptions.vsFindOptionsFromStart option doesn't work textSelObj.StartOfDocument(); bool res = textSelObj.FindText("PROC ; " + functionOfInterest, (int)vsFindOptions.vsFindOptionsMatchCase); if (!res && mode == 1) { dte.StatusBar.Text = "Couldn't find function '" + functionOfInterest + "'"; dte.StatusBar.Highlight(true); } // then search for the code line // it might not be there if it's optimized away if (!string.IsNullOrWhiteSpace(curCodeLine)) { textSelObj.FindText(curCodeLine.Trim(), (int)vsFindOptions.vsFindOptionsMatchCase); } textSelObj.StartOfLine(); }