/// <summary> /// This method solves all problems that are caused if the designer generates /// code that is not supported in a previous version of .NET. (3.5 and below) /// Currently it fixes: /// - remove calls to ISupportInitialize.BeginInit/EndInit, if the interface is not implemented by the type in the target framework. /// </summary> /// <remarks>When adding new workarounds make sure that the code does not remove too much code!</remarks> void RemoveUnsupportedCode(CodeTypeDeclaration codeClass, CodeMemberMethod initializeComponent) { if (compilation.GetProject() is MSBuildBasedProject) { MSBuildBasedProject p = (MSBuildBasedProject)compilation.GetProject(); string v = (p.GetEvaluatedProperty("TargetFrameworkVersion") ?? "").Trim('v'); Version version; if (!Version.TryParse(v, out version) || version.Major >= 4) { return; } } List <CodeStatement> stmtsToRemove = new List <CodeStatement>(); var iSupportInitializeInterface = compilation.FindType(typeof(ISupportInitialize)).GetDefinition(); if (iSupportInitializeInterface == null) { return; } foreach (var stmt in initializeComponent.Statements.OfType <CodeExpressionStatement>().Where(ces => ces.Expression is CodeMethodInvokeExpression)) { CodeMethodInvokeExpression invocation = (CodeMethodInvokeExpression)stmt.Expression; CodeCastExpression expr = invocation.Method.TargetObject as CodeCastExpression; if (expr != null) { if (expr.TargetType.BaseType != "System.ComponentModel.ISupportInitialize") { continue; } var fieldType = GetTypeOfControl(expr.Expression, initializeComponent, codeClass).GetDefinition(); if (fieldType == null) { continue; } if (!fieldType.IsDerivedFrom(iSupportInitializeInterface)) { stmtsToRemove.Add(stmt); } } } foreach (var stmt in stmtsToRemove) { initializeComponent.Statements.Remove(stmt); } }
public Task <ResolveResult> ResolveAsync(FileName fileName, TextLocation location, ITextSource fileContent, ICompilation compilation, CancellationToken cancellationToken) { var entry = GetFileEntry(fileName, true); if (entry.parser == null || location.IsEmpty) { return(Task.FromResult <ResolveResult>(ErrorResolveResult.UnknownError)); } IProject project = compilation != null?compilation.GetProject() : null; return(entry.ParseAsync(fileContent, project, cancellationToken).ContinueWith( delegate(Task <ParseInformation> parseInfoTask) { var parseInfo = parseInfoTask.Result; if (parseInfo == null) { return ErrorResolveResult.UnknownError; } if (compilation == null) { compilation = GetCompilationForFile(fileName); } ResolveResult rr = entry.parser.Resolve(parseInfo, location, compilation, cancellationToken); LoggingService.Debug("Resolved " + location + " to " + rr); return rr ?? ErrorResolveResult.UnknownError; }, cancellationToken)); }
public ResolveResult ResolveSnippet(FileName fileName, TextLocation fileLocation, string codeSnippet, ITextSource fileContent, ICompilation compilation, CancellationToken cancellationToken) { var entry = GetFileEntry(fileName, true); if (entry.parser == null) { return(ErrorResolveResult.UnknownError); } IProject project = compilation != null?compilation.GetProject() : null; var parseInfo = entry.Parse(fileContent, project, cancellationToken); if (parseInfo == null) { return(ErrorResolveResult.UnknownError); } if (compilation == null) { compilation = GetCompilationForFile(fileName); } ResolveResult rr = entry.parser.ResolveSnippet(parseInfo, fileLocation, codeSnippet, compilation, cancellationToken); LoggingService.Debug("Resolved " + fileLocation + " to " + rr); return(rr ?? ErrorResolveResult.UnknownError); }
public ICodeContext ResolveContext(FileName fileName, TextLocation location, ITextSource fileContent = null, ICompilation compilation = null, CancellationToken cancellationToken = default(CancellationToken)) { if (compilation == null) { compilation = GetCompilationForFile(fileName); } var entry = GetFileEntry(fileName, true); if (entry.parser == null) { return(new UnknownCodeContext(compilation)); } IProject project = compilation != null?compilation.GetProject() : null; var parseInfo = entry.Parse(fileContent, project, cancellationToken); if (parseInfo == null) { return(new UnknownCodeContext(compilation)); } var context = entry.parser.ResolveContext(parseInfo, location, compilation, cancellationToken); if (context == null) { return(new UnknownCodeContext(compilation, parseInfo.UnresolvedFile, location)); } return(context); }
public override async void Execute(EditorRefactoringContext context) { SyntaxTree st = await context.GetSyntaxTreeAsync().ConfigureAwait(false); ICompilation compilation = await context.GetCompilationAsync().ConfigureAwait(false); CSharpFullParseInformation info = await context.GetParseInformationAsync().ConfigureAwait(false) as CSharpFullParseInformation; EntityDeclaration node = (EntityDeclaration)st.GetNodeAt(context.CaretLocation, n => n is TypeDeclaration || n is DelegateDeclaration); IDocument document = context.Editor.Document; FileName newFileName = FileName.Create(Path.Combine(Path.GetDirectoryName(context.FileName), MakeValidFileName(node.Name))); string header = CopyFileHeader(document, info); string footer = CopyFileEnd(document, info); AstNode newNode = node.Clone(); foreach (var ns in node.Ancestors.OfType <NamespaceDeclaration>()) { var newNS = new NamespaceDeclaration(ns.Name); newNS.Members.AddRange(ns.Children.Where(ch => ch is UsingDeclaration || ch is UsingAliasDeclaration || ch is ExternAliasDeclaration).Select(usingDecl => usingDecl.Clone())); newNS.AddMember(newNode); newNode = newNS; } var topLevelUsings = st.Children.Where(ch => ch is UsingDeclaration || ch is UsingAliasDeclaration || ch is ExternAliasDeclaration); StringBuilder newCode = new StringBuilder(header); CSharpOutputVisitor visitor = new CSharpOutputVisitor(new StringWriter(newCode), FormattingOptionsFactory.CreateSharpDevelop()); foreach (var topLevelUsing in topLevelUsings) { topLevelUsing.AcceptVisitor(visitor); } newNode.AcceptVisitor(visitor); newCode.AppendLine(footer); IViewContent viewContent = FileService.NewFile(newFileName, newCode.ToString()); viewContent.PrimaryFile.SaveToDisk(newFileName); // now that the code is saved in the other file, remove it from the original document RemoveExtractedNode(context, node); IProject project = (IProject)compilation.GetProject(); if (project != null) { FileProjectItem projectItem = new FileProjectItem(project, ItemType.Compile); projectItem.FileName = newFileName; ProjectService.AddProjectItem(project, projectItem); FileService.FireFileCreated(newFileName, false); project.Save(); ProjectBrowserPad.RefreshViewAsync(); } }
/// <summary> /// Gets the ambience for the specified compilation. /// Never returns null. /// </summary> public static IAmbience GetAmbience(this ICompilation compilation) { IProject p = compilation.GetProject(); if (p != null) { return(p.GetAmbience()); } else { return(AmbienceService.GetCurrentAmbience()); } }
public async Task FindLocalReferencesAsync(FileName fileName, IVariable variable, Action <SearchResultMatch> callback, ITextSource fileContent, ICompilation compilation, CancellationToken cancellationToken) { var entry = GetFileEntry(fileName, true); if (entry.parser == null) { return; } if (fileContent == null) { fileContent = entry.parser.GetFileContent(fileName); } if (compilation == null) { compilation = GetCompilationForFile(fileName); } var parseInfo = await entry.ParseAsync(fileContent, compilation.GetProject(), cancellationToken).ConfigureAwait(false); await Task.Run( () => entry.parser.FindLocalReferences(parseInfo, fileContent, variable, compilation, callback, cancellationToken) ); }
public override void Execute(EditorRefactoringContext context) { AlFullParseInformation parseInformation = context.GetParseInformation() as AlFullParseInformation; if (parseInformation != null) { SyntaxTree st = parseInformation.SyntaxTree; Identifier identifier = (Identifier)st.GetNodeAt(context.CaretLocation, node => node.Role == Roles.Identifier); if (identifier == null) { return; } ICompilation compilation = context.GetCompilation(); IProject project = compilation.GetProject(); RenameFile(project, context.FileName, Path.Combine(Path.GetDirectoryName(context.FileName), MakeValidFileName(identifier.Name))); if (project != null) { project.Save(); } } }
public async Task FindLocalReferencesAsync(FileName fileName, IVariable variable, Action<SearchResultMatch> callback, ITextSource fileContent, ICompilation compilation, CancellationToken cancellationToken) { var entry = GetFileEntry(fileName, true); if (entry.parser == null) return; if (fileContent == null) fileContent = entry.parser.GetFileContent(fileName); if (compilation == null) compilation = GetCompilationForFile(fileName); var parseInfo = await entry.ParseAsync(fileContent, compilation.GetProject(), cancellationToken).ConfigureAwait(false); await Task.Run( () => entry.parser.FindLocalReferences(parseInfo, fileContent, variable, compilation, callback, cancellationToken) ); }
public Task<ResolveResult> ResolveAsync(FileName fileName, TextLocation location, ITextSource fileContent, ICompilation compilation, CancellationToken cancellationToken) { var entry = GetFileEntry(fileName, true); if (entry.parser == null) return Task.FromResult<ResolveResult>(ErrorResolveResult.UnknownError); IProject project = compilation != null ? compilation.GetProject() : null; return entry.ParseAsync(fileContent, project, cancellationToken).ContinueWith( delegate (Task<ParseInformation> parseInfoTask) { var parseInfo = parseInfoTask.Result; if (parseInfo == null) return ErrorResolveResult.UnknownError; if (compilation == null) compilation = GetCompilationForFile(fileName); ResolveResult rr = entry.parser.Resolve(parseInfo, location, compilation, cancellationToken); LoggingService.Debug("Resolved " + location + " to " + rr); return rr ?? ErrorResolveResult.UnknownError; }, cancellationToken); }
public ResolveResult ResolveSnippet(FileName fileName, TextLocation fileLocation, string codeSnippet, ITextSource fileContent, ICompilation compilation, CancellationToken cancellationToken) { var entry = GetFileEntry(fileName, true); if (entry.parser == null) return ErrorResolveResult.UnknownError; IProject project = compilation != null ? compilation.GetProject() : null; var parseInfo = entry.Parse(fileContent, project, cancellationToken); if (parseInfo == null) return ErrorResolveResult.UnknownError; if (compilation == null) compilation = GetCompilationForFile(fileName); ResolveResult rr = entry.parser.ResolveSnippet(parseInfo, fileLocation, codeSnippet, compilation, cancellationToken); LoggingService.Debug("Resolved " + fileLocation + " to " + rr); return rr ?? ErrorResolveResult.UnknownError; }
public ICodeContext ResolveContext(FileName fileName, TextLocation location, ITextSource fileContent = null, ICompilation compilation = null, CancellationToken cancellationToken = default(CancellationToken)) { if (compilation == null) compilation = GetCompilationForFile(fileName); var entry = GetFileEntry(fileName, true); if (entry.parser == null) return new UnknownCodeContext(compilation); IProject project = compilation != null ? compilation.GetProject() : null; var parseInfo = entry.Parse(fileContent, project, cancellationToken); if (parseInfo == null) return new UnknownCodeContext(compilation); var context = entry.parser.ResolveContext(parseInfo, location, compilation, cancellationToken); if (context == null) return new UnknownCodeContext(compilation, parseInfo.UnresolvedFile, location); return context; }