Example #1
0
        //_____________________________________________________________________________________________________________________________________________________________
        private cFunction GetSelectedFileName(List <cFunction> functions, string currentFile, ISEEditor editor)
        {
            if (functions.Count == 1)
            {
                return(functions[0]);
            }
            List <cScriptLocation> locations = mProjects.Project.Functions.GetScriptLocations(functions);

            using (frmReferences f = new frmReferences()) {
                f.Locations = locations;
                f.SelectDefaultLocation(Path.GetFileName(currentFile), editor.CaretLine);
                f.Command = "Found more than one candidate. Select one of them.";
                f.ShowDialog(new WindowWrapper(WindowWrapper.GetSafeHandle()));
                if (!f.ReferenceSelected)
                {
                    return(null);
                }
                return(functions.Where(func => func.FullName == f.SelectedLocation.fileName && func.Line == f.SelectedLocation.line).FirstOrDefault());
            }
        }
Example #2
0
        //_____________________________________________________________________________________________________________________________________________________________
        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); }
            }
        }