//_____________________________________________________________________________________________________________________________________________________________ private void cLst_SelectedIndexChanged(object sender, EventArgs e) { if (cLst.SelectedItems.Count != 1) { return; } cContext.Text = ""; cScriptLocation f = (cScriptLocation)cLst.SelectedItems[0].Tag; PSSyntaxHelper.AddText(cContext, string.Join("\r\n", f.lineContext), f.ContextSize, f.position, f.word); }
//_____________________________________________________________________________________________________________________________________________________________ public void SelectDefaultLocation(string fileName, int line) { foreach (ListViewItem i in cLst.Items) { cScriptLocation f = (cScriptLocation)i.Tag; if (!f.fileName.iEquals(fileName)) { continue; } if (f.line != line) { continue; } i.Selected = true; break; } }
//_____________________________________________________________________________________________________________________________________________________________ public void GetReferences() { LogHelper.Add("GetReferences"); ISEEditor editor = hostObject.CurrentPowerShellTab.Files.SelectedFile.Editor; string caretLineText = editor.CaretLineText; string currentFile = hostObject.CurrentPowerShellTab.Files.SelectedFile.FullPath; Tuple <string, int, int> pos = new Tuple <string, int, int>(currentFile, editor.CaretLine, editor.CaretColumn); List <PSToken> list = PSParser.Tokenize(caretLineText, out Collection <PSParseError> errors).Where(t => t.Type == PSTokenType.Command && t.StartColumn <= editor.CaretColumn && t.EndColumn >= editor.CaretColumn).ToList(); if (list.Count == 0) { list = PSParser.Tokenize(caretLineText, out errors).Where(t => t.Type == PSTokenType.CommandParameter && t.StartColumn <= editor.CaretColumn && t.EndColumn >= editor.CaretColumn).ToList(); } if (list.Count == 0) { list = PSParser.Tokenize(caretLineText, out errors).Where(t => t.Type == PSTokenType.CommandArgument && t.StartColumn <= editor.CaretColumn && t.EndColumn >= editor.CaretColumn).ToList(); } if (list.Count == 0) { list = PSParser.Tokenize(caretLineText, out errors).Where(t => t.StartColumn <= editor.CaretColumn && t.EndColumn >= editor.CaretColumn).ToList(); } if (list.Count == 0) { return; } PSToken tkn = list[0]; List <cScriptLocation> lst = GetAllReferenceLocations(tkn); string title = $"{tkn.Content} ({tkn.Type}) [{lst.Count}]"; if (tkn.Type == PSTokenType.String) // if tkn is a string with less than 2 results, try to searh into the string for additional tokens { PSToken tknsubstr = Token.GetPSTokenInLocation(caretLineText, tkn, editor.CaretColumn); if (tknsubstr != null) { List <cScriptLocation> lstsubstr = GetAllReferenceLocations(tknsubstr); if (lstsubstr.Count > 0) { lst.AddRange(lstsubstr);// is string found more tokens replace the list with the new result title += $" / {tknsubstr.Content} ({tknsubstr.Type}) [{lstsubstr.Count}]"; } } } if (lst.Count == 0) { return; } using (frmReferences f = new frmReferences()) { f.Locations = lst; f.SelectDefaultLocation(Path.GetFileName(currentFile), editor.CaretLine); f.Command = $"References {title}"; f.ShowDialog(new WindowWrapper(WindowWrapper.GetSafeHandle())); if (!f.ReferenceSelected) { return; } cScriptLocation l = f.SelectedLocation; try { hostObject.CurrentPowerShellTab.Files.SetSelectedFile(hostObject.CurrentPowerShellTab.Files.Where(file => Path.GetFileName(file.FullPath).iEquals(l.fileName)).FirstOrDefault()); hostObject.CurrentPowerShellTab.Files.SelectedFile.Editor.SetCaretPosition(l.line, l.position); hostObject.CurrentPowerShellTab.Files.SelectedFile.Editor.Select(l.line, l.position, l.line, l.position + l.word.Length); BackList.Push(pos); } catch (Exception ex) { LogHelper.AddException(ex, "GetReferences", null); } } }