ICodeContext CreateContext(ITextEditor editor) { var compilation = SD.ParserService.GetCompilationForFile(editor.FileName); var project = SD.ProjectService.FindProjectContainingFile(editor.FileName); var resolveContext = new SimpleTypeResolveContext(compilation.MainAssembly); var currentTypeDefinition = new DefaultUnresolvedTypeDefinition(project.RootNamespace, Path.GetFileNameWithoutExtension(editor.FileName)); ITypeReference baseTypeReference = new GetClassTypeReference("System.Web.Mvc", "WebViewPage", 1); baseTypeReference = new ParameterizedTypeReference(baseTypeReference, new[] { FindModelType(editor) }); currentTypeDefinition.BaseTypes.Add(baseTypeReference); var currentMethod = new DefaultUnresolvedMethod(currentTypeDefinition, "__ContextStub__"); currentMethod.ReturnType = KnownTypeReference.Void; currentTypeDefinition.Members.Add(currentMethod); var currentResolvedTypeDef = new DefaultResolvedTypeDefinition(resolveContext, currentTypeDefinition); var projectContent = compilation.MainAssembly.UnresolvedAssembly as IProjectContent; var currentFile = new CSharpUnresolvedFile(); currentFile.RootUsingScope.AddSimpleUsing("System.Web.Mvc"); currentFile.RootUsingScope.AddSimpleUsing("System.Web.Mvc.Ajax"); currentFile.RootUsingScope.AddSimpleUsing("System.Web.Mvc.Html"); currentFile.RootUsingScope.AddSimpleUsing("System.Web.Routing"); currentFile.TopLevelTypeDefinitions.Add(currentTypeDefinition); if (projectContent != null) { compilation = projectContent.AddOrUpdateFiles(currentFile).CreateCompilation(SD.ParserService.GetCurrentSolutionSnapshot()); } var context = new CSharpTypeResolveContext(compilation.MainAssembly, currentFile.RootUsingScope.Resolve(compilation), currentResolvedTypeDef, currentMethod.CreateResolved(resolveContext.WithCurrentTypeDefinition(currentResolvedTypeDef))); return(new CSharpResolver(context)); }
T FindEntity <T>(string targetClass) where T : IEntity { var compilation = SD.ParserService.GetCompilationForFile(textEditor.FileName); var parseInfo = SD.ParserService.Parse(textEditor.FileName, textEditor.Document); int i = textEditor.Document.IndexOf(targetClass, 0, textEditor.Document.TextLength, StringComparison.Ordinal); Assert.Greater(i, -1); TextLocation location = textEditor.Document.GetLocation(i); var member = parseInfo.UnresolvedFile.GetMember(location); var type = parseInfo.UnresolvedFile.GetInnermostTypeDefinition(location); var context = new SimpleTypeResolveContext(compilation.MainAssembly); var rt = type.Resolve(context).GetDefinition(); if (member != null) { return((T)member.CreateResolved(context.WithCurrentTypeDefinition(rt))); } return((T)rt); }
ICodeContext CreateContext(ITextEditor editor) { var compilation = SD.ParserService.GetCompilationForFile(editor.FileName); var project = SD.ProjectService.FindProjectContainingFile(editor.FileName); var resolveContext = new SimpleTypeResolveContext(compilation.MainAssembly); var currentTypeDefinition = new DefaultUnresolvedTypeDefinition(project.RootNamespace, Path.GetFileNameWithoutExtension(editor.FileName)); ITypeReference baseTypeReference = new GetClassTypeReference("System.Web.Mvc", "WebViewPage", 1); baseTypeReference = new ParameterizedTypeReference(baseTypeReference, new[] { FindModelType(editor) }); currentTypeDefinition.BaseTypes.Add(baseTypeReference); var currentMethod = new DefaultUnresolvedMethod(currentTypeDefinition, "__ContextStub__"); currentMethod.ReturnType = KnownTypeReference.Void; currentTypeDefinition.Members.Add(currentMethod); var currentResolvedTypeDef = new DefaultResolvedTypeDefinition(resolveContext, currentTypeDefinition); var projectContent = compilation.MainAssembly.UnresolvedAssembly as IProjectContent; var currentFile = new CSharpUnresolvedFile(); currentFile.RootUsingScope.AddSimpleUsing("System.Web.Mvc"); currentFile.RootUsingScope.AddSimpleUsing("System.Web.Mvc.Ajax"); currentFile.RootUsingScope.AddSimpleUsing("System.Web.Mvc.Html"); currentFile.RootUsingScope.AddSimpleUsing("System.Web.Routing"); currentFile.TopLevelTypeDefinitions.Add(currentTypeDefinition); if (projectContent != null) { compilation = projectContent.AddOrUpdateFiles(currentFile).CreateCompilation(SD.ParserService.GetCurrentSolutionSnapshot()); } var context = new CSharpTypeResolveContext(compilation.MainAssembly, currentFile.RootUsingScope.Resolve(compilation), currentResolvedTypeDef, currentMethod.CreateResolved(resolveContext.WithCurrentTypeDefinition(currentResolvedTypeDef))); return new CSharpResolver(context); }