/// <summary>Queries if this action supports the provided project item and its containing project</summary>
 /// <param name="item">Project item to query support for</param>
 /// <returns>True if the action is supported, false otherwise. This implementation supports all C# projects.</returns>
 public override bool QuerySupportForProject(EnvDTE.ProjectItem item)
 {
     if (item == null)
     {
         return(false);
     }
     return(ExtensibilityMethods.GetProjectType(item.ContainingProject) == ProjectType.CSharp && item.Document.Language.Equals("CSharp"));
 }
예제 #2
0
 /// <summary>Queries if this action supports the provided project item and its containing project</summary>
 /// <param name="item">Project item to query support for</param>
 /// <returns>True if the action is supported, false otherwise. This implementation supports Rator (VBHTML) fiels in VB projects</returns>
 public override bool QuerySupportForProject(ProjectItem item)
 {
     if (item == null)
     {
         return(false);
     }
     return
         (ExtensibilityMethods.GetProjectType(item.ContainingProject) == ProjectType.VB &&
          item.Document.Language.Equals("HTMLX") && item.Document.Name.EndsWith(".vbhtml", StringComparison.CurrentCultureIgnoreCase));;
 }
예제 #3
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));
 }
        /// <summary>Returns a collection of namespaces imported in the CSHTML file itself</summary>
        /// <param name="namespaces">A collection to add namespaces imported in the CSHTML file to</param>
        private void GetNamespacesFromFile(Collection <NamespaceImport> namespaces)
        {
            TextDocument doc      = (TextDocument)this.Parent.Document.Object("TextDocument");
            string       contents = ExtensibilityMethods.GetDocumentText(doc);

            System.Text.RegularExpressions.Regex regExp = new Regex("@Imports[ \\t]+((.)*)", RegexOptions.IgnoreCase);
            Match m = regExp.Match(contents);

            if (m.Groups.Count > 0)
            {
                if (m.Groups[1].Value.Contains("="))
                {
                    var parts = m.Groups[1].Value.Split(new[] { '=' }, 2);
                    namespaces.Add(new NamespaceImport(m.Groups[1].Value, parts[0].Trim(), parts[1].Trim()));
                }
                else
                {
                    string ns = m.Groups[1].Value.Trim();
                    namespaces.Add(new NamespaceImport(ns, ns));
                }
            }
        }
예제 #5
0
        /// <summary>Returns the code reference to resource specified in the parameters</summary>
        /// <param name="file">Resource file containing the resource</param>
        /// <param name="resourceName">Name of the resource</param>
        /// <param name="project">Project current file belongs to</param>
        /// <param name="string">String being extracted</param>
        /// <returns>a piece of code that would reference to the resource provided</returns>
        /// <remarks>This method does not verify if resource actually exists</remarks>
        public override string GetResourceReference(ResourceFile file, string resourceName, Project project, BaseHardCodedString @string)
        {
            string prefix = "";

            if (ExtensibilityMethods.GetProjectType(project) == ProjectType.VB)
            {
                try {
                    prefix = (string)project.Properties.Item("RootNamespace").Value;
                    if (!string.IsNullOrEmpty(prefix))
                    {
                        prefix += ".";
                    }
                } catch { }
            }

            if (file == null)
            {
                throw new ArgumentNullException("file");
            }
            if (String.IsNullOrEmpty(resourceName))
            {
                throw new ArgumentException(Strings.InvalidResourceName, "resourceName");
            }
            string namespacePrefix = GetNamespacePrefix(file);

            if (!file.IsDefaultResXFile())
            {
                namespacePrefix += Path.GetFileNameWithoutExtension(file.ShortFileName).Replace(' ', '_') + ".";
            }
            else
            {
                namespacePrefix += Path.GetFileNameWithoutExtension(file.FileName).Replace(' ', '_') + ".";
            }
            string reference = namespacePrefix + resourceName.Replace(' ', '_');

            return(prefix + reference);
        }
        /// <summary>Creates a new code file collection that lists all the code files in a project that can be safely edited</summary>
        /// <param name="project">Project to list code files</param>
        /// <remarks>Designer code files are not excluded</remarks>
        public CodeFileCollection(Project project) : base(project, null)
        {
            ProjectType type = ExtensibilityMethods.GetProjectType(project);

            switch (type)
            {
            case ProjectType.CSharp:
                this.codeTypeFilter = CodeType.CSharp;
                break;

            case ProjectType.VB:
                this.codeTypeFilter = CodeType.VB;
                break;

            case ProjectType.WebProject:
                this.codeTypeFilter = CodeType.Both;
                break;

            default:
                throw new ArgumentException(Strings.ProjectFileInvalid, "project");
            }
            this.FilteringMethod = new FilterMethod(this.IsValidCodeFile);
            this.RefreshListOfFiles();
        }
예제 #7
0
 /// <summary>Queries if this action supports the provided project item and its containing project</summary>
 /// <param name="item">Project item to query support for</param>
 /// <returns>True if the action is supported, false otherwise.</returns>
 /// <remarks>This implementation supports all ASPX files and C# projects</remarks>
 public override bool QuerySupportForProject(ProjectItem item)
 {
     return(item != null && item.Document.Language.Equals("CSharp") && ExtensibilityMethods.GetProjectType(item.ContainingProject) == ProjectType.WebProject);
 }
 /// <summary>Returns the position of all comments in the given document using regular expressions</summary>
 /// <param name="item">Project item representing the document</param>
 /// <returns><see cref="Match"/> objects representing positions of all comments in the given documen</returns>
 public MatchCollection FindCommentsInDocument(ProjectItem item)
 {
     return(this.CommentRegularExpression.Matches(ExtensibilityMethods.GetDocumentText(GetDocumentForItem(item)).Replace("\r\n", "\n")));
 }
 /// <summary>Checks if selection in <see cref="TextDocument"/> is inside a hard coded string and return a <see cref="MatchResult"/> pointing to string.</summary>
 /// <param name="document">A <see cref="TextDocument"/> to look in part of</param>
 /// <param name="line">Current line</param>
 /// <param name="selectionStart">Starting index of the selection</param>
 /// <param name="selectionEnd">Ending index of the selection</param>
 /// <returns>A <see cref="MatchResult"/> object, result is false if a string could not be located.</returns>
 public MatchResult CheckForHardCodedString(TextDocument document, int selectionStart, int selectionEnd)
 {
     return(CheckForHardCodedString(ExtensibilityMethods.GetDocumentText(document).Replace("\r\n", "\n"), selectionStart, selectionEnd));
 }
예제 #10
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");
        }