Ejemplo n.º 1
0
        public string Load(string configPath)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            IVsTextView configTextView = TextViewUtil.FindTextViewFor(configPath);
            ITextUtil   textUtil;

            if (configTextView != null)
            {
                textUtil = new VsTextViewTextUtil(configTextView);
            }
            else
            {
                textUtil = new FileTextUtil(configPath);
            }

            string fileText = textUtil.ReadAllText();
            var    body     = JObject.Parse(fileText);

            var bindings = body[BindingsName] as JObject;

            if (bindings != null)
            {
                var bindingsElement = XElement.Parse("<binding />");

                foreach (JProperty property in bindings.Properties())
                {
                    string[] tasks = property.Value.Values <string>().ToArray();

                    for (int i = 0; i < tasks.Length; ++i)
                    {
                        tasks[i] = _provider.GetDynamicName(tasks[i]);
                    }

                    bindingsElement.SetAttributeValue(property.Name, string.Join(",", tasks));
                }

                return(bindingsElement.ToString());
            }

            return("<binding />");
        }
        public bool Save(string configPath, string bindingsXml)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            bindingsXml = bindingsXml.Replace("\u200B", string.Empty);

            var  bindingsXmlObject = XElement.Parse(bindingsXml);
            var  bindingsXmlBody   = JObject.Parse(@"{}");
            bool anyAdded          = false;

            foreach (XAttribute attribute in bindingsXmlObject.Attributes())
            {
                var type = new JArray();
                bindingsXmlBody[attribute.Name.LocalName] = type;
                string[] tasks = attribute.Value.Split(',');

                foreach (string task in tasks)
                {
                    anyAdded = true;
                    type.Add(task.Trim());
                }
            }

            IVsTextView configTextView = TextViewUtil.FindTextViewFor(configPath);
            ITextUtil   textUtil;

            if (configTextView != null)
            {
                textUtil = new VsTextViewTextUtil(configTextView);
            }
            else
            {
                textUtil = new FileTextUtil(configPath);
            }

            string    currentContents = textUtil.ReadAllText();
            var       fileModel       = JObject.Parse(currentContents);
            bool      commaRequired   = fileModel.Properties().Any();
            JProperty currentBindings = fileModel.Property(BindingsName);
            bool      insert          = currentBindings == null;

            fileModel[BindingsName] = bindingsXmlBody;

            JProperty property    = fileModel.Property(BindingsName);
            string    bindingText = property.ToString(Formatting.None);

            textUtil.Reset();

            if (!anyAdded)
            {
                insert = false;
            }

            if (insert)
            {
                if (commaRequired)
                {
                    bindingText = ", " + bindingText;
                }

                int lineNumber = 0, candidateLine = -1, lastBraceIndex = -1, characterCount = 0;
                while (textUtil.TryReadLine(out string line))
                {
                    if (!string.IsNullOrWhiteSpace(line))
                    {
                        int brace = line.LastIndexOf('}');
                        if (brace >= 0)
                        {
                            lastBraceIndex = brace;
                            candidateLine  = lineNumber;
                        }
                    }

                    characterCount += line?.Length ?? 0;
                    ++lineNumber;
                }

                if (candidateLine >= 0 && lastBraceIndex >= 0)
                {
                    if (textUtil.Insert(new Range {
                        LineNumber = candidateLine, LineRange = new LineRange {
                            Start = lastBraceIndex, Length = bindingText.Length
                        }
                    }, bindingText, true))
                    {
                        textUtil.FormatRange(new LineRange {
                            Start = characterCount, Length = bindingText.Length
                        });
                        return(true);
                    }
                }

                return(false);
            }

            int bindingsIndex      = currentContents.IndexOf(@"""" + BindingsName + @"""", StringComparison.Ordinal);
            int closeBindingsBrace = currentContents.IndexOf('}', bindingsIndex) + 1;
            int length             = closeBindingsBrace - bindingsIndex;

            textUtil.GetExtentInfo(bindingsIndex, length, out int startLine, out int startLineOffset, out _, out _);

            if (!anyAdded)
            {
                int previousComma = currentContents.LastIndexOf(',', bindingsIndex);
                int tail          = 0;

                if (previousComma > -1)
                {
                    tail += bindingsIndex - previousComma;
                    textUtil.GetExtentInfo(previousComma, length, out startLine, out startLineOffset, out _, out _);
                }

                if (textUtil.Delete(new Range {
                    LineNumber = startLine, LineRange = new LineRange {
                        Start = startLineOffset, Length = length + tail
                    }
                }))
                {
                    textUtil.Reset();
                    textUtil.FormatRange(new LineRange {
                        Start = bindingsIndex, Length = 2
                    });
                    return(true);
                }

                return(false);
            }

            bool success = textUtil.Replace(new Range {
                LineNumber = startLine, LineRange = new LineRange {
                    Start = startLineOffset, Length = length
                }
            }, bindingText);

            if (success)
            {
                textUtil.FormatRange(new LineRange {
                    Start = bindingsIndex, Length = bindingText.Length
                });
            }

            return(success);
        }