async Task FormatFile(Project p, FilePath file) { string mime = DesktopService.GetMimeTypeForUri(file); if (mime == null) { return; } var formatter = CodeFormatterService.GetFormatter(mime); if (formatter != null) { try { var content = await TextFileUtility.ReadAllTextAsync(file); var formatted = formatter.FormatText(p.Policies, content.Text); if (formatted != null) { TextFileUtility.WriteText(file, formatted, content.Encoding); } } catch (Exception ex) { LoggingService.LogError("File formatting failed", ex); } } }
protected override async Task OnLoad() { var file = await TextFileUtility.ReadAllTextAsync(FilePath); text = file.Text; Encoding = file.Encoding; UseByteOrderMark = file.HasByteOrderMark; }
public async Task Bug57840() { var templatingService = new TemplatingService(); var mutliplatformLibraryCategory = templatingService.GetProjectTemplateCategories().Single(c => c.Id == "multiplat") .Categories.Single(c => c.Id == "library") .Categories.Single(c => c.Id == "general"); var pclTemplate = mutliplatformLibraryCategory.Templates.Single(t => t.GroupId == "md-project-portable-library").GetTemplate("C#"); var standardTemplate = mutliplatformLibraryCategory.Templates.Single(t => t.GroupId == "Microsoft.Common.Library").GetTemplate("C#"); var tempDirectory = Util.CreateTmpDir("Bug57840Test"); var result = await templatingService.ProcessTemplate(pclTemplate, new Ide.Projects.NewProjectConfiguration() { CreateSolution = true, Location = tempDirectory, SolutionName = "Bug57840Test", ProjectName = "Bug57840PclTestProject", CreateProjectDirectoryInsideSolutionDirectory = false }, null); solution = result.WorkspaceItems.OfType <Solution> ().Single(); await solution.SaveAsync(Util.GetMonitor()); var project = solution.GetAllProjects().Single(); project.Policies.Set <TextStylePolicy> (new TextStylePolicy(1, 1, 1, true, true, true, EolMarker.Mac), "text/x-csharp"); var file = project.Files.Single(f => f.FilePath.FileName == "MyClass.cs").FilePath; var fileContentBeforeFormat = await TextFileUtility.ReadAllTextAsync(file); await FormatFile(project, file); var fileContentAfterFormat = await TextFileUtility.ReadAllTextAsync(file); Assert.AreNotEqual(fileContentBeforeFormat.Text, fileContentAfterFormat.Text); //Make sure our weird formatting applied solution.Policies.Set <TextStylePolicy> (new TextStylePolicy(3, 3, 3, true, true, true, EolMarker.Mac), "text/x-csharp"); var result2 = await templatingService.ProcessTemplate(standardTemplate, new Ide.Projects.NewProjectConfiguration() { CreateSolution = false, Location = solution.BaseDirectory, ProjectName = "Bug57840StandardTestProject", CreateProjectDirectoryInsideSolutionDirectory = false }, solution.RootFolder); var standardProject = result2.WorkspaceItems.OfType <DotNetProject> ().Single(); solution.RootFolder.AddItem(standardProject); await solution.SaveAsync(Util.GetMonitor()); var fileContentAfterSecondProject = await TextFileUtility.ReadAllTextAsync(file); Assert.AreEqual(fileContentAfterSecondProject.Text, fileContentAfterFormat.Text); //Make sure our weird formatting is preserved var class1File = standardProject.Files.Single(f => f.FilePath.FileName == "Class1.cs").FilePath; var fileContentAfterCreation = await TextFileUtility.ReadAllTextAsync(class1File); standardProject.Policies.Set <TextStylePolicy> (new TextStylePolicy(3, 3, 3, true, true, true, EolMarker.Mac), "text/x-csharp"); await FormatFile(standardProject, class1File); standardProject.Dispose(); var fileContentAfterForceFormatting = await TextFileUtility.ReadAllTextAsync(class1File); Assert.AreEqual(fileContentAfterForceFormatting.Text, fileContentAfterCreation.Text, "We expect them to be same because we placed same formatting policy on solution before creataion as after creation on project when we manually formatted."); }
public async Task Load() { Document.Editor.Text = (await TextFileUtility.ReadAllTextAsync(FilePath)).Text; }