Esempio n. 1
0
        public static bool ContainsInvalidFileNameChars(string name)
        {
            if (String.IsNullOrEmpty(name))
            {
                return(true);
            }

            try {
                if (Path.IsPathRooted(name) && !name.StartsWith(@"\\", StringComparison.Ordinal))
                {
                    string root = Path.GetPathRoot(name);
                    name = name.Substring(root.Length);
                }
            }
            // The Path methods used by ContainsInvalidFileNameChars return argument exception if the filePath contains invalid characters.
            catch (ArgumentException) {
                return(true);
            }

            Microsoft.VisualStudio.Shell.Url uri = new Microsoft.VisualStudio.Shell.Url(name);

            // This might be confusing bur Url.IsFile means that the uri represented by the name is either absolut or relative.
            if (uri.IsFile)
            {
                string[] segments = uri.Segments;
                if (segments != null && segments.Length > 0)
                {
                    foreach (string segment in segments)
                    {
                        if (IsFilePartInValid(segment))
                        {
                            return(true);
                        }
                    }

                    // Now the last segment should be specially taken care, since that cannot be all dots or spaces.
                    string lastSegment = segments[segments.Length - 1];
                    string filePart    = Path.GetFileNameWithoutExtension(lastSegment);
                    // if the file is only an extension (.fob) then it's ok, otherwise we need to do the special checks.
                    if (filePart.Length != 0 && (IsFileNameAllGivenCharacter('.', filePart) || IsFileNameAllGivenCharacter(' ', filePart)))
                    {
                        return(true);
                    }
                }
            }
            else
            {
                // The assumption here is that we got a file name.
                string filePart = Path.GetFileNameWithoutExtension(name);
                if (IsFileNameAllGivenCharacter('.', filePart) || IsFileNameAllGivenCharacter(' ', filePart))
                {
                    return(true);
                }


                return(IsFilePartInValid(name));
            }

            return(false);
        }
Esempio n. 2
0
            /// <summary>
            /// Gets the fullpath of an item.
            /// Relative pathes are treated as relative to the project file.
            /// </summary>
            /// <param name="item">The item.</param>
            /// <returns>The ful path of the item.</returns>
            private string GetFullPath(string item)
            {
                Url url;

                if (Path.IsPathRooted(item))
                {
                    // Use absolute path
                    url = new Microsoft.VisualStudio.Shell.Url(item);
                }
                else
                {
                    // Path is relative, so make it relative to project path
                    url = new Url(new Url(this.projectFolder.LocalPath), item);
                }

                return(url.AbsoluteUrl);
            }
Esempio n. 3
0
        internal static string GetFilePathInternal(object obj)
        {
            string      fname = null;
            int         hr    = 0;
            IVsUserData ud    = obj as IVsUserData;

            if (ud != null)
            {
                object oname;
                Guid   GUID_VsBufferMoniker = typeof(IVsUserData).GUID;
                hr = ud.GetData(ref GUID_VsBufferMoniker, out oname);
                if (Microsoft.VisualStudio.ErrorHandler.Succeeded(hr) && oname != null)
                {
                    fname = oname.ToString();
                }
            }
            if (string.IsNullOrEmpty(fname))
            {
                IPersistFileFormat fileFormat = obj as IPersistFileFormat;
                if (fileFormat != null)
                {
                    uint format;
                    hr = fileFormat.GetCurFile(out fname, out format);
                }
            }
            if (!string.IsNullOrEmpty(fname))
            {
                Microsoft.VisualStudio.Shell.Url url = new Microsoft.VisualStudio.Shell.Url(fname);
                if (!url.Uri.IsAbsoluteUri)
                {
                    // make the file name absolute using app startup path...
                    Microsoft.VisualStudio.Shell.Url baseUrl = new Microsoft.VisualStudio.Shell.Url(Application.StartupPath + Path.DirectorySeparatorChar);
                    url   = new Microsoft.VisualStudio.Shell.Url(baseUrl, fname);
                    fname = url.AbsoluteUrl;
                }
            }
            return(fname);
        }
