private string FindRuleSetInFile(IEnumerable <string> extensions, string id, out RuleSet rule)
        {
            string root = ProjectHelpers.GetProjectFolder(peekableItem._textbuffer.GetFileName());
            string result = null;
            bool   isLow = false, isMedium = false;

            rule = null;

            foreach (string ext in extensions)
            {
                ICssParser parser = CssParserLocator.FindComponent(ProjectHelpers.GetContentType(ext.Trim('.'))).CreateParser();

                foreach (string file in Directory.EnumerateFiles(root, "*" + ext, SearchOption.AllDirectories))
                {
                    if (file.EndsWith(".min" + ext, StringComparison.OrdinalIgnoreCase) ||
                        file.Contains("node_modules") ||
                        file.Contains("bower_components") ||
                        file.Contains("\\obj\\Release") ||
                        file.Contains("\\obj\\Debug\\") ||
                        file.Contains("\\obj\\publish\\"))
                    {
                        continue;
                    }

                    string text  = File.ReadAllText(file);
                    int    index = text.IndexOf("#" + id, StringComparison.Ordinal);

                    if (index == -1)
                    {
                        continue;
                    }

                    StyleSheet css     = parser.Parse(text, true);
                    var        visitor = new CssItemCollector <IdSelector>(false);
                    css.Accept(visitor);

                    IEnumerable <IdSelector> selectors = visitor.Items.Where(c => c.HashName.Text == "#" + id);
                    IdSelector high = selectors.FirstOrDefault(c => c.FindType <AtDirective>() == null && (c.Parent.NextSibling == null || c.Parent.NextSibling.Text == ","));

                    if (high != null)
                    {
                        rule = high.FindType <RuleSet>();
                        return(file);
                    }

                    IdSelector medium = selectors.FirstOrDefault(c => c.Parent.NextSibling == null || c.Parent.NextSibling.Text == ",");

                    if (medium != null && !isMedium)
                    {
                        rule     = medium.FindType <RuleSet>();
                        result   = file;
                        isMedium = true;
                        continue;
                    }

                    IdSelector low = selectors.FirstOrDefault();

                    if (low != null && !isMedium && !isLow)
                    {
                        rule   = low.FindType <RuleSet>();
                        result = file;
                        isLow  = true;
                        continue;
                    }
                }
            }

            return(result);
        }
Example #2
0
        private string FindFile(IEnumerable <string> extensions, out int position)
        {
            string root = ProjectHelpers.GetProjectFolder(TextView.TextBuffer.GetFileName());

            position = -1;
            bool   isLow = false, isMedium = false;
            string result = null;

            foreach (string ext in extensions)
            {
                ICssParser parser = CssParserLocator.FindComponent(ProjectHelpers.GetContentType(ext.Trim('.'))).CreateParser();

                foreach (string file in Directory.EnumerateFiles(root, "*" + ext, SearchOption.AllDirectories))
                {
                    if (file.EndsWith(".min" + ext, StringComparison.OrdinalIgnoreCase) ||
                        file.Contains("node_modules") ||
                        file.Contains("bower_components"))
                    {
                        continue;
                    }

                    string text  = File.ReadAllText(file);
                    int    index = text.IndexOf("." + _className, StringComparison.Ordinal);

                    if (index == -1)
                    {
                        continue;
                    }

                    Microsoft.WebTools.Languages.Css.TreeItems.StyleSheet css = parser.Parse(text, true);
                    var visitor = new CssItemCollector <ClassSelector>(false);
                    css.Accept(visitor);

                    IEnumerable <ClassSelector> selectors = visitor.Items.Where(c => c.ClassName.Text == _className);
                    ClassSelector high = selectors.FirstOrDefault(c => c.FindType <AtDirective>() == null && (c.Parent.NextSibling == null || c.Parent.NextSibling.Text == ","));

                    if (high != null)
                    {
                        position = high.Start;
                        return(file);
                    }

                    ClassSelector medium = selectors.FirstOrDefault(c => c.Parent.NextSibling == null || c.Parent.NextSibling.Text == ",");

                    if (medium != null && !isMedium)
                    {
                        position = medium.Start;
                        result   = file;
                        isMedium = true;
                        continue;
                    }

                    ClassSelector low = selectors.FirstOrDefault();

                    if (low != null && !isMedium && !isLow)
                    {
                        position = low.Start;
                        result   = file;
                        isLow    = true;
                        continue;
                    }
                }
            }

            return(result);
        }