private RdTask <RdXamlStylerFormattingResult> PerformReformatHandler( Lifetime requestLifetime, RdXamlStylerFormattingRequest request) { return(Task.Run(() => { _lifetime.ThrowIfNotAlive(); // Fetch settings var settings = _solution.GetSettingsStore().SettingsStore.BindToContextLive(_lifetime, ContextRange.Smart(_solution.ToDataContext())); var stylerOptions = StylerOptionsFactory.FromSettings( settings, _solution, null, request.FilePath); // Bail out early if needed if (stylerOptions.SuppressProcessing || !stylerOptions.FormatOnSave) { return new RdXamlStylerFormattingResult(false, false, ""); } // Perform styling var styler = new StylerService(stylerOptions); var formattedText = styler.StyleDocument(request.DocumentText).Replace("\r\n", "\n"); if (request.DocumentText == formattedText) { return new RdXamlStylerFormattingResult(true, false, ""); } return new RdXamlStylerFormattingResult(true, true, formattedText); }, requestLifetime).ToRdTask()); }
protected override Action <ITextControl> ExecutePsiTransaction( [NotNull] ISolution solution, [NotNull] IProgressIndicator progress) { // Fetch settings var lifetime = solution.GetLifetime(); var settings = solution.GetSettingsStore().SettingsStore.BindToContextLive(lifetime, ContextRange.Smart(solution.ToDataContext())); var stylerOptions = StylerOptionsFactory.FromSettings( settings, solution, _dataProvider.Project, _actionAppliesTo == ActionAppliesTo.File ? _dataProvider.SourceFile as IPsiSourceFileWithLocation // Traverse config chain from file path : null // Traverse config chain from project path ); // Bail out early if needed if (stylerOptions.SuppressProcessing) { return(null); } // Perform styling var styler = new StylerService(stylerOptions); var psiSourceFiles = _actionAppliesTo == ActionAppliesTo.File ? _dataProvider.Document.GetPsiSourceFiles(solution).AsIReadOnlyList() : _actionAppliesTo == ActionAppliesTo.Project ? _dataProvider.Project.GetAllProjectFiles(it => it.LanguageType.Is <XamlProjectFileType>()).SelectMany(file => file.ToSourceFiles().AsIReadOnlyList()) : _dataProvider.Solution.GetAllProjects().SelectMany(project => project.GetAllProjectFiles(it => it.LanguageType.Is <XamlProjectFileType>()).SelectMany(file => file.ToSourceFiles().AsIReadOnlyList())); foreach (var prjItem in psiSourceFiles) { foreach (var file in prjItem.GetPsiFiles <XamlLanguage>()) { var sourceFile = file.GetSourceFile(); if (sourceFile?.Document != null) { var oldText = sourceFile.Document.GetText(); var newText = styler.StyleDocument(oldText).Replace("\r\n", "\n"); file.ReParse(new TreeTextRange(new TreeOffset(0), new TreeOffset(oldText.Length)), newText); } } } return(null); }