Exemple #1
0
        public override void GenerateCodeFromCompileUnit(CodeCompileUnit codeCompileUnit, TextWriter writer, CodeGeneratorOptions options)
        {
            if (options == null)
            {
                options = GetCodeGeneratorOptions();
            }

            ProjectInfo projectInfo = ProjectInfo.FindProject(PathOfMainFile());

            if (projectInfo == null)
            {
                throw new ApplicationException("The component is not in the project!");
            }

            var changes    = projectInfo.Engine.MergeCodeCompileUnit(codeCompileUnit);
            var sourcesInf = (List <TupleStringIntInt>)codeCompileUnit.UserData["NemerleSources"];

            var indentInfo = ToIndentInfo(projectInfo.LanguageService.Preferences);

            using (var helper = new NemerleProjectSourcesButchEditHelper(projectInfo, "form designer update", indentInfo))
            {
                var definedIn   = changes.Declaration.UserData["Member"] as TopDeclaration;
                var typeBuilder = definedIn.TypeBuilder;

                var mainFilePath  = PathOfMainFile();
                var mainFileIndex = Location.GetFileIndex(mainFilePath);
                var mainPart      = typeBuilder.AstParts.First(td => td.Location.FileIndex == mainFileIndex);

                var cls = changes.Class;

                if (changes.InitializeComponent != null)
                {
                    var text = CodeGenerator.ToString(changes.NewInitializeComponentStatements);
                    // update InitializeComponent() body...
                    helper.ReplaseMethodBody(changes.InitializeComponent, text);
                }

                foreach (CodeMemberField codeMemberField in changes.InsertedFields)
                {
                    helper.AddField(cls, codeMemberField);
                }

                foreach (CodeMemberMethod codeMemberMethod in changes.InsertedMethods)
                {
                    helper.AddMethod(mainPart, codeMemberMethod, changes.Declaration);
                }

                foreach (ClassMember.Field field in changes.DelitedFields)
                {
                    helper.RemoveField(field);
                }

                helper.ApplyEdits();

                var relatedDocDatas = (RelatedDocDataCollection)codeCompileUnit.UserData[typeof(RelatedDocDataCollection)];
                if (relatedDocDatas != null)
                {
                    foreach (DocData docData in relatedDocDatas)
                    {
                        docData.Modify();
                        break;
                    }
                }
            }
        }
        int IProjectSourceNode.ExcludeFromProject()
        {
            if (ProjectMgr == null || ProjectMgr.IsClosed)
            {
                return((int)OleConstants.OLECMDERR_E_NOTSUPPORTED);
            }

            if (IsNonMemberItem)
            {
                return(VSConstants.S_OK);                // do nothing, just ignore it.
            }
            ((NemerlePackage)ProjectMgr.Package).SetWaitCursor();
            // Ask Document tracker listeners if we can remove the item.
            {             // just to limit the scope.
                string   documentToRemove = GetMkDocument();
                string[] filesToBeDeleted = new[] { documentToRemove };
                VSQUERYREMOVEFILEFLAGS[] queryRemoveFlags = GetQueryRemoveFileFlags(filesToBeDeleted);
                if (!ProjectMgr.Tracker.CanRemoveItems(filesToBeDeleted, queryRemoveFlags))
                {
                    return((int)OleConstants.OLECMDERR_E_CANCELED);
                }

                // Close the document if it has a manager.
                DocumentManager manager = GetDocumentManager();
                if (manager != null)
                {
                    if (manager.Close(__FRAMECLOSE.FRAMECLOSE_PromptSave) == VSConstants.E_ABORT)
                    {
                        return(VSConstants.OLE_E_PROMPTSAVECANCELLED);
                    }
                }

                if (!ProjectMgr.QueryEditProjectFile(false))
                {
                    throw Marshal.GetExceptionForHR(VSConstants.OLE_E_PROMPTSAVECANCELLED);
                }
            }

            // close the document window if open.
            CloseDocumentWindow(this);

            NemerleProjectNode projectNode = ProjectMgr as NemerleProjectNode;

            if (projectNode != null && projectNode.ShowAllFilesEnabled && File.Exists(Url))
            {
                string url = Url;                 // need to store before removing the node.
                ItemNode.RemoveFromProjectFile();
                ProjectMgr.Tracker.OnItemRemoved(url, VSREMOVEFILEFLAGS.VSREMOVEFILEFLAGS_NoFlags);
                SetProperty((int)__VSHPROPID.VSHPROPID_IsNonMemberItem, true);         // Set it as non member item
                ItemNode = new ProjectElement(ProjectMgr, null, true);                 // now we have to set a new ItemNode to indicate that this is virtual node.
                ItemNode.Rename(url);
                ItemNode.SetMetadata(ProjectFileConstants.Name, url);

                //ProjectMgr.OnItemDeleted();
                var proj = ProjectInfo.FindProject(ProjectMgr);
                if (proj != null)
                {
                    proj.RemoveSource(this.Url);
                }

                ReDraw(UIHierarchyElement.Icon);     // We have to redraw the icon of the node as it is now not a member of the project and should be drawn using a different icon.
                ReDraw(UIHierarchyElement.SccState); // update the SCC state icon.
            }
            else if (Parent != null)                 // the project node has no parentNode
            {
                // Remove from the Hierarchy
                OnItemDeleted();
                Parent.RemoveChild(this);
                ItemNode.RemoveFromProjectFile();
            }

            ResetProperties();

            HierarchyHelpers.RefreshPropertyBrowser(this);

            return(VSConstants.S_OK);
        }