Esempio n. 4
0
            /// <summary>
            /// Gets the fullpath of an item. 
            /// Relative pathes are treated as relative to the project file.
            /// </summary>
            /// <param name="item">The item.</param>
            /// <returns>The ful path of the item.</returns>
            private string GetFullPath(string item)
            {
                Url url;
                if(Path.IsPathRooted(item))
                {
                    // Use absolute path
                    url = new Microsoft.VisualStudio.Shell.Url(item);
                }
                else
                {
                    // Path is relative, so make it relative to project path
                    url = new Url(new Url(this.projectFolder.LocalPath), item);
                }

                return url.AbsoluteUrl;
            }
Esempio n. 5
0
        public static bool ContainsInvalidFileNameChars(string name)
        {
            if (String.IsNullOrEmpty(name))
            {
                return true;
            }

            try
            {
                if (Path.IsPathRooted(name) && !name.StartsWith(@"\\", StringComparison.Ordinal))
                {
                    string root = Path.GetPathRoot(name);
                    name = name.Substring(root.Length);
                }
            }
            // The Path methods used by ContainsInvalidFileNameChars return argument exception if the filePath contains invalid characters.
            catch (ArgumentException)
            {
                return true;
            }

            Microsoft.VisualStudio.Shell.Url uri = new Microsoft.VisualStudio.Shell.Url(name);

            // This might be confusing bur Url.IsFile means that the uri represented by the name is either absolut or relative.
            if (uri.IsFile)
            {
                string[] segments = uri.Segments;
                if (segments != null && segments.Length > 0)
                {
                    foreach (string segment in segments)
                    {
                        if (IsFilePartInValid(segment))
                        {
                            return true;
                        }
                    }

                    // Now the last segment should be specially taken care, since that cannot be all dots or spaces.
                    string lastSegment = segments[segments.Length - 1];
                    string filePart = Path.GetFileNameWithoutExtension(lastSegment);
                    // if the file is only an extension (.foo) then it's ok, otherwise we need to do the special checks.
                    if (filePart.Length != 0 && (IsFileNameAllGivenCharacter('.', filePart) || IsFileNameAllGivenCharacter(' ', filePart)))
                    {
                        return true;
                    }
                }
            }
            else
            {
                // The assumption here is that we got a file name.
                string filePart = Path.GetFileNameWithoutExtension(name);
                if (IsFileNameAllGivenCharacter('.', filePart) || IsFileNameAllGivenCharacter(' ', filePart))
                {
                    return true;
                }

                return IsFilePartInValid(name);
            }

            return false;
        }
