|
| 1 | +using ColorCode; |
| 2 | +using Files.Extensions; |
| 3 | +using Files.Filesystem; |
| 4 | +using Files.ViewModels.Properties; |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Diagnostics; |
| 8 | +using System.Linq; |
| 9 | +using System.Text; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using Windows.Storage; |
| 12 | + |
| 13 | +namespace Files.ViewModels.Previews |
| 14 | +{ |
| 15 | + public class CodePreviewViewModel : BasePreviewModel |
| 16 | + { |
| 17 | + public CodePreviewViewModel(ListedItem item) : base(item) |
| 18 | + { |
| 19 | + } |
| 20 | + |
| 21 | + private string textValue; |
| 22 | + public string TextValue |
| 23 | + { |
| 24 | + get => textValue; |
| 25 | + set => SetProperty(ref textValue, value); |
| 26 | + } |
| 27 | + |
| 28 | + private ILanguage codeLanguage; |
| 29 | + public ILanguage CodeLanguage |
| 30 | + { |
| 31 | + get => codeLanguage; |
| 32 | + set => SetProperty(ref codeLanguage, value); |
| 33 | + } |
| 34 | + |
| 35 | + public static List<string> Extensions => new List<List<string>>(languageExtensions.Values).SelectMany(i => i).Distinct().ToList(); |
| 36 | + |
| 37 | + public async override Task<List<FileProperty>> LoadPreviewAndDetails() |
| 38 | + { |
| 39 | + var details = new List<FileProperty>(); |
| 40 | + |
| 41 | + try |
| 42 | + { |
| 43 | + var text = TextValue ?? await FileIO.ReadTextAsync(Item.ItemFile); |
| 44 | + CodeLanguage = GetCodeLanguage(Item.FileExtension); |
| 45 | + |
| 46 | + details.Add(new FileProperty() |
| 47 | + { |
| 48 | + NameResource = "PropertyLineCount", |
| 49 | + Value = text.Split("\n").Length, |
| 50 | + }); |
| 51 | + |
| 52 | + var displayText = text.Length < Constants.PreviewPane.TextCharacterLimit ? text : text.Remove(Constants.PreviewPane.TextCharacterLimit); |
| 53 | + TextValue = displayText; |
| 54 | + } |
| 55 | + catch (Exception e) |
| 56 | + { |
| 57 | + Debug.WriteLine(e); |
| 58 | + } |
| 59 | + |
| 60 | + return details; |
| 61 | + } |
| 62 | + |
| 63 | + private static Dictionary<ILanguage, List<string>> languageExtensions = new Dictionary<ILanguage, List<string>>() |
| 64 | + { |
| 65 | + {Languages.Xml, new List<string> {".xml", ".axml", ".xaml"} }, |
| 66 | + {Languages.CSharp, new List<string> {".cs", ".cake", ".csx", ".linq"} }, |
| 67 | + {Languages.Html, new List<string> {".razor", ".cshtml", ".vbhtml", ".svelte"} }, |
| 68 | + {Languages.AspxCs, new List<string> { ".acsx" } }, |
| 69 | + {Languages.FSharp, new List<string> {".fs", ".fsi", ".fsx" } }, |
| 70 | + {Languages.Java, new List<string> {".java" } }, |
| 71 | + {Languages.VbDotNet, new List<string> {".vb", ".vbs" } }, |
| 72 | + {Languages.Cpp, new List<string> {".cpp", ".c++", ".cc", ".cp", ".cxx", ".h", ".h++", ".hh", ".hpp", ".hxx", ".inc", ".inl", ".ino", ".ipp", ".re", ".tcc", ".tpp" } }, |
| 73 | + {Languages.PowerShell, new List<string> {".pwsh", ".ps1", ".psd1", ".psm1" } }, |
| 74 | + {Languages.Typescript, new List<string> {".ts", ".tsx"} }, |
| 75 | + {Languages.JavaScript, new List<string> {".js", ".jsx"} }, |
| 76 | + {Languages.Php, new List<string> {".php"} }, |
| 77 | + {Languages.Css, new List<string> {".css", ".scss"} }, |
| 78 | + {Languages.Haskell, new List<string> {".hs"} }, |
| 79 | + {Languages.Aspx, new List<string> {".aspx"} }, |
| 80 | + }; |
| 81 | + |
| 82 | + private static ILanguage GetCodeLanguage(string ext) => languageExtensions.FirstOrDefault(x => x.Value.Contains(ext)).Key; |
| 83 | + } |
| 84 | +} |
0 commit comments