public IReadOnlyList <OpenedFile> GetSourceFiles(FormsDesignerViewContent viewContent, out OpenedFile designerCodeFile) { // get new initialize components var parsedFile = SD.ParserService.ParseFile(viewContent.PrimaryFileName, viewContent.PrimaryFileContent); var compilation = SD.ParserService.GetCompilationForFile(viewContent.PrimaryFileName); foreach (var utd in parsedFile.TopLevelTypeDefinitions) { var td = utd.Resolve(new SimpleTypeResolveContext(compilation.MainAssembly)).GetDefinition(); if (FormsDesignerSecondaryDisplayBinding.IsDesignable(td)) { var initializeComponents = FormsDesignerSecondaryDisplayBinding.GetInitializeComponents(td); if (initializeComponents != null) { string designerFileName = initializeComponents.Region.FileName; if (designerFileName != null) { designerCodeFile = SD.FileService.GetOrCreateOpenedFile(designerFileName); return(td.Parts .Select(p => SD.FileService.GetOrCreateOpenedFile(p.UnresolvedFile.FileName)) .Distinct().ToList()); } } } } throw new FormsDesignerLoadException("Could not find InitializeComponent method in any part of the open class."); }
protected override CodeDomLocalizationModel GetCurrentLocalizationModelFromDesignedFile() { SD.Log.Debug("AlDesignerLoader.GetCurrentLocalizationModelFromDesignedFile()"); var primaryParseInfo = context.GetPrimaryFileParseInformation(); var compilation = context.GetCompilation(); // Find designer class ITypeDefinition designerClass = FormsDesignerSecondaryDisplayBinding.GetDesignableClass(primaryParseInfo.UnresolvedFile, compilation, out primaryPart); IMethod initializeComponents = FormsDesignerSecondaryDisplayBinding.GetInitializeComponents(designerClass); if (initializeComponents == null) { throw new FormsDesignerLoadException("The InitializeComponent method was not found. Designer cannot be loaded."); } AlFullParseInformation designerParseInfo; var initializeComponentsDeclaration = initializeComponents.GetDeclaration(out designerParseInfo); FindLocalizationModelVisitor visitor = new FindLocalizationModelVisitor(); initializeComponentsDeclaration.AcceptVisitor(visitor); if (visitor.Model != CodeDomLocalizationModel.None) { return(visitor.Model); } return(CodeDomLocalizationModel.None); }
public AlDesignerGenerator(IAlDesignerLoaderContext context) { this.context = context; this.primaryParseInfo = context.GetPrimaryFileParseInformation(); this.compilation = context.GetCompilation(); // Find designer class formClass = FormsDesignerSecondaryDisplayBinding.GetDesignableClass(primaryParseInfo.UnresolvedFile, compilation, out primaryPart); initializeComponents = FormsDesignerSecondaryDisplayBinding.GetInitializeComponents(formClass); if (initializeComponents == null) { throw new FormsDesignerLoadException("Could not find InitializeComponents"); } }
protected override void OnComponentRename(object component, string oldName, string newName) { base.OnComponentRename(component, oldName, newName); if (oldName != newName) { var primaryParseInfo = context.GetPrimaryFileParseInformation(); var compilation = context.GetCompilation(); // Find designer class ITypeDefinition designerClass = FormsDesignerSecondaryDisplayBinding.GetDesignableClass(primaryParseInfo.UnresolvedFile, compilation, out primaryPart); ISymbol controlSymbol; if (DesignerLoaderHost != null && DesignerLoaderHost.RootComponent == component) { controlSymbol = designerClass; } else { controlSymbol = designerClass.GetFields(f => f.Name == oldName, GetMemberOptions.IgnoreInheritedMembers) .SingleOrDefault(); } if (controlSymbol != null) { AsynchronousWaitDialog.ShowWaitDialogForAsyncOperation( "${res:SharpDevelop.Refactoring.Rename}", progressMonitor => FindReferenceService.RenameSymbol(controlSymbol, newName, progressMonitor) .ObserveOnUIThread() .Subscribe(error => SD.MessageService.ShowError(error.Message), // onNext ex => SD.MessageService.ShowException(ex), // onError // onCompleted - force refresh of the DesignerCodeFile's parse info, because the code generator // seems to work with an outdated version, when the document is saved. () => SD.ParserService.Parse(new FileName(context.DesignerCodeFileDocument.FileName), context.DesignerCodeFileDocument) ) ); } } }
// Steps to load the designer: // - Parse main file // - Find other files containing parts of the form // - Parse all files and look for fields (for controls) and InitializeComponents method // - Create CodeDom objects for fields and InitializeComponents statements // - If debug build and Ctrl pressed, output CodeDom to console // - Return CodeDom objects to the .NET designer protected override CodeCompileUnit Parse() { SD.Log.Debug("AlDesignerLoader.Parse()"); lastTextContentVersion = context.DesignerCodeFileDocument.Version; var primaryParseInfo = context.GetPrimaryFileParseInformation(); var compilation = context.GetCompilation(); // Find designer class ITypeDefinition designerClass = FormsDesignerSecondaryDisplayBinding.GetDesignableClass(primaryParseInfo.UnresolvedFile, compilation, out primaryPart); IMethod initializeComponents = FormsDesignerSecondaryDisplayBinding.GetInitializeComponents(designerClass); if (initializeComponents == null) { throw new FormsDesignerLoadException("The InitializeComponent method was not found. Designer cannot be loaded."); } Debug.Assert(primaryPart != null); Debug.Assert(designerClass != null); bool isFirstClassInFile = primaryParseInfo.UnresolvedFile.TopLevelTypeDefinitions[0] == primaryPart; // TODO: translate const string missingReferenceMessage = "Your project is missing a reference to '${Name}' - please add it using 'Project > Add Reference'."; if (compilation.FindType(typeof(System.Drawing.Point)).Kind == TypeKind.Unknown) { throw new FormsDesignerLoadException(StringParser.Parse(missingReferenceMessage, new StringTagPair("Name", "System.Drawing"))); } if (compilation.FindType(typeof(System.Windows.Forms.Form)).Kind == TypeKind.Unknown) { throw new FormsDesignerLoadException(StringParser.Parse(missingReferenceMessage, new StringTagPair("Name", "System.Windows.Forms"))); } CodeDomConvertVisitor cv = new CodeDomConvertVisitor(); cv.UseFullyQualifiedTypeNames = true; AlFullParseInformation designerParseInfo; MethodDeclaration initializeComponentsDeclaration = initializeComponents.GetDeclaration(out designerParseInfo) as MethodDeclaration; if (initializeComponentsDeclaration == null) { throw new FormsDesignerLoadException("Could not find source code for InitializeComponents"); } var resolver = designerParseInfo.GetResolver(compilation); var codeMethod = (CodeMemberMethod)cv.Convert(initializeComponentsDeclaration, resolver); var codeClass = new CodeTypeDeclaration(designerClass.Name); codeClass.Attributes = MemberAttributes.Public; codeClass.BaseTypes.AddRange(designerClass.DirectBaseTypes.Select(cv.Convert).ToArray()); codeClass.Members.Add(codeMethod); foreach (var field in designerClass.Fields) { var codeField = new CodeMemberField(cv.Convert(field.Type), field.Name); codeField.Attributes = GetAccessibility(field); codeClass.Members.Add(codeField); } var codeNamespace = new CodeNamespace(designerClass.Namespace); codeNamespace.Types.Add(codeClass); var codeUnit = new CodeCompileUnit(); codeUnit.Namespaces.Add(codeNamespace); // output generated CodeDOM to the console : #if DEBUG if ((Control.ModifierKeys & Keys.Control) == Keys.Control) { CodeDomVerboseOutputGenerator outputGenerator = new CodeDomVerboseOutputGenerator(); outputGenerator.GenerateCodeFromMember(codeMethod, Console.Out, null); this.CodeDomProvider.GenerateCodeFromCompileUnit(codeUnit, Console.Out, null); } #endif LoggingService.Debug("NRefactoryDesignerLoader.Parse() finished"); if (!isFirstClassInFile) { MessageService.ShowWarning("The form must be the first class in the file in order for form resources to be compiled correctly.\n" + "Please move other classes below the form class definition or move them to other files."); } return(codeUnit); }