Esempio n. 6
0
        /// <summary>
        /// Set the build project with the new project instance value
        /// </summary>
        /// <param name="project">The new build project instance</param>
        protected virtual void SetBuildProject(MSBuild.Project project)
        {
            this.buildProject = project;
            this.baseUri = null;
            if (this.buildProject != null)
            {
                SetupProjectGlobalPropertiesThatAllProjectSystemsMustSet();

                _availableFileBuildActions.Clear();
                _availableFileBuildActions.Add(new KeyValuePair<string, prjBuildAction>("None", prjBuildAction.prjBuildActionNone));
                _availableFileBuildActions.Add(new KeyValuePair<string, prjBuildAction>("Compile", prjBuildAction.prjBuildActionCompile));
                _availableFileBuildActions.Add(new KeyValuePair<string, prjBuildAction>("Content", prjBuildAction.prjBuildActionContent));
                _availableFileBuildActions.Add(new KeyValuePair<string, prjBuildAction>("EmbeddedResource", prjBuildAction.prjBuildActionEmbeddedResource));
                ICollection<MSBuild.ProjectItem> availableItemNames = buildProject.GetItems("AvailableItemName");
                foreach (var itemName in availableItemNames)
                {
                    if (!_availableFileBuildActions.Any(i => string.Equals(i.Key, itemName.EvaluatedInclude, StringComparison.OrdinalIgnoreCase)))
                        _availableFileBuildActions.Add(new KeyValuePair<string, prjBuildAction>(itemName.EvaluatedInclude, (prjBuildAction)_availableFileBuildActions.Count));
                }
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Saves project file related information to the new file name. It also calls msbuild API to save the project file.
        /// It is called by the SaveAs method and the SetEditLabel before the project file rename related events are triggered. 
        /// An implementer can override this method to provide specialized semantics on how the project file is renamed in the msbuild file.
        /// </summary>
        /// <param name="newFileName">The new full path of the project file</param>
        protected virtual void SaveMSBuildProjectFileAs(string newFileName)
        {
            Debug.Assert(!String.IsNullOrEmpty(newFileName), "Cannot save project file for an empty or null file name");

            this.buildProject.FullPath = newFileName;
            this.baseUri = null;

            this.FileName = newFileName;

            string newFileNameWithoutExtension = Path.GetFileNameWithoutExtension(newFileName);

            // Refresh solution explorer
            this.SetProjectProperty(ProjectFileConstants.Name, _PersistStorageType.PST_PROJECT_FILE, newFileNameWithoutExtension);

            // Saves the project file on disk.
            this.buildProject.Save(newFileName);

            if (_userBuildProject != null)
                _userBuildProject.Save(UserFileName);
        }
Esempio n. 8
0
        //---------------------------------------------------------------------------
        // IVsDeferredSaveProject
        //---------------------------------------------------------------------------
        // Implemented by a project that supports being a "zero-impact" project.
        // The flags in the VSDIR file for the project must include VSDIRFLAG_DeferredSaveProject.
        // This means that the project name will be automatically generated, as will the location,
        // which will be at some temporary location (imitating the "in-memory" project experience).
        // Then, when the project is created, CPF_DEFERREDSAVE is passed as one of the grfCreateFlags
        // to IVsProjectFactory::CreateProject and/or IVsAggregatableProject::InitializeForOuter.
        // The solution will be limited to containing one project, and nothing will be considered
        // "dirty" while the solution/project are "zero-impact".
        // When the user chooses to save the project permanently, he will use File.SaveAll, which
        // will present him with a file browser with which he is to choose the new filename/location
        // for his project file.  Once he chooses the filename, SaveProjectToLocation is called and
        // the project must save itself to the specified filename and save all its other contents to
        // the folder where the filename was specified.  At this point, the solution/project will no
        // longer be considered "zero-impact" and normal solution/project operations will be available.
        public virtual int SaveProjectToLocation(string pszProjectFilename)
        {
            // first rename the project file from temporary location
            this.RenameProjectFile(pszProjectFilename);

            // now rename all documents in the project
            this.RenameAllChildren(this);

            // reset BaseUri because it was cached
            this.baseUri = null;

            // links may be changed so save the project file
            this.SaveAs(pszProjectFilename);

            // once the project is saved, it is no longer zero-impact project
            this.isZeroImpact = false;

            return VSConstants.S_OK;
        }
Esempio n. 9
0
        public virtual bool Navigate(string file, int line, int column)
        {
            Url url;
            if (Path.IsPathRooted(file))
                // Use absolute path
                url = new Microsoft.VisualStudio.Shell.Url(file);
            else
                // Path is relative, so make it relative to the project path
                url = new Url(new Url(ProjectFolder + "\\"), file);

            IVsUIShellOpenDocument openDocService = GetService(typeof(IVsUIShellOpenDocument)) as IVsUIShellOpenDocument;
            if (openDocService == null)
                return false;

            IVsWindowFrame frame;
            Microsoft.VisualStudio.OLE.Interop.IServiceProvider sp;
            IVsUIHierarchy hier;
            uint itemid;
            Guid logicalView = VSConstants.LOGVIEWID_Code;

            if (Microsoft.VisualStudio.ErrorHandler.Failed(openDocService.OpenDocumentViaProject(url.AbsoluteUrl, ref logicalView, out sp, out hier, out itemid, out frame)) || frame == null)
                return false;

            object docData;
            Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(frame.GetProperty((int)__VSFPROPID.VSFPROPID_DocData, out docData));

            // Get the VsTextBuffer
            VsTextBuffer buffer = docData as VsTextBuffer;
            if (buffer == null)
            {
                IVsTextBufferProvider bufferProvider = docData as IVsTextBufferProvider;
                if (bufferProvider != null)
                {
                    IVsTextLines lines;
                    Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(bufferProvider.GetTextBuffer(out lines));
                    buffer = lines as VsTextBuffer;
                    System.Diagnostics.Debug.Assert(buffer != null, "IVsTextLines does not implement IVsTextBuffer");
                    if (buffer == null)
                        return false;
                }
            }

            return Navigate(buffer, line+1, column+1);
        }