Exemple #1
0
 /// <summary>Replaces the string with the specified text.</summary>
 /// <param name="text">Text to replace the current string</param>
 /// <returns>Full line where the text was replaced</returns>
 /// <remarks>
 /// Behaviour of BaseHardCodedString instance will be undetermined after this method is called, so object should not be used afterwards.
 /// <para>
 /// Changes should not be saved to the document immediatly.
 /// Once this method is called and returns successfully the object behaviour is allowed to become undefined.
 /// </para>
 /// </remarks>
 /// <exception cref="FileCheckoutException"><see cref="Parent"/> is under souzrce control and failed to check it out for editing</exception>
 /// <exception cref="FileReadOnlyException"><see cref="Parent"/> is read only</exception>
 override public string Replace(string text)
 {
     this.Parent.Document.Activate();
     if (!ExtensibilityMethods.CheckoutItem(this.Parent))
     {
         throw new FileCheckoutException(this.Parent.Name);
     }
     if (this.Parent.Document.ReadOnly)
     {
         throw new FileReadOnlyException(this.Parent.Name);
     }
     this.BeginEditPoint.Delete(this.TextLength);
     this.BeginEditPoint.Insert("\"" + text + "\"");
     return(this.BeginEditPoint.GetLines(this.BeginEditPoint.Line, this.BeginEditPoint.Line + 1));
 }
Exemple #2
0
        /// <summary>Shortens a full namespace reference by looking at a list of namespaces that are imported in the code</summary>
        /// <param name="reference">Reference to shorten</param>
        /// <param name="namespaces">Collection of namespaces imported in the file</param>
        /// <returns>Shortest form the of the reference valid for the file.</returns>
        public override string GetShortestReference(string reference, Collection <NamespaceImport> namespaces)
        {
            // Retrieve the property part of the reference
            string property = reference;

            string[] parts           = reference.Split(new[] { ':' }, 2);
            string   typeAndProperty = parts[1];
            string   @namespace      = parts[0];

            foreach (var ns in namespaces)
            {
                if (ns.NamespaceName == string.Format("clr-namespace:{0}", @namespace))
                {
                    if (ns.Alias != null)
                    {
                        return(string.Format("{{x:Static {0}:{1}}}", ns.Alias, typeAndProperty));
                    }
                    else
                    {
                        return(string.Format("{{x:Static {1}}}", typeAndProperty));
                    }
                }
            }

            TextSelection selection = (TextSelection)Parent.Document.Selection;
            var           xamlDoc   = selection.Parent;
            var           start     = xamlDoc.CreateEditPoint(xamlDoc.StartPoint);
            var           end       = xamlDoc.CreateEditPoint(xamlDoc.EndPoint);
            string        allText   = start.GetText(end);

            using (TextReader tr = new StringReader(allText))
                using (XmlReader xr = XmlReader.Create(tr)) {
                    while (xr.Read())
                    {
                        if (xr.NodeType == XmlNodeType.Element)//First element
                        {
                            if (xr.MoveToFirstAttribute())
                            {
                                do
                                {
                                } while (xr.MoveToNextAttribute());
                                xr.ReadAttributeValue();
                            }
                            IXmlLineInfo linfo = (IXmlLineInfo)xr;
                            this.Parent.Document.Activate();
                            if (!ExtensibilityMethods.CheckoutItem(this.Parent))
                            {
                                throw new FileCheckoutException(this.Parent.Name);
                            }
                            if (this.Parent.Document.ReadOnly)
                            {
                                throw new FileReadOnlyException(this.Parent.Name);
                            }
                            var ep = xamlDoc.CreateEditPoint();
                            ep.MoveToLineAndOffset(linfo.LineNumber, linfo.LinePosition);
                            ep.MoveToAbsoluteOffset(ep.AbsoluteCharOffset + xr.Value.Length + 1);
                            ep.ReplaceText(ep, string.Format(" xmlns:{0}=\"clr-namespace:{0}\"", @namespace), (int)vsEPReplaceTextOptions.vsEPReplaceTextTabsSpaces);
                            return(string.Format("{{x:Static {0}}}", reference));
                        }
                    }
                }
            throw new InvalidOperationException("Neither namespace import (xmlns) nor position to insert it found");
        }