public PowershellItem AddRegions(PowershellItem rootItem, string fileContents)
        {
            if (!ShowRegions)
            {
                return(rootItem);
            }
            List <PowershellRegion> regions = GetPowershellRegions(fileContents);

            if (!regions.Any())
            {
                return(rootItem);
            }
            foreach (PowershellRegion region in regions)
            {
                List <PowershellItem> itemsInRegion = GetItemsInRegionWithMinimalNesting(rootItem, region);
                if (!itemsInRegion.Any())
                {
                    continue;
                }
                PowershellItem firstItemInRegion = itemsInRegion.First();
                PowershellItem regionItem        = ConvertRegionToPowershellItem(region, firstItemInRegion);
                foreach (PowershellItem item in itemsInRegion)
                {
                    item.Reparent(regionItem);
                }
            }
            return(rootItem);
        }
        private List <PowershellItem> GetItemsInRegionWithMinimalNesting(PowershellItem rootItem, PowershellRegion region)
        {
            List <PowershellItem> itemsInRegion = GetItemsInRegion(rootItem, region, new List <PowershellItem>());

            if (!itemsInRegion.Any())
            {
                return(itemsInRegion);
            }
            int minimalNesting = itemsInRegion.Min(item => item.NestingLevel);

            return(itemsInRegion.Where(item => item.NestingLevel == minimalNesting).ToList());
        }
        private static string GetNodePath(string filePath, PowershellItem item)
        {
            if (filePath == null)
            {
                throw new ArgumentNullException("filePath");
            }
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            return(System.IO.Path.Combine(filePath, item.StartLine.ToString() + "_" + item.StartColumn.ToString()));
        }
 private List <PowershellItem> GetItemsInRegion(PowershellItem item, PowershellRegion region, List <PowershellItem> result)
 {
     if (item.StartLine > region.StartLine && item.StartLine < region.EndLine)
     {
         result.Add(item);
         return(result);
     }
     foreach (PowershellItem child in item.Children)
     {
         result = GetItemsInRegion(child, region, result);
     }
     return(result);
 }
        private static string GetNodePath(string filePath, PowershellItem item)
        {
            if (filePath == null)
            {
                throw new ArgumentNullException("filePath");
            }
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            return System.IO.Path.Combine(filePath, item.StartLine.ToString() + "_" + item.StartColumn.ToString());
        }
        private static string GetNodePath(string filePath, PowershellItem item, INode parent)
        {
            if (filePath == null)
            {
                throw new ArgumentNullException("filePath");
            }
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            string basePath = parent != null && parent is PowershellItemNode ? parent.Path : filePath;
            return System.IO.Path.Combine(basePath, item.StartLine.ToString() + "_" + item.StartColumn.ToString());
        }
        private static string GetNodePath(string filePath, PowershellItem item, INode parent)
        {
            if (filePath == null)
            {
                throw new ArgumentNullException("filePath");
            }
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            string basePath = parent != null && parent is PowershellItemNode ? parent.Path : filePath;

            return(System.IO.Path.Combine(basePath, item.StartLine.ToString() + "_" + item.StartColumn.ToString()));
        }
Example #8
0
 public INode CreateNewPowershellItemNode(string filePath, PowershellItem item, INode parent)
 {
     if (item.Type != PowershellItemType.Root)
     {
         var lockObject = parent == null ? RootLockObject : parent;
         lock (lockObject)
         {
             parent = new PowershellItemNode(filePath, item, parent);
             this.NodeMap.Add(parent.Path, parent);
             this.FullTextDirectory.DocumentCreator.AddPowershellItemEntry(parent.Path, item.Name);
         }
     }
     foreach (var itemChild in item.Children)
     {
         this.CreateNewPowershellItemNode(filePath, itemChild, parent);
     }
     return(parent);
 }
 public INode CreateNewPowershellItemNode(string filePath, PowershellItem item, INode parent)
 {
     if (item.Type != PowershellItemType.Root)
     {
         var lockObject = parent == null ? RootLockObject : parent;
         lock (lockObject)
         {
             parent = new PowershellItemNode(filePath, item, parent);
             this.NodeMap.Add(parent.Path, parent);
             this.FullTextDirectory.DocumentCreator.AddPowershellItemEntry(parent.Path, item.Name);
         }
     }
     foreach (var itemChild in item.Children)
     {
         this.CreateNewPowershellItemNode(filePath, itemChild, parent);
     }
     return parent;
 }
 protected AbstractNode(string path, string name, INode parent, PowershellItem powershellItem, bool isExcluded, bool isValid, string metadata)
 {
     if (path == null)
     {
         throw new ArgumentNullException("path");
     }
     this.Path           = path;
     this.Name           = name;
     this.PowershellItem = powershellItem;
     this.IsExcluded     = isExcluded;
     this.IsValid        = isValid;
     this.Metadata       = metadata;
     this.Parent         = parent;
     this.Children       = new SortedSet <INode>(NodeComparerProvider.NodeComparer);
     if (this.Parent != null)
     {
         if (!this.Parent.Children.Add(this))
         {
             throw new InvalidOperationException(String.Format("Adding element '{0}' failed", path));
         }
     }
 }
Example #11
0
        public PowershellItem GetPowershellItems(string path, string contents)
        {
            bool parsePowershellItems = configValues.ParsePowershellItems;

            if (!parsePowershellItems || !this.parsePowershellDscWithExternalImports && ImportDscRegex.IsMatch(contents))
            {
                return(this.createRootItem(null));
            }

            ParseError[] errors;
            Token[]      tokens;
            AddedItems = new HashSet <string>();


            Ast ast       = Parser.ParseInput(contents, out tokens, out errors);
            var errorsLog = !errors.Any() ? null :
                            "Parsing error(s): " + Environment.NewLine + string.Join(Environment.NewLine, errors.OrderBy(err => err.Extent.StartLineNumber).Select(err => "Line " + err.Extent.StartLineNumber + ": " + err.Message));

            RootItem = this.createRootItem(errorsLog);

            VisitTokens(ast);

            return(RootItem);
        }
