private async Task <SyntaxNode> GetNewRoot(CancellationToken cancellationToken) { var semanticModel = await _document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); var documentOptions = await _document.GetOptionsAsync(cancellationToken).ConfigureAwait(false); if (_service.TryConvertToLocalDeclaration(_state.LocalType, _state.IdentifierToken, documentOptions, semanticModel, cancellationToken, out var newRoot)) { return(newRoot); } var syntaxFactory = _document.GetLanguageService <SyntaxGenerator>(); var initializer = _state.IsOnlyWrittenTo ? null : syntaxFactory.DefaultExpression(_state.LocalType); var type = _state.LocalType; var localStatement = syntaxFactory.LocalDeclarationStatement(type, _state.IdentifierToken.ValueText, initializer); localStatement = localStatement.WithAdditionalAnnotations(Formatter.Annotation); var codeGenService = _document.GetLanguageService <ICodeGenerationService>(); var root = _state.IdentifierToken.GetAncestors <SyntaxNode>().Last(); return(codeGenService.AddStatements( root, SpecializedCollections.SingletonEnumerable(localStatement), options: new CodeGenerationOptions(beforeThisLocation: _state.IdentifierToken.GetLocation()), cancellationToken: cancellationToken)); }
private SyntaxNode GetNewRoot(CancellationToken cancellationToken) { SyntaxNode newRoot; if (_service.TryConvertToLocalDeclaration(_state.LocalType, _state.IdentifierToken, _document.Project.Solution.Workspace.Options, out newRoot)) { return(newRoot); } var syntaxFactory = _document.GetLanguageService <SyntaxGenerator>(); var initializer = _state.IsOnlyWrittenTo ? null : syntaxFactory.DefaultExpression(_state.LocalType); var type = _state.LocalType; var localStatement = syntaxFactory.LocalDeclarationStatement(type, _state.IdentifierToken.ValueText, initializer); localStatement = localStatement.WithAdditionalAnnotations(Microsoft.CodeAnalysis.Formatting.Formatter.Annotation); var codeGenService = new CSharpCodeGenerationService(_document.Project.Solution.Workspace); var root = _state.IdentifierToken.GetAncestors <SyntaxNode>().Last(); return(codeGenService.AddStatements( root, SpecializedCollections.SingletonEnumerable(localStatement), options: new CodeGenerationOptions(beforeThisLocation: _state.IdentifierToken.GetLocation()), cancellationToken: cancellationToken)); }