Example #12
0
        protected PowershellItem CreateNewPowershellItem(PowershellItemType type, string itemName, IScriptExtent extent, PowershellItem parent, int nestingLevel)
        {
            int startColumnNumber = extent.StartColumnNumber + extent.Text.IndexOf(itemName);
            int endColumnNumber   = startColumnNumber + itemName.Length;
            int startLineNumber   = extent.StartLineNumber;

            return(CreateNewPowershellItem(type, itemName, startColumnNumber, endColumnNumber, startLineNumber, parent, nestingLevel));
        }
Example #13
0
 private PowershellItem createRootItem(string errorsLog)
 {
     RootItem = new PowershellItem(PowershellItemType.Root, null, 0, 0, 0, 0, null, errorsLog);
     return(RootItem);
 }
 public PowershellItemNode(string filePath, PowershellItem item, INode parent)
     : base(GetNodePath(filePath, item), item.Name, parent)
 {
     FilePath = filePath;
     PowershellItem = item;
 }
Example #15
0
        public static PowershellItem GetPowershellItems(string path, string contents)
        {
            Collection <PSParseError> errors;
            bool dscParse = false;

            // this is fix for performance issue in PSParser.Tokenize - when file contains Import-DSCResource pointing to a non-installed resource,
            // parsing takes long time and 'Unable to load resource' errors appear
            if (importDscRegex.IsMatch(contents))
            {
                contents = importDscRegex.Replace(contents, "#Import-DSCResource");
                dscParse = true;
            }
            IEnumerable <PSToken> tokens = PSParser.Tokenize(contents, out errors);
            var errorsLog = !errors.Any() || dscParse ? null :
                            "Parsing error(s): " + Environment.NewLine + string.Join(Environment.NewLine, errors.OrderBy(err => err.Token.StartLine).Select(err => "Line " + err.Token.StartLine + ": " + err.Message));
            PowershellItem rootItem = new PowershellItem(PowershellItemType.Root, null, 0, 0, 0, null, errorsLog);

            if (errorsLog != null)
            {
                Logger.Debug("File " + path + " - " + errorsLog);
            }
            PowershellItem currentItem = rootItem;

            bool    nextTokenIsFunctionName = false;
            PSToken commandToken            = null;

            int nestingLevel = 0;

            foreach (PSToken token in tokens)
            {
                if (nextTokenIsFunctionName)
                {
                    var item = new PowershellItem(PowershellItemType.Function, token.Content, token.StartLine, token.StartColumn, nestingLevel, currentItem, null);
                    // currentItem = item;
                    nextTokenIsFunctionName = false;
                }

                /*else if (commandToken != null)
                 * {
                 *  var item = new PowershellItem(PowershellItemType.Command, commandToken.Content, commandToken.StartLine, commandToken.StartColumn, nestingLevel, currentItem);
                 *  currentItem = item;
                 *  commandToken = null;
                 *  continue;
                 * }
                 * else if (token.Type == PSTokenType.NewLine)
                 * {
                 *  if (currentItem != null && nestingLevel <= currentItem.NestingLevel)
                 *  {
                 *      currentItem = currentItem.Parent ?? rootItem;
                 *  }
                 * }
                 * else if (token.Type == PSTokenType.GroupStart && token.Content.Contains("{"))
                 * {
                 *  nestingLevel++;
                 * }
                 * else if (token.Type == PSTokenType.GroupEnd && token.Content.Contains("}"))
                 * {
                 *  nestingLevel--;
                 * }
                 * else if (token.Type == PSTokenType.Command)
                 * {
                 *  commandToken = token;
                 * }*/
                else if (token.Type == PSTokenType.Keyword)
                {
                    string tokenContent = token.Content.ToLowerInvariant();
                    if (tokenContent == "function" || tokenContent == "filter" || tokenContent == "configuration" || tokenContent == "workflow")
                    {
                        nextTokenIsFunctionName = true;
                    }
                }
            }
            return(rootItem);
        }
 private PowershellItem ConvertRegionToPowershellItem(PowershellRegion region, PowershellItem firstItemInRegion)
 {
     return(new PowershellItem(Enums.PowershellItemType.Region, region.Name, region.StartLine, 0, region.Name.Length + 8, firstItemInRegion.NestingLevel, firstItemInRegion.Parent, null));
 }
Example #17
0
        protected PowershellItem CreateNewPowershellItem(PowershellItemType type, string itemName, int startColumnNumber, int endColumnNumber, int startLineNumber, PowershellItem parent, int nestingLevel)
        {
            string itemKey = startLineNumber + "_" + startColumnNumber;

            if (!AddedItems.Contains(itemKey))
            {
                AddedItems.Add(itemKey);
                if (parent == null)
                {
                    parent = RootItem;
                }
                return(new PowershellItem(type, itemName, startLineNumber, startColumnNumber, endColumnNumber, nestingLevel, parent, null));
            }
            else
            {
                return(null);
            }
        }
 public PowershellItemNode(string filePath, PowershellItem item, INode parent)
     : base(GetNodePath(filePath, item, parent), item.Name, parent)
 {
     this.FilePath       = filePath;
     this.PowershellItem = item;
 }
 protected AbstractNode(string path, string name, INode parent, PowershellItem powershellItem)
     : this(path, name, parent, powershellItem, false, true, null)
 {